25 #include <condition_variable>
29 #include <VirtualRobot/Robot.h>
37 #include <ArmarXSimulation/interface/RobotUnitSimulationInterface.h>
47 using ClockT = std::chrono::high_resolution_clock;
53 return std::chrono::duration_cast<std::chrono::nanoseconds>(
54 ClockT::now().time_since_epoch())
65 std::unique_lock<std::recursive_mutex>
68 return std::unique_lock<std::recursive_mutex>{m};
80 return (lastwriteNs + 1000000000 <
nowNS());
86 tb.reinitAllBuffers(i);
92 return tb.getWriteBuffer();
98 return tb.getReadBuffer();
105 lastwriteNs =
nowNS();
113 tb.updateReadBuffer();
117 mutable std::recursive_mutex m;
119 std::atomic<std::uintmax_t> lastwriteNs{0};
120 std::condition_variable*
cv;
133 defineOptionalProperty<std::string>(
136 "Name of the simulator component that should be used");
137 defineOptionalProperty<bool>(
"SynchronizedSimulation",
139 "If the simulation should be synchronized",
141 defineOptionalProperty<std::size_t>(
142 "ControlIterationMs", 10,
"Time each iteration should take (in ms)");
143 defineOptionalProperty<std::string>(
144 "SimulatorRobotListenerInterfaceTopic",
146 "Name of the simulator topic for SimulatorRobotListenerInterface (empty defaults "
147 "to Simulator_Robot_<ROBOTNAME>");
148 defineOptionalProperty<std::string>(
149 "SimulatorForceTorqueListenerInterfaceTopic",
150 "ForceTorqueDynamicSimulationValues",
151 "Name of the simulator topic for SimulatorForceTorqueListenerInterface");
153 defineOptionalProperty<float>(
"maxLinearPlatformVelocity", 2000,
"[mm/s]");
154 defineOptionalProperty<float>(
"maxAngularPlatformVelocity", 1,
"[rad/s]");
156 defineOptionalProperty<std::string>(
157 "ForceTorqueSensorMapping",
159 "A list of mappings to modify the forceTorque sensors given in the robot format. "
160 "Syntax (comma SV of colon SV): "
161 "'NameInFile:ReportName[:ReportFrame],NameInFile2:ReportName2[:ReportFrame2]' "
162 "This would cause reporting of the ft sensor called 'NameInFile' under the name "
163 "'ReportName' and optionally change the report frame to 'ReportFrame'. "
164 "IMPORTANT: (1) The original reporting frame of the simulator has to be the parent "
165 "node of 'NameInFile'."
166 " (2) The transformation from the original reporting frame to 'ReportFrame' should "
168 "(there should be no joints in the kinematic chain between them),"
169 " since the transformation is done without access to the joint values");
186 virtual public RobotUnitSimulationSinterface
190 torquesTB{&cvGotSensorData},
191 anglesTB{&cvGotSensorData},
192 velocitiesTB{&cvGotSensorData},
193 robPoseTB{&cvGotSensorData},
194 robVelTB{&cvGotSensorData},
195 forceTorqueTB{&cvGotSensorData}
205 return "RobotUnitSimulation";
244 const Ice::Current& = Ice::emptyCurrent)
override;
247 void updateForceTorque(ForceTorqueData
const& ftData,
IceUtil::Time timestamp);
250 const std::string name)
const;
251 void rtUpdateSensors(
bool wait);
252 void rtSendCommands();
254 bool skipReport()
const;
257 getMappedFTName(
const std::string& name)
const
259 return ftMappings.count(name) ? ftMappings.at(name).sensorName : name;
263 getMappedFTReportingFrame(
const std::string& name,
const std::string& unmappedFrame)
const
265 return ftMappings.count(name) ? ftMappings.at(name).reportFrame : unmappedFrame;
269 getMappedFTReportingTransformation(
const std::string& name)
const
271 return ftMappings.count(name) ? ftMappings.at(name).reportTransformation
275 void rtPollRobotState();
276 std::atomic_bool isPolling{
false};
280 Eigen::Vector3f lin{std::nanf(
""), std::nanf(
""), std::nanf(
"")};
281 Eigen::Vector3f ang{std::nanf(
""), std::nanf(
""), std::nanf(
"")};
286 Eigen::Vector3f force{std::nanf(
""), std::nanf(
""), std::nanf(
"")};
287 Eigen::Vector3f torque{std::nanf(
""), std::nanf(
""), std::nanf(
"")};
293 std::condition_variable cvGotSensorData;
294 std::atomic<bool> gotSensorData{
false};
297 KeyValueVector<std::string, JointSimulationDevicePtr> jointDevs;
298 TripleBufferWithGuardAndTime<std::vector<float>> torquesTB;
299 TripleBufferWithGuardAndTime<std::vector<float>> anglesTB;
300 TripleBufferWithGuardAndTime<std::vector<float>> velocitiesTB;
305 PlatformSimulationDevicePtr platformDev;
307 TripleBufferWithGuardAndTime<Eigen::Matrix4f> robPoseTB;
308 TripleBufferWithGuardAndTime<RobVel> robVelTB;
310 KeyValueVector<std::string, ForceTorqueSimulationSensorDevicePtr> forceTorqueDevs;
311 TripleBufferWithGuardAndTime<std::vector<FT>> forceTorqueTB;
312 std::vector<AtomicWrapper<std::uintmax_t>> forceTorqueTimes;
316 std::string sensorName;
317 std::string originalReportFrame;
318 std::string reportFrame;
322 std::map<std::string, FTMappingData> ftMappings;
330 std::atomic_bool synchronizedSimulation;
332 std::string simulatorRobotListenerInterfaceTopic;
333 std::string simulatorForceTorqueListenerInterfaceTopic;
335 std::atomic<std::uintmax_t> iterationCount{0};
342 return controlIterationMs;