Skip to content

Commit 5b49d98

Browse files
committed
cleanup vehicle mode setting api
1 parent 29d81b0 commit 5b49d98

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

dronekit/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,15 +1163,14 @@ def listener(self, name, m):
11631163
def listener(self, name, m):
11641164
self._armed = (m.base_mode & mavutil.mavlink.MAV_MODE_FLAG_SAFETY_ARMED) != 0
11651165
self.notify_attribute_listeners('armed', self.armed, cache=True)
1166-
if self._master.mode_mapping() != None:
1167-
flightmodesById = {v: k for k, v in self._master.mode_mapping().items()}
1168-
if m.custom_mode in flightmodesById:
1169-
self._flightmode = flightmodesById[m.custom_mode]
1166+
self._autopilot_type = m.autopilot
1167+
self._vehicle_type = m.type
1168+
if self._is_mode_available(m.custom_mode) == False:
1169+
raise APIException("mode %s not available on mavlink definition" % m.custom_mode)
1170+
self._flightmode = self._mode_mapping_bynumber[m.custom_mode]
11701171
self.notify_attribute_listeners('mode', self.mode, cache=True)
11711172
self._system_status = m.system_status
11721173
self.notify_attribute_listeners('system_status', self.system_status, cache=True)
1173-
self._autopilot_type = m.autopilot
1174-
self._vehicle_type = m.type
11751174

11761175
# Waypoints.
11771176

@@ -1493,6 +1492,13 @@ def flush(self):
14931492
def _mode_mapping(self):
14941493
return self._master.mode_mapping()
14951494

1495+
@property
1496+
def _mode_mapping_bynumber(self):
1497+
return mavutil.mode_mapping_bynumber(self._vehicle_type)
1498+
1499+
def _is_mode_available(self, mode_code):
1500+
return mode_code in self._mode_mapping_bynumber
1501+
14961502
#
14971503
# Operations to support the standard API.
14981504
#
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import time
2+
import math
3+
from dronekit import connect
4+
from dronekit.test import with_sitl
5+
from nose.tools import assert_equals
6+
7+
8+
@with_sitl
9+
def test_modes_set(connpath):
10+
vehicle = connect(connpath)
11+
12+
def listener(self, name, m):
13+
assert_equals('STABILIZE', self._flightmode)
14+
15+
vehicle.add_message_listener('HEARTBEAT', listener)

0 commit comments

Comments
 (0)