Skip to content

Commit 0c63fde

Browse files
committed
Send correct yaw sign based on the Crazyflie's CRTP version; maintain backward compatibility
The lib now checks the Crazyflie's CRTP version before sending velocity, zdistance, or hover setpoints. If the Crazyflie is outdated, it sends the legacy type with the original (negated) yaw to maintain compatibility. If the Crazyflie supports the new format, it sends the updated type with the correct yaw sign. This ensures a smooth transition when updating either the lib or Crazyflie separately. The legacy type will be deprecated in a future update.
1 parent 1b6ebc3 commit 0c63fde

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

cflib/crazyflie/commander.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Used for sending control setpoints to the Crazyflie
2727
"""
2828
import struct
29+
import warnings
2930

3031
from cflib.crtp.crtpstack import CRTPPacket
3132
from cflib.crtp.crtpstack import CRTPPort
@@ -38,11 +39,14 @@
3839
META_COMMAND_CHANNEL = 1
3940

4041
TYPE_STOP = 0
41-
TYPE_VELOCITY_WORLD = 1
42-
TYPE_ZDISTANCE = 2
43-
TYPE_HOVER = 5
42+
TYPE_VELOCITY_WORLD_LEGACY = 1
43+
TYPE_ZDISTANCE_LEGACY = 2
44+
TYPE_HOVER_LEGACY = 5
4445
TYPE_FULL_STATE = 6
4546
TYPE_POSITION = 7
47+
TYPE_VELOCITY_WORLD = 8
48+
TYPE_ZDISTANCE = 9
49+
TYPE_HOVER = 10
4650

4751
TYPE_META_COMMAND_NOTIFY_SETPOINT_STOP = 0
4852

@@ -122,8 +126,16 @@ def send_velocity_world_setpoint(self, vx, vy, vz, yawrate):
122126
pk = CRTPPacket()
123127
pk.port = CRTPPort.COMMANDER_GENERIC
124128
pk.channel = SET_SETPOINT_CHANNEL
125-
pk.data = struct.pack('<Bffff', TYPE_VELOCITY_WORLD,
126-
vx, vy, vz, yawrate)
129+
if self._cf.platform.get_protocol_version() <= 8:
130+
warnings.warn(
131+
'Using legacy TYPE_VELOCITY_WORLD_LEGACY. Please update your crazyflie-firmware.',
132+
DeprecationWarning,
133+
)
134+
pk.data = struct.pack('<Bffff', TYPE_VELOCITY_WORLD_LEGACY,
135+
vx, vy, vz, -yawrate)
136+
else:
137+
pk.data = struct.pack('<Bffff', TYPE_VELOCITY_WORLD,
138+
vx, vy, vz, yawrate)
127139
self._cf.send_packet(pk)
128140

129141
def send_zdistance_setpoint(self, roll, pitch, yawrate, zdistance):
@@ -139,8 +151,16 @@ def send_zdistance_setpoint(self, roll, pitch, yawrate, zdistance):
139151
pk = CRTPPacket()
140152
pk.port = CRTPPort.COMMANDER_GENERIC
141153
pk.channel = SET_SETPOINT_CHANNEL
142-
pk.data = struct.pack('<Bffff', TYPE_ZDISTANCE,
143-
roll, pitch, yawrate, zdistance)
154+
if self._cf.platform.get_protocol_version() <= 8:
155+
warnings.warn(
156+
'Using legacy TYPE_ZDISTANCE_LEGACY. Please update your crazyflie-firmware.',
157+
DeprecationWarning,
158+
)
159+
pk.data = struct.pack('<Bffff', TYPE_ZDISTANCE_LEGACY,
160+
roll, pitch, -yawrate, zdistance)
161+
else:
162+
pk.data = struct.pack('<Bffff', TYPE_ZDISTANCE,
163+
roll, pitch, yawrate, zdistance)
144164
self._cf.send_packet(pk)
145165

146166
def send_hover_setpoint(self, vx, vy, yawrate, zdistance):
@@ -156,8 +176,16 @@ def send_hover_setpoint(self, vx, vy, yawrate, zdistance):
156176
pk = CRTPPacket()
157177
pk.port = CRTPPort.COMMANDER_GENERIC
158178
pk.channel = SET_SETPOINT_CHANNEL
159-
pk.data = struct.pack('<Bffff', TYPE_HOVER,
160-
vx, vy, yawrate, zdistance)
179+
if self._cf.platform.get_protocol_version() <= 8:
180+
warnings.warn(
181+
'Using legacy TYPE_HOVER_LEGACY. Please update your crazyflie-firmware.',
182+
DeprecationWarning,
183+
)
184+
pk.data = struct.pack('<Bffff', TYPE_HOVER_LEGACY,
185+
vx, vy, -yawrate, zdistance)
186+
else:
187+
pk.data = struct.pack('<Bffff', TYPE_HOVER,
188+
vx, vy, yawrate, zdistance)
161189
self._cf.send_packet(pk)
162190

163191
def send_full_state_setpoint(self, pos, vel, acc, orientation, rollrate, pitchrate, yawrate):

0 commit comments

Comments
 (0)