2012-11-03 136 views
2

我想爲TurtleBot編寫程序,但是機器人的教程非常缺乏,我一直無法編寫自己的C++程序。我試圖使用另一個機器人的教程,以便在按下按鍵時使機器人移動。TurtleBot使用Twist移動​​的ROS

源教程發現here:,我只是修改了發佈主題,以「/ 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(); 
} 

代碼編譯和運行正常,但發出命令時,turtlebot不動。任何想法爲什麼?

附加信息:

當我在提供我Turtlebot消息的筆記本電腦出現無法發送(或不被傳送)。在獨立的終端,我有:

[email protected]:~$ sudo service turtlebot start 
[sudo] password for turtlebot: 
turtlebot start/running, process 1470 
[email protected]:~$ rostopic echo /cmd_vel 

而且

[email protected]:~$ 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 

隨着信息:

[email protected]:~$ 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/) 

沒有輸出迴音都沒有。

+2

你會得到更多的運氣在這裏問這個問題:HTTP://answers.ros .org/questions/ – Dunes

回答

1

我知道這個帖子現在已經有一千年了,但是我沒有讓這段代碼運行的問題。我必須使用ncurses庫才能在每次輸入字符後不必按Enter鍵的情況下運行它。

只是想我會讓每個人都知道這是行不通的。 :P我開着一個iRobot創建使用它。

0

您可以嘗試使用sudo service turtlebot stop停止自動初始設置,然後使用roslaunch turtlebot bringup minimal.launch僅運行所需的最小設置。然後,您可以嘗試使用roslaunch turtlebot_teleop keyboard_teleop.launch來使用帶鍵盤的遙控操作。

0

Hello World for TurtleBot寫了一篇關於剛剛開始的任何人的教程。

對於那些試圖讓這個人工作的人來說,

變化/cmd_velcmd_vel_mux/input/teleop

我使用:

  • 靛藍
  • TurtleBot2
+1

鏈接已死。 –