-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathHumanoidKinematicsSimulation.java
More file actions
618 lines (538 loc) · 34.7 KB
/
Copy pathHumanoidKinematicsSimulation.java
File metadata and controls
618 lines (538 loc) · 34.7 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package us.ihmc.avatar.kinematicsSimulation;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import controller_msgs.msg.dds.*;
import std_msgs.msg.dds.Empty;
import us.ihmc.avatar.AvatarControllerThread;
import us.ihmc.avatar.drcRobot.DRCRobotModel;
import us.ihmc.avatar.initialSetup.RobotInitialSetup;
import us.ihmc.avatar.logging.IntraprocessYoVariableLogger;
import us.ihmc.commonWalkingControlModules.capturePoint.LinearMomentumRateControlModule;
import us.ihmc.commonWalkingControlModules.configurations.WalkingControllerParameters;
import us.ihmc.commonWalkingControlModules.controlModules.pelvis.PelvisHeightControlState;
import us.ihmc.commonWalkingControlModules.controllerAPI.input.ControllerNetworkSubscriber;
import us.ihmc.commonWalkingControlModules.controllerCore.WholeBodyControlCoreToolbox;
import us.ihmc.commonWalkingControlModules.controllerCore.WholeBodyControllerCore;
import us.ihmc.commonWalkingControlModules.controllerCore.command.ControllerCoreCommand;
import us.ihmc.commonWalkingControlModules.controllerCore.command.inverseDynamics.InverseDynamicsCommandList;
import us.ihmc.commonWalkingControlModules.dynamicPlanning.bipedPlanning.CoPTrajectoryParameters;
import us.ihmc.commonWalkingControlModules.highLevelHumanoidControl.factories.ContactableBodiesFactory;
import us.ihmc.commonWalkingControlModules.highLevelHumanoidControl.factories.ControllerAPIDefinition;
import us.ihmc.commonWalkingControlModules.highLevelHumanoidControl.factories.HighLevelControlManagerFactory;
import us.ihmc.commonWalkingControlModules.highLevelHumanoidControl.highLevelStates.KinematicsSimulationContactStateHolder;
import us.ihmc.commonWalkingControlModules.highLevelHumanoidControl.highLevelStates.WalkingHighLevelHumanoidController;
import us.ihmc.commonWalkingControlModules.messageHandlers.WalkingMessageHandler;
import us.ihmc.commonWalkingControlModules.momentumBasedController.HighLevelHumanoidControllerToolbox;
import us.ihmc.commonWalkingControlModules.sensors.footSwitch.SettableFootSwitch;
import us.ihmc.commons.Conversions;
import us.ihmc.commons.thread.ThreadTools;
import us.ihmc.commons.time.Stopwatch;
import us.ihmc.communication.HumanoidControllerAPI;
import us.ihmc.communication.ROS2Tools;
import us.ihmc.communication.controllerAPI.CommandInputManager;
import us.ihmc.communication.controllerAPI.MessageUnpackingTools;
import us.ihmc.communication.controllerAPI.StatusMessageOutputManager;
import us.ihmc.communication.ros2.ROS2Heartbeat;
import us.ihmc.euclid.referenceFrame.FramePose3D;
import us.ihmc.euclid.referenceFrame.FrameVector2D;
import us.ihmc.euclid.referenceFrame.ReferenceFrame;
import us.ihmc.graphicsDescription.yoGraphics.YoGraphicsListRegistry;
import us.ihmc.humanoidRobotics.bipedSupportPolygons.ContactableFoot;
import us.ihmc.humanoidRobotics.communication.controllerAPI.converter.FrameMessageCommandConverter;
import us.ihmc.humanoidRobotics.communication.packets.walking.FootstepStatus;
import us.ihmc.humanoidRobotics.communication.packets.walking.WalkingStatus;
import us.ihmc.humanoidRobotics.frames.HumanoidReferenceFrames;
import us.ihmc.humanoidRobotics.model.CenterOfMassStateProvider;
import us.ihmc.log.LogTools;
import us.ihmc.mecano.multiBodySystem.RevoluteJoint;
import us.ihmc.mecano.multiBodySystem.interfaces.JointBasics;
import us.ihmc.mecano.multiBodySystem.interfaces.OneDoFJointBasics;
import us.ihmc.mecano.multiBodySystem.interfaces.OneDoFJointReadOnly;
import us.ihmc.mecano.tools.MultiBodySystemStateIntegrator;
import us.ihmc.robotDataLogger.YoVariableServer;
import us.ihmc.robotDataLogger.logger.DataServerSettings;
import us.ihmc.robotModels.FullHumanoidRobotModel;
import us.ihmc.robotModels.FullRobotModelUtils;
import us.ihmc.robotics.MultiBodySystemMissingTools;
import us.ihmc.robotics.contactable.ContactablePlaneBody;
import us.ihmc.robotics.robotSide.RobotSide;
import us.ihmc.robotics.robotSide.SideDependentList;
import us.ihmc.robotics.sensors.ForceSensorDataHolder;
import us.ihmc.robotics.sensors.ForceSensorDataHolderReadOnly;
import us.ihmc.robotics.sensors.IMUDefinition;
import us.ihmc.ros2.ROS2Node;
import us.ihmc.ros2.ROS2Topic;
import us.ihmc.ros2.RealtimeROS2Node;
import us.ihmc.sensorProcessing.communication.producers.RobotConfigurationDataPublisher;
import us.ihmc.sensorProcessing.communication.producers.RobotConfigurationDataPublisherFactory;
import us.ihmc.sensorProcessing.frames.CommonHumanoidReferenceFrames;
import us.ihmc.sensorProcessing.imu.IMUSensor;
import us.ihmc.sensorProcessing.model.RobotMotionStatus;
import us.ihmc.sensorProcessing.model.RobotMotionStatusHolder;
import us.ihmc.sensorProcessing.outputData.JointDesiredOutputList;
import us.ihmc.sensorProcessing.outputData.JointDesiredOutputListReadOnly;
import us.ihmc.sensorProcessing.outputData.JointDesiredOutputReadOnly;
import us.ihmc.sensorProcessing.sensorProcessors.FloatingJointStateReadOnly;
import us.ihmc.sensorProcessing.sensorProcessors.OneDoFJointStateReadOnly;
import us.ihmc.sensorProcessing.sensorProcessors.SensorTimestampHolder;
import us.ihmc.simulationConstructionSetTools.util.HumanoidFloatingRootJointRobot;
import us.ihmc.tools.thread.PausablePeriodicThread;
import us.ihmc.wholeBodyController.RobotContactPointParameters;
import us.ihmc.wholeBodyController.parameters.ParameterLoaderHelper;
import us.ihmc.yoVariables.registry.YoRegistry;
import us.ihmc.yoVariables.variable.YoDouble;
import us.ihmc.yoVariables.variable.YoVariable;
public class HumanoidKinematicsSimulation
{
public static final ROS2Topic<Empty> KINEMATICS_SIMULATION_HEARTBEAT
= ROS2Tools.IHMC_ROOT.withModule("kinematics_simulation").withOutput().withSuffix("heartbeat").withType(Empty.class);
private static final double GRAVITY_Z = 9.81;
private static final double LIDAR_SPINDLE_SPEED = 2.5;
private static final ReferenceFrame worldFrame = ReferenceFrame.getWorldFrame();
private final HumanoidKinematicsSimulationParameters kinematicsSimulationParameters;
private final PausablePeriodicThread controlThread;
private final ROS2Node ros2Node;
private final RealtimeROS2Node realtimeROS2Node;
private final ROS2Heartbeat heartbeat;
private final RobotConfigurationDataPublisher robotConfigurationDataPublisher;
private final YoRegistry registry = new YoRegistry(getClass().getSimpleName());
private final YoGraphicsListRegistry yoGraphicsListRegistry = new YoGraphicsListRegistry();
private final SimulatedHandKinematicController simulatedHandKinematicController;
private final RobotMotionStatusHolder robotMotionStatusHolder = new RobotMotionStatusHolder(RobotMotionStatus.UNKNOWN);
private double yoVariableServerTime = 0.0;
private final Stopwatch monotonicTimer = new Stopwatch();
private final Stopwatch updateTimer = new Stopwatch();
private final YoDouble yoTime;
private final FullHumanoidRobotModel fullRobotModel;
private final CommonHumanoidReferenceFrames referenceFrames;
private final SideDependentList<SettableFootSwitch> footSwitches = new SideDependentList<>();
private final HighLevelHumanoidControllerToolbox controllerToolbox;
private final WholeBodyControllerCore controllerCore;
private final LinearMomentumRateControlModule linearMomentumRateControlModule;
private final HighLevelControlManagerFactory managerFactory;
private final WalkingHighLevelHumanoidController walkingController;
private final CommandInputManager walkingInputManager = new CommandInputManager("walking_preview_internal",
ControllerAPIDefinition.getControllerSupportedCommands());
private final StatusMessageOutputManager walkingOutputManager = new StatusMessageOutputManager(ControllerAPIDefinition.getControllerSupportedStatusMessages());
private final MultiBodySystemStateIntegrator integrator = new MultiBodySystemStateIntegrator();
private AtomicReference<WalkingStatus> latestWalkingStatus = new AtomicReference<>();
private SideDependentList<KinematicsSimulationContactStateHolder> contactStateHolders = new SideDependentList<>();
private InverseDynamicsCommandList inverseDynamicsContactHolderCommandList = new InverseDynamicsCommandList();
private YoVariableServer yoVariableServer = null;
private IntraprocessYoVariableLogger intraprocessYoVariableLogger;
private JointDesiredOutputListReadOnly jointDesiredOutputList;
public static HumanoidKinematicsSimulation create(DRCRobotModel robotModel, HumanoidKinematicsSimulationParameters kinematicsSimulationParameters)
{
HumanoidKinematicsSimulation humanoidKinematicsSimulation = new HumanoidKinematicsSimulation(robotModel, kinematicsSimulationParameters);
humanoidKinematicsSimulation.setRunning(true);
return humanoidKinematicsSimulation;
}
public static HumanoidKinematicsSimulation createForPreviews(DRCRobotModel robotModel, HumanoidKinematicsSimulationParameters kinematicsSimulationParameters)
{
return new HumanoidKinematicsSimulation(robotModel, kinematicsSimulationParameters);
}
private HumanoidKinematicsSimulation(DRCRobotModel robotModel, HumanoidKinematicsSimulationParameters kinematicsSimulationParameters)
{
this.kinematicsSimulationParameters = kinematicsSimulationParameters;
// instantiate some existing controller ROS2 API?
ros2Node = ROS2Tools.createROS2Node(kinematicsSimulationParameters.getPubSubImplementation(), HumanoidControllerAPI.HUMANOID_KINEMATICS_CONTROLLER_NODE_NAME);
heartbeat = new ROS2Heartbeat(ros2Node, KINEMATICS_SIMULATION_HEARTBEAT);
String robotName = robotModel.getSimpleRobotName();
fullRobotModel = robotModel.createFullRobotModel();
CenterOfMassStateProvider centerOfMassStateProvider = CenterOfMassStateProvider.createJacobianBasedStateCalculator(fullRobotModel.getElevator(),
worldFrame);
referenceFrames = new HumanoidReferenceFrames(fullRobotModel, centerOfMassStateProvider, null);
// Create registries to match controller so the XML gets loaded properly.
yoTime = new YoDouble("time", registry);
YoRegistry drcControllerThreadRegistry = new YoRegistry("DRCControllerThread");
YoRegistry drcMomentumBasedControllerRegistry = new YoRegistry("DRCMomentumBasedController");
YoRegistry humanoidHighLevelControllerManagerRegistry = new YoRegistry("HumanoidHighLevelControllerManager");
YoRegistry managerParentRegistry = new YoRegistry("HighLevelHumanoidControllerFactory");
YoRegistry walkingParentRegistry = new YoRegistry("WalkingControllerState");
registry.addChild(drcControllerThreadRegistry);
drcControllerThreadRegistry.addChild(drcMomentumBasedControllerRegistry);
drcMomentumBasedControllerRegistry.addChild(humanoidHighLevelControllerManagerRegistry);
humanoidHighLevelControllerManagerRegistry.addChild(walkingParentRegistry);
humanoidHighLevelControllerManagerRegistry.addChild(managerParentRegistry);
ContactableBodiesFactory<RobotSide> contactableBodiesFactory = new ContactableBodiesFactory<>();
RobotContactPointParameters<RobotSide> contactPointParameters = robotModel.getContactPointParameters();
contactableBodiesFactory.setFootContactPoints(contactPointParameters.getFootContactPoints());
contactableBodiesFactory.setToeContactParameters(contactPointParameters.getControllerToeContactPoints(),
contactPointParameters.getControllerToeContactLines());
for (int i = 0; i < contactPointParameters.getAdditionalContactNames().size(); i++)
{
contactableBodiesFactory.addAdditionalContactPoint(contactPointParameters.getAdditionalContactRigidBodyNames().get(i),
contactPointParameters.getAdditionalContactNames().get(i),
contactPointParameters.getAdditionalContactTransforms().get(i));
}
contactableBodiesFactory.setFullRobotModel(fullRobotModel);
contactableBodiesFactory.setReferenceFrames(referenceFrames);
SideDependentList<ContactableFoot> feet = new SideDependentList<>(contactableBodiesFactory.createFootContactableFeet());
List<ContactablePlaneBody> allContactableBodies = new ArrayList<>(contactableBodiesFactory.createAdditionalContactPoints());
allContactableBodies.addAll(feet.values());
contactableBodiesFactory.disposeFactory();
double totalRobotWeight = MultiBodySystemMissingTools.computeSubTreeMass(fullRobotModel.getElevator());
for (RobotSide robotSide : RobotSide.values)
{
SettableFootSwitch footSwitch = new SettableFootSwitch(feet.get(robotSide), totalRobotWeight, 2, registry);
footSwitch.setFootContactState(true);
footSwitches.put(robotSide, footSwitch);
}
JointBasics[] jointsToIgnore = AvatarControllerThread.createListOfJointsToIgnore(fullRobotModel, robotModel, robotModel.getSensorInformation());
controllerToolbox = new HighLevelHumanoidControllerToolbox(fullRobotModel,
centerOfMassStateProvider,
referenceFrames,
footSwitches,
null,
yoTime,
GRAVITY_Z,
robotModel.getWalkingControllerParameters().getOmega0(),
feet,
kinematicsSimulationParameters.getDt(),
false,
Collections.emptyList(),
allContactableBodies,
yoGraphicsListRegistry,
jointsToIgnore);
humanoidHighLevelControllerManagerRegistry.addChild(controllerToolbox.getYoVariableRegistry());
WalkingControllerParameters walkingControllerParameters = robotModel.getWalkingControllerParameters();
CoPTrajectoryParameters copTrajectoryParameters = robotModel.getCoPTrajectoryParameters();
WalkingMessageHandler walkingMessageHandler = new WalkingMessageHandler(walkingControllerParameters.getDefaultTransferTime(),
walkingControllerParameters.getDefaultSwingTime(),
walkingControllerParameters.getDefaultInitialTransferTime(),
walkingControllerParameters.getDefaultFinalTransferTime(),
controllerToolbox.getContactableFeet(),
walkingOutputManager,
yoTime,
yoGraphicsListRegistry,
controllerToolbox.getYoVariableRegistry());
controllerToolbox.setWalkingMessageHandler(walkingMessageHandler);
controllerToolbox.attachRobotMotionStatusChangedListener((newStatus, time) -> robotMotionStatusHolder.setCurrentRobotMotionStatus(newStatus));
// Initializes this desired robot to the most recent robot configuration data received from the walking controller.
RobotInitialSetup<HumanoidFloatingRootJointRobot> robotInitialSetup = robotModel.getDefaultRobotInitialSetup(kinematicsSimulationParameters.getInitialGroundHeight(),
kinematicsSimulationParameters.getInitialRobotYaw(),
kinematicsSimulationParameters.getInitialRobotX(),
kinematicsSimulationParameters.getInitialRobotY(),
kinematicsSimulationParameters.getInitialRobotZ());
robotInitialSetup.initializeFullRobotModel(fullRobotModel);
managerFactory = new HighLevelControlManagerFactory(managerParentRegistry);
managerFactory.setHighLevelHumanoidControllerToolbox(controllerToolbox);
managerFactory.setCopTrajectoryParameters(copTrajectoryParameters);
managerFactory.setWalkingControllerParameters(walkingControllerParameters);
walkingController = new WalkingHighLevelHumanoidController(walkingInputManager,
walkingOutputManager,
managerFactory,
walkingControllerParameters,
controllerToolbox);
walkingParentRegistry.addChild(walkingController.getYoVariableRegistry());
// create controller network subscriber here!!
realtimeROS2Node = ROS2Tools.createRealtimeROS2Node(kinematicsSimulationParameters.getPubSubImplementation(),
HumanoidControllerAPI.HUMANOID_KINEMATICS_CONTROLLER_NODE_NAME + "_rt");
ROS2Topic inputTopic = HumanoidControllerAPI.getInputTopic(robotName);
ROS2Topic outputTopic = HumanoidControllerAPI.getOutputTopic(robotName);
ControllerNetworkSubscriber controllerNetworkSubscriber = new ControllerNetworkSubscriber(inputTopic,
walkingInputManager,
outputTopic,
walkingOutputManager,
realtimeROS2Node);
controllerNetworkSubscriber.addMessageFilter(message ->
{
if (message instanceof FootstepDataListMessage)
{
FootstepDataListMessage footstepDataListMessage = (FootstepDataListMessage) message;
footstepDataListMessage.setOffsetFootstepsHeightWithExecutionError(false); // fixes +Z drift for each step
}
return true;
});
walkingInputManager.registerConversionHelper(new FrameMessageCommandConverter(controllerToolbox.getReferenceFrameHashCodeResolver()));
controllerNetworkSubscriber.registerSubcriberWithMessageUnpacker(WholeBodyTrajectoryMessage.class,
9,
MessageUnpackingTools.createWholeBodyTrajectoryMessageUnpacker());
controllerNetworkSubscriber.registerSubcriberWithMessageUnpacker(WholeBodyStreamingMessage.class,
9,
MessageUnpackingTools.createWholeBodyStreamingMessageUnpacker());
controllerNetworkSubscriber.addMessageCollectors(ControllerAPIDefinition.createDefaultMessageIDExtractor(), 3);
controllerNetworkSubscriber.addMessageValidator(ControllerAPIDefinition.createDefaultMessageValidation());
simulatedHandKinematicController = robotModel.createSimulatedHandKinematicController(fullRobotModel, realtimeROS2Node, yoTime);
robotConfigurationDataPublisher = createRobotConfigurationDataPublisher(robotModel.getSimpleRobotName());
realtimeROS2Node.spin();
WholeBodyControlCoreToolbox controlCoreToolbox = new WholeBodyControlCoreToolbox(kinematicsSimulationParameters.getDt(),
GRAVITY_Z,
fullRobotModel.getRootJoint(),
controllerToolbox.getControlledJoints(),
controllerToolbox.getCenterOfMassFrame(),
walkingControllerParameters.getMomentumOptimizationSettings(),
yoGraphicsListRegistry,
registry);
controlCoreToolbox.setJointPrivilegedConfigurationParameters(walkingControllerParameters.getJointPrivilegedConfigurationParameters());
controlCoreToolbox.setFeedbackControllerSettings(walkingControllerParameters.getFeedbackControllerSettings());
controlCoreToolbox.setupForInverseDynamicsSolver(controllerToolbox.getContactablePlaneBodies());
controllerCore = new WholeBodyControllerCore(controlCoreToolbox,
managerFactory.createFeedbackControlTemplate(),
new JointDesiredOutputList(controllerToolbox.getControlledOneDoFJoints()),
walkingParentRegistry);
jointDesiredOutputList = controllerCore.getOutputForLowLevelController();
walkingController.setControllerCoreOutput(controllerCore.getOutputForHighLevelController());
linearMomentumRateControlModule = new LinearMomentumRateControlModule(centerOfMassStateProvider,
referenceFrames,
controllerToolbox.getContactableFeet(),
fullRobotModel.getElevator(),
walkingControllerParameters,
controllerToolbox.getWalkingMessageHandler(),
controllerToolbox.getTotalMassProvider(),
GRAVITY_Z,
controllerToolbox.getControlDT(),
walkingParentRegistry,
yoGraphicsListRegistry);
ParameterLoaderHelper.loadParameters(this, robotModel, drcControllerThreadRegistry);
YoVariable defaultHeight = registry.findVariable(PelvisHeightControlState.class.getSimpleName(),
PelvisHeightControlState.class.getSimpleName() + "DefaultHeight");
if (Double.isNaN(defaultHeight.getValueAsDouble()))
{
throw new RuntimeException("Need to load a default height.");
}
if (kinematicsSimulationParameters.getLogToFile())
{
Path incomingLogsDirectory;
intraprocessYoVariableLogger = new IntraprocessYoVariableLogger(getClass().getSimpleName(),
robotModel.getLogModelProvider(),
registry,
fullRobotModel.getElevator(),
yoGraphicsListRegistry,
100000,
0.01);
intraprocessYoVariableLogger.start();
}
if (kinematicsSimulationParameters.getCreateYoVariableServer())
{
LogTools.info("Starting YoVariable server...");
yoVariableServer = new YoVariableServer(getClass().getSimpleName(), robotModel.getLogModelProvider(), new DataServerSettings(false), 0.01);
// Some robots have four bar linkages and need the `getSubtreeJointsIncludingFourBars` call
yoVariableServer.setMainRegistry(registry,
MultiBodySystemMissingTools.getSubtreeJointsIncludingFourBars(fullRobotModel.getElevator()),
yoGraphicsListRegistry);
yoVariableServer.start();
LogTools.info("YoVariable server started.");
}
walkingOutputManager.attachStatusMessageListener(FootstepStatusMessage.class, this::processFootstepStatus);
walkingOutputManager.attachStatusMessageListener(WalkingStatusMessage.class, this::processWalkingStatus);
if (kinematicsSimulationParameters.isPeriodicThreadEnabled())
{
controlThread = new PausablePeriodicThread(getClass().getSimpleName(), kinematicsSimulationParameters.getUpdatePeriod(), 5, this::controllerTick);
}
else
{
controlThread = null;
}
}
public RobotConfigurationDataPublisher createRobotConfigurationDataPublisher(String robotName)
{
RobotConfigurationDataPublisherFactory rcdPublisherFactory = new RobotConfigurationDataPublisherFactory();
rcdPublisherFactory.setDefinitionsToPublish(fullRobotModel);
SensorTimestampHolder sensorTimestampHolder = new SensorTimestampHolder()
{
@Override
public long getWallTime()
{
return Conversions.secondsToNanoseconds(yoTime.getValue());
}
@Override
public long getSyncTimestamp()
{
return getWallTime();
}
@Override
public long getMonotonicTime()
{
return getWallTime();
}
};
FloatingJointStateReadOnly rootJointStateOutput = FloatingJointStateReadOnly.fromFloatingJoint(fullRobotModel.getRootJoint());
List<OneDoFJointStateReadOnly> jointSensorOutputs = new ArrayList<>();
for (OneDoFJointReadOnly joint : FullRobotModelUtils.getAllJointsExcludingHands(fullRobotModel))
{
jointSensorOutputs.add(OneDoFJointStateReadOnly.createFromOneDoFJoint(joint, true));
}
ForceSensorDataHolderReadOnly forceSensorDataHolder = new ForceSensorDataHolder(fullRobotModel.getForceSensorDefinitions());
List<IMUSensor> imuSensorOutputs = new ArrayList<>();
for (IMUDefinition imuDefinition : fullRobotModel.getIMUDefinitions())
{
IMUSensor imu = new IMUSensor(imuDefinition, null);
// TODO Consider making an updater that fills in IMU data using controller robot state
imuSensorOutputs.add(imu);
}
rcdPublisherFactory.setSensorSource(sensorTimestampHolder, rootJointStateOutput, jointSensorOutputs, forceSensorDataHolder, imuSensorOutputs);
rcdPublisherFactory.setPublishPeriod(0L);
rcdPublisherFactory.setRobotMotionStatusHolder(robotMotionStatusHolder);
rcdPublisherFactory.setROS2Info(realtimeROS2Node, HumanoidControllerAPI.getOutputTopic(robotName));
return rcdPublisherFactory.createRobotConfigurationDataPublisher();
}
public void setState()
{
}
public void setRunning(boolean running)
{
if(controlThread == null)
{
return;
}
if (running && !controlThread.isRunning())
{
initialize();
controlThread.start();
heartbeat.setAlive(true);
}
else if (!running && controlThread.isRunning())
{
controlThread.stop();
heartbeat.setAlive(false);
}
}
public void initialize()
{
zeroMotion();
referenceFrames.updateFrames();
controllerCore.initialize();
walkingController.initialize();
// walkingController.requestImmediateTransitionToStandingAndHoldCurrent();
for (RobotSide robotSide : RobotSide.values)
{
contactStateHolders.put(robotSide,
KinematicsSimulationContactStateHolder.holdAtCurrent(controllerToolbox.getFootContactStates().get(robotSide)));
}
if (simulatedHandKinematicController != null)
simulatedHandKinematicController.initialize();
monotonicTimer.start();
}
public void controllerTick()
{
updateTimer.reset();
doControl();
robotConfigurationDataPublisher.write();
if (kinematicsSimulationParameters.runNoFasterThanMaxRealtimeRate())
{
while (updateTimer.totalElapsed() < kinematicsSimulationParameters.getDt() / kinematicsSimulationParameters.getMaxRealtimeRate())
ThreadTools.sleep(1);
}
}
public void doControl()
{
yoTime.add(kinematicsSimulationParameters.getDt());
fullRobotModel.updateFrames();
referenceFrames.updateFrames();
controllerToolbox.update();
inverseDynamicsContactHolderCommandList.clear();
for (RobotSide side : contactStateHolders.sides())
{
contactStateHolders.get(side).doControl();
inverseDynamicsContactHolderCommandList.addCommand(contactStateHolders.get(side).getOutput());
}
// Trigger footstep completion based on swing time alone
if (contactStateHolders.sides().length == 1 && managerFactory.getOrCreateBalanceManager().isICPPlanDone())
{
footSwitches.get(contactStateHolders.sides()[0].getOppositeSide()).setFootContactState(true);
}
walkingController.doAction();
linearMomentumRateControlModule.setInputFromWalkingStateMachine(walkingController.getLinearMomentumRateControlModuleInput());
if (!linearMomentumRateControlModule.computeControllerCoreCommands())
{
controllerToolbox.reportControllerFailureToListeners(new FrameVector2D());
}
walkingController.setLinearMomentumRateControlModuleOutput(linearMomentumRateControlModule.getOutputForWalkingStateMachine());
ControllerCoreCommand controllerCoreCommand = walkingController.getControllerCoreCommand();
controllerCoreCommand.addInverseDynamicsCommand(linearMomentumRateControlModule.getMomentumRateCommand());
controllerCoreCommand.addInverseDynamicsCommand(inverseDynamicsContactHolderCommandList);
controllerCore.compute(controllerCoreCommand);
linearMomentumRateControlModule.setInputFromControllerCore(controllerCore.getControllerCoreOutput());
linearMomentumRateControlModule.computeAchievedCMP();
fullRobotModel.getRootJoint().setJointAcceleration(0, controllerCore.getOutputForRootJoint().getDesiredAcceleration());
JointDesiredOutputListReadOnly jointDesiredOutputList = controllerCore.getOutputForLowLevelController();
for (OneDoFJointBasics joint : controllerToolbox.getControlledOneDoFJoints())
{
JointDesiredOutputReadOnly jointDesiredOutput = jointDesiredOutputList.getJointDesiredOutput(joint);
joint.setQdd(jointDesiredOutput.getDesiredAcceleration());
joint.setTau(jointDesiredOutput.getDesiredTorque());
}
integrator.setIntegrationDT(kinematicsSimulationParameters.getDt());
integrator.doubleIntegrateFromAcceleration(Arrays.asList(controllerToolbox.getControlledJoints()));
// spin lidar
JointBasics hokuyoJoint = fullRobotModel.getLidarJoint("head_hokuyo_sensor");
if (hokuyoJoint instanceof RevoluteJoint)
{
RevoluteJoint revoluteHokuyoJoint = (RevoluteJoint) hokuyoJoint;
revoluteHokuyoJoint.setQ(revoluteHokuyoJoint.getQ() + LIDAR_SPINDLE_SPEED * kinematicsSimulationParameters.getUpdatePeriod());
}
if (simulatedHandKinematicController != null)
simulatedHandKinematicController.doControl();
yoVariableServerTime += Conversions.millisecondsToSeconds(1);
if (kinematicsSimulationParameters.getLogToFile())
{
intraprocessYoVariableLogger.update(Conversions.secondsToNanoseconds(yoVariableServerTime));
}
if (kinematicsSimulationParameters.getCreateYoVariableServer())
{
yoVariableServer.update(Conversions.secondsToNanoseconds(yoVariableServerTime));
}
}
private void zeroMotion()
{
for (JointBasics joint : fullRobotModel.getElevator().childrenSubtreeIterable())
{
joint.setJointAccelerationToZero();
joint.setJointTwistToZero();
}
}
private void processFootstepStatus(FootstepStatusMessage statusMessage)
{
RobotSide side = RobotSide.fromByte(statusMessage.getRobotSide());
FootstepStatus status = FootstepStatus.fromByte(statusMessage.getFootstepStatus());
FramePose3D desiredFootstep = new FramePose3D(worldFrame,
statusMessage.getDesiredFootPositionInWorld(),
statusMessage.getDesiredFootOrientationInWorld());
switch (status)
{
case STARTED:
contactStateHolders.remove(side);
footSwitches.get(side).setFootContactState(false);
break;
case COMPLETED:
contactStateHolders.put(side,
new KinematicsSimulationContactStateHolder(controllerToolbox.getFootContactStates().get(side), desiredFootstep));
break;
default:
throw new RuntimeException("Unexpected status: " + status);
}
}
private void processWalkingStatus(WalkingStatusMessage status)
{
latestWalkingStatus.set(WalkingStatus.fromByte(status.getWalkingStatus()));
}
public void destroy()
{
LogTools.info("Shutting down...");
if (simulatedHandKinematicController != null)
simulatedHandKinematicController.cleanup();
controlThread.destroy();
heartbeat.destroy();
ros2Node.destroy();
realtimeROS2Node.destroy();
if (yoVariableServer != null)
yoVariableServer.close();
}
public FullHumanoidRobotModel getFullRobotModel()
{
return fullRobotModel;
}
public JointDesiredOutputListReadOnly getJointDesiredOutputList()
{
return jointDesiredOutputList;
}
public YoRegistry getYoRegistry()
{
return registry;
}
}