Logging Sensor Data with Python IDLE

Post here to get support
Post Reply
VeryConfused
Beginner
Posts: 9
Joined: Mon Mar 05, 2018 5:43 am

Logging Sensor Data with Python IDLE

Post by VeryConfused »

Hello,

I am currently trying to create a simple Python code which will instruct the Crazyflie to perform some basic flight maneuvers and store some sensor data from the flight in a separate file. I have already been able to program the flight maneuvers and am now attempting to add the data storage capability to my code. To learn to do this, I have been looking at the basiclog.py and basiclogSync.py examples, but I have been unable to get them to work and I have a few questions about them.

First of all, why do both the basiclog.py and basiclogSync.py examples exist? They both seem to serve the exact same purpose. Obviously basiclogSync.py utilizes the SyncCrazyflie and SyncLogger classes while basiclog.py does not, which leads to drastic changes in the code, but the overall function of the code seems the same nonetheless. Is there a specific reason for the existence of both of these codes, rather than just one general purpose logging function?

Secondly, when connecting to the Crazyflie at the beginning of the code, why don't the logging examples use the URI? The flight examples I have been using (flowsequenceSync.py and the example given on the "Getting Started with STEM Bundle" tutorial) use the URI to connect, but the logging examples seem to scan any nearby Crazyflies, store them in a struct, and then access sensor data for the first one scanned. Is there a reason for this?

Finally, and most importantly, when I attempt to run either example (basiclog.py or basiclogSync.py), the process of attempting to connect to the Crazyflie outputs an error saying "Cannot find a Crazyradio Dongle." This confuses me because the Crazyradio works perfectly fine when executing the flight examples, but evidently not while executing the logging examples. This is why I asked about the difference between using the specific URI to connect to the Crazyflie in comparison to using "cflib.crtp.scan_interfaces()" to scan nearby drones. There must be some difference between the flight examples and logging examples which causes the radio to properly function on the former but not on the latter. Any idea what this could be?

Thanks so much for your time!

EDIT:
Managed to get the sensor data printed by replacing all instances of 'available[0][0]' with 'URI'. Am now working on writing it to a file.

If the data from the log_entry is output as a string, how do you write it to a spreadsheet?
Attachments
Output from basiclogSync.py
Output from basiclogSync.py
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Logging Sensor Data with Python IDLE

Post by arnaud »

Hi,

basiclog.py is using the generic general purpose API, is is an asynchronous API using callback which is powerful but can be tedious to use, mostly for simple case. basiclogSync.py uses the sync API which makes the most simple case, controlling one Crazyflie getting data from one log block, much easier by hiding the callbacks behind a synchronous API.

The logging example where written that way to make them work out of the box when you have a single Crazyflie: the user can run them unmodified without having to change the URI to match their Crazyflie. It also make the example slightly more complex but documents the scann API. There was some discussion about what to put in the example when we wrote the first ones and being able to use the example unmodified was the main reason for writing them like that if I remember well. It is a living project so we can change the examples to make them better, so if you have any feedback the best might be to create a github ticket so we can discuss how to make them better.

The cannot find crazyradio dongle is indeed surprising, was it from unmodified example? If so, this is a bug.

To open the data in a spreadsheet the easiest is to make a CSV file. There is nothing in the Crazyflie lib to help you with that, but it is quite easy code to write in python. You can either use the CSV python module or open the file and write the csv row by yourself.
VeryConfused
Beginner
Posts: 9
Joined: Mon Mar 05, 2018 5:43 am

Re: Logging Sensor Data with Python IDLE

Post by VeryConfused »

Thanks Arnaud,

The "cannot find a Crazyradio dongle" was the output from both the basiclog.py and basiclogSync.py. I managed to make it work by connecting using the URI, but yes it was strange.

Something that I am not understanding is how to get sensor data during the actual flight of the drone. I am aware that computers only want to perform one task at a time, so getting it to do both at the same time seems difficult. Resources I've found online mention things like callback functions and synchronous and asynchronous libraries, but I don't really understand what that means. Could you point in the right direction?
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Logging Sensor Data with Python IDLE

Post by arnaud »

The asynchronous API allows you to do any number of things at the same time, the synchronous API will basically allow you to fly the Crazyflie while logging 1 log block.

If you want to fly while using the synchronous API you can send the commander setpoint at this point in the code: https://github.com/bitcraze/crazyflie-l ... py#L69-L74. This loop runs when a log block is received and so in this example it is going to loop at 100Hz.

If you use the async API, everything runs in the background and calls callback functions when data are available, in that case you can log as many variables as you want and you can run your code either from the main program, for example this loop is running all along: https://github.com/bitcraze/crazyflie-l ... #L139-L143, or you can setup a repetitive timer to run your fly routine at regular intervarl (there is at least a timer setup in the example) or you could send the setpoint in one of the log callback https://github.com/bitcraze/crazyflie-l ... og.py#L105.
Post Reply