Good day, I would like to ask how is it possible to use an ultrasonic sensor for altitude hold in a quadcopter if the sampling rate of the Ultrasonic sensor (HC-SR04) is only 20Hz before incurring any errors through polling when I had tested it. I have seen this sensor being implemented on other projects however I could not find any papers that explain the use of this sensor in better detail. I have seen possible solutions on the raspberry pi one using interrupts and the other using Linux's multithreading.

If my understanding is right, to use interrupts, I need a some sort of data ready signal from the ultrasonic sensor. However this is not available in this particular sensor. Is it possible to use the echo pin as the positive edge trigger for the interrupt service routine (read sonar/calculate distance function). But would this not introduce inconsistent loop execution times which is bad for a consistent PID loop.

Another approach is to use multithreading using the wiring-pi library which enables me to run a function, let's say a function that triggers the sonar and calculates the distance along side the pid control loop. How would this affect the PID control loop rate?

Which is the best way to implement sonar sensor based altitude hold?

  • $\begingroup$ More information about the specific sensor would be helpful; make and model number. Does it have a built in ADC or do you need to design one? $\endgroup$ – MechanicalMan Mar 2 '16 at 14:49

The best solution is to use interrupts triggered by echo pins change of state. joan's pigpio example worked beautifully. Using multithreading did not give satisfactory results as there is still problem with the timing since multithreading on the raspberry pi operates via pipelining.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.