qq_33670784
qq_33670784
采纳率50%
2017-11-30 02:16 阅读 2.0k
已采纳

请教一个ROS编写发布者程序的问题

按照机器人操作系统浅析中发布者程序编写的步骤 到最后一步catkin时候一直报错报错如下:
[ 50%] Built target hello
[ 75%] Building CXX object agitr/CMakeFiles/pubvel.dir/pubvel.cpp.o
/home/lucky/ros/src/agitr/pubvel.cpp:12:3: error: stray ‘\342’ in program
msg.angular.z=2*double(rand())/double(RAND_MAX)−1;
^
/home/lucky/ros/src/agitr/pubvel.cpp:12:3: error: stray ‘\210’ in program
/home/lucky/ros/src/agitr/pubvel.cpp:12:3: error: stray ‘\222’ in program
/home/lucky/ros/src/agitr/pubvel.cpp: In function ‘int main(int, char**)’:
/home/lucky/ros/src/agitr/pubvel.cpp:12:53: error: expected ‘;’ before numeric constant
msg.angular.z=2*double(rand())/double(RAND_MAX)−1;
^
agitr/CMakeFiles/pubvel.dir/build.make:62: recipe for target 'agitr/CMakeFiles/pubvel.dir/pubvel.cpp.o' failed
make[2]: *** [agitr/CMakeFiles/pubvel.dir/pubvel.cpp.o] Error 1
CMakeFiles/Makefile2:963: recipe for target 'agitr/CMakeFiles/pubvel.dir/all' failed
make[1]: *** [agitr/CMakeFiles/pubvel.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j1 -l1" failed

pubvel 文件为
#include
#include
#include
int main (int argc,char **argv) {
ros::init(argc,argv,"publish_velocity") ;
ros::NodeHandle nh ;
ros::Publisher pub=nh.advertise("turtle1/cmd_vel",1000); srand(time(0));
ros::Rate rate(2);
while(ros::ok()){
geometry_msgs::Twist msg;
msg.linear.x = double (rand ( ) ) / double(RAND_MAX);
msg.angular.z=2*double(rand())/double(RAND_MAX)−1;
pub.publish(msg);
ROS_INFO_STREAM("Sending random velocity command:"<<"linear="<<msg.linear.x<<"angular="<<msg.angular.z);
rate.sleep();
}
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    tingge1992 zxtchiu 2018-04-21 02:51

    msg.angular.z=2*double(rand())/double(RAND_MAX)−1;

    这句代码你是从pdf上粘贴过来的吧,问题在于最后的-1,前面的不是减号,可能被翻译成中文的了!!!

    点赞 4 评论 复制链接分享
  • fjzhzp fjzhzp 2018-01-12 10:18

    msg.linear.x = double (rand ( ) ) / double(RAND_MAX);
    msg.angular.z=2*double(rand())/double(RAND_MAX)−1;中的double放在最外面

    点赞 1 评论 复制链接分享
  • qq_44715174 青衫无尘、 2019-09-04 09:19

    //This program publishes randomly−generated velocity

    //messages for turtlesim.

    #include

    #include //For geometry_msgs::Twist

    #include //For rand() and RAND_MAX

    int main (int argc, char **argv) {

    //Initialize the ROS system and become a node.

    ros::init (argc, argv, "publish_velocity");

    ros::NodeHandle nh;

    //Create a publisher object.

    ros::Publisher pub = nh.advertise (

    "turtle1/cmd_vel", 1000);

    //Seed the random number generator.

    srand (time (0));

    //Loop at 2Hz until the node is shut down.

    // ros::Raterate (2);

    while (ros::ok ()) {

      //Create and fill in the message.The other four
    
      //fields, which are ignored by turtlesim, default to 0.
    
      geometry_msgs::Twist msg;
    
      msg.linear.x = double (rand ())/double (RAND_MAX);
    
      msg.angular.z = 2* double (rand ())/double (RAND_MAX) - 1;
    
    
    
      //Publish the message.
    
      pub.publish (msg);
    
    
    
      //Send a message to rosout with the details.
    
      ROS_INFO_STREAM("Sending random velocity command:"
    
      << "linear=" << msg.linear.x
    
      << "angular=" << msg.angular.z);
    
    
    
      //Wait untilit's time for another iteration.
    
     // rate.sleep ();
    

    }

    }

    点赞 评论 复制链接分享

相关推荐