0
0
Drone Programmingprogramming~20 mins

MAVLink message structure in Drone Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MAVLink Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this MAVLink message header parsing code?

Consider a MAVLink message header represented as a byte array. The first byte is the start sign (0xFE), the second byte is the payload length, the third byte is the sequence number, the fourth byte is the system ID, and the fifth byte is the component ID.

What will be the printed output of the following code?

Drone Programming
header = bytes([0xFE, 10, 5, 1, 20])
start_sign = header[0]
payload_len = header[1]
seq_num = header[2]
sys_id = header[3]
comp_id = header[4]
print(f"Start: {start_sign}, Payload Length: {payload_len}, Seq: {seq_num}, SysID: {sys_id}, CompID: {comp_id}")
AStart: 0xFE, Payload Length: 10, Seq: 5, SysID: 1, CompID: 20
BStart: 254, Payload Length: 16, Seq: 5, SysID: 1, CompID: 20
CStart: 254, Payload Length: 10, Seq: 5, SysID: 20, CompID: 1
DStart: 254, Payload Length: 10, Seq: 5, SysID: 1, CompID: 20
Attempts:
2 left
💡 Hint

Remember that bytes are integers in Python and 0xFE is 254 in decimal.

🧠 Conceptual
intermediate
1:30remaining
Which field in a MAVLink message ensures message ordering?

MAVLink messages include several fields in their header. Which field is primarily used to keep track of the order of messages sent?

ASequence Number
BComponent ID
CMessage ID
DSystem ID
Attempts:
2 left
💡 Hint

Think about which field increments with each message sent.

Predict Output
advanced
2:30remaining
What error does this MAVLink message checksum calculation code raise?

Given the following Python code snippet that attempts to calculate a MAVLink message checksum, what error will it raise when run?

Drone Programming
def checksum(data):
    crc = 0xFFFF
    for b in data:
        crc ^= b << 8
        for _ in range(8):
            if crc & 0x8000:
                crc = (crc << 1) ^ 0x1021
            else:
                crc <<= 1
    return crc & 0xFFFF

msg = [0xFE, 10, 5, 1, 20]
print(checksum(msg))
ATypeError: unsupported operand type(s) for <<: 'str' and 'int'
BNo error, prints an integer checksum
CIndexError: list index out of range
DAttributeError: 'list' object has no attribute 'encode'
Attempts:
2 left
💡 Hint

Check the data type of elements in the list and how bitwise operations work on integers.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a MAVLink message dictionary with message ID as key and payload length as value?

Choose the option that creates a dictionary mapping MAVLink message IDs to their payload lengths correctly.

A{msg_id: payload_len for msg_id, payload_len in [(1, 10), (2, 20), (3, 30)] if payload_len > 15}
B{msg_id: payload_len if payload_len > 15 for msg_id, payload_len in [(1, 10), (2, 20), (3, 30)]}
C{msg_id: payload_len for msg_id, payload_len in [(1, 10), (2, 20), (3, 30)]}
D{msg_id: payload_len for (msg_id, payload_len) in [(1, 10), (2, 20), (3, 30)] if payload_len > 15 else 0}
Attempts:
2 left
💡 Hint

Remember the syntax for dictionary comprehensions with conditions.

🚀 Application
expert
3:00remaining
What is the value of the 'payload' variable after parsing this MAVLink message byte stream?

Given a MAVLink message byte stream where the payload length is 3, and the payload bytes are [0x01, 0x02, 0x03], what will be the value of the payload variable after running the code below?

Drone Programming
message = bytes([0xFE, 3, 10, 1, 20, 0x01, 0x02, 0x03, 0xAA, 0xBB])
payload_len = message[1]
payload = message[5:5+payload_len]
print(payload)
Ab'\x03\x10\x01'
B[1, 2, 3]
Cb'\x01\x02\x03'
Db'\xFE\x03\x0A'
Attempts:
2 left
💡 Hint

Remember that slicing bytes returns a bytes object with the selected bytes.