|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# || ____ _ __ |
| 4 | +# +------+ / __ )(_) /_______________ _____ ___ |
| 5 | +# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ |
| 6 | +# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ |
| 7 | +# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ |
| 8 | +# |
| 9 | +# Copyright (C) 2025 Bitcraze AB |
| 10 | +# |
| 11 | +# This program is free software; you can redistribute it and/or |
| 12 | +# modify it under the terms of the GNU General Public License |
| 13 | +# as published by the Free Software Foundation; either version 2 |
| 14 | +# of the License, or (at your option) any later version. |
| 15 | +# |
| 16 | +# This program is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | +""" |
| 23 | +Simple script that connects to multiple Crazyflies in sequence and uploads |
| 24 | +the lighthouse configuration file. The Crazyflies that successfully received |
| 25 | +the file are powered off, while the ones that didn't get it remain powered on. |
| 26 | +This could be really helpful if you're dealing with a big swarm of Crazyflies. |
| 27 | +
|
| 28 | +Make sure that each Crazyflie has a lighthouse deck attached. |
| 29 | +""" |
| 30 | +import os |
| 31 | +import sys |
| 32 | +import time |
| 33 | + |
| 34 | +import cflib.crtp |
| 35 | +from cflib.crazyflie import Crazyflie |
| 36 | +from cflib.crazyflie.syncCrazyflie import SyncCrazyflie |
| 37 | +from cflib.localization import LighthouseConfigWriter |
| 38 | +from cflib.utils import uri_helper |
| 39 | +from cflib.utils.power_switch import PowerSwitch |
| 40 | + |
| 41 | +file_path = 'lighthouse.yaml' # Add the path to your .yaml file |
| 42 | + |
| 43 | +# Modify the list of Crazyflies according to your setup |
| 44 | +uris = [ |
| 45 | + 'radio://0/80/2M/E7E7E7E7E7', |
| 46 | + 'radio://0/80/2M/E7E7E7E7E8', |
| 47 | + 'radio://0/80/2M/E7E7E7E7E9', |
| 48 | + 'radio://0/80/2M/E7E7E7E7EA', |
| 49 | + 'radio://0/80/2M/E7E7E7E7EB', |
| 50 | + 'radio://0/80/2M/E7E7E7E7EC', |
| 51 | +] |
| 52 | + |
| 53 | + |
| 54 | +def write_one(file_name, scf: SyncCrazyflie): |
| 55 | + print(f'Writing to \033[92m{uri}\033[97m...', end='', flush=True) |
| 56 | + writer = LighthouseConfigWriter(scf.cf) |
| 57 | + writer.write_and_store_config_from_file(None, file_name) |
| 58 | + print('Success!') |
| 59 | + time.sleep(1) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + if not os.path.exists(file_path): |
| 64 | + print('Error: file not found!') |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + print(f'Using file {file_path}') |
| 68 | + |
| 69 | + cflib.crtp.init_drivers() |
| 70 | + |
| 71 | + for uri in uris: |
| 72 | + try: |
| 73 | + Drone = uri_helper.uri_from_env(default=uri) |
| 74 | + with SyncCrazyflie(Drone, cf=Crazyflie(rw_cache='./cache')) as scf: |
| 75 | + print(f'\033[92m{uri} \033[97mFully connected ', end='', flush=True) |
| 76 | + while scf.is_params_updated() is False: |
| 77 | + print('.', end='', flush=True) |
| 78 | + time.sleep(0.1) |
| 79 | + print(f'{scf.is_params_updated()}') |
| 80 | + time.sleep(0.5) |
| 81 | + write_one(file_path, scf) |
| 82 | + ps = PowerSwitch(Drone) |
| 83 | + ps.platform_power_down() |
| 84 | + time.sleep(2) |
| 85 | + |
| 86 | + except (Exception): |
| 87 | + print(f'Couldnt connect to \033[91m{uri}\033[97m') |
| 88 | + continue |
0 commit comments