mavros订阅PX4的定高激光以及悬停油门估计数据

借用已有的Altitude这个话题发布定高数据和基础油门估计

备注:v.1.11.3之后的PX4固件才加入了基础油门估计。


src/modules/mavlink/mavlink_messages.cppclass MavlinkStreamAltitude : public MavlinkStream函数里面
修改增加部分如下:

	uORB::Subscription _local_pos_sub{ORB_ID(vehicle_local_position)};
	uORB::Subscription _home_sub{ORB_ID(home_position)};
	uORB::Subscription _air_data_sub{ORB_ID(vehicle_air_data)};

	//wzy
    uORB::Subscription _hover_thrust_estimate_sub{ORB_ID(hover_thrust_estimate)};
    uORB::Subscription _distance_sensor_sub{ORB_ID(distance_sensor)};

	/* do not allow top copying this class */
	MavlinkStreamAltitude(MavlinkStreamAltitude &) = delete;
	MavlinkStreamAltitude &operator = (const MavlinkStreamAltitude &) = delete;

以及

		//wzy
        hover_thrust_estimate_s thrust_estimate;
		_hover_thrust_estimate_sub.copy(&thrust_estimate);
		msg.altitude_amsl = thrust_estimate.hover_thrust;
        //wzy
        distance_sensor_s distanceSensor;
        _distance_sensor_sub.copy(&distanceSensor);
        msg.altitude_local = distanceSensor.current_distance;

		// local position timeout after 10 ms
		// avoid publishing only baro altitude_monotonic if possible
		bool lpos_timeout = (hrt_elapsed_time(&local_pos.timestamp) > 10_ms);

		if (lpos_updated || (air_data_updated && lpos_timeout)) {
			msg.time_usec = hrt_absolute_time();
			mavlink_msg_altitude_send_struct(_mavlink->get_channel(), &msg);

			return true;
		}

		return false;

另外,为了增加该话题的发布速度,修改src/modules/mavlink/mavlink_main.cpp的如下部分修改

case MAVLINK_MODE_ONBOARD:
		// Note: streams requiring low latency come first
		configure_stream_local("TIMESYNC", 10.0f);
		configure_stream_local("CAMERA_TRIGGER", unlimited_rate);
		configure_stream_local("HIGHRES_IMU", 50.0f);
		configure_stream_local("LOCAL_POSITION_NED", 30.0f);
		configure_stream_local("ATTITUDE", 100.0f);
//		configure_stream_local("ALTITUDE", 10.0f);
		configure_stream_local("ALTITUDE", 30.0f);//wzy
		configure_stream_local("DISTANCE_SENSOR", 10.0f);
		configure_stream_local("MOUNT_ORIENTATION", 10.0f);
		configure_stream_local("OBSTACLE_DISTANCE", 10.0f);
		configure_stream_local("ODOMETRY", 30.0f);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++程序,它使用mavros库向PX4飞控发布指令,让四旋翼无人机悬停: ```c++ #include <ros/ros.h> #include <mavros_msgs/CommandBool.h> #include <mavros_msgs/CommandTOL.h> #include <mavros_msgs/OverrideRCIn.h> int main(int argc, char **argv) { // 初始化ROS节点 ros::init(argc, argv, "hover"); ros::NodeHandle nh; // 发布mavros命令 ros::Publisher rc_pub = nh.advertise<mavros_msgs::OverrideRCIn>("/mavros/rc/override", 10); ros::Publisher arming_pub = nh.advertise<mavros_msgs::CommandBool>("/mavros/cmd/arming", 10); ros::Publisher set_mode_pub = nh.advertise<mavros_msgs::SetMode>("/mavros/set_mode", 10); // 等待连接到飞控 while(ros::ok() && !ros::service::exists("/mavros/set_mode", true)) { ROS_INFO("Waiting for service /mavros/set_mode to become available"); ros::Duration(1).sleep(); } // 解锁飞控 mavros_msgs::CommandBool arming_cmd; arming_cmd.request.value = true; if(!arming_pub.call(arming_cmd) || !arming_cmd.response.success) { ROS_ERROR("Failed to arm the vehicle"); return -1; } // 将飞控模式设置为悬停模式 mavros_msgs::SetMode set_mode_cmd; set_mode_cmd.request.custom_mode = "ALT_HOLD"; if(!set_mode_pub.call(set_mode_cmd) || !set_mode_cmd.response.success) { ROS_ERROR("Failed to set the vehicle mode to ALT_HOLD"); return -1; } // 发布悬停指令 mavros_msgs::OverrideRCIn rc_cmd; rc_cmd.channels[0] = 1500; // 油门通道,1500表示无动作 rc_cmd.channels[1] = 1500; // 副翼通道,1500表示无动作 rc_cmd.channels[2] = 1500; // 方向舵通道,1500表示无动作 rc_cmd.channels[3] = 1500; // 升降舵通道,1500表示无动作 rc_cmd.channels[4] = 1500; // 油门自动控制通道,1500表示关闭自动控制 rc_cmd.channels[5] = 1500; // 油门自动控制通道,1500表示关闭自动控制 rc_cmd.channels[6] = 1500; // 油门自动控制通道,1500表示关闭自动控制 rc_cmd.channels[7] = 1500; // 油门自动控制通道,1500表示关闭自动控制 rc_pub.publish(rc_cmd); // 等待5秒钟 ros::Duration(5).sleep(); // 关闭无人机 arming_cmd.request.value = false; if(!arming_pub.call(arming_cmd) || !arming_cmd.response.success) { ROS_ERROR("Failed to disarm the vehicle"); return -1; } return 0; } ``` 这个程序首先通过mavros库初始化ROS节点,并创建了三个发布器,用于发布解锁飞控、设置飞控模式和控制指令。接下来,程序进入一个while循环,等待连接到飞控。一旦连接成功,程序发送解锁指令并等待响应。如果解锁成功,则将飞控模式设置为悬停模式,并等待响应。最后,程序使用mavros库的OverrideRCIn消息类型发布了一个悬停的控制指令。这个指令将无人机的所有通道都设为1500,表示无动作。程序等待5秒钟后,发送一个解锁指令将无人机关闭。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值