Robotics StackExchange | Archived questions

turtlebot odom

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;
}

Asked by dylankc on 2015-03-19 02:54:12 UTC

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

Asked by kokirits on 2015-03-19 03:59:03 UTC

http://answers.ros.org/question/205364/navigation-of-turtlebot-moving-1m-forward-1m-left/

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

Asked by dylankc on 2015-03-19 04:05:23 UTC

Answers

#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("/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;

}

Asked by new_forROS on 2015-05-23 15:08:57 UTC

Comments