0

阿木币

0

精华

1 小时

在线时间

应届白菜

Rank: 1

发表于 2022-1-11 16:22:23 2113 浏览 2 回复

PX4+gazebo仿真的unecpected comand176报错

我想使用PX4+gazebo跑一个起飞,悬停,降落的程序;结果遇到了图中错误;
问题表现为:
  • invalid setpoints
  • CMD: Unexpected command 176,result 0
  • 在程序中的"Offboard enabled"循环出不来
  • PX4不断地启动和关闭失效安全模式

  1. mavros_msgs::State current_state;
  2. void state_cb(const mavros_msgs::State::ConstPtr& msg)
  3. {
  4.     current_state = *msg;
  5. }

  6. geometry_msgs::PoseStamped current_pose;

  7. void local_cb(const geometry_msgs::PoseStamped::ConstPtr& msg)
  8. {
  9.     current_pose = *msg;
  10. }

  11. int main(int argc, char **argv)
  12. {
  13.     ros::init(argc, argv, "offb_node");
  14.     ros::NodeHandle nh;

  15.     ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
  16.             ("mavros/state", 10, state_cb);
  17.     ros::Subscriber local_pos_sub = nh.subscribe<geometry_msgs::PoseStamped>
  18.             ("mavros/local_position/pose",10,local_cb);
  19.     ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
  20.             ("mavros/setpoint_position/local", 10);
  21.     ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
  22.             ("mavros/cmd/arming");
  23.     ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
  24.             ("mavros/set_mode");

  25.     //the setpoint publishing rate MUST be faster than 2Hz
  26.     ros::Rate rate(20.0);

  27.     // wait for FCU connection
  28.     while(ros::ok() && !current_state.connected){
  29.         ros::spinOnce();
  30.         rate.sleep();
  31.     }

  32.     geometry_msgs::PoseStamped pose;
  33.     pose.pose.position.x = 0;
  34.     pose.pose.position.y = 0;
  35.     pose.pose.position.z = 2;

  36.     //send a few setpoints before starting
  37.     for(int i = 100; ros::ok() && i > 0; --i){
  38.         local_pos_pub.publish(pose);
  39.         ros::spinOnce();
  40.         rate.sleep();
  41.     }

  42.     mavros_msgs::SetMode offb_set_mode;
  43.     offb_set_mode.request.custom_mode = "OFFBOARD";

  44.     mavros_msgs::CommandBool arm_cmd;
  45.     arm_cmd.request.value = true;

  46.     ros::Time last_request = ros::Time::now();
  47.     ROS_INFO("Offboarding");
  48.     while(ros::ok())
  49.     {
  50.         if( current_state.mode != "OFFBOARD" &&
  51.             (ros::Time::now() - last_request > ros::Duration(5.0))){
  52.             if( set_mode_client.call(offb_set_mode) &&
  53.                 offb_set_mode.response.mode_sent){
  54.                 ROS_INFO("Offboard enabled");
  55.             }
  56.             last_request = ros::Time::now();
  57.         } else {
  58.             if( !current_state.armed &&
  59.                 (ros::Time::now() - last_request > ros::Duration(5.0))){
  60.                 if( arming_client.call(arm_cmd) &&
  61.                     arm_cmd.response.success){
  62.                     ROS_INFO("Vehicle armed");
  63.                 }
  64.                 last_request = ros::Time::now();
  65.             }
  66.         }

  67.         local_pos_pub.publish(pose);

  68.         ros::spinOnce();

  69.         geometry_msgs::Point curr,aim;
  70.         curr = current_pose.pose.position;
  71.         aim = pose.pose.position;
  72.         double dist = sqrt(pow((curr.x-aim.x),2)+pow((curr.y-aim.y),2)+pow((curr.z-aim.z),2));
  73.         if(dist < 0.08)
  74.         {
  75.             ROS_INFO("Reach the goal.");
  76.             break;
  77.         }
  78.         rate.sleep();
  79.     }

  80.     for(int i = 100; ros::ok() && i > 0; --i)//悬停一会
  81.     {
  82.         local_pos_pub.publish(pose);
  83.         ros::spinOnce();
  84.         rate.sleep();
  85.     }

  86.     ROS_INFO("Landing");
  87.     mavros_msgs::SetMode land_set_mode;
  88.     land_set_mode.request.custom_mode = "AUTO.LAND";
  89.     while (ros::ok())
  90.     {
  91.         if( current_state.mode != "AUTO.LAND" &&
  92.             (ros::Time::now() - last_request > ros::Duration(5.0))){
  93.             if( set_mode_client.call(land_set_mode) &&
  94.                 land_set_mode.response.mode_sent){
  95.                 ROS_INFO("Land enabled");
  96.             }
  97.             last_request = ros::Time::now();
  98.         }
  99.         if(!current_state.armed)
  100.         {
  101.             break;
  102.         }
  103.         // local_pos_pub.publish(pose);

  104.         ros::spinOnce();
  105.         rate.sleep();
  106.     }
  107.     return 0;
  108. }
复制代码
不知道是什么原因,请各位同学老师指点。

2022-01-11 13-24-42 的屏幕截图.png


hazyparker已获得悬赏 3 阿木币

最佳答案

这个程序在别的机子上能跑,而且我看能跑的也会报这个176的错,但是不影响运行;不知道是什么原因卡在了“offboard enable”出不来,导致解锁不了,没法向下进行 ...

扫一扫浏览分享
回复

使用道具 举报

140

阿木币

0

精华

31 小时

在线时间

老司机

Rank: 2

发表于 2022-1-11 16:25:51
字面意思 是你给的命令没对吧
回复 点赞

使用道具 举报

0

阿木币

0

精华

1 小时

在线时间

应届白菜

Rank: 1

 楼主| 发表于 2022-1-11 16:59:20
shanghuo 发表于 2022-1-11 16:25
字面意思 是你给的命令没对吧

这个程序在别的机子上能跑,而且我看能跑的也会报这个176的错,但是不影响运行;不知道是什么原因卡在了“offboard enable”出不来,导致解锁不了,没法向下进行
回复 点赞

使用道具 举报

返回列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表