为了自主控制turtlebot的行走,我们需要在代码中实现。
一.准备
- 在这个过程中,我们需要知道向哪一个主题发送消息。网上说是cmd_vel,但是我尝试并未成功。我们可以使用rostopic list,查看当前输出活动主题的列表。我们可以看到/cmd_vel_mux/input/teleop就是我们需要的,代码如下:
young@young-Lenovo:~$ rostopic list
/capability_server/bonds
/capability_server/events
/cmd_vel_mux/active
/cmd_vel_mux/input/navi
/cmd_vel_mux/input/safety_controller
/cmd_vel_mux/input/teleop
/cmd_vel_mux/parameter_descriptions
/cmd_vel_mux/parameter_updates
/diagnostics
/diagnostics_agg
/diagnostics_toplevel_state
/gateway/force_update
/gateway/gateway_info
/info
/interactions/interactive_clients
/interactions/pairing
/joint_states
/mobile_base/commands/velocity
/mobile_base/sensors/core
/mobile_base/sensors/imu_data
/mobile_base/sensors/imu_data_raw
/mobile_base_nodelet_manager/bond
/odom
/robot_pose_ekf/odom_combined
/rosout
/rosout_agg
/tf
/tf_static
/turtlebot/incompatible_rapp_list
/turtlebot/rapp_list
/turtlebot/status
/turtlebot_node/parameter_descriptions
/turtlebot_node/parameter_updates
/zeroconf/lost_connections
/zeroconf/new_connections
代码
#include <ros/ros.h>
#include <signal.h>
#include <geometry_msgs/Twist.h>
ros::Publisher cmdVelPub;
int main(int argc, char **argv)
{
ros::init(argc, argv, "exbotxi_example_move");
std::string topic = "cmd_vel_mux/input/teleop";
ros::NodeHandle node;
cmdVelPub = node.advertise<geometry_msgs::Twist>("cmd_vel", 1);
ros::Rate loopRate(50);
//signal(SIGINT, shutdown);
ROS_INFO("exbot_example_move cpp start...");
int rate = 50;
double linear_speed = 0.2;
double goal_distance = 1.0;
double linear_duration = goal_distance / linear_speed;
geometry_msgs::Twist speed;
for(int i=0; i<1; i++)
{
speed = geometry_msgs::Twist(); //stop car
int ticks = int(linear_duration * rate);
speed.linear.x = linear_speed;
for(int t=0; t<ticks; t++)
{
cmdVelPub.publish(speed);
loopRate.sleep();
}
speed = geometry_msgs::Twist();
}
return 0;
}