ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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: https://imgur.com/a/nnKSZro

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()
{
  nh.initNode();
  nh.advertise(potpub);
}

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

Subscriber Code:

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

#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 serial_node.py /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 = potval.data ; //declare variable POT as the analogRead value of pin A0 which is msg.data 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
  nh.spinOnce(); 
  /* 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.*/
  delay(100);
}
edit retag flag offensive close merge delete

Comments

Do you solve this issue? I am also facing the same issue , can you help me how you solved this problem ?

Rakee003 gravatar image Rakee003  ( 2021-10-09 06:33:36 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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 https://stackoverflow.com/questions/7... you find some Solution how to do it

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 1,768 times

Last updated: Jul 25 '19