Control Robot Using keyboard by publishing cmd_vel Twist msg (C++)

Share:
Control Robot Using keyboard by publishing cmd_vel Twist msg (C++) cover # Control robot using keyboard # References - https://docs.ros.org/en/diamondback/api/geometry_msgs/html/Twist_8h_source.html # cmd_vel and Twist - [geometry_msgs Msg/Srv Documentation](http://docs.ros.org/en/jade/api/geometry_msgs/html/index-msg.html) ## cmd_vel topic ```sh ➜ src git:(master) ✗ rostopic type /cmd_vel geometry_msgs/Twist ``` ## geometry_msgs/Twist ```sh # This expresses velocity in free space broken into its linear and angular parts. Vector3 linear Vector3 angular ``` ## geometry_msgs/Vector3.msg - http://docs.ros.org/en/jade/api/geometry_msgs/html/msg/Vector3.html - http://docs.ros.org/en/diamondback/api/geometry_msgs/html/Vector3_8h_source.html ```sh # This represents a vector in free space. # It is only meant to represent a direction. Therefore, it does not # make sense to apply a translation to it (e.g., when applying a # generic rigid transformation to a Vector3, tf2 will only apply the # rotation). If you want your data to be translatable too, use the # geometry_msgs/Point message instead. float64 x float64 y float64 z ``` # code ```cpp #include "ros/ros.h" #include "std_msgs/String.h" #include #include #include int main(int argc, char** argv) { ros::init(argc, argv, "turtlebot3_teleop"); ros::NodeHandle nh; ros::Publisher pub = nh.advertise("/cmd_vel", 10); ros::Rate loop_rate(10); ROS_INFO("Ready to control robot"); int count = 0; while (ros::ok()) { // geometry_msgs::Twist msg; geometry_msgs::TwistPtr msgPtr(new geometry_msgs::Twist); msgPtr->linear.x = 1.0; msgPtr->linear.y = 0.0; msgPtr->linear.z = 0.0; msgPtr->angular.x = 0.0; msgPtr->angular.y = 0.0; msgPtr->angular.z = 0.0; pub.publish(msgPtr); ros::spinOnce(); loop_rate.sleep(); } return 0; } ```

No comments