AGX Dynamics  2.37.3.2
ROS2ControlInterface.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 #pragma once
17 #include <agxROS2/export.h>
18 #include <agxROS2/Publisher.h>
19 #include <agxROS2/Subscriber.h>
20 #include <agxROS2/Qos.h>
21 
22 #include <agx/Constraint.h>
24 #include <agxSDK/Assembly.h>
25 #include <agxSDK/Simulation.h>
26 #include <agx/agx_vector_types.h>
27 
28 #include <map>
29 #include <string>
30 #include <tuple>
31 
32 namespace agxROS2
33 {
42  {
51  public:
53  POSITION = 0,
54  VELOCITY = 1,
55  EFFORT = 2
56  };
57 
66  const std::string& jointCommandTopic,
67  const std::string& jointStateTopic,
68  bool syncWithSystemTime = false
69  );
70 
77  bool addJoint(agx::Constraint1DOF* joint, ROS2ControlCommandInterface commandInterface);
78 
84  bool removeJoint(const std::string& name);
85 
91  bool removeJoint(agx::Constraint1DOF* joint);
92 
96  virtual void pre(const agx::TimeStamp& time) override;
97 
101  virtual void post(const agx::TimeStamp& time) override;
102 
103  private:
109  void populateJointStateMessage(const agx::TimeStamp& time);
114  void setCommandOnJoints(agxROS2::sensorMsgs::JointState& jointStateMsg);
120  void setHeader(agxROS2::sensorMsgs::JointState& jointStateMsg, const agx::TimeStamp& time);
121 
122  struct Joint {
123  agx::Constraint1DOFRef constraint;
124  agx::RangeReal effortLimit;
125  ROS2ControlCommandInterface commandInterface;
126  };
127 
128  private:
129  agxROS2::sensorMsgs::PublisherJointState m_pub;
130  agxROS2::sensorMsgs::JointState m_stateMsg;
131 
132  agxROS2::sensorMsgs::SubscriberJointState m_sub;
133  agxROS2::sensorMsgs::JointState m_commandMsg;
134 
135  std::map<std::string, Joint> m_joints;
136 
137  // ros2_control syncs its control loop against the system clock.
138  // even if we publish the simulation time to /clock the ros2_control control
139  // loop will sleep with std::this_thread::sleep_until(next_iteration_time);
140  // where next_iteration_time is 0 and the system clock is larger so it will not sleep at all.
141  // Thus making the controller loop running as fast as possible ~4k hz always depending on hardware.
142  // Currently an issue in ros2_control repo https://github.com/ros-controls/ros2_control/issues/859
143  //
144  // A workaround is to set the simulation time to the system time. This assumes that the simulation
145  // runs in realtime. Not faster or slower. This will not make the control-loop and simulation synchronous,
146  // it only makes the controller update rate to be the specified rate. If the simulation runs faster than
147  // realtime then control signals will be missing when stepping. If it runs slower then the simulation
148  // will miss intermediate control signals.
149  bool m_syncWithSystemTime;
150  };
151 }
Derive from this class to implement a listener for simulation step events.
Possible to add joints (Constraint1DOF) and the listener will publish the position and velocity for t...
ROS2ControlCommandInterface
The ROS2ControlCommandInterface sets how the joints should be controlled.
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Specialization for constraints that have only one degree of freedom such as Hinge and Prismatic...
Definition: Constraint.h:673
agx::Real TimeStamp
Definition: TimeStamp.h:26
#define AGXROS2_EXPORT