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

turtlebot odom

asked 2015-03-19 02:54:12 -0600

dylankc gravatar image

updated 2015-03-19 08:19:43 -0600

I want to make a node that moves the turtlebot2 accurately for certain distances. This is my code currently. However, it is not working and I know there are many errors. Please do help. The code covers what i intend to do which is: 1) subscribe to the /odom topic to get the turtlebot's current location 2) compare the location to its desired end point location (x_epos) 3) move towards the end point 4) keep moving until the current location from odometry is equal to the end location

This is the code that I have:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "nav_msgs/Odometry.h"

void odomCallback(const nav_msgs::Odometry::ConstPtr& odom)

{
ROS_INFO("I received odom: [%f,%f,%f]", odom->twist.twist.linear.x, odom->pose.pose.position.y,odom->pose.pose.position.z); //store x,y,z position values
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");
  ros::NodeHandle nh;
  ros::Subscriber sub_odom = nh.subscribe("odom", 1000, odomCallback);

double x_pos = odom.pose.pose.position.x;
double y_pos = odom.pose.pose.position.y;
double z_pos = odom.pose.pose.position.z;

  double x_epos = 0.2; //desired end position

while (x_pos < x_epos && ros::ok()) //while robot is not at the end position

{
//move the robot towards the end position
    ros::Publisher movement_pub = nh.advertise<geometry_msgs::Twist>("/mobile_base/commands/velocity", 10);
    geometry_msgs::Twist vel;
    vel.linear.x = 2.0;
    vel.angular.z = 0.0;

   movement_pub.publish(vel);

}

  ros::spin();
  return 0;
}
edit retag flag offensive close merge delete

Comments

Do you see any msgs published in the /mobile_base/commands/velocity topic? You can see that by:

rostopic echo /mobile_base/commands/velocity
kokirits gravatar image kokirits  ( 2015-03-19 03:59:03 -0600 )edit

http://answers.ros.org/question/20536...

The robot is able to move now. However I still cant tie the movement with the odometry topic to make it accurate.

dylankc gravatar image dylankc  ( 2015-03-19 04:05:23 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-05-23 15:08:57 -0600

new_forROS gravatar image

#include "ros/ros.h"

#include "std_msgs/String.h"

#include "nav_msgs/Odometry.h"

void odomCallback(const nav_msgs::Odometry::ConstPtr& odom)

{

ROS_INFO("I received odom: [%f,%f,%f]", odom->twist.twist.linear.x,

odom->pose.pose.position.y,odom->pose.pose.position.z); //store x,y,z position values

}

int main(int argc, char **argv) { ros::init(argc, argv, "listener");

ros::NodeHandle nh;

ros::Publisher movement_pub = nh.advertise<geometry_msgs::twist>("/mobile_base/commands/velocity", 10);

geometry_msgs::Twist vel;

ros::Subscriber sub_odom = nh.subscribe("odom", 1000, odomCallback);

double x_pos = odom.pose.pose.position.x;

double y_pos = odom.pose.pose.position.y;

double z_pos = odom.pose.pose.position.z;

double x_epos = 0.2; //desired end position

while (x_pos != x_epos && ros::ok()) //while robot is not at the end position

{

//move the robot towards the end position

if (x_pos <x_epos)
 { vel.linear.x = 0.19;
vel.angular.z = 0.0;}
 else
 {
      vel.linear.x = -0.19;
vel.angular.z = 0.0;
 }

movement_pub.publish(vel);

}

ros::spin();

return 0;

}

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-03-19 02:54:12 -0600

Seen: 4,555 times

Last updated: May 23 '15