Avoiding the compound statement "with" for the crazyflie

Post here to get support
Post Reply
hagel360
Beginner
Posts: 3
Joined: Wed Apr 06, 2022 10:17 am

Avoiding the compound statement "with" for the crazyflie

Post by hagel360 »

Hi

I would like to be able to start multiple drones in a future product, which is why i want to be able to declare the drones into a variable, and boot them up. But i cannot figure out how to.

I've made two example programs to highlight my issue, the first one doesn't work, the second one does.

Code: Select all

scf = SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache'))
mr = Multiranger(scf)

while True:
    print(mr.front)
    time.sleep(0.5)
vs.

Code: Select all

with SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache')) as scf:
    with Multiranger(scf) as mr:
        while True:
            print(mr.front)
            time.sleep(0.5)
How can i get the first one to work?


Initialization (works for both programs)

Code: Select all

import time
import logging

import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.utils.multiranger import Multiranger

logging.basicConfig(level=logging.ERROR)

URI = 'radio://0/80/2M/E7E7E7E701'
cflib.crtp.init_drivers()
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: Avoiding the compound statement "with" for the crazyflie

Post by kristoffer »

hagel360
Beginner
Posts: 3
Joined: Wed Apr 06, 2022 10:17 am

Re: Avoiding the compound statement "with" for the crazyflie

Post by hagel360 »

I've read it, but I cannot figure out if it is possible to send two or more different functions to the drones.
For example imagine giving one drone a functions that makes it fly in a random pattern, and another drone following it behind.
As I understood the swarm library, was that it is for using the same function for all drones. But maybe I just need to figure out another way.
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: Avoiding the compound statement "with" for the crazyflie

Post by kristoffer »

The Swarm class is probably not the best solution for you, even if it probably is possible to do it :-)

The key is to understand what "with" is doing, it is called a "context manager" in python, see https://docs.python.org/3/library/stdty ... extmanager).

The "with" is essentially calling two methods when entering and exiting the scope; __enter__(self) and __exit__(self, exc_type, exc_val, exc_tb).
As you can see in the SyncCrazyflie class (https://github.com/bitcraze/crazyflie-l ... #L118-L131) they in turn call open_link() and close_link().
Also see the Multiranger class https://github.com/bitcraze/crazyflie-l ... #L108-L113

Instead of using "with" you can do something like:
scf = SyncCrazyflie(my_uri)
scf.open_link()
mr = Multiranger(scf)
mr.start()
# Do your thing
mr.stop()
scf.close_link()
hagel360
Beginner
Posts: 3
Joined: Wed Apr 06, 2022 10:17 am

Re: Avoiding the compound statement "with" for the crazyflie

Post by hagel360 »

Thank you so much!
This is what i needed.
Post Reply