编程辅导 * ROSCPP demo publisher.

* ROSCPP demo publisher.
* Sends twist messages for controlling a robot base.
* October 2018.

Copyright By PowCoder代写 加微信 powcoder

#include
#include

#include

#include
#include
#include

* This tutorial demonstrates simple sending of messages over the ROS system
* and controlling a robot.
int main(int argc, char **argv)
/// Name your node
ros::init(argc, argv, “stage_mover”);
/// Every ros node needs a node handle, similar to your usual file handle.
ros::NodeHandle nh_;
/// Publisher object that decides what kind of topic to publish and how fast.
ros::Publisher cmd_vel_pub_ = nh_.advertise(“/cmd_vel”, 1);
// We will be sending commands of type “twist”
geometry_msgs::Twist base_cmd;
// User input
char cmd[50];

std::cout << "Type a command and then press enter. " "Use 'f' to move forward, 'l' to turn left, " "'r' to turn right, '.' to exit.\n"; /// The main loop will run at a rate of 10Hz, i.e., 10 times per second. ros::Rate loop_rate(10); /// Standard way to run ros code. Will quite if ROS is not OK, that is, the master is dead. while (ros::ok()) std::cin.getline(cmd, 50); if(cmd[0]!='f' && cmd[0]!='l' && cmd[0]!='r' && cmd[0]!='.') std::cout << "unknown command:" << cmd << "\n"; base_cmd.linear.x = base_cmd.linear.y = base_cmd.angular.z = 0; //move forward if(cmd[0]=='f'){ base_cmd.linear.x = 0.25; base_cmd.angular.z = 0; //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; else if(cmd[0]=='.'){ /// Here's where we publish the actual message. cmd_vel_pub_.publish(base_cmd); /// Spin the ros main loop once ros::spinOnce(); /// Sleep for as long as needed to achieve the loop rate. loop_rate.sleep(); 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com