Ask Your Question
0

subscribe to arduino message

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

hana gravatar image

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

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 -0500

Naman gravatar image

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

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 -0500 )edit

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

hana gravatar image hana  ( 2015-10-24 12:26:23 -0500 )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 -0500 )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 -0500 )edit

Ask a new question

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

Your Answer

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

Add Answer

Question Tools

Stats

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

Seen: 424 times

Last updated: Oct 24 '15