27 #include <condition_variable>
29 #include <VirtualRobot/Robot.h>
37 #include <ArmarXSimulation/interface/RobotUnitSimulationInterface.h>
47 using ClockT = std::chrono::high_resolution_clock;
52 return std::chrono::duration_cast<std::chrono::nanoseconds>(ClockT::now().time_since_epoch()).count();
60 std::unique_lock<std::recursive_mutex>
guard()
62 return std::unique_lock<std::recursive_mutex> {m};
71 return (lastwriteNs + 1000000000 <
nowNS());
76 tb.reinitAllBuffers(i);
81 return tb.getWriteBuffer();
85 return tb.getReadBuffer();
90 lastwriteNs =
nowNS();
96 tb.updateReadBuffer();
100 mutable std::recursive_mutex m;
102 std::atomic<std::uintmax_t> lastwriteNs {0};
103 std::condition_variable*
cv;
119 defineOptionalProperty<std::string>(
"SimulatorName",
"Simulator",
"Name of the simulator component that should be used");
121 defineOptionalProperty<std::size_t>(
"ControlIterationMs", 10,
"Time each iteration should take (in ms)");
122 defineOptionalProperty<std::string>(
"SimulatorRobotListenerInterfaceTopic",
"",
"Name of the simulator topic for SimulatorRobotListenerInterface (empty defaults to Simulator_Robot_<ROBOTNAME>");
123 defineOptionalProperty<std::string>(
"SimulatorForceTorqueListenerInterfaceTopic",
"ForceTorqueDynamicSimulationValues",
"Name of the simulator topic for SimulatorForceTorqueListenerInterface");
125 defineOptionalProperty<float>(
"maxLinearPlatformVelocity", 2000,
"[mm/s]");
126 defineOptionalProperty<float>(
"maxAngularPlatformVelocity", 1,
"[rad/s]");
128 defineOptionalProperty<std::string>(
129 "ForceTorqueSensorMapping",
"",
130 "A list of mappings to modify the forceTorque sensors given in the robot format. "
131 "Syntax (comma SV of colon SV): 'NameInFile:ReportName[:ReportFrame],NameInFile2:ReportName2[:ReportFrame2]' "
132 "This would cause reporting of the ft sensor called 'NameInFile' under the name 'ReportName' and optionally change the report frame to 'ReportFrame'. "
133 "IMPORTANT: (1) The original reporting frame of the simulator has to be the parent node of 'NameInFile'."
134 " (2) The transformation from the original reporting frame to 'ReportFrame' should be fix "
135 "(there should be no joints in the kinematic chain between them),"
136 " since the transformation is done without access to the joint values"
155 virtual public RobotUnitSimulationSinterface
159 torquesTB {&cvGotSensorData},
160 anglesTB {&cvGotSensorData},
161 velocitiesTB {&cvGotSensorData},
162 robPoseTB {&cvGotSensorData},
163 robVelTB {&cvGotSensorData},
164 forceTorqueTB {&cvGotSensorData}
171 return "RobotUnitSimulation";
207 void reportState(SimulatedRobotState
const& state,
const Ice::Current& = Ice::emptyCurrent)
override;
210 void updateForceTorque(ForceTorqueData
const& ftData,
IceUtil::Time timestamp);
212 void rtUpdateSensors(
bool wait);
213 void rtSendCommands();
215 bool skipReport()
const;
217 const std::string& getMappedFTName(
const std::string& name)
const
219 return ftMappings.count(name) ? ftMappings.at(name).sensorName : name;
221 const std::string& getMappedFTReportingFrame(
const std::string& name,
const std::string& unmappedFrame)
const
223 return ftMappings.count(name) ? ftMappings.at(name).reportFrame : unmappedFrame;
225 Eigen::Matrix3f getMappedFTReportingTransformation(
const std::string& name)
const
230 void rtPollRobotState();
231 std::atomic_bool isPolling {
false};
235 Eigen::Vector3f lin {std::nanf(
""), std::nanf(
""), std::nanf(
"")};
236 Eigen::Vector3f ang {std::nanf(
""), std::nanf(
""), std::nanf(
"")};
240 Eigen::Vector3f force {std::nanf(
""), std::nanf(
""), std::nanf(
"")};
241 Eigen::Vector3f torque {std::nanf(
""), std::nanf(
""), std::nanf(
"")};
246 std::condition_variable cvGotSensorData;
247 std::atomic<bool> gotSensorData{
false};
250 KeyValueVector<std::string, JointSimulationDevicePtr> jointDevs;
251 TripleBufferWithGuardAndTime<std::vector<float>> torquesTB;
252 TripleBufferWithGuardAndTime<std::vector<float>> anglesTB;
253 TripleBufferWithGuardAndTime<std::vector<float>> velocitiesTB;
258 PlatformSimulationDevicePtr platformDev;
260 TripleBufferWithGuardAndTime<Eigen::Matrix4f> robPoseTB;
261 TripleBufferWithGuardAndTime<RobVel> robVelTB;
263 KeyValueVector<std::string, ForceTorqueSimulationSensorDevicePtr> forceTorqueDevs;
264 TripleBufferWithGuardAndTime<std::vector<FT>> forceTorqueTB;
265 std::vector<AtomicWrapper<std::uintmax_t>> forceTorqueTimes;
268 std::string sensorName;
269 std::string originalReportFrame;
270 std::string reportFrame;
273 std::map<std::string, FTMappingData> ftMappings;
281 std::atomic_bool synchronizedSimulation;
283 std::string simulatorRobotListenerInterfaceTopic;
284 std::string simulatorForceTorqueListenerInterfaceTopic;
286 std::atomic<std::uintmax_t> iterationCount {0};
292 return controlIterationMs;