|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# || ____ _ __ |
| 4 | +# +------+ / __ )(_) /_______________ _____ ___ |
| 5 | +# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ |
| 6 | +# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ |
| 7 | +# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ |
| 8 | +# |
| 9 | +# Copyright (C) 2017-2018 Bitcraze AB |
| 10 | +# |
| 11 | +# Crazyflie Nano Quadcopter Client |
| 12 | +# |
| 13 | +# This program is free software; you can redistribute it and/or |
| 14 | +# modify it under the terms of the GNU General Public License |
| 15 | +# as published by the Free Software Foundation; either version 2 |
| 16 | +# of the License, or (at your option) any later version. |
| 17 | +# |
| 18 | +# This program is distributed in the hope that it will be useful, |
| 19 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +# GNU General Public License for more details. |
| 22 | +# You should have received a copy of the GNU General Public License |
| 23 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 24 | +""" |
| 25 | +Simple example of reading the state of the Crazyflie through the supervisor. |
| 26 | +
|
| 27 | +Based on its state, the Crazyflie will arm (if it can be armed), take off |
| 28 | +(if it can fly), and land (if it is flying). After each action, we call |
| 29 | +the supervisor to check if the Crazyflie is crashed, locked or tumbled. |
| 30 | +""" |
| 31 | +import time |
| 32 | + |
| 33 | +import cflib.crtp |
| 34 | +from cflib.crazyflie import Crazyflie |
| 35 | +from cflib.crazyflie.syncCrazyflie import SyncCrazyflie |
| 36 | +from cflib.utils import uri_helper |
| 37 | +from cflib.utils.reset_estimator import reset_estimator |
| 38 | +from cflib.utils.supervisor_state import SupervisorState |
| 39 | + |
| 40 | + |
| 41 | +# URI to the Crazyflie to connect to |
| 42 | +uri = uri_helper.uri_from_env(default='radio://0/80/2M/E7E7E7E7E7') |
| 43 | + |
| 44 | + |
| 45 | +def safety_check(sup: SupervisorState): |
| 46 | + if sup.is_crashed(): |
| 47 | + raise Exception('Crazyflie crashed!') |
| 48 | + if sup.is_locked(): |
| 49 | + raise Exception('Crazyflie locked!') |
| 50 | + if sup.is_tumbled(): |
| 51 | + raise Exception('Crazyflie tumbled!') |
| 52 | + |
| 53 | + |
| 54 | +def run_sequence(scf: SyncCrazyflie, sup: SupervisorState): |
| 55 | + commander = scf.cf.high_level_commander |
| 56 | + |
| 57 | + try: |
| 58 | + if sup.can_be_armed(): |
| 59 | + safety_check(sup) |
| 60 | + scf.cf.platform.send_arming_request(True) |
| 61 | + time.sleep(1) |
| 62 | + |
| 63 | + if sup.can_fly(): |
| 64 | + print('The Crazyflie can fly...taking off!') |
| 65 | + commander.takeoff(1.0, 2.0) |
| 66 | + time.sleep(3) |
| 67 | + safety_check(sup) |
| 68 | + if sup.is_flying(): |
| 69 | + print('The Crazyflie is flying...landing!') |
| 70 | + commander.land(0.0, 2.0) |
| 71 | + time.sleep(3) |
| 72 | + safety_check(sup) |
| 73 | + |
| 74 | + except Exception as e: |
| 75 | + print(e) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + cflib.crtp.init_drivers() |
| 80 | + |
| 81 | + with SyncCrazyflie(uri, cf=Crazyflie(rw_cache='./cache')) as scf: |
| 82 | + time.sleep(1) |
| 83 | + supervisor = SupervisorState(scf) |
| 84 | + reset_estimator(scf) |
| 85 | + time.sleep(1) |
| 86 | + run_sequence(scf, supervisor) |
0 commit comments