Ask Your Question

rosserial_arduino Integer Printing

asked 2019-07-24 13:36:02 -0600

yg97 gravatar image

How do you get integers to print under node handler.loginfo or any of the other .() <---Various logging types? I'm trying to have my subscriber post the data that I have in my subscriber servo_cb function arduino code but it only prints the string not the int values.

See attached photos:

Publisher Code:

//Include libraries.
#include <Arduino.h>
#include <ros.h>
#include <std_msgs/UInt16.h>

//Set up the ros node handler.
ros::NodeHandle nh;
std_msgs::UInt16 potval;//std_msgs is of type UInt16 called potval.

ros::Publisher potpub("pot", &potval); 
//Setup the Publisher with name potpub.
//Create the publisher on topic pot and use potval.
void setup()

void loop() { = analogRead(A0); //Assign to be the analog values from A0.
  potpub.publish( &potval ); //Publish the data.

Subscriber Code:

//Include libraries.
#if (ARDUINO >= 100)
#include <Arduino.h>
#include <WProgram.h>

#include <Servo.h>
#include <ros.h>
#include <std_msgs/UInt16.h>
ros::NodeHandle  nh;//node handler declaration

Servo servo;

/*In ROS, nodes are uniquely named. 
If two nodes with the same name are launched, the previous one is kicked off, hence use this template for multiple nodes/communications 

rosrun rosserial_python /dev/ttyACM0 __name:=node1 , *and change port # and node name*


void servo_cb( const std_msgs::UInt16& potval) { //servo_cb is the function for the subscriber to perform. 

  int POT = ; //declare variable POT as the analogRead value of pin A0 which is of pubpot.

  int DIRECTION = map (POT, 0, 1023, 0, 180); //map the values of the potentiometer (0 - 1023) to radial values (0 - 180).
  nh.loginfo("The POT is at: "); //ROS terminal debug.
  nh.loginfo(POT); //Print data.

  servo.write(DIRECTION); //move the servo to the mapped value corresponding to the potentiometer position.

  nh.loginfo("Servo is at: "); //ROS terminal debug.
  nh.loginfo(DIRECTION); //Print data.

ros::Subscriber<std_msgs::UInt16> sub("pot", servo_cb);
<std_msgs> is a standard message. 
//UInt16 is a type of message. 
//pot is the topic name to subscribe to.
//servo_cb is the function to perform.
void setup() {
  servo.attach(9); //attach it to pin 9

  nh.initNode(); //initialize node

void loop() {
  nh.subscribe(sub); //subscribe to the sub node, and do what's in the servo_cb function
  /* ros::spin() will not return until the node has been shut down. 
  Thus, you have no control over your program while ROS is spinning. 
  The advantage of ros::spinOnce() is that it will check for callbacks/service calls/etc only as often as you call it. 
  Why is this useful? Being able to control the rate at which ROS updates allows you to do certain calculations at a certain frequency.*/
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-07-25 12:18:16 -0600

duck-development gravatar image

You may use one function inside Arduino to convert the number first to a String an then print it.

here you find some Solution how to do it

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-07-24 13:36:02 -0600

Seen: 386 times

Last updated: Jul 25 '19