我正在尝试为 TurtleBot 编程,但机器人教程严重缺乏,我无法编写自己的 C++ 工作。我正在尝试使用另一个机器人的教程,只是为了让机器人在按下键时移动。
源教程在这里找到:,我只是将发布主题修改为“/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/)
根本没有回声的输出。