我正在尝试为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/)
回声根本没有输出。
答案 0 :(得分:1)
我知道这篇文章已经有一千年的历史了,但我没有遇到这个代码运行的问题。我不得不使用ncurses库来运行它,而不必在输入每个字符后按Enter键。
我想我会让每个人都知道这确实有效。 :P我驾驶iRobot创造了使用它。
答案 1 :(得分:0)
您可以尝试sudo service turtlebot stop
停止自动初始设置,然后roslaunch turtlebot bringup minimal.launch
仅运行您需要的最小设置。然后,您可以尝试roslaunch turtlebot_teleop keyboard_teleop.launch
使用带键盘的远程操作。
答案 2 :(得分:0)
在Hello World for TurtleBot上为任何刚刚开始的人写了一个教程。
对于那些试图让这个人工作的人来说。
将/cmd_vel
更改为cmd_vel_mux/input/teleop
我正在使用: