Requesting data via mavlink

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

What is the type of msg ???

It's probably a struct that has functions to access internal fields like "pitch", etc.

BTW: You probably want to use cargo fmt to format your code.

match msg {
    mavlink::ardupilotmega::MavMessage::ATTITUDE(
        mavlink::ardupilotmega::ATTITUDE_DATA {
            roll,
            pitch,
            ..
        }
    ) => {
        println!("roll: {roll}, pitch: {pitch}");
    }

    _ => {}
}

It's a miracle—it works!
Here's the result on my screen:

roll: 0.039741054, pitch: -0.015719308
roll: 0.039747275, pitch: -0.015701864
roll: 0.03974411, pitch: -0.015694147
roll: 0.039736528, pitch: -0.015685942
roll: 0.039722808, pitch: -0.015683198

A big thank you to you.

It is customary to mark the reply that gave you the answer as the solution to the topic, to give the poster credit. But it is up to you of course.

i'm sorry, but i'm not very familiar with this forum.

i'll make sure to mention that it was Jesper who gave me the solution

You can check the box at the bottom of @Jesper 's post to mark that post as the solution.

Thanks for the advice.

It's done.