Skip to content

Commit 697e813

Browse files
authored
Merge pull request #103 from CarletonURocketry/eh/voltage-db
Voltage DataBlock
2 parents 102f71b + 6f9ade4 commit 697e813

File tree

4 files changed

+81
-16
lines changed

4 files changed

+81
-16
lines changed

modules/telemetry/telemetry_packet.json

+4
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@
3737
"coordinates": {
3838
"latitude": { "1": { "9": "latitude" } },
3939
"longitude": { "1": { "9": "longitude" } }
40+
},
41+
"voltage": {
42+
"id": { "1": { "10": "id" } },
43+
"voltage": { "1": { "10": "voltage"} }
4044
}
4145
}

modules/telemetry/telemetry_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def parse_radio_block(pkt_version: int, block_header: BlockHeader, hex_block_con
8787
except NotImplementedError:
8888
logger.warning(
8989
f"Block parsing for type {block_header.message_type}, with subtype {block_header.message_subtype} not \
90-
implemented!"
90+
implemented!"
9191
)
9292
return
9393
except v1db.DataBlockException as e:

modules/telemetry/v1/data_block.py

+47-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DataBlockSubtype(IntEnum):
3030
ANGULAR_VELOCITY = 0x07
3131
HUMIDITY = 0x08
3232
COORDINATES = 0x09
33+
VOLTAGE = 0x0A
3334

3435
def __str__(self):
3536
match self:
@@ -53,6 +54,8 @@ def __str__(self):
5354
return "HUMIDITY"
5455
case DataBlockSubtype.COORDINATES:
5556
return "COORDINATES"
57+
case DataBlockSubtype.VOLTAGE:
58+
return "VOLTAGE"
5659

5760

5861
class DataBlock(ABC):
@@ -105,6 +108,7 @@ def parse(block_subtype: DataBlockSubtype, payload: bytes) -> DataBlock:
105108
DataBlockSubtype.LIN_ACCEL_REL: RelativeLinearAccelerationDB,
106109
DataBlockSubtype.LIN_ACCEL_ABS: AbsoluteLinearAccelerationDB,
107110
DataBlockSubtype.ANGULAR_VELOCITY: AngularVelocityDB,
111+
DataBlockSubtype.VOLTAGE: VoltageDB,
108112
}
109113

110114
subtype = SUBTYPE_CLASSES.get(block_subtype)
@@ -435,17 +439,46 @@ def __iter__(self):
435439
yield "angular_velocity", {"x": self.x_axis, "y": self.y_axis, "z": self.z_axis, "magnitude": self.magnitude}
436440

437441

438-
# TODO: Remove this function
439-
def parse_data_block(type: DataBlockSubtype, payload: bytes) -> DataBlock:
440-
"""
441-
Parses a bytes payload into the correct data block type.
442-
Args:
443-
type: The type of data block to parse the bytes into.
444-
payload: The bytes payload to parse into a data block.
445-
Returns:
446-
The parse data block.
447-
Raises:
448-
ValueError: Raised if the bytes cannot be parsed into the corresponding type.
449-
"""
450-
451-
return DataBlock.parse(type, payload)
442+
class VoltageDB(DataBlock):
443+
"""Represents a voltage data block"""
444+
445+
def __init__(self, mission_time: int, id: int, voltage: int) -> None:
446+
"""
447+
Constructus a voltage data block.
448+
449+
Args:
450+
mission_time: The mission time the voltage was measured in milliseconds since launch.
451+
id: A numerical id associated with the voltage measurement for identification by the receiver
452+
voltage: The measured voltage in units of millivolts
453+
"""
454+
super().__init__(mission_time)
455+
self.id: int = id
456+
self.voltage: int = voltage
457+
458+
@classmethod
459+
def from_bytes(cls, payload: bytes) -> Self:
460+
"""
461+
Constructs a voltage data block from bytes.
462+
Returns:
463+
A voltage data block.
464+
"""
465+
parts = struct.unpack("<IHh", payload)
466+
return cls(parts[0], parts[1], parts[2])
467+
468+
def __len__(self) -> int:
469+
"""
470+
Get the length of a voltage data block in bytes
471+
Returns:
472+
The length of a voltage data block in bytes not including the block header.
473+
"""
474+
return 8
475+
476+
def __str__(self):
477+
return (
478+
f"""{self.__class__.__name__} -> time: {self.mission_time} ms, id: {self.id}, voltage: {self.voltage} mV"""
479+
)
480+
481+
def __iter__(self):
482+
yield "mission_time", self.mission_time
483+
yield "id", self.id
484+
yield "voltage", self.voltage

tests/parsing/test_block_data.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
__author__ = "Elias Hawa"
33

44
import pytest
5-
from modules.telemetry.v1.data_block import PressureDB, TemperatureDB, LinearAccelerationDB, AngularVelocityDB
5+
from modules.telemetry.v1.data_block import (
6+
PressureDB,
7+
TemperatureDB,
8+
LinearAccelerationDB,
9+
AngularVelocityDB,
10+
VoltageDB,
11+
)
612

713

814
@pytest.fixture
@@ -53,6 +59,19 @@ def angular_velocity_data_content() -> bytes:
5359
return b"\x00\x00\x00\x00\x06\x00\x0b\x00\xfd\xff\x00\x00"
5460

5561

62+
@pytest.fixture
63+
# 9b0d00000200ee0c
64+
# DEBUG:modules.telemetry.telemetry_utils:VoltageDB -> time: 3483 ms, id: 2, voltage: 3310 mV
65+
def voltage_data_content() -> bytes:
66+
"""
67+
Returns a voltage sensor reading with the following attributes
68+
mission time: 3483 ms
69+
id: 2
70+
voltage: 3310 mV
71+
"""
72+
return b"\x9b\x0d\x00\x00\x02\x00\xee\x0c"
73+
74+
5675
def test_pressure_data_block(pressure_data_content: bytes) -> None:
5776
"""Test that the pressure data block is parsed correctly."""
5877
pdb = PressureDB.from_bytes(pressure_data_content)
@@ -89,3 +108,12 @@ def test_angular_velocity_data_block(angular_velocity_data_content: bytes) -> No
89108
assert ang_vel.y_axis == 1.1
90109
assert ang_vel.z_axis == -0.3
91110
assert ang_vel.magnitude == 1.29
111+
112+
113+
def test_voltage_data_block(voltage_data_content: bytes) -> None:
114+
"""Test that the voltage data block is parsed correctly."""
115+
vdb = VoltageDB.from_bytes(voltage_data_content)
116+
117+
assert vdb.mission_time == 3483
118+
assert vdb.id == 2
119+
assert vdb.voltage == 3310

0 commit comments

Comments
 (0)