mrp2_hardware.h
Go to the documentation of this file.
1 // ROS
2  #include <ros/ros.h>
3  #include <std_msgs/Float64.h>
4  #include <std_msgs/Float32.h>
5  #include <std_srvs/Empty.h>
6  #include <std_msgs/Empty.h>
7  #include <std_msgs/Int32.h>
8  #include <std_msgs/MultiArrayLayout.h>
9  #include <std_msgs/MultiArrayDimension.h>
10  #include <std_msgs/Int32MultiArray.h>
11  #include <std_msgs/Float32MultiArray.h>
12  #include <std_msgs/Bool.h>
13 
14  #include <nav_msgs/Odometry.h>
15  #include <sensor_msgs/Range.h>
16 
17  // ros_control
24 
25  // NaN
26  #include <limits>
27 
28  // ostringstream
29  #include <sstream>
30 
32 
33  #include <dynamic_reconfigure/server.h>
34  #include <mrp2_hardware/ParametersConfig.h>
35 
37  {
38  public:
40  : running_(true)
43  {
44 
45  pos_[0] = 0.0; pos_[1] = 0.0;
46  vel_[0] = 0.0; vel_[1] = 0.0;
47  eff_[0] = 0.0; eff_[1] = 0.0;
48  cmd_[0] = 0.0; cmd_[1] = 0.0;
49 
50  hardware_interface::JointStateHandle state_handle_1("wheel_right_joint", &pos_[0], &vel_[0], &eff_[0]);
51  jnt_state_interface_.registerHandle(state_handle_1);
52 
53  hardware_interface::JointStateHandle state_handle_2("wheel_left_joint", &pos_[1], &vel_[1], &eff_[1]);
54  jnt_state_interface_.registerHandle(state_handle_2);
55 
57 
58  hardware_interface::JointHandle vel_handle_1(jnt_state_interface_.getHandle("wheel_right_joint"), &cmd_[0]);
59  jnt_vel_interface_.registerHandle(vel_handle_1);
60 
61  hardware_interface::JointHandle vel_handle_2(jnt_state_interface_.getHandle("wheel_left_joint"), &cmd_[1]);
62  jnt_vel_interface_.registerHandle(vel_handle_2);
63 
65 
66  /*hardware_interface::ImuSensorHandle::Data data;
67  data.name = "base_imu";
68  data.frame_id = "base_imu_link";
69  hardware_interface::ImuSensorHandle imu_handle(data);
70  imu_sens_interface_.registerHandle(imu_handle);*/
71 
72  sym_tuning = false;
73  publish_pos = false;
74  publish_ref = false;
75  publish_feed = false;
76 
77  //registerInterface(&imu_sens_interface_);
78  //server = new dynamic_reconfigure::Server<mrp2_hardware::ParametersConfig>(dynamic_reconfigure_mutex_, nh_);
79  f = boost::bind(&MRP2HW::callback, this, _1, _2);
80  server.setCallback(f);
81 
82  pos_reset_sub = nh_.subscribe<std_msgs::Empty>("positions_reset", 1, &MRP2HW::positions_reset_callback, this);
83  estop_clear_sub = nh_.subscribe<std_msgs::Empty>("estop_clear", 1, &MRP2HW::estop_clear_callback, this);
84 
85  sep_cmd_vel_pub = nh_.advertise<std_msgs::Float32MultiArray>("motor_controller/sep_cmd_vel", 100);
86 
87  bumpers_pub = nh_.advertise<std_msgs::Int32MultiArray>("bumpers", 100);
88  estop_pub = nh_.advertise<std_msgs::Bool>("estop", 100);
89  estop_btn_pub = nh_.advertise<std_msgs::Bool>("estop_btn", 100);
90  motor_stall_l_pub = nh_.advertise<std_msgs::Bool>("hw_monitor/diagnostics/motor_stall_l", 100);
91  motor_stall_r_pub = nh_.advertise<std_msgs::Bool>("hw_monitor/diagnostics/motor_stall_r", 100);
92  batt_low_pub = nh_.advertise<std_msgs::Bool>("hw_monitor/diagnostics/batt_low", 100);
93  batt_high_pub = nh_.advertise<std_msgs::Bool>("hw_monitor/diagnostics/batt_high", 100);
94  controller_pub = nh_.advertise<std_msgs::Bool>("hw_monitor/diagnostics/controller", 100);
95  aux_lights_pub = nh_.advertise<std_msgs::Bool>("hw_monitor/diagnostics/aux_lights", 100);
96 
97 
98  robot_serial = new MRP2_Serial("/dev/mrp2_powerboard", 921600, "8N1");
99  //robot_serial = new MRP2_Serial(0x0483, 0x5740, 0x81, 0x01);
100  robot_serial->update();
101 
102  bumper_states.resize(4);
103  bumper_states = robot_serial->get_bumpers();
104 
105  // Init first parameter values
106  std::vector<int> imax ;
107 
108 
109  /*if (imax.size() < 1) {
110  ROS_ERROR("MRP2_Hardware: Connection error to Powerboard. Aborting...");
111  exit(0);
112  }*/
113 
114 
115  init_config.BUMPER_ESTOP = robot_serial->get_bumper_estop(true);
116 
117  //robot_serial->set_bumper_estop(0);
118 
119  //boost::recursive_mutex::scoped_lock dyn_reconf_lock(dynamic_reconfigure_mutex_);
120  server.updateConfig(init_config);
121  //dyn_reconf_lock.unlock();
122 
123  estop_state = robot_serial->get_estop(true);
124  estop_release = false;
125  robot_serial->reset_positions();
126  robot_serial->clear_diag(0); // TODO How to implement diag_t ?
127 
130  pos_left = 0;
131  pos_right = 0;
132 
133  }
134 
135  ros::Time getTime() const {return ros::Time::now();}
136  ros::Duration getPeriod() const {return ros::Duration(0.01);}
137 
139 
140  void read()
141  {
142  std_msgs::Int32MultiArray array32;
143  std_msgs::Bool b;
144  std_msgs::Int32 i;
145  std_msgs::Float32 f;
146 
148 
150 
151 
152  long last_pos_l=0, now_pos_l=0;
153  long last_pos_r=0, now_pos_r=0;
154 
155  //speeds = robot_serial->get_speeds(true);
156 
157  //now_pos_l = robot_serial->get_position_l(true);
158  //now_pos_r = robot_serial->get_position_r(true);
159 
160  //double pos_l = (now_pos_l/(21600.0))*M_PI*2; // 2652: 11pulse x 4quadrature x 51gearratio
161  //double pos_r = (now_pos_r/(21600.0))*M_PI*2;
162 
163  //pos_[0] = pos_r;
164  //pos_[1] = pos_l;
165 
166  double dt = (current_time - last_time).toSec();
168 
169  double qpps_l, qpps_r;
170  qpps_l = robot_serial->get_speed_l(true);
171  qpps_r = robot_serial->get_speed_r(true);
172  //qpps_l = speeds[0];
173  //qpps_r = speeds[1];
174  double speed_l = (qpps_l/(21600.0))*2*M_PI; // 21600: 360pulse x 4quadrature x 15gearratio
175  double speed_r = (qpps_r/(21600.0))*2*M_PI; //
176 
177  vel_[0] = speed_r;
178  vel_[1] = speed_l;
179 
180  pos_left += speed_l*dt;
181  pos_right += speed_r*dt;
182 
183  //ROS_INFO("sp_l: %f, sp_r: %f, pos_l: %f, pos_r: %f, n_pos_l: %ld, n_pos_r: %ld\n", speed_l, speed_r, pos_left*2244/(2*M_PI), pos_right*2244/(2*M_PI), now_pos_l, now_pos_r);
184 
185  pos_[0] = pos_right;
186  pos_[1] = pos_left;
187 
188  array32.data.clear();
189  array32.data.push_back(bumper_states[2]);
190  array32.data.push_back(bumper_states[3]);
191  array32.data.push_back(bumper_states[0]);
192  array32.data.push_back(bumper_states[1]);
193  bumpers_pub.publish(array32);
194 
196  if(estop_state == true)
197  {
199  if(estop_state == false)
200  estop_release = true;
201  }else{
203  }
204 
205  b.data = estop_state;
206  estop_pub.publish(b);
207 
208  bool estop_button = robot_serial->get_estop_button(true);
209  b.data = estop_button;
211 
212  /*robot_serial->update_diag();
213  b.data = robot_serial->get_diag(DIAG_MOTOR_STALL_L);
214  motor_stall_l_pub.publish(b);
215  b.data = robot_serial->get_diag(DIAG_MOTOR_STALL_R);
216  motor_stall_r_pub.publish(b);
217  b.data = robot_serial->get_diag(DIAG_BATT_LOW);
218  batt_low_pub.publish(b);
219  b.data = robot_serial->get_diag(DIAG_BATT_HIGH);
220  batt_high_pub.publish(b);
221  b.data = robot_serial->get_diag(DIAG_MOTOR_DRVR_ERR);
222  controller_pub.publish(b);
223  b.data = robot_serial->get_diag(DIAG_AUX_LIGHTS_ERR);
224  aux_lights_pub.publish(b);
225  i.data = robot_serial->get_batt_volt(true);
226  batt_volt_pub.publish(i);
227  i.data = robot_serial->get_batt_current(true);
228  batt_current_pub.publish(i);
229  i.data = robot_serial->get_batt_soc(true);
230  batt_soc_pub.publish(i);*/
231 
232  }
233 
234  void write()
235  {
236 
237  std_msgs::Float32MultiArray spd_array;
238  spd_array.data.clear();
239 
240  /*if(publish_feed)
241  {
242  speeds = robot_serial->get_speeds();
243  speed.data = speeds[0];
244  feedback_l_pub.publish(speed);
245  speed.data = speeds[1];
246  feedback_r_pub.publish(speed);
247  }
248 
249  if(publish_ref)
250  {
251  speed.data = right_vel;
252  ref_r_pub.publish(speed);
253  speed.data = left_vel;
254  ref_l_pub.publish(speed);
255  }*/
256 
257  if(estop_state)
258  {
259  spd_array.data.push_back(0);
260  spd_array.data.push_back(0);
261  }else{
262  spd_array.data.push_back(cmd_[0]*60*15/(2*M_PI)); // 15: Motor Gear Ratio
263  spd_array.data.push_back(cmd_[1]*60*15/(2*M_PI)); // Unit is RPM, cmd_[0] unit is rad/s
264  }
265 
266  sep_cmd_vel_pub.publish(spd_array);
267 
268  }
269 
270  bool start_callback(std_srvs::Empty::Request& req, std_srvs::Empty::Response& res)
271  {
272  running_ = true;
273  return true;
274  }
275 
276  bool stop_callback(std_srvs::Empty::Request& req, std_srvs::Empty::Response& res)
277  {
278  running_ = false;
279  return true;
280  }
281 
282  void positions_reset_callback(const std_msgs::Empty::ConstPtr& msg)
283  {
284  ROS_WARN("Positions reset.");
286  }
287 
288  void estop_clear_callback(const std_msgs::Empty::ConstPtr& msg)
289  {
290  ROS_WARN("Estop is cleared.");
292  }
293 
294  void callback(mrp2_hardware::ParametersConfig &config, uint32_t level)
295  {
296  switch (level)
297  {
299  robot_serial->set_bumper_estop(config.BUMPER_ESTOP);
300  break;
301  }
302 
303  //ROS_INFO("got: %F and level: %d",config.P_L, level);
304  }
305 
306 
307  private:
311  double cmd_[2];
312  double pos_[2];
313  double vel_[2];
314  double eff_[2];
315  bool running_;
316 
319 
321 
322 
324  std::vector<int> bumper_states, speeds, diags;
325 
329 
331 
340 
342 
345 
347 
348  dynamic_reconfigure::Server<mrp2_hardware::ParametersConfig> server;
349  dynamic_reconfigure::Server<mrp2_hardware::ParametersConfig>::CallbackType f;
350  boost::recursive_mutex dynamic_reconfigure_mutex_;
351  boost::mutex connect_mutex_;
352 
353  mrp2_hardware::ParametersConfig init_config;
354 
355  double _ftod(float fValue)
356  {
357  char cz_dummy[30];
358  sprintf(cz_dummy,"%9.5f",fValue);
359  double dValue = strtod(cz_dummy,NULL);
360  return dValue;
361  }
362 
363  };
hardware_interface::VelocityJointInterface jnt_vel_interface_
ros::Time current_time
ros::Time getTime() const
void set_estop(bool value)
ros::Publisher sep_cmd_vel_pub
bool publish_feed
void read()
void positions_reset_callback(const std_msgs::Empty::ConstPtr &msg)
dynamic_reconfigure::Server< mrp2_hardware::ParametersConfig >::CallbackType f
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::Publisher motor_stall_r_pub
bool estop_release
std::vector< int > speeds
ros::Publisher controller_pub
std::vector< int > bumper_states
ServiceServer advertiseService(ros::NodeHandle n, std::string name, boost::function< bool(const typename ActionSpec::_action_goal_type::_goal_type &, typename ActionSpec::_action_result_type::_result_type &result)> service_cb)
bool stop_callback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
double pos_[2]
mrp2_hardware::ParametersConfig init_config
int get_speed_r(bool update=false)
ros::ServiceServer start_srv_
ros::Publisher bumpers_pub
ros::Publisher estop_btn_pub
#define ROS_WARN(...)
ros::Publisher estop_pub
bool get_estop(bool update=false)
ros::Publisher batt_low_pub
void publish(const boost::shared_ptr< M > &message) const
double eff_[2]
int get_speed_l(bool update=false)
hardware_interface::ImuSensorInterface imu_sens_interface_
void callback(mrp2_hardware::ParametersConfig &config, uint32_t level)
boost::mutex connect_mutex_
ros::Time last_time
bool publish_ref
ros::Duration getPeriod() const
bool sym_tuning
bool get_estop_button(bool update=false)
dynamic_reconfigure::Server< mrp2_hardware::ParametersConfig > server
bool running_
hardware_interface::JointStateInterface jnt_state_interface_
bool publish_pos
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool start_callback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
double vel_[2]
ros::ServiceServer stop_srv_
void registerHandle(const JointStateHandle &handle)
bool estop_state
ros::NodeHandle nh_
void estop_clear_callback(const std_msgs::Empty::ConstPtr &msg)
void set_bumper_estop(bool value)
JointStateHandle getHandle(const std::string &name)
double pos_left
ros::Publisher aux_lights_pub
void reset_positions()
static Time now()
boost::recursive_mutex dynamic_reconfigure_mutex_
std::vector< int > diags
ros::Publisher motor_stall_l_pub
ros::Subscriber estop_clear_sub
double cmd_[2]
std::vector< int > get_bumpers(bool update=false)
double _ftod(float fValue)
ros::Subscriber pos_reset_sub
ros::Publisher batt_high_pub
void write()
double pos_right
MRP2_Serial * robot_serial


mrp2_hardware
Author(s): Akif Hacinecipoglu
autogenerated on Mon Feb 28 2022 22:53:03