user1797209
user1797209

Reputation: 31

TurtleBot ROS moving using Twist

I am trying to program for a TurtleBot, but there is a significant lack of tutorials for the robot and I have been unable to write my own C++ which works. I am trying to use a tutorial from another robot just to make the robot move when a key is pressed.

The source tutorial is found here: , which I only modified the publish topic to "/cmd_vel"

#include <iostream>

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

class RobotDriver
{
private:
  //! The node handle we'll be using
  ros::NodeHandle nh_;
  //! We will be publishing to the "/base_controller/command" topic to issue commands
  ros::Publisher cmd_vel_pub_;

public:
  //! ROS node initialization
  RobotDriver(ros::NodeHandle &nh)
  {
    nh_ = nh;
    //set up the publisher for the cmd_vel topic
    cmd_vel_pub_ = nh_.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
  }

  //! Loop forever while sending drive commands based on keyboard input
  bool driveKeyboard()
  {
    std::cout << "Type a command and then press enter.  "
      "Use '+' to move forward, 'l' to turn left, "
      "'r' to turn right, '.' to exit.\n";

    //we will be sending commands of type "twist"
    geometry_msgs::Twist base_cmd;

    char cmd[50];
    while(nh_.ok()){

      std::cin.getline(cmd, 50);
      if(cmd[0]!='+' && cmd[0]!='l' && cmd[0]!='r' && cmd[0]!='.')
      {
        std::cout << "unknown command:" << cmd << "\n";
        continue;
      }

      base_cmd.linear.x = base_cmd.linear.y = base_cmd.angular.z = 0;   
      //move forward
      if(cmd[0]=='+'){
        base_cmd.linear.x = 0.25;
      } 
      //turn left (yaw) and drive forward at the same time
      else if(cmd[0]=='l'){
        base_cmd.angular.z = 0.75;
        base_cmd.linear.x = 0.25;
      } 
      //turn right (yaw) and drive forward at the same time
      else if(cmd[0]=='r'){
        base_cmd.angular.z = -0.75;
        base_cmd.linear.x = 0.25;
      } 
      //quit
      else if(cmd[0]=='.'){
        break;
      }

      //publish the assembled command
      cmd_vel_pub_.publish(base_cmd);
    }
    return true;
  }

};

int main(int argc, char** argv)
{
  //init the ROS node
  ros::init(argc, argv, "robot_driver");
  ros::NodeHandle nh;

  RobotDriver driver(nh);
  driver.driveKeyboard();
}

The code compiles and runs correctly, but the turtlebot does not move when commands are issued. Any ideas why?

Additional Info:

When I'm on the laptop provided with my Turtlebot messages appear to not be being sent (or are not being delivered). In separate terminals, I have:

turtlebot@turtlebot-0516:~$ sudo service turtlebot start
[sudo] password for turtlebot:
turtlebot start/running, process 1470
turtlebot@turtlebot-0516:~$ rostopic echo /cmd_vel

And

turtlebot@turtlebot-0516:~$ rostopic pub /cmd_vel geometry_msgs/Twist '[1.0, 0.0, 0.0]' '[0.0, 0.0, 0.0]'
publishing and latching message. Press ctrl-C to terminate

With info:

turtlebot@turtlebot-0516:~$ rostopic info /cmd_vel
Type: geometry_msgs/Twist

Publishers:
 * /rosttopic_2547_1352476947372 (http://turtlebot-0516:40275/)

Subscribers:
 * /turtlebot_node (http://10.143.7.81:58649/)
 * /rostopic_2278_1352476884936 (http://turtlebot-0516:39291/)

There is no output for the echo at all.

Upvotes: 3

Views: 17613

Answers (3)

Mark
Mark

Reputation: 9

Wrote a tutorial on Hello World for TurtleBot for anyone that's just getting going.

For anyone else that's pulling their hair out trying to get this one to work.

Change /cmd_vel to cmd_vel_mux/input/teleop

I'm using:

  • Indigo
  • TurtleBot2

Upvotes: 0

Yuri Tessera
Yuri Tessera

Reputation: 1

You could try with sudo service turtlebot stop to stop the automatic initial setup and then roslaunch turtlebot bringup minimal.launch to run only the minimal setup that you need. Then, you could try with roslaunch turtlebot_teleop keyboard_teleop.launch to use the teleoperation with keyboard.

Upvotes: 0

user3734936
user3734936

Reputation: 11

I know this post is a thousand years old now, but I had no problems getting this code to run. I had to use the ncurses library to get it to run without having to press the enter key after each character entered.

Just thought I'd let everyone know that this does work. :P I drove an iRobot create around using it.

Upvotes: 1

Related Questions