-
Notifications
You must be signed in to change notification settings - Fork 0
/
osim_model_generator.cpp
95 lines (83 loc) · 4.12 KB
/
osim_model_generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <OpenSim/OpenSim.h>
#include <iostream>
int main()
{
const size_t BODIES_NUMBER = 2;
const SimTK::Vec3 BODY_COLORS[] = { SimTK::Red, SimTK::Blue, SimTK::Green, SimTK::Yellow };
const double BODY_SIZE = 0.5;
const double BODY_DISTANCE = 3 * BODY_SIZE;
std::cout << "creating osim model" << std::endl;
try
{
OpenSim::Model osimModel;
//osimModel.setUseVisualizer( true );
osimModel.setName( "osim_robot" );
osimModel.setGravity( SimTK::Vec3( 0, 0, 0 ) );
for( size_t bodyIndex = 0; bodyIndex < BODIES_NUMBER; bodyIndex++ )
{
std::cout << "creating osim body" << std::endl;
std::string indexString = std::to_string( bodyIndex );
OpenSim::Body* body = new OpenSim::Body( "body_" + indexString, 1.0, SimTK::Vec3( 0, 0, 0 ), SimTK::Inertia( 1, 1, 1 ) );
osimModel.addBody( body );
const OpenSim::PhysicalFrame& refFrame = ( bodyIndex == 0 ) ? (const OpenSim::PhysicalFrame&) osimModel.getGround() : (const OpenSim::PhysicalFrame&) osimModel.getBodySet().get( 0 );
OpenSim::PinJoint* pinJoint = new OpenSim::PinJoint( "joint_" + indexString,
refFrame, SimTK::Vec3( 0, BODY_DISTANCE, 0 ), SimTK::Vec3( 0, 0, 0 ),
*(body), SimTK::Vec3( 0, 0, 0 ), SimTK::Vec3( 0, 0, 0 ) );
osimModel.addJoint( pinJoint );
OpenSim::Cylinder* bodyMesh = new OpenSim::Cylinder( BODY_SIZE, BODY_SIZE );
bodyMesh->setColor( BODY_COLORS[ bodyIndex ] );
OpenSim::PhysicalOffsetFrame* offsetFrame = new OpenSim::PhysicalOffsetFrame();
offsetFrame->setParentFrame( *(body) );
offsetFrame->set_orientation( SimTK::Vec3( SimTK::Pi / 2, 0.0, 0.0 ) );
offsetFrame->attachGeometry( bodyMesh );
body->addComponent( offsetFrame );
offsetFrame = new OpenSim::PhysicalOffsetFrame();
offsetFrame->setParentFrame( *(body) );
offsetFrame->set_translation( SimTK::Vec3( 0.0, BODY_DISTANCE / 2, 0.0 ) );
offsetFrame->attachGeometry( new OpenSim::Brick( SimTK::Vec3( BODY_SIZE / 5, BODY_DISTANCE / 2, BODY_SIZE / 2 ) ) );
body->addComponent( offsetFrame );
offsetFrame = new OpenSim::PhysicalOffsetFrame();
offsetFrame->setParentFrame( *(body) );
offsetFrame->set_translation( SimTK::Vec3( 0.0, BODY_DISTANCE, 0.0 ) );
offsetFrame->set_orientation( SimTK::Vec3( SimTK::Pi / 2, 0.0, 0.0 ) );
bodyMesh = new OpenSim::Cylinder( BODY_SIZE / 2, BODY_SIZE );
bodyMesh->setColor( BODY_COLORS[ bodyIndex ] );
offsetFrame->attachGeometry( bodyMesh );
body->addComponent( offsetFrame );
if( bodyIndex > 0 )
{
OpenSim::Marker* effectorMarker = new OpenSim::Marker( "effector_ref", *body, SimTK::Vec3( 0.0, BODY_DISTANCE, 0.0 ) );
osimModel.addMarker( effectorMarker );
}
OpenSim::Coordinate& coordinate = pinJoint->updCoordinate();
OpenSim::CoordinateActuator* userActuator = new OpenSim::CoordinateActuator( coordinate.getName() );
userActuator->setName( coordinate.getName() + "_user" );
osimModel.addForce( userActuator );
OpenSim::CoordinateActuator* controlActuator = new OpenSim::CoordinateActuator( coordinate.getName() );
controlActuator->setName( coordinate.getName() + "_control" );
osimModel.addForce( controlActuator );
}
OpenSim::Muscle* userMuscle = new OpenSim::Millard2012EquilibriumMuscle( "muscle", 1.0, 1.0, 1.0, 1.0 );
userMuscle->addNewPathPoint( "origin", osimModel.getBodySet().get( 0 ), SimTK::Vec3( 0.0 ) );
userMuscle->addNewPathPoint( "insertion", osimModel.getBodySet().get( 1 ), SimTK::Vec3( 0.0 ) );
osimModel.addForce( userMuscle );
SimTK::State& state = osimModel.initSystem();
osimModel.print( "osim-robot_arm.osim" );
}
catch( OpenSim::Exception exception )
{
std::cout << exception.getMessage() << std::endl;
exit( -1 );
}
catch( std::exception exception )
{
std::cout << exception.what() << std::endl;
exit( -1 );
}
catch( ... )
{
std::cout << "UNRECOGNIZED EXCEPTION" << std::endl;
exit( -1 );
}
exit( 0 );
}