Skip to content

Commit 314d537

Browse files
committed
Add CRTP protocol version guard to supervisor subsystem
All public methods now warn and bail early if the connected Crazyflie reports a protocol version < 12, which is required for the supervisor port.
1 parent b659673 commit 314d537

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

cflib/crazyflie/supervisor.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"""
2525
import logging
2626
import time
27+
import warnings
2728

2829
from cflib.crtp.crtpstack import CRTPPacket
2930
from cflib.crtp.crtpstack import CRTPPort
@@ -91,6 +92,19 @@ def __init__(self, crazyflie):
9192
self._cf.add_port_callback(CRTPPort.SUPERVISOR, self._supervisor_callback)
9293
self._bitfield_response_received = False
9394

95+
def _check_protocol_version(self):
96+
"""Returns True if the protocol version is supported, False otherwise."""
97+
version = self._cf.platform.get_protocol_version()
98+
if version < 12:
99+
warnings.warn(
100+
'The supervisor subsystem requires CRTP protocol version 12 or later. '
101+
f'Connected Crazyflie reports version {version}. '
102+
'Update your Crazyflie firmware.',
103+
stacklevel=3,
104+
)
105+
return False
106+
return True
107+
94108
def _supervisor_callback(self, pk: CRTPPacket):
95109
"""
96110
Called when a packet is received.
@@ -121,6 +135,8 @@ def _fetch_bitfield(self, timeout=0.2):
121135
Request the bitfield and wait for response (blocking).
122136
Uses time-based cache to avoid sending packages too frequently.
123137
"""
138+
if not self._check_protocol_version():
139+
return 0
124140
now = time.time()
125141

126142
# Return cached value if it's recent enough
@@ -242,6 +258,8 @@ def send_arming_request(self, do_arm: bool):
242258
Args:
243259
do_arm (bool): True = arm the system, False = disarm the system
244260
"""
261+
if not self._check_protocol_version():
262+
return
245263
pk = CRTPPacket()
246264
pk.set_header(CRTPPort.SUPERVISOR, SUPERVISOR_CH_COMMAND)
247265
pk.data = (CMD_ARM_SYSTEM, do_arm)
@@ -252,6 +270,8 @@ def send_crash_recovery_request(self):
252270
"""
253271
Send crash recovery request.
254272
"""
273+
if not self._check_protocol_version():
274+
return
255275
pk = CRTPPacket()
256276
pk.set_header(CRTPPort.SUPERVISOR, SUPERVISOR_CH_COMMAND)
257277
pk.data = (CMD_RECOVER_SYSTEM,)
@@ -262,6 +282,8 @@ def send_emergency_stop(self):
262282
"""
263283
Send emergency stop. The Crazyflie will immediately stop all motors.
264284
"""
285+
if not self._check_protocol_version():
286+
return
265287
pk = CRTPPacket()
266288
pk.set_header(CRTPPort.SUPERVISOR, SUPERVISOR_CH_COMMAND)
267289
pk.data = (CMD_EMERGENCY_STOP,)
@@ -273,6 +295,8 @@ def send_emergency_stop_watchdog(self):
273295
Send emergency stop watchdog. The Crazyflie will stop all motors
274296
unless this command is repeated at regular intervals.
275297
"""
298+
if not self._check_protocol_version():
299+
return
276300
pk = CRTPPacket()
277301
pk.set_header(CRTPPort.SUPERVISOR, SUPERVISOR_CH_COMMAND)
278302
pk.data = (CMD_EMERGENCY_STOP_WATCHDOG,)

0 commit comments

Comments
 (0)