Skip to content

Commit

Permalink
Add synchronicity to writing persistant params.
Browse files Browse the repository at this point in the history
After each param, write it persistent and wait for the result
  • Loading branch information
ToveRumar committed Jun 5, 2024
1 parent d970c83 commit 7af6558
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 9 deletions.
23 changes: 20 additions & 3 deletions cflib/utils/param_file_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,35 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from cflib.crazyflie import Crazyflie
from cflib.localization.param_io import ParamFileManager
from threading import Event


class ParamFileHelper:
def __init__(self, crazyflie):
if isinstance(crazyflie, Crazyflie):
self._cf = crazyflie
self.persistent_sema = None
self.success = False
else:
raise TypeError("ParamFileHelper only takes a Crazyflie Object")



def _persistent_stored_callback(self, complete_name, success):
self.success = success
if not success:
print(f'Persistent params: failed to store {complete_name}!')
else:
print(f'Persistent params: stored {complete_name}!')
self.persistent_sema.set()


def store_params_from_file(self, filename):
print(self._cf.param)
params = ParamFileManager().read(filename)
for param, state in params.items():
self.persistent_sema = Event()
self._cf.param.set_value(param, state.stored_value)
self._cf.param.persistent_store(param)
self._cf.param.persistent_store(param, self._persistent_stored_callback)
self.persistent_sema.wait()
if not self.success:
break
return self.success
23 changes: 23 additions & 0 deletions test/utils/fixtures/five_params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
params:
activeMarker.back:
default_value: 3
is_stored: true
stored_value: 10
activeMarker.front:
default_value: 3
is_stored: true
stored_value: 10
activeMarker.left:
default_value: 3
is_stored: true
stored_value: 10
cppm.angPitch:
default_value: 50.0
is_stored: true
stored_value: 55.0
ctrlMel.i_range_z:
default_value: 0.4000000059604645
is_stored: true
stored_value: 0.44999998807907104
type: persistent_param_state
version: '1'
60 changes: 54 additions & 6 deletions test/utils/test_param_file_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from cflib.utils.param_file_helper import ParamFileHelper
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from threading import Event

class ParamFileHelperTests(unittest.TestCase):

Expand All @@ -49,22 +50,69 @@ def test_ParamFileHelper_Crazyflie_Object(self):

@patch('cflib.crazyflie.Param')
def test_ParamFileHelper_writesAndStoresParamFromFileToCrazyflie(self, mock_Param):
#Setup
cf_mock = MagicMock(spec=Crazyflie)
cf_mock.param = mock_Param
helper = ParamFileHelper(cf_mock)
helper.store_params_from_file('test/utils/fixtures/single_param.yaml')
mock_Param.set_value.assert_called_once_with('activeMarker.back',10)
mock_Param.persistent_store.assert_called_once_with('activeMarker.back')
#Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world
def mock_wait(self,timeout=None):
helper._persistent_stored_callback('activeMarker.back', True)
return

with patch.object(Event, 'wait', new=mock_wait):
self.assertTrue(helper.store_params_from_file('test/utils/fixtures/single_param.yaml'))
mock_Param.set_value.assert_called_once_with('activeMarker.back',10)
mock_Param.persistent_store.assert_called_once_with('activeMarker.back', helper._persistent_stored_callback)

@patch('cflib.crazyflie.Param')
def test_ParamFileHelper_writesParamAndFailsToSetPersistantShouldReturnFalse(self, mock_Param):
#Setup
cf_mock = MagicMock(spec=Crazyflie)
cf_mock.param = mock_Param
helper = ParamFileHelper(cf_mock)
#Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world
def mock_wait(self,timeout=None):
helper._persistent_stored_callback('activeMarker.back', False)
return

with patch.object(Event, 'wait', new=mock_wait):
self.assertFalse(helper.store_params_from_file('test/utils/fixtures/single_param.yaml'))
mock_Param.set_value.assert_called_once_with('activeMarker.back',10)
mock_Param.persistent_store.assert_called_once_with('activeMarker.back', helper._persistent_stored_callback)

@patch('cflib.crazyflie.Param')
def test_ParamFileHelper_TryWriteSeveralParamsPersistantShouldBreakAndReturnFalse(self, mock_Param):
#Setup
cf_mock = MagicMock(spec=Crazyflie)
cf_mock.param = mock_Param
helper = ParamFileHelper(cf_mock)
#Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world
def mock_wait(self,timeout=None):
helper._persistent_stored_callback('activeMarker.back', False)
return

with patch.object(Event, 'wait', new=mock_wait):
#Test and assert
self.assertFalse(helper.store_params_from_file('test/utils/fixtures/five_params.yaml'))
#Assert it breaks directly by checking number of calls
mock_Param.set_value.assert_called_once_with('activeMarker.back',10)
mock_Param.persistent_store.assert_called_once_with('activeMarker.back', helper._persistent_stored_callback)

@patch('cflib.crazyflie.Param')
def test_ParamFileHelper_writesAndStoresAllParamsFromFileToCrazyflie(self, mock_Param):
#Setup
cf_mock = MagicMock(spec=Crazyflie)
cf_mock.param = mock_Param
helper = ParamFileHelper(cf_mock)
helper.store_params_from_file('test/utils/fixtures/five_params.yaml')
self.assertEquals(5,len(mock_Param.set_value.mock_calls))
self.assertEquals(5,len(mock_Param.persistent_store.mock_calls))
#Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world
def mock_wait(self,timeout=None):
helper._persistent_stored_callback('something', True)
return
with patch.object(Event, 'wait', new=mock_wait):
#Test and Assert
self.assertTrue(helper.store_params_from_file('test/utils/fixtures/five_params.yaml'))
self.assertEquals(5,len(mock_Param.set_value.mock_calls))
self.assertEquals(5,len(mock_Param.persistent_store.mock_calls))



Expand Down

0 comments on commit 7af6558

Please sign in to comment.