I'm new to Rust.
I want to retrieve pitch and roll data from a flight controller via MAVLink.
I copied a program that lets me display this data using the print function.
Here is the program:
use mavlink::error::MessageReadError;
use mavlink::MavConnection;
use std::{env, sync::Arc, thread, time::Duration};
fn main() {
let mut mavconn = mavlink::connect::mavlink::ardupilotmega::MavMessage("serial:/dev/ttyS2:500000").unwrap();
mavconn.set_protocol_version(mavlink::MavlinkVersion::V2);
let vehicle = Arc::new(mavconn);
vehicle
.send(&mavlink::MavHeader::default(), &request_data())
.unwrap();
loop {
match vehicle.recv() {
Ok((_header, msg)) => {
println!("received: {msg:?}");
//println!("{}", msg);
//println!("pitch: {}", attitude);
}
Err(MessageReadError::Io(e)) => {
if e.kind() == std::io::ErrorKind::WouldBlock {
//no messages currently available to receive -- wait a while
thread::sleep(Duration::from_secs(1));
continue;
} else {
println!("recv error: {e:?}");
break;
}
}
_ => {}
}
}
}
/// Create a message enabling command_long_data
pub fn request_data() -> mavlink::ardupilotmega::MavMessage {
mavlink::ardupilotmega::MavMessage::COMMAND_LONG(
mavlink::ardupilotmega::COMMAND_LONG_DATA {
param1 : 30.0,
param2 : 20000.0,
param3 : 0.0,
param4 : 0.0,
param5 : 0.0,
param6 : 0.0,
param7 : 0.0,
command : mavlink::ardupilotmega::MavCmd::MAV_CMD_SET_MESSAGE_INTERVAL,
target_system: 1,
target_component: 0,
confirmation: 0,
},
)
}
Here is the result on my screen :
received: ATTITUDE(ATTITUDE_DATA { time_boot_ms: 48198, roll: 0.027933115, pitch: -0.019092046, yaw: 1.0921404, rollspeed: -0.00025783258, pitchspeed: -0.00036752608, yawspeed: 0.00013963185 })
received: ATTITUDE(ATTITUDE_DATA { time_boot_ms: 48217, roll: 0.027932042, pitch: -0.019089786, yaw: 1.0921417, rollspeed: -0.00011153106, pitchspeed: -0.00022060832, yawspeed: -0.00023562514 })
received: ATTITUDE(ATTITUDE_DATA { time_boot_ms: 48237, roll: 0.027932605, pitch: -0.019088112, yaw: 1.0921559, rollspeed: -8.973977e-5, pitchspeed: -0.00029933767, yawspeed: 5.7152065e-5 })
received: ATTITUDE(ATTITUDE_DATA { time_boot_ms: 48257, roll: 0.02793914, pitch: -0.01909101, yaw: 1.0921576, rollspeed: 0.00042133417, pitchspeed: -0.00045995237, yawspeed: -9.257565e-5 })
received: ATTITUDE(ATTITUDE_DATA { time_boot_ms: 48277, roll: 0.0279463, pitch: -0.019081376, yaw: 1.0921631, rollspeed: 0.00019980557, pitchspeed: -0.00014700776, yawspeed: -0.00013636192 })
But I'd like to retrieve only the pitch and roll values as variables so I can use them later to display an artificial horizon.
Can you help me?
Thank you
Reply