Simvik

Drone: pack a telemetry packet

Serialise a telemetry struct into an exact 12-byte, high-byte-first packet for the radio. A medium embedded C problem (Structs, unions and memory layout),…

medium · Structs, unions and memory layout · serialisation · big-endian · padding · product

Product: Drone flight controller (Aerospace and drones)

Requirement. The drone sends telemetry to the ground station 50 times a second. The ground station runs on a different processor and compiler, so the packet layout is fixed by the protocol, not by the struct:

BytesField
0–1seq
2–5altitude_cm (signed)
6–7roll_cd (signed)
8–9pitch_cd (signed)
10battery_pct
11flags

Every multi-byte field is sent high byte first. Fill out with the 12 bytes.

Starter code

void pack_telemetry(const telem_t *t, uint8_t out[12]) {
  memcpy(out, t, 12);
}

Example tests

In flight

telem_t t = { 0x0102, 1000, -150, 300, 87, 0x05 };
uint8_t o[12];
pack_telemetry(&t, o);
for (int i = 0; i < 12; i++) printf("%02X ", o[i]);
printf("\n");

Expected output:

01 02 00 00 03 E8 FF 6A 01 2C 57 05

Below the take-off point

telem_t t = { 65535, -250, 0, 0, 0, 0 };
uint8_t o[12];
pack_telemetry(&t, o);
for (int i = 0; i < 12; i++) printf("%02X ", o[i]);
printf("\n");

Expected output:

FF FF FF FF FF 06 00 00 00 00 00 00

Learn it first

More Structs, unions and memory layout problems