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.