This is an example that uses a squat-to-stand motion to create synthetic accelerometer data, and then creates a tracking simulation to track the synthetic accelerometer signals.
3import exampleIMUTracking_helpers
as helpers
6sys.path.insert(1,
'../')
7import mocoPlotTrajectory
as plot
15model = helpers.getTorqueDrivenSquatToStandModel()
23helpers.addIMUFrame(model,
'torso',
24 osim.Vec3(0.08, 0.3, 0), osim.Vec3(0, 0.5*np.pi, 0.5*np.pi))
25helpers.addIMUFrame(model,
'femur_r',
26 osim.Vec3(0, -0.2, 0.05), osim.Vec3(0, 0, 0.5*np.pi))
27helpers.addIMUFrame(model,
'tibia_r', osim.Vec3(0, -0.2, 0.05),
28 osim.Vec3(0, 0, 0.5*np.pi))
33imuFramePaths = osim.StdVectorString()
34imuFramePaths.append(
'/bodyset/torso/torso_imu_offset')
35imuFramePaths.append(
'/bodyset/femur_r/femur_r_imu_offset')
36imuFramePaths.append(
'/bodyset/tibia_r/tibia_r_imu_offset')
37osim.OpenSenseUtilities().addModelIMUs(model, imuFramePaths)
46study = osim.MocoStudy()
49problem = study.updProblem()
50problem.setModel(model)
72problem.setTimeBounds(0, 1)
76problem.setStateInfo(
'/jointset/hip_r/hip_flexion_r/value',
78problem.setStateInfo(
'/jointset/knee_r/knee_angle_r/value',
80problem.setStateInfo(
'/jointset/ankle_r/ankle_angle_r/value',
84problem.setStateInfoPattern(
'/jointset/.*/speed', [], 0, 0)
87problem.addGoal(osim.MocoControlGoal(
'myeffort'))
90solver = study.initCasADiSolver()
91solver.set_num_mesh_intervals(25)
92solver.set_optim_constraint_tolerance(1e-4)
93solver.set_optim_convergence_tolerance(1e-4)
95if not os.path.isfile(
'predictSolution.sto'):
97 predictSolution = study.solve()
98 predictSolution.write(
'predictSolution.sto')
99 study.visualize(predictSolution)
109predictSolution = osim.MocoTrajectory(
'predictSolution.sto')
114outputPaths = osim.StdVectorString()
115outputPaths.append(
'.*accelerometer_signal')
116accelerometerSignals = osim.analyzeVec3(model,
117 predictSolution.exportToStatesTable(),
118 predictSolution.exportToControlsTable(),
124accelerometerSignals.setColumnLabels(imuFramePaths)
127helpers.plotAccelerationSignals(accelerometerSignals)
136tracking = osim.MocoAccelerationTrackingGoal(
'acceleration_tracking')
137tracking.setFramePaths(imuFramePaths)
138tracking.setAccelerationReference(accelerometerSignals)
139tracking.setGravityOffset(
True)
140tracking.setExpressAccelerationsInTrackingFrames(
True)
141problem.addGoal(tracking)
145problem.updGoal(
'myeffort').setWeight(0.001)
147if not os.path.isfile(
'trackingSolution.sto'):
149 trackingSolution = study.solve()
150 trackingSolution.write(
'trackingSolution.sto')
151 study.visualize(trackingSolution)
157plot.mocoPlotTrajectory(
'predictSolution.sto',
'trackingSolution.sto',
162trackingSolution = osim.MocoTrajectory(
'trackingSolution.sto')
163accelerometerSignalsTracking = osim.analyzeVec3(model,
164 trackingSolution.exportToStatesTable(),
165 trackingSolution.exportToControlsTable(),
167accelerometerSignalsTracking.setColumnLabels(imuFramePaths)
168helpers.plotAccelerationSignals(accelerometerSignals,
169 accelerometerSignalsTracking)