Best practices for sensor data acquisition
I'm looking to implement my own version of an altitude controller on a CoDrone Lite using Python. The examples I've seen so far use a loop of the form:
for i in range(100): h = drone.get_height() # if h is too large, make the drone go DOWN # if h is too small, make the drone go UP time.sleep(0.1)
First question: how small can I make the sleep delay? Of course, you'd want to make this as small as possible to have a more responsive controller, but at the same time, I'm not sure what happens if I try to acquire sensor data too frequently... (will it return inaccurate data? will it simply wait until it's ready to give me more data?) Likewise, what happens if I try to send motor commands to the drone too frequently?
Second question: I'd like to implement a more sophisticated controller that uses many sources of sensor data. For example, I want to use vertical speed. I can do this two ways; by differentiating height (from the IR sensor), or by integrating acceleration (from the accelerometer). What are the best practices for implementing something like this? Do you still recommend the for-loop construction above for implementing the controller, or is there a better way? I’m curious because I saw that the
draw_plot_sensor()use some sort of continuous data acquisition that runs in the background, and this seems quite elegant and probably easier to debug. I looked into the code for these functions but unfortunately, they call some low-level functions for which I can't find any published documentation. What I would love is a way to access the raw data stream that is being collected when I run
Third question: Related to the previous one --- how can I implement an event-triggered “kill switch”, such as: "if at any time the drone gets to 1500mm altitude, stop everything and land safely". The problem is that if I code instructions sequentially in the for-loop format above, the best I can do is check the altitude at every iteration of the loop. Unfortunately, when things go wrong, they happen in between altitude checks. Is there some way I can do continuous data acquisition and then implement some sort of event-triggered code? I guess what I’m looking for is a way to run code asynchronously.
robolink_leila last edited by
Thank you for your patience as we are experiencing delays because of our account validation issue.
In general, all of our functions have built-in delays to prevent skipped or dropped commands and messages, but this will vary on the sensor that you are trying to request. The smallest interval we may suggest is usually 50ms for any sensor readings, but I would increase it to prevent package drops because of Bluetooth. The best way to know is by testing. This is the same for any command.
Beyond what is available in our functions guide, we don't give any more advanced tutorials for the kind of project you are thinking of. CoDrone is an educational drone meant to teach introductory programming and may not have the sensor update rate that you are looking for.
You can look into the library when you install the package and implement this yourself if you would like! However, we do not offer any support for these kinds of advanced topics. Maybe someone else on the forum can help you out.