diffbot_hw_interface.h
Go to the documentation of this file.
1 #ifndef DIFFBOT_HW_INTERFACE_H
2 #define DIFFBOT_HW_INTERFACE_H
3 
4 
5 // ROS
6 #include <ros/ros.h>
7 #include <urdf/model.h>
11 #include <sensor_msgs/JointState.h>
12 
13 // ROS Controls
17 
18 #include <diffbot_base/pid.h>
19 
20 
21 namespace diffbot_base
22 {
23  const unsigned int NUM_JOINTS = 2;
24 
25  struct JointState
26  {
29  };
30 
33  {
34  public:
40  DiffBotHWInterface(ros::NodeHandle &nh, urdf::Model *urdf_model = NULL);
41 
43  virtual ~DiffBotHWInterface() {}
44 
64  virtual bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh);
65 
82  virtual void read(const ros::Time& time, const ros::Duration& period) override;
83 
100  virtual void write(const ros::Time& time, const ros::Duration& period);
101 
111 
113  virtual void printState();
114  std::string printStateHelper();
115 
117  std::string printCommandHelper();
118 
119  protected:
120 
122  virtual void loadURDF(const ros::NodeHandle& nh, std::string param_name);
123 
125  void encoderTicksCallback(const diffbot_msgs::EncodersStamped::ConstPtr& msg_encoders);
126 
128  void measuredJointStatesCallback(const sensor_msgs::JointState::ConstPtr& msg_joint_states);
129 
131  double ticksToAngle(const int &ticks) const;
132 
134  double normalizeAngle(double &angle) const;
135 
136 
137  // The following functions are currently unused
138  // DiffBot directly calculates normalized angles from encoder ticks
139  // The joint_commands from ros_control are mapped to percentage values for the motor driver
140  // The following comments are incorrect
142  double linearToAngular(const double &distance) const;
144  double angularToLinear(const double &angle) const;
145 
146 
147  // Short name of this class
148  std::string name_;
149 
150  // Startup and shutdown of the internal node inside a roscpp program
152 
153  // Hardware interfaces
154  // hardware_interface::JointStateInterface gives read access to all joint values
155  // without conflicting with other controllers.
157  // hardware_interface::VelocityJointInterface inherits from
158  // hardware_interface::JointCommandInterface and is used for reading and writing
159  // joint velocities. Because this interface reserves the joints for write access,
160  // conflicts with other controllers writing to the same joints might occure.
161  // To only read joint velocities, avoid conflicts using
162  // hardware_interface::JointStateInterface.
164 
165  // Configuration
166  std::vector<std::string> joint_names_;
167  std::size_t num_joints_;
169 
173 
174  // Hardware related parameters to hold values from the parameter server
175  // The parameters are defined in diffbot_base/config/base.yaml
177  // Parameters for the gain trim model
178  double gain_;
179  double trim_;
180  // Assume the same motor constant for both motors
182  double pwm_limit_;
183 
184  // Enable/Disable debug output
185  // Setting only possible during first start of the hw interface
186  // to avoid reading permanently form the parameter server
187  bool debug_;
188 
189 
190  // Data member array to store the controller commands which are sent to the
191  // robot's resources (joints, actuators)
192  // The diff_drive_controller uses the hardware_interface::VelocityJointInterface
193  // It provides semantic meaning to the wheel joints describing that
194  // they require velocity commands.
196 
197  // Data member arrays to store the state of the robot's resources (joints, sensors)
198  // These values are filled in the read() method and are registered to the
199  // joint_state_interface_ of type hardware_interface::JointStateInterface.
203 
206 
207  // Declare publishers for the motor driver
210 
211 
212  // Declare publishers for angular wheel joint velocities
214 
215  // Declare publisher to reset the wheel encoders
216  // used during first launch of hardware interface to avoid large difference in encoder ticks from a previous run
218  // Declare subscriber for the wheel encoders
219  // This subscriber receives the encoder ticks in the custom diffbot_msgs/Encoder message
221  // Declare subscriber for the measured joint states (absolute angular position (rad) and angular velocity (rad/s) of the wheel)
222  // This subscriber receives the measured angular wheel joint velocity in the custom diffbot_msgs/AngularVelocitiesStamped message
224 
225  // Array to store the received encoder tick values from the \ref sub_encoder_ticks_ subscriber
228 
230  }; // class DiffBotHWInterface
231 
232 } // namespace
233 
234 #endif // DIFFBOT_HW_INTERFACE_H
diffbot_base::DiffBotHWInterface::velocity_joint_interface_
hardware_interface::VelocityJointInterface velocity_joint_interface_
Definition: diffbot_hw_interface.h:163
ros::Publisher
diffbot_base::DiffBotHWInterface::pub_left_motor_value_
ros::Publisher pub_left_motor_value_
Definition: diffbot_hw_interface.h:208
hardware_interface::JointStateInterface
diffbot_base::DiffBotHWInterface::pids_
PID pids_[NUM_JOINTS]
Definition: diffbot_hw_interface.h:229
WheelsCmdStamped.h
ros.h
diffbot_base::DiffBotHWInterface::read
virtual void read(const ros::Time &time, const ros::Duration &period) override
Read data from the robot hardware.
Definition: diffbot_hw_interface.cpp:136
diffbot_base::DiffBotHWInterface::write
virtual void write(const ros::Time &time, const ros::Duration &period)
Write commands to the robot hardware.
Definition: diffbot_hw_interface.cpp:163
diffbot_base::DiffBotHWInterface::printState
virtual void printState()
Helper for debugging a joint's state.
Definition: diffbot_hw_interface.cpp:323
diffbot_base::DiffBotHWInterface::encoderTicksCallback
void encoderTicksCallback(const diffbot_msgs::EncodersStamped::ConstPtr &msg_encoders)
Callback to receive the encoder ticks from Teensy MCU.
Definition: diffbot_hw_interface.cpp:358
diffbot_base::DiffBotHWInterface::joint_state_interface_
hardware_interface::JointStateInterface joint_state_interface_
Definition: diffbot_hw_interface.h:156
diffbot_base::DiffBotHWInterface::sub_measured_joint_states_
ros::Subscriber sub_measured_joint_states_
Definition: diffbot_hw_interface.h:223
diffbot_base::DiffBotHWInterface::linearToAngular
double linearToAngular(const double &distance) const
DiffBot reports travel distance in metres, need radians for ros_control RobotHW.
Definition: diffbot_hw_interface.cpp:401
urdf::Model
diffbot_base::DiffBotHWInterface::printStateHelper
std::string printStateHelper()
Definition: diffbot_hw_interface.cpp:330
diffbot_base::DiffBotHWInterface::loadURDF
virtual void loadURDF(const ros::NodeHandle &nh, std::string param_name)
Get the URDF XML from the parameter server.
Definition: diffbot_hw_interface.cpp:292
diffbot_base::DiffBotHWInterface::printCommandHelper
std::string printCommandHelper()
Helper for debugging a joint's command.
Definition: diffbot_hw_interface.cpp:344
diffbot_base::DiffBotHWInterface::sub_encoder_ticks_
ros::Subscriber sub_encoder_ticks_
Definition: diffbot_hw_interface.h:220
diffbot_base::DiffBotHWInterface::pub_right_motor_value_
ros::Publisher pub_right_motor_value_
Definition: diffbot_hw_interface.h:209
ros::ServiceServer
diffbot_base::DiffBotHWInterface::joint_velocity_commands_
double joint_velocity_commands_[NUM_JOINTS]
Definition: diffbot_hw_interface.h:195
diffbot_base::DiffBotHWInterface::measured_joint_states_
JointState measured_joint_states_[NUM_JOINTS]
Definition: diffbot_hw_interface.h:227
diffbot_base::DiffBotHWInterface::angularToLinear
double angularToLinear(const double &angle) const
RobotHW provides velocity command in rad/s, DiffBot needs m/s.
Definition: diffbot_hw_interface.cpp:406
diffbot_base::DiffBotHWInterface::srv_stop_
ros::ServiceServer srv_stop_
Definition: diffbot_hw_interface.h:205
diffbot_base::DiffBotHWInterface::nh_
ros::NodeHandle nh_
Definition: diffbot_hw_interface.h:151
diffbot_base::DiffBotHWInterface::encoder_resolution_
double encoder_resolution_
Definition: diffbot_hw_interface.h:176
hardware_interface::VelocityJointInterface
diffbot_base::DiffBotHWInterface::init
virtual bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh)
The init function is called to initialize the RobotHW from a non-realtime thread.
Definition: diffbot_hw_interface.cpp:79
diffbot_base::DiffBotHWInterface::pwm_limit_
double pwm_limit_
Definition: diffbot_hw_interface.h:182
diffbot_base::DiffBotHWInterface::DiffBotHWInterface
DiffBotHWInterface(ros::NodeHandle &nh, urdf::Model *urdf_model=NULL)
Constructor.
Definition: diffbot_hw_interface.cpp:14
joint_command_interface.h
pid.h
hardware_interface::RobotHW
diffbot_base::DiffBotHWInterface::normalizeAngle
double normalizeAngle(double &angle) const
Normalize angle in the range of [0, 360)
Definition: diffbot_hw_interface.cpp:388
EncodersStamped.h
diffbot_base::JointState::angular_position_
float angular_position_
Definition: diffbot_hw_interface.h:27
model.h
diffbot_base::DiffBotHWInterface::~DiffBotHWInterface
virtual ~DiffBotHWInterface()
Destructor.
Definition: diffbot_hw_interface.h:43
diffbot_base::DiffBotHWInterface::srv_start_
ros::ServiceServer srv_start_
Definition: diffbot_hw_interface.h:204
nh
ros::NodeHandle nh
Definition: main.cpp:8
diffbot_base::DiffBotHWInterface::wheel_radius_
double wheel_radius_
Definition: diffbot_hw_interface.h:170
diffbot_base::DiffBotHWInterface::trim_
double trim_
Definition: diffbot_hw_interface.h:179
diffbot_base::DiffBotHWInterface::joint_velocities_
double joint_velocities_[NUM_JOINTS]
Definition: diffbot_hw_interface.h:201
diffbot_base::DiffBotHWInterface::pub_reset_encoders_
ros::Publisher pub_reset_encoders_
Definition: diffbot_hw_interface.h:217
diffbot_base::DiffBotHWInterface::gain_
double gain_
Definition: diffbot_hw_interface.h:178
diffbot_base::DiffBotHWInterface::joint_names_
std::vector< std::string > joint_names_
Definition: diffbot_hw_interface.h:166
diffbot_base::DiffBotHWInterface::encoder_ticks_
int encoder_ticks_[NUM_JOINTS]
Definition: diffbot_hw_interface.h:226
diffbot_base::DiffBotHWInterface::joint_efforts_
double joint_efforts_[NUM_JOINTS]
Definition: diffbot_hw_interface.h:202
diffbot_base::PID
Definition: include/diffbot_base/pid.h:11
diffbot_base::DiffBotHWInterface::urdf_model_
urdf::Model * urdf_model_
Definition: diffbot_hw_interface.h:168
diffbot_base::NUM_JOINTS
const unsigned int NUM_JOINTS
Definition: diffbot_hw_interface.h:23
diffbot_base::DiffBotHWInterface::pub_wheel_cmd_velocities_
ros::Publisher pub_wheel_cmd_velocities_
Definition: diffbot_hw_interface.h:213
diffbot_base::DiffBotHWInterface::measuredJointStatesCallback
void measuredJointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg_joint_states)
Callback to receive the measured joint states from Teensy MCU.
Definition: diffbot_hw_interface.cpp:367
joint_state_interface.h
diffbot_base
Definition: diffbot_hw_interface.h:21
diffbot_base::DiffBotHWInterface
Hardware interface for a robot.
Definition: diffbot_hw_interface.h:32
diffbot_base::DiffBotHWInterface::joint_positions_
double joint_positions_[NUM_JOINTS]
Definition: diffbot_hw_interface.h:200
ros::Time
diffbot_base::JointState::angular_velocity_
float angular_velocity_
Definition: diffbot_hw_interface.h:28
diffbot_base::JointState
Definition: diffbot_hw_interface.h:25
robot_hw.h
diffbot_base::DiffBotHWInterface::ticksToAngle
double ticksToAngle(const int &ticks) const
Convert number of encoder ticks to angle in radians.
Definition: diffbot_hw_interface.cpp:380
diffbot_base::DiffBotHWInterface::max_velocity_
double max_velocity_
Definition: diffbot_hw_interface.h:172
AngularVelocitiesStamped.h
diffbot_base::DiffBotHWInterface::motor_constant_
double motor_constant_
Definition: diffbot_hw_interface.h:181
ros::Duration
diffbot_base::DiffBotHWInterface::num_joints_
std::size_t num_joints_
Definition: diffbot_hw_interface.h:167
diffbot_base::DiffBotHWInterface::wheel_diameter_
double wheel_diameter_
Definition: diffbot_hw_interface.h:171
diffbot_base::DiffBotHWInterface::name_
std::string name_
Definition: diffbot_hw_interface.h:148
diffbot_base::DiffBotHWInterface::isReceivingMeasuredJointStates
bool isReceivingMeasuredJointStates(const ros::Duration &timeout=ros::Duration(1))
Check if measured joint states (position and velocity) are received.
Definition: diffbot_hw_interface.cpp:264
ros::NodeHandle
ros::Subscriber
diffbot_base::DiffBotHWInterface::debug_
bool debug_
Definition: diffbot_hw_interface.h:187


diffbot_base
Author(s):
autogenerated on Thu Sep 7 2023 02:35:23