c++ - TurtleBot ROS 使用 Twist 移动

标签 c++ ros

我正在尝试为 TurtleBot 编程,但严重缺乏机器人教程,而且我一直无法编写自己的 C++ 来运行。我正在尝试使用另一个机器人的教程,只是为了让机器人在按下某个键时移动。

源码教程找到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 消息似乎没有被发送(或没有被传送)。在不同的终端,我有:

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

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

有信息:

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/)

回声根本没有输出。

最佳答案

我知道这篇文章已有一千年的历史了,但我可以毫无问题地运行这段代码。我不得不使用 ncurses 库让它运行,而不必在输入每个字符后按回车键。

只是想让每个人都知道这确实有效。 :P 我用它驱动了一个 iRobot create。

关于c++ - TurtleBot ROS 使用 Twist 移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13213917/

相关文章:

c++ - 试图调用模板类的友元函数

c++ - `moveToThread(nullptr)` 是从源线程拉出目标线程内的 QObject 的有效方法吗?

c++ - 如何对传递给可变参数宏的可变参数求和?

java - 动态内存处理 Java 与 C++

package - 如何在不共享源代码的情况下分发 ROS 包

c - 在 ROS 包中链接外部头文件

c++ - 使用 Qt Creator 读取 rosbag 文件时出错

c++ - 无法在 MFC/C++ 项目中将参数 1 从 'const wchar_t *' 转换为 'LPCTSTR'

python - 如何计算两个物体之间的相对位姿并将它们放入变换矩阵中

c++ - 如何在 C++ 中的 ROS 包文件中写入字符串消息