SyncCrazyflie object has no attribute scf

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
youngbin1130
Member
Posts: 46
Joined: Thu Dec 12, 2019 4:28 am

SyncCrazyflie object has no attribute scf

Post by youngbin1130 »

Hi. I am working on a research project which deals with moving Crazyflie using an OpenAI model in a swarm scenario.

Basically in the code which we've working on, the OpenAI simulation should went the next location to each of the drones and once it reaches there, it will go to the next location and so on.

While we managed to get one drone working, we keep encountering the error "scf is not defined". The idea is to have a function inside the simulation model which sends the next point. Our closest attempt opened up four instances of the simulation (because there were four drones connected) and they would all receive slightly different actions.

Do let me know if you require additional information regarding the situation
Last edited by youngbin1130 on Mon Mar 22, 2021 11:56 am, edited 1 time in total.
whoenig
Expert
Posts: 395
Joined: Mon Oct 27, 2014 2:55 am

Re: 'scf' is not defined

Post by whoenig »

Error: Could you share the full error message that you are getting? Also, I assume you are using crazyflie-lib-python?

Multiple drones: You can ran multiple scripts separately, as long as you use one radio per Crazyflie. If you share a radio, you will need to find another way, as only one script can access a Crazyradio over USB. One option would be to communicate with your simulator, for example using ZeroMQ.
youngbin1130
Member
Posts: 46
Joined: Thu Dec 12, 2019 4:28 am

Re: 'scf' is not defined

Post by youngbin1130 »

Hi whoenig,

Thank you for your reply. Yes, I am using the crazyflie-lib-python.

I think I had mistaken the error. I am usually getting the error below.

Code: Select all

AtrributeError: 'SyncCrazyflie' object has no attribute 'scf'
I believe its a Python problem and since I'm new to python, I'm not sure how I would go about solving it.
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: SyncCrazyflie object has no attribute scf

Post by kristoffer »

We usually call the variable that holds a SyncCrazyflie instance "scf" (but it is just a variable and could be called anything)

The typical use case is something like this:

Code: Select all

with SyncCrazyflie() as scf:
    # the scf variable now contains a connected SyncCrazyflie instance that can be used to communicate with the Crazyflie
    # The scf object has a member called "cf" that is a Crazyflie instance
    my_cf = scf.cf
    my_cf.commander.send_position_setpoint(0, 0, 0, 0)
The "with SyncCrazyflie() as scf" construct (https://docs.python.org/3/reference/com ... -statement) is a way to create a SyncCrazyflie instance, open the connection automatically and assign it to the "scf" variable. When you leave the scope (the last line that is indented) the scf will automatically close the connection. It is a safe way to handle objects that has en open/close behaviour.
youngbin1130
Member
Posts: 46
Joined: Thu Dec 12, 2019 4:28 am

Re: SyncCrazyflie object has no attribute scf

Post by youngbin1130 »

Thanks for the explanation. I think I do have a clearer idea now.

From my experiments, it seems like the line:

Code: Select all

with Swarm(uris, factory=factory) as swarm:
establishes a connection to all of the existing drones. Would there be a way to leave this connection open until an action (location) is given? Or any way for me to access the variable outside of the scope. The idea is for all the drones to take off first and every time a simulation generates an action (a for-loop inside a function without a scf/swarm reference), the drones will execute it.

I could send you a message for the code is that is necessary.
whoenig
Expert
Posts: 395
Joined: Mon Oct 27, 2014 2:55 am

Re: SyncCrazyflie object has no attribute scf

Post by whoenig »

You are not forced to use the "with" statement. It essentially provides some more safety to you, as it will automatically clean-up if you leave the scope for any reason (e.g., an exception was thrown). You can, however, always create a Swarm object manually and replicate the with behavior (https://github.com/bitcraze/crazyflie-l ... #L103-L108). Specifically:

Code: Select all

swarm = Swarm(uris, factory=factory)
swarm.open_links()
// Do whatever you want

swarm.close_links()
For your purpose, however you can also rely on multiprocessing or multithreading and exchange information with a queue. In your main loop you first start a process or thread in order to execute your simulation. Then, you create a swarm object using the "with" statement. Inside the scope, you wait to receive data from the queue and, once received, send it to the copters. In your simulation thread, you can compute the next step ans push new actions to the queue. You can find examples at https://docs.python.org/3/library/queue.html.
Post Reply