uc_bridge.h
Go to the documentation of this file.
1 
7 #ifndef UC_BRIDGE_H
8 #define UC_BRIDGE_H
9 
10 #include <ros/ros.h>
11 #include <signal.h>
15 #include <ros/package.h>
16 #include <std_msgs/Float64.h>
17 #include <std_msgs/UInt8.h>
18 #include <std_msgs/Int16.h>
19 #include <std_msgs/String.h>
20 #include <sensor_msgs/Range.h>
21 #include <sensor_msgs/Imu.h>
22 #include <sensor_msgs/MagneticField.h>
23 #include <sensor_msgs/BatteryState.h>
24 
33 namespace uc_bridge
34 {
43 {
44  std::string cmd = "Reset Controller";
47  try
48  {
49  com->sendCommand(cmd, input, output);
50  }
51  catch (std::exception& e)
52  {
53  ROS_WARN_STREAM(
54  "An error occured during initial Reset!\n Description: " << e.what());
55  }
56 }
64 void shutdownSignalHandler(int sig)
65 {
66  if (rstOnShutdown)
67  {
68  resetController(com_ptr);
69  ros::Duration(0.1).sleep();
70  }
71  try
72  {
73  com_ptr->stopCommunication();
74  com_ptr->disconnect();
75  }
76  catch (std::exception& e)
77  {
78  ROS_WARN_STREAM("An error occured while trying to shut down the "
79  "connection.\n Description: "
80  << e.what());
81  }
82  ros::shutdown();
83 }
90 {
91  // register sensor groups
92  try
93  {
94  if (!com->registerSensorGroups("Set Group"))
95  ROS_WARN_STREAM("Registering all sensor groups failed!");
96  }
97  catch (std::exception& e)
98  {
99  ROS_WARN_STREAM("An error occured while trying to register sensor "
100  "groups!\n Description: "
101  << e.what());
102  }
103 }
109 {
110  bool was_set = false;
113  std::string cmd = "Drive Forward";
114  short level = 0;
115  input.insertParameter("speed", "int16_t", level);
116  try
117  {
118  was_set = com->sendCommand(cmd, input, output);
119  if (!was_set)
120  ROS_WARN_STREAM("Activating motor controller failed!");
121  }
122  catch (std::exception& e)
123  {
124  ROS_WARN_STREAM("An error occured while trying to activate the motor "
125  "controller!\n Description: "
126  << e.what());
127  }
128 }
135 {
136  bool was_set = false;
139  std::string cmd = "Set Steering Level";
140  short level = 0;
141  input.insertParameter("steering", "int16_t", level);
142  try
143  {
144  was_set = com->sendCommand(cmd, input, output);
145  if (!was_set)
146  ROS_WARN_STREAM("Activating steering controller failed!");
147  }
148  catch (std::exception& e)
149  {
150  ROS_WARN_STREAM("An error occured while trying to activate the steering "
151  "controller!\n Description: "
152  << e.what());
153  }
154 }
161 {
162  bool was_set = false;
165  std::string cmd = "Toggle Kinect On";
166  try
167  {
168  was_set = com->sendCommand(cmd, input, output);
169  if (!was_set)
170  ROS_WARN_STREAM("Activating kinect failed!");
171  }
172  catch (std::exception& e)
173  {
174  ROS_WARN_STREAM(
175  "An error occured while trying to activate kinect!\n Description: "
176  << e.what());
177  }
178 }
185 {
186  bool was_set = false;
189  std::string cmd = "Toggle US On";
190  try
191  {
192  was_set = com->sendCommand(cmd, input, output);
193  if (!was_set)
194  ROS_WARN_STREAM("Activating us-sensors failed!");
195  }
196  catch (std::exception& e)
197  {
198  ROS_WARN_STREAM(
199  "An error occured while trying to activate us-sensors!\n Description: "
200  << e.what());
201  }
202 }
208 {
209  bool was_set = false;
212  std::string cmd = "Start DAQ";
213  try
214  {
215  was_set = com->sendCommand(cmd, input, output);
216  if (!was_set)
217  ROS_WARN_STREAM("Activating daq failed!");
218  }
219  catch (std::exception& e)
220  {
221  ROS_WARN_STREAM(
222  "An error occured while trying to activate daq!\n Description: "
223  << e.what());
224  }
225 }
226 
227 // sensor group callbacks
237  SensorGroup* grp, std::unordered_map<std::string, ros::Publisher*>& pub)
238 {
239  sensor_msgs::Range usl, usr, usf;
240  double l, r, f;
241  ros::Time t = ros::Time::now();
242  try
243  {
244  grp->getChannelValueConverted("USL", l);
245  grp->getChannelValueConverted("USF", f);
246  grp->getChannelValueConverted("USR", r);
247  usl.range = l;
248  usf.range = f;
249  usr.range = r;
250 
251  usl.max_range = 3;
252  usl.min_range = 0.06;
253  usl.field_of_view = 0.76;
254  usl.radiation_type = 0;
255  usl.header.frame_id = "left_sensor";
256  usl.header.stamp = t;
257 
258  usf.max_range = 3;
259  usf.min_range = 0.06;
260  usf.field_of_view = 0.76;
261  usf.radiation_type = 0;
262  usf.header.frame_id = "front_sensor";
263  usf.header.stamp = t;
264 
265  usr.max_range = 3;
266  usr.min_range = 0.06;
267  usr.field_of_view = 0.76;
268  usr.radiation_type = 0;
269  usr.header.frame_id = "right_sensor";
270  usr.header.stamp = t;
271 
272  pub["USL"]->publish(usl);
273  pub["USF"]->publish(usf);
274  pub["USR"]->publish(usr);
275  }
276  catch (std::exception& e)
277  {
278  ROS_WARN_STREAM("An error in Message 'Sensor grp1' occured!\n Description: "
279  << e.what());
280  }
281 }
291  SensorGroup* grp, std::unordered_map<std::string, ros::Publisher*>& pub)
292 {
293  sensor_msgs::Imu imu;
294  ros::Time t = ros::Time::now();
295  try
296  {
297  grp->getChannelValueConverted("GX", imu.angular_velocity.x);
298  grp->getChannelValueConverted("GY", imu.angular_velocity.y);
299  grp->getChannelValueConverted("GZ", imu.angular_velocity.z);
300  grp->getChannelValueConverted("AX", imu.linear_acceleration.x);
301  grp->getChannelValueConverted("AY", imu.linear_acceleration.y);
302  grp->getChannelValueConverted("AZ", imu.linear_acceleration.z);
303  imu.header.stamp = t;
304  imu.header.frame_id = "robot_imu";
305 
306  pub["IMU"]->publish(imu);
307  }
308  catch (std::exception& e)
309  {
310  ROS_WARN_STREAM("An error in Message 'Sensor grp2' occured!\n Description: "
311  << e.what());
312  }
313 }
323  SensorGroup* grp, std::unordered_map<std::string, ros::Publisher*>& pub)
324 {
325  std_msgs::UInt8 hallcnt;
326  std_msgs::Float64 halldt, halldt8;
327  try
328  {
329  grp->getChannelValue("HALL_CNT", hallcnt.data);
330  grp->getChannelValueConverted("HALL_DT", halldt.data);
331  pub["HALL_CNT"]->publish(hallcnt);
332  pub["HALL_DT"]->publish(halldt);
333  }
334  catch (std::exception& e)
335  {
336  ROS_WARN_STREAM("An error in Message 'Sensor grp3' occured!\n Description: "
337  << e.what());
338  }
339 }
349  SensorGroup* grp, std::unordered_map<std::string, ros::Publisher*>& pub)
350 {
351  sensor_msgs::MagneticField mag;
352  // short mx, my, mz;
353  ros::Time t = ros::Time::now();
354  try
355  {
356  grp->getChannelValueConverted("MX", mag.magnetic_field.x);
357  grp->getChannelValueConverted("MY", mag.magnetic_field.y);
358  grp->getChannelValueConverted("MZ", mag.magnetic_field.z);
359  mag.header.stamp = t;
360  mag.header.frame_id = "robot_magnetometer";
361  pub["MAG"]->publish(mag);
362  }
363  catch (std::exception& e)
364  {
365  ROS_WARN_STREAM("An error in Message 'Sensor grp4' occured!\n Description: "
366  << e.what());
367  }
368 }
378  SensorGroup* grp, std::unordered_map<std::string, ros::Publisher*>& pub)
379 {
380  sensor_msgs::BatteryState batVD, batVS;
381  double vsbat, vdbat;
382  ros::Time t = ros::Time::now();
383  try
384  {
385  grp->getChannelValueConverted("VDBAT", vdbat);
386  grp->getChannelValueConverted("VSBAT", vsbat);
387  batVD.voltage = vdbat;
388  batVS.voltage = vsbat;
389  batVD.header.stamp = t;
390  batVS.header.stamp = t;
391 
392  pub["VDBAT"]->publish(batVD);
393  pub["VSBAT"]->publish(batVS);
394  }
395  catch (std::exception& e)
396  {
397  ROS_WARN_STREAM("An error in Message 'Sensor grp5' occured!\n Description: "
398  << e.what());
399  }
400 }
410  SensorGroup* grp, std::unordered_map<std::string, ros::Publisher*>& pub)
411 {
412  std_msgs::UInt8 hallcnt;
413  std_msgs::Float64 halldt, halldt8;
414  try
415  {
416  grp->getChannelValueConverted("HALL_DT8", halldt8.data);
417  pub["HALL_DT8"]->publish(halldt8);
418  }
419  catch (std::exception& e)
420  {
421  ROS_WARN_STREAM("An error in Message 'Sensor grp6' occured!\n Description: "
422  << e.what());
423  }
424 }
425 
426 // debug/error/text callbacks
432 void errorCallback(const std::string& msg)
433 {
434  ROS_WARN_STREAM("A communication Error occured!\n" << msg);
435 }
441 void textCallback(const std::string& msg)
442 {
443  ROS_INFO_STREAM("UC board sent the following info:\n" << msg);
444 }
453 void publishDebugMessage(const std::string& msg, ros::Publisher* pub)
454 {
455  std_msgs::String debug;
456  debug.data = msg;
457  pub->publish(debug);
458 }
459 
460 // ros command message callbacks
470 void motorLevelCallback(std_msgs::Int16::ConstPtr motorLevel,
471  Communication* com)
472 {
473  bool was_set = false;
474  std::string cmd = "Drive Forward";
475  short level = motorLevel->data;
476  if (level < 0)
477  {
478  cmd = "Drive Backward";
479  level = -level;
480  }
483  input.insertParameter("speed", "int16_t", level);
484  try
485  {
486  was_set = com->sendCommand(cmd, input, output);
487  }
488  catch (std::exception& e)
489  {
490  ROS_WARN_STREAM(
491  "An error in Message 'set_motor_level_msg' occured!\n Description: "
492  << e.what());
493  was_set = false;
494  }
495  if (!was_set)
496  ROS_WARN_STREAM("Motor level set to: " << motorLevel->data << " failed!");
497 }
507 void steeringLevelCallback(std_msgs::Int16::ConstPtr steeringLevel,
508  Communication* com)
509 {
510  bool was_set = false;
511  std::string cmd = "Set Steering Level";
514  input.insertParameter("steering", "int16_t", steeringLevel->data);
515  try
516  {
517  was_set = com->sendCommand(cmd, input, output);
518  }
519  catch (std::exception& e)
520  {
521  ROS_WARN_STREAM(
522  "An error in Message 'set_steering_level_msg' occured!\n Description: "
523  << e.what());
524  was_set = false;
525  }
526  if (!was_set)
527  ROS_WARN_STREAM("Steering level set to: " << steeringLevel->data
528  << " failed!");
529 }
530 
531 // debug raw message callback
541 void ucBoardMessageCallback(std_msgs::String::ConstPtr msg, Communication* com)
542 {
543  try
544  {
545  com->sendRawMessage(msg->data);
546  }
547  catch (std::exception& e)
548  {
549  ROS_WARN_STREAM(
550  "An error in Message 'send_uc_board_msg' occured!\n Description: "
551  << e.what());
552  }
553 }
554 }
555 
556 #endif // UC_BRIDGE_H
Header file for the CommunicationConfig class.
void motorLevelCallback(std_msgs::Int16::ConstPtr motorLevel, Communication *com)
This function will be called whenever a ros node published a message on the motor level topic...
Definition: uc_bridge.h:470
Header file for the Communication class.
bool registerSensorGroups(const std::string &cmdName, unsigned int timeout=100000)
Register all predefined sensor groups on the serial device.
void registerSensorGroups(Communication *com)
This function will trigger the registration of all predefined sensor groups on the serial device...
Definition: uc_bridge.h:89
const bool getChannelValue(const std::string &name, T &value) const
Get a received value from a certain channel.
Definition: sensorgroup.h:143
void textCallback(const std::string &msg)
This function will be called whenever the communication received a plain text message which will be p...
Definition: uc_bridge.h:441
void shutdownSignalHandler(int sig)
This function is the callback for the shutdown system signal.
Definition: uc_bridge.h:64
void sendRawMessage(const std::string &msg)
Method to send a string directly to the serial device.
void stopCommunication()
Stop reading the serial input buffer and shutdown parsing threads.
void ucBoardMessageCallback(std_msgs::String::ConstPtr msg, Communication *com)
This function will be called whenever a ros node published a message on the raw communication topic...
Definition: uc_bridge.h:541
void activateSteeringController(Communication *com)
This function will activate the steering controller of the serial device.
Definition: uc_bridge.h:134
void publishSensorGroupMessage2(SensorGroup *grp, std::unordered_map< std::string, ros::Publisher * > &pub)
This function will be called whenever a message from sensor group 2 has been received and publish the...
Definition: uc_bridge.h:290
The Parameter::ParameterMap class provides a map functionality for Parameter::Parameter objects conta...
Definition: parameter.h:163
bool sendCommand(const std::string &command, const Parameter::ParameterMap &inputParams, Parameter::ParameterMap &outputParams, unsigned int timeout=100000)
Send a command to the servial device.
void publishSensorGroupMessage3(SensorGroup *grp, std::unordered_map< std::string, ros::Publisher * > &pub)
This function will be called whenever a message from sensor group 3 has been received and publish the...
Definition: uc_bridge.h:322
void publishSensorGroupMessage1(SensorGroup *grp, std::unordered_map< std::string, ros::Publisher * > &pub)
This function will be called whenever a message from sensor group 1 has been received and publish the...
Definition: uc_bridge.h:236
void disconnect()
Terminates a connection to a serial devive with a certain device-tag.
The SensorGroup class builds the syntactic template for its specific command that results from the se...
Definition: sensorgroup.h:82
void activateUS(Communication *com)
This function will activate the ultra sonic range sensors of the serial device.
Definition: uc_bridge.h:184
Communication * com_ptr
Definition: uc_bridge.h:35
The Communication class provides access to microcontroller communication.
Definition: communication.h:36
void errorCallback(const std::string &msg)
This function will be called whenever a communication error occured and post its content as a ROS War...
Definition: uc_bridge.h:432
void activateKinect(Communication *com)
This function will activate the kinect power supply of the serial device.
Definition: uc_bridge.h:160
const bool getChannelValueConverted(const std::string &name, double &value) const
Get a received converted value from a certain channel.
void insertParameter(const std::string &name, const std::string &type, const T &value, const bool isValid=true)
Insert a new parameter in this map, with the given name, type and value.
Definition: parameter.h:219
void activateDAQ(Communication *com)
This function will activate the daq of the serial device.
Definition: uc_bridge.h:207
void steeringLevelCallback(std_msgs::Int16::ConstPtr steeringLevel, Communication *com)
This function will be called whenever a ros node published a message on the steering level topic...
Definition: uc_bridge.h:507
void publishSensorGroupMessage5(SensorGroup *grp, std::unordered_map< std::string, ros::Publisher * > &pub)
This function will be called whenever a message from sensor group 5 has been received and publish the...
Definition: uc_bridge.h:377
Contains all service functions and their respective implementations.
void resetController(Communication *com)
This function send a soft reset command to the serial device.
Definition: uc_bridge.h:42
This namespace contains functions used in the uc_bridge node.
void activateMotorController(Communication *com)
This function will activate the motor controller of the serial device.
Definition: uc_bridge.h:108
bool rstOnShutdown
Definition: uc_bridge.h:36
void publishSensorGroupMessage6(SensorGroup *grp, std::unordered_map< std::string, ros::Publisher * > &pub)
This function will be called whenever a message from sensor group 6 has been received and publish the...
Definition: uc_bridge.h:409
void publishSensorGroupMessage4(SensorGroup *grp, std::unordered_map< std::string, ros::Publisher * > &pub)
This function will be called whenever a message from sensor group 4 has been received and publish the...
Definition: uc_bridge.h:348
void publishDebugMessage(const std::string &msg, ros::Publisher *pub)
This function will be called whenever the communication received any message if the debug mode is ena...
Definition: uc_bridge.h:453


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13