文件
以垂起切换为例
send_vehicle_command(vehicle_command_s::VEHICLE_CMD_DO_VTOL_TRANSITION,
(float)(status.vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROTARY_WING ?
vtol_vehicle_status_s::VEHICLE_VTOL_STATE_FW :
vtol_vehicle_status_s::VEHICLE_VTOL_STATE_MC));
函数send_vehicle_command
第一个参数vehicle_command_s::VEHICLE_CMD_DO_VTOL_TRANSITION就是一个标志位
第二个参数vtol_vehicle_status_s::VEHICLE_VTOL_STATE_FW :也是一个标志位,值是4,表示由旋翼切到固定翼,如果有固定翼切到旋翼,就是3.
参看send_vehicle_command函数可以知道,这个函数是通过发布vehicle_command_s切换模式,我们也可以直接通过发布这个话题实现切换,主要注意两点
一个是vcmd.command = cmd,也就是vcmd.command=vehicle_command_s::VEHICLE_CMD_DO_VTOL_TRANSITION
还有vcmd.param1 = param1;也就是
vcmd.param1 = 3的话就是切到旋翼=4就是切到固定翼。这两个赋值之后再对
vcmd.source_system = vehicle_status_sub.get().system_id;
vcmd.target_system = vehicle_status_sub.get().system_id;
vcmd.source_component = vehicle_status_sub.get().component_id;
vcmd.target_component = vehicle_status_sub.get().component_id;
vcmd.timestamp = hrt_absolute_time();
这些赋值完就可以发布了。发布之后就可以切换模式了
static bool send_vehicle_command(uint16_t cmd, float param1 = NAN, float param2 = NAN, float param3 = NAN,
float param4 = NAN, float param5 = NAN, float param6 = NAN, float param7 = NAN)
{
vehicle_command_s vcmd{};
vcmd.param1 = param1;
vcmd.param2 = param2;
vcmd.param3 = param3;
vcmd.param4 = param4;
vcmd.param5 = (double)param5;
vcmd.param6 = (double)param6;
vcmd.param7 = param7;
vcmd.command = cmd;
uORB::SubscriptionData<vehicle_status_s> vehicle_status_sub{ORB_ID(vehicle_status)};
vcmd.source_system = vehicle_status_sub.get().system_id;
vcmd.target_system = vehicle_status_sub.get().system_id;
vcmd.source_component = vehicle_status_sub.get().component_id;
vcmd.target_component = vehicle_status_sub.get().component_id;
vcmd.timestamp = hrt_absolute_time();
uORB::PublicationQueued<vehicle_command_s> vcmd_pub{ORB_ID(vehicle_command)};
return vcmd_pub.publish(vcmd);
}
如果不知道自己想切换的模式的command和param是多少,可以查看commander.cpp文件里是怎么赋值的