From c7be40d1f20a146271efc5b457a5e4627eb02c8b Mon Sep 17 00:00:00 2001 From: Tove Rumar Date: Wed, 5 Jun 2024 11:10:14 +0200 Subject: [PATCH] Add example script that uses the ParamFileHelper. The script will write all supplied parameters to the crazyflie and store them --- cflib/utils/param_file_helper.py | 1 + .../parameters/persistent_params_from_file.py | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 examples/parameters/persistent_params_from_file.py diff --git a/cflib/utils/param_file_helper.py b/cflib/utils/param_file_helper.py index eccc60b8d..f53fa7de2 100644 --- a/cflib/utils/param_file_helper.py +++ b/cflib/utils/param_file_helper.py @@ -25,6 +25,7 @@ class ParamFileHelper: + '''ParamFileHelper is a helper to synchonously write multiple paramteters from a file and store them in persistent memory''' def __init__(self, crazyflie): if isinstance(crazyflie, Crazyflie): self._cf = crazyflie diff --git a/examples/parameters/persistent_params_from_file.py b/examples/parameters/persistent_params_from_file.py new file mode 100644 index 000000000..223e6fb43 --- /dev/null +++ b/examples/parameters/persistent_params_from_file.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2021 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, in version 3. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +Example to show how to write several persistent parameters from a yaml file. +The params in the file should be formatted like this; + +params: + activeMarker.back: + default_value: 3 + is_stored: true + stored_value: 30 +type: persistent_param_state +version: '1' +""" +import argparse +import logging +import cflib.crtp +from cflib.crazyflie import Crazyflie +from cflib.crazyflie.syncCrazyflie import SyncCrazyflie +from cflib.utils import uri_helper +from cflib.utils.param_file_helper import ParamFileHelper + + +# uri = uri_helper.uri_from_env(default='usb://0') +uri = uri_helper.uri_from_env(default='radio://0/37/2M/E7E7E7E7E7') + +# Only output errors from the logging framework +logging.basicConfig(level=logging.ERROR) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('-f', '--file', type=str, help='The yaml file containing the arguments. ') + args = parser.parse_args() + + cflib.crtp.init_drivers() + + cf = Crazyflie(rw_cache='./cache') + with SyncCrazyflie(uri, cf=cf) as scf: + writer = ParamFileHelper(scf.cf) + writer.store_params_from_file(args.file) + +