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

subscribe to arduino message

asked 2015-10-23 13:26:27 -0600

hana gravatar image

updated 2015-10-23 15:48:56 -0600

Hello all,

My arduino send geometry twist message about robot path on /robotpath topic, when i type rostopic echo /robotpath i see the values (x,y,theta) change, but my subscribe node on ros read just the first value when it runs and hold it. I need to build a path controller for my project.

Thanks a lot.

my code is:

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

class robocont
{
public:
  robocont();

private:
  void roboCallback(const geometry_msgs::Twist::ConstPtr& robotpath);
  ros::NodeHandle nh_controller;
  ros::Subscriber robo_sub; 
};

 robocont::robocont()
{ 
  robo_sub = nh_controller.subscribe<geometry_msgs::Twist>("/robotpath", 1000, &robocont::roboCallback, this);
}

void robocont::roboCallback(const geometry_msgs::Twist::ConstPtr& robotpath)
{
  while(nh_controller.ok()){  
  ROS_INFO("Robot Postion:(%.2f, %.2f. %.2f)" ,robotpath->linear.x, robotpath->linear.y, robotpath->angular.z);
}
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "odometry_Control");
  robocont odometry_Control;
  ros::spin();
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-10-23 14:00:40 -0600

Naman gravatar image

updated 2015-10-24 10:25:05 -0600

Did you write ros::spin() in your code? See this for more information. Most probably, that is the problem. Otherwise, can you post your ros subscriber code?

Update:

This is the correct code:

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

class robocont
{
public:
  robocont();

private:
  void roboCallback(const geometry_msgs::Twist::ConstPtr& robotpath);
  ros::NodeHandle nh_controller;
  ros::Subscriber robo_sub; 
};

 robocont::robocont()
{ 
  robo_sub = nh_controller.subscribe<geometry_msgs::Twist>("/robotpath", 1000, &robocont::roboCallback, this);
}

void robocont::roboCallback(const geometry_msgs::Twist::ConstPtr& robotpath)
{
  ROS_INFO("Robot Postion:(%.2f, %.2f. %.2f)" ,robotpath->linear.x, robotpath->linear.y, robotpath->angular.z);
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "odometry_Control");
  robocont odometry_Control;
  ros::spin();
}

In your callback function, you had while(nh_controller.ok()){ } because of which it was stuck in the loop and kept on printing the same value.

edit flag offensive delete link more

Comments

Thank you, this is my code, i don't know where the problem

hana gravatar image hana  ( 2015-10-24 03:55:37 -0600 )edit

Thank u very much .... the problem has been solved

hana gravatar image hana  ( 2015-10-24 12:26:23 -0600 )edit

Then, can you accept the answer so that it will be helpful for others also

Naman gravatar image Naman  ( 2015-10-24 13:44:35 -0600 )edit

I need to ask if the Callback function can read two topic at the same time (refernce path and real path)

hana gravatar image hana  ( 2015-10-24 14:48:55 -0600 )edit

Ask a new question

Naman gravatar image Naman  ( 2015-10-25 23:26:18 -0600 )edit

Question Tools

Stats

Asked: 2015-10-23 13:26:27 -0600

Seen: 841 times

Last updated: Oct 24 '15