運行roscore,turtlesim
設置速度rosparam set /publish_velocity/max_vel 1
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <cstdlib>
int main(int argc,char **argv)
{
ros::init(argc,argv,"publish_velocity");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<geometry_msgs::Twist>("turtle1/cmd_vel",1000);
srand(time(0));
const std::string PARAM_NAME = "~max_vel" ;
double maxVel;
bool ok = ros::param::get (PARAM_NAME, maxVel) ;
if(!ok)
{
ROS_FATAL_STREAM("Could not get parameter "<<PARAM_NAME);
exit(1);
}
ros::Rate rate(2);
while(ros::ok())
{
geometry_msgs::Twist msg;
msg.linear.x=maxVel*double(rand())/double(RAND_MAX);
msg.angular.z=6*double(rand())/double(RAND_MAX)-3;
pub.publish(msg);
ROS_INFO_STREAM("Randomly control"
<<" v:"<<msg.linear.x
<<" theta:"<<msg.angular.z);
rate.sleep();
}
}