-
Notifications
You must be signed in to change notification settings - Fork 0
/
osim_model.cpp
287 lines (246 loc) · 11.7 KB
/
osim_model.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <OpenSim/OpenSim.h>
#include <OpenSim/Simulation/Model/Model.h>
#include <OpenSim/Actuators/CoordinateActuator.h>
#include <OpenSim/Simulation/InverseDynamicsSolver.h>
#include <iostream>
#include <string>
#include "interface/robot_control.h"
#ifndef USE_NN
#include "nms_processor-nn.h"
#else
#include "nms_processor-osim.h"
#endif
struct
{
OpenSim::Model* osimModel;
SimTK::State state;
std::vector<OpenSim::CoordinateActuator*> actuatorsList;
std::vector<int> accelerationIndexesList;
std::vector<char*> jointNamesList;
std::vector<char*> axisNamesList;
enum ControlState controlState;
SimTK::Vector emgInputs;
NMSProcessor* nmsProcessor;
}
controller;
DECLARE_MODULE_INTERFACE( ROBOT_CONTROL_INTERFACE );
bool InitController( const char* data )
{
try
{
// Create an OpenSim model from XML (.osim) file
controller.osimModel = new OpenSim::Model( std::string( "config/robots/" ) + data + ".osim" );
controller.osimModel->printBasicInfo( std::cout );
controller.osimModel->setGravity( SimTK::Vec3( 0.0, -9.80665, 0.0 ) );
controller.osimModel->setUseVisualizer( false ); // not for RT
// Initialize the system
controller.state = controller.osimModel->initSystem();
std::cout << "OpenSim model loaded successfully ! (" << controller.osimModel->getNumCoordinates() << " coordinates)" << std::endl;
OpenSim::Set<OpenSim::Muscle> muscleSet = controller.osimModel->getMuscles();
for( int muscleIndex = 0; muscleIndex < muscleSet.getSize(); muscleIndex++ )
#ifdef OSIM_LEGACY
muscleSet[ muscleIndex ].setDisabled( controller.state, true );
#else
muscleSet[ muscleIndex ].setAppliesForce( controller.state, false );
#endif
const OpenSim::Set<OpenSim::Actuator>& actuatorSet = controller.osimModel->getActuators();
for( int actuatorIndex = 0; actuatorIndex < actuatorSet.getSize(); actuatorIndex++ )
{
std::string actuatorName = actuatorSet[ actuatorIndex ].getName();
if( not muscleSet.contains( actuatorName ) )
{
OpenSim::CoordinateActuator* actuator = dynamic_cast<OpenSim::CoordinateActuator*>(&(actuatorSet[ actuatorIndex ]));
if( actuator != NULL )
{
#ifdef OSIM_LEGACY
actuator->overrideForce( controller.state, true );
#else
actuator->overrideActuation( controller.state, true );
#endif
OpenSim::CoordinateSet coordinateSet = controller.osimModel->getCoordinateSet();
OpenSim::Coordinate& actuatorCoordinate = coordinateSet.get( actuator->getName() );
actuator->setCoordinate( &actuatorCoordinate );
controller.actuatorsList.push_back( actuator );
controller.jointNamesList.push_back( (char*) actuator->getCoordinate()->getName().c_str() );
controller.axisNamesList.push_back( (char*) actuator->getCoordinate()->getName().c_str() );
controller.accelerationIndexesList.push_back( coordinateSet.getIndex( &actuatorCoordinate ) );
}
}
}
std::cout << "Initial locations taken" << std::endl;
controller.nmsProcessor = new NMSProcessor( *(controller.osimModel), controller.actuatorsList, 1000 );
std::cout << "Neuromusculoskeletal processor created" << std::endl;
SetControlState( /*CONTROL_PASSIVE*/CONTROL_PREPROCESSING );
std::cout << "OSim: integration manager created" << std::endl;
}
catch( OpenSim::Exception ex )
{
std::cout << ex.getMessage() << std::endl;
EndController();
return false;
}
catch( std::exception ex )
{
std::cout << ex.what() << std::endl;
EndController();
return false;
}
catch( ... )
{
std::cout << "UNRECOGNIZED EXCEPTION" << std::endl;
EndController();
return false;
}
std::cout << "OpenSim controller initialized successfully !" << std::endl;
return true;
}
void EndController()
{
delete controller.osimModel;
controller.jointNamesList.clear();
controller.axisNamesList.clear();
controller.emgInputs.clear();
}
size_t GetJointsNumber() { return (size_t) controller.jointNamesList.size(); }
const char** GetJointNamesList() { return (const char**) controller.jointNamesList.data(); }
size_t GetAxesNumber() { return (size_t) controller.axisNamesList.size(); }
const char** GetAxisNamesList() { return (const char**) controller.axisNamesList.data(); }
size_t GetExtraInputsNumber( void ) { return controller.osimModel->getMuscles().getSize(); }
void SetExtraInputsList( double* inputsList )
{
controller.emgInputs = SimTK::Vector( controller.osimModel->getMuscles().getSize(), inputsList );
}
size_t GetExtraOutputsNumber( void ) { return 0; }
void GetExtraOutputsList( double* outputsList ) { }
void SetControlState( enum ControlState newControlState )
{
std::cout << "setting new control state: " << newControlState;
const OpenSim::ForceSet &forceSet = controller.osimModel->getForceSet();
for( int forceIndex = 0; forceIndex < forceSet.getSize(); forceIndex++ )
#ifdef OSIM_LEGACY
forceSet[ forceIndex ].setDisabled( controller.state, true );
#else
forceSet[ forceIndex ].setAppliesForce( controller.state, false );
#endif
if( newControlState == CONTROL_OFFSET )
{
std::cout << "starting offset phase" << std::endl;
}
else if( newControlState == CONTROL_CALIBRATION )
{
std::cout << "starting calibration phase" << std::endl;
}
else if( newControlState == CONTROL_PREPROCESSING )
{
std::cout << "reseting sampling count" << std::endl;
controller.nmsProcessor->ResetSamplesStorage();
}
else
{
if( newControlState == CONTROL_OPERATION )
{
if( controller.controlState == CONTROL_PREPROCESSING )
{
std::cout << "starting optimization" << std::endl;
SimTK::Vector parametersList = controller.nmsProcessor->GetInitialParameters();
SimTK::Optimizer optimizer( *(controller.nmsProcessor), SimTK::LBFGSB );
optimizer.setConvergenceTolerance( 0.05 );
optimizer.useNumericalGradient( true );
optimizer.setMaxIterations( 1000 );
optimizer.setLimitedMemoryHistory( 500 );
SimTK::Real remainingError = optimizer.optimize( parametersList );
std::cout << "optimization ended with residual: " << remainingError << std::endl;
controller.nmsProcessor->SetParameters( parametersList );
for( int forceIndex = 0; forceIndex < forceSet.getSize(); forceIndex++ )
#ifdef OSIM_LEGACY
forceSet[ forceIndex ].setDisabled( controller.state, false );
#else
forceSet[ forceIndex ].setAppliesForce( controller.state, true );
#endif
}
}
}
controller.controlState = newControlState;
}
void PreProcessSample( SimTK::Vector& inputSample, SimTK::Vector& outputSample )
{
OpenSim::InverseDynamicsSolver idSolver( *(controller.osimModel) );
SimTK::Vector accelerationsList( controller.osimModel->getCoordinateSet().getSize(), 0.0 );
for( size_t jointIndex = 0; jointIndex < controller.actuatorsList.size(); jointIndex++ )
{
OpenSim::Coordinate* jointCoordinate = controller.actuatorsList[ jointIndex ]->getCoordinate();
int actuatorInputsIndex = controller.emgInputs.size() + jointIndex * NMS_INPUT_VARS_NUMBER;
jointCoordinate->setValue( controller.state, inputSample[ actuatorInputsIndex + NMS_POSITION ] );
jointCoordinate->setSpeedValue( controller.state, inputSample[ actuatorInputsIndex + NMS_VELOCITY ] );
int jointAccelerationIndex = controller.accelerationIndexesList[ jointIndex ];
accelerationsList[ jointAccelerationIndex ] = inputSample[ actuatorInputsIndex + NMS_ACCELERATION ];
#ifdef OSIM_LEGACY
controller.actuatorsList[ jointIndex ]->setOverrideForce( controller.state, inputSample[ actuatorInputsIndex + NMS_TORQUE_EXT ] );
#else
controller.actuatorsList[ jointIndex ]->setOverrideActuation( controller.state, inputSample[ actuatorInputsIndex + NMS_TORQUE_EXT ] );
#endif
}
try
{
SimTK::Vector idForcesList = idSolver.solve( controller.state, accelerationsList );
for( int jointIndex = 0; jointIndex < controller.actuatorsList.size(); jointIndex++ )
{
int actuatorInputsIndex = controller.emgInputs.size() + jointIndex * NMS_INPUT_VARS_NUMBER;
double positionError = inputSample[ actuatorInputsIndex + NMS_SETPOINT ] - inputSample[ actuatorInputsIndex + NMS_POSITION ];
int jointTorqueIndex = controller.accelerationIndexesList[ jointIndex ];
std::cout << "joint " << jointIndex << " coordinate index: " << jointTorqueIndex << std::endl;
int actuatorOutputsIndex = jointIndex * NMS_OUTPUT_VARS_NUMBER;
outputSample[ actuatorOutputsIndex + NMS_TORQUE_INT ] = idForcesList[ jointIndex ];
outputSample[ actuatorOutputsIndex + NMS_STIFFNESS ] = ( std::abs( positionError ) > 1.0e-6 ) ? idForcesList[ jointIndex ] / ( positionError ) : 100.0;
}
}
catch( OpenSim::Exception ex )
{
std::cout << ex.getMessage() << std::endl;
}
catch( std::exception ex )
{
std::cout << ex.what() << std::endl;
}
}
void RunControlStep( DoFVariables** jointMeasuresList, DoFVariables** axisMeasuresList, DoFVariables** jointSetpointsList, DoFVariables** axisSetpointsList, double timeDelta )
{
controller.state.updTime() = 0.0;
SimTK::Vector actuatorInputs( NMS_INPUT_VARS_NUMBER * controller.actuatorsList.size() );
SimTK::Vector actuatorOutputs( NMS_OUTPUT_VARS_NUMBER * controller.actuatorsList.size() );
for( size_t jointIndex = 0; jointIndex < controller.actuatorsList.size(); jointIndex++ )
{
size_t actuatorInputsIndex = jointIndex * NMS_INPUT_VARS_NUMBER;
actuatorInputs[ actuatorInputsIndex + NMS_POSITION ] = jointMeasuresList[ jointIndex ]->position;
actuatorInputs[ actuatorInputsIndex + NMS_VELOCITY ] = jointMeasuresList[ jointIndex ]->velocity;
actuatorInputs[ actuatorInputsIndex + NMS_ACCELERATION ] = jointMeasuresList[ jointIndex ]->acceleration;
actuatorInputs[ actuatorInputsIndex + NMS_SETPOINT ] = jointMeasuresList[ jointIndex ]->acceleration;
actuatorInputs[ actuatorInputsIndex + NMS_TORQUE_EXT ] = jointMeasuresList[ jointIndex ]->force;
}
PreProcessSample( actuatorInputs, actuatorOutputs );
if( controller.controlState == CONTROL_PREPROCESSING )
controller.nmsProcessor->StoreSamples( actuatorInputs, controller.emgInputs, actuatorOutputs );
else if( controller.controlState == CONTROL_OPERATION )
actuatorOutputs = controller.nmsProcessor->CalculateOutputs( actuatorInputs, controller.emgInputs );
OpenSim::Manager manager( *(controller.osimModel) );
#ifdef OSIM_LEGACY
manager.integrate( controller.state, timeDelta );
#else
controller.state = manager.integrate( timeDelta );
#endif
for( size_t jointIndex = 0; jointIndex < controller.actuatorsList.size(); jointIndex++ )
{
size_t actuatorInputsIndex = jointIndex * NMS_INPUT_VARS_NUMBER;
axisMeasuresList[ jointIndex ]->position = actuatorInputs[ actuatorInputsIndex + NMS_POSITION ];
axisMeasuresList[ jointIndex ]->velocity = actuatorInputs[ actuatorInputsIndex + NMS_VELOCITY ];
axisMeasuresList[ jointIndex ]->acceleration = actuatorInputs[ actuatorInputsIndex + NMS_ACCELERATION ];
size_t actuatorOutputsIndex = jointIndex * NMS_OUTPUT_VARS_NUMBER;
axisMeasuresList[ jointIndex ]->force = actuatorOutputs[ actuatorOutputsIndex + NMS_TORQUE_INT ];
axisMeasuresList[ jointIndex ]->stiffness = actuatorOutputs[ actuatorOutputsIndex + NMS_STIFFNESS ];
jointSetpointsList[ jointIndex ]->position = axisSetpointsList[ jointIndex ]->position;
jointSetpointsList[ jointIndex ]->velocity = axisSetpointsList[ jointIndex ]->velocity;
jointSetpointsList[ jointIndex ]->acceleration = axisSetpointsList[ jointIndex ]->acceleration;
jointSetpointsList[ jointIndex ]->force = axisSetpointsList[ jointIndex ]->force;
}
//std::cout << "joint 0 position: " << controller.actuatorsList[ 0 ]->getCoordinate()->getValue( state ) << std::endl;
}