原本是想用ROS控制机械臂,后来突发奇想,想通过rand函数产生随机数,来让机械臂随便动,于是写了如下一小段
#include <ros/ros.h> //可以发送轨迹的
#include <trajectory_msgs/JointTrajectory.h>
#include "ros/time.h"
#include <stdlib.h>
ros::Publisher arm_pub;
int main(int argc, char** argv)
{
int y,z;
ros::init(argc, argv, "talker");
ros::NodeHandle n;
arm_pub = n.advertise<trajectory_msgs::JointTrajectory>("joint_path_command",1000);
ros::Rate loop_rate(10);
trajectory_msgs::JointTrajectory traj;
trajectory_msgs::JointTrajectoryPoint points_n;
traj.header.frame_id = "base_link";
traj.joint_names.resize(6);
y=6;
traj.points.resize(y);
for(z=0;z<y;z++)
{
traj.points[z].positions.resize(6);
traj.points[z].velocities.resize(6);//-----
}
traj.joint_names[0] ="joint_1";
traj.joint_names[1] ="joint_2";
traj.joint_names[2] ="joint_3";
traj.joint_names[3] ="joint_4";
traj.joint_names[4] ="joint_5";
traj.joint_names[5] ="joint_6";
//int i(100);
int i=1;
int j=0;
double k;
double times=3.6;
while(ros::ok()&&i<5)//????
{
traj.header.stamp = ros::Time::now();
//k=-0.5;
for(j=0; j<6; j++)
{
traj.points[0].positions[j] = rand()/double(RAND_MAX)*2-1;//(i/5)%5;
}
traj.points[0].time_from_start = ros::Duration(times+0.1);//1 second
for(j=0; j<6; j++)
{
traj.points[1].positions[j] = rand()/double(RAND_MAX)*2-1;
}
traj.points[1].time_from_start = ros::Duration(times+0.2);//1 second
for(j=0; j<6; j++)
{
traj.points[2].positions[j]=rand()/double(RAND_MAX)*2-1;
}
traj.points[2].time_from_start = ros::Duration(times+0.3);//1 second
for(j=0; j<6; j++)
{
traj.points[3].positions[j]=rand()/double(RAND_MAX)*2-1;
}
traj.points[3].time_from_start = ros::Duration(times+0.4);
for(j=0; j<6; j++)
{
traj.points[4].positions[j]=rand()/double(RAND_MAX)*2-1;
}
traj.points[4].time_from_start = ros::Duration(times+0.5);
for(j=0; j<6; j++)
{
traj.points[5].positions[j]=rand()/double(RAND_MAX)*2-1;
}
traj.points[5].time_from_start = ros::Duration(times+0.6);
arm_pub.publish(traj);
ros::spinOnce();
loop_rate.sleep();
i++;
}
return 0;
}
一段极不规范的C++代码,勉强能让机器动起来.
然而在我编译完(catkin_make)一次之后,每次rosrun这个cpp,得到的轨迹都是一样的,是不是编译完之后,每个rand随机的结果就确定了,不会改变了呢?
但我的目标也的确不是让机械臂随便动(这没有意义),所以我也不会再考虑这个问题了.
while(ros::ok()&&i<5)//????
这个是我最近想的问题,为什么那个数大于5才能让机器动起来.
再思考吧