Note: This tutorial assumes that you have completed the previous tutorials: Hello World.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Logging in rosserial

Description: This tutorial shows step-by-step how to use logging for rosserial.

Tutorial Level: BEGINNER

Next Tutorial: Using Time and TF

Logging

Like any ROS node, you can log messages at the appropriate verbosity level. rosserial forwards the string to the standard ROS network and outputs to rosout and to its log file.

If you try to log information before the device has connected to ROS, the logged information will be lost.

Keep in mind that for low memory systems, this kind of logging is very expensive. You need to store the string in memory before you send it. For more memory thrifty logging, try defining a custom message type with exactly the information you need sent over. You can then view your program's status by observing it using rostopic echo.

The Code

   1 #include <ros.h>
   2 ros::NodeHandle nh;
   3 
   4 void setup()
   5 {
   6   nh.initNode();
   7 }
   8 
   9 void loop()
  10 {
  11   //wait until you are actually connected
  12   while (!nh.connected())
  13   {
  14     nh.spinOnce();
  15   }
  16  
  17   //Now you can send all of the statements you want
  18   //to be logged at the appropriate verbosity level
  19   nh.logdebug("Debug Statement");
  20   nh.loginfo("Program info");
  21   nh.logwarn("Warnings.");
  22   nh.logerror("Errors..");
  23   nh.logfatal("Fatalities!");
  24   delay(5000);
  25 }

Uploading the Code

To upload the code to your Arduino, use the upload function within the Arduino IDE. This is no different from uploading any other sketch.

Running the Code

Now, launch the roscore in a new terminal window:

roscore

Next, run the rosserial client application that forwards your Arduino messages to the rest of ROS. Make sure to use the correct serial port:

rosrun rosserial_python serial_node.py /dev/ttyUSB0

rosrun rosserial_python serial_node.py _port:=/dev/ttyUSB0

rosrun rosserial_python serial_node.py /dev/ttyUSB0

You should be able to see all the log messages in the same terminal that you used to run the serial node.

Further Reading

Please see rosserial/Overview for more information on publishers and subscribers. Also see limitations for information about more complex data types.

Wiki: rosserial_arduino/Tutorials/Logging (last edited 2017-11-06 00:14:26 by BeiChenLiu)