Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bitcraze/crazyflie-lib-python
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Dec 17, 2017
2 parents dcb65a6 + 5f6e556 commit 88d9130
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
18 changes: 12 additions & 6 deletions cflib/positioning/motion_commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,36 @@

class MotionCommander:
"""The motion commander"""
HEIGHT = 0.3
VELOCITY = 0.2
RATE = 360.0 / 5

def __init__(self, crazyflie):
def __init__(self, crazyflie, default_height=0.3):
"""
Construct an instance of a MotionCommander
:param crazyflie: a Crazyflie or SyncCrazyflie instance
:param default_height: the default height to fly at
"""
if isinstance(crazyflie, SyncCrazyflie):
self._cf = crazyflie.cf
else:
self._cf = crazyflie

self.default_height = default_height

self._is_flying = False
self._thread = None

# Distance based primitives

def take_off(self, height=HEIGHT, velocity=VELOCITY):
def take_off(self, height=None, velocity=VELOCITY):
"""
Takes off, that is starts the motors, goes straigt up and hovers.
Do not call this function if you use the with keyword. Take off is
done automatically when the context is created.
:param height: the height (meters) to hover
:param height: the height (meters) to hover at. None uses the default
height set when constructed.
:param velocity: the velocity (meters/second) when taking off
:return:
"""
Expand All @@ -95,6 +98,9 @@ def take_off(self, height=HEIGHT, velocity=VELOCITY):
self._thread = _SetPointThread(self._cf)
self._thread.start()

if height is None:
height = self.default_height

self.up(height, velocity)

def land(self, velocity=VELOCITY):
Expand All @@ -116,8 +122,8 @@ def land(self, velocity=VELOCITY):
self._cf.commander.send_stop_setpoint()
self._is_flying = False

def __enter__(self, height=HEIGHT, velocity=VELOCITY):
self.take_off(height, velocity)
def __enter__(self):
self.take_off()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
Expand Down
39 changes: 38 additions & 1 deletion test/positioning/test_motion_commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ def test_that_it_goes_up_on_take_off(
])
sleep_mock.assert_called_with(0.4 / 0.5)

def test_that_it_goes_up_to_default_height(
self, _SetPointThread_mock, sleep_mock):
# Fixture
thread_mock = _SetPointThread_mock()
sut = MotionCommander(self.cf_mock, default_height=0.4)

# Test
sut.take_off(velocity=0.6)

# Assert
thread_mock.set_vel_setpoint.assert_has_calls([
call(0.0, 0.0, 0.6, 0.0),
call(0.0, 0.0, 0.0, 0.0)
])
sleep_mock.assert_called_with(0.4 / 0.6)

def test_that_the_thread_is_started_on_takeoff(
self, _SetPointThread_mock, sleep_mock):
# Fixture
Expand All @@ -117,7 +133,6 @@ def test_that_it_goes_down_on_landing(
thread_mock.get_height.return_value = 0.4

self.sut.take_off()
thread_mock = _SetPointThread_mock()
thread_mock.reset_mock()

# Test
Expand All @@ -130,6 +145,28 @@ def test_that_it_goes_down_on_landing(
])
sleep_mock.assert_called_with(0.4 / 0.5)

def test_that_it_takes_off_and_lands_as_context_manager(
self, _SetPointThread_mock, sleep_mock):
# Fixture
thread_mock = _SetPointThread_mock()
thread_mock.reset_mock()

thread_mock.get_height.return_value = 0.3

# Test
with self.sut:
pass

# Assert
thread_mock.set_vel_setpoint.assert_has_calls([
call(0.0, 0.0, 0.2, 0.0),
call(0.0, 0.0, 0.0, 0.0),
call(0.0, 0.0, -0.2, 0.0),
call(0.0, 0.0, 0.0, 0.0)
])
sleep_mock.assert_called_with(0.3 / 0.2)
sleep_mock.assert_called_with(0.3 / 0.2)

def test_that_it_starts_moving_multi_dimensional(
self, _SetPointThread_mock, sleep_mock):
# Fixture
Expand Down

0 comments on commit 88d9130

Please sign in to comment.