Keeping constant altitude with a drone is not a trivial task. Specially, if drone is supposed to keep give altitude very precisely few meters above the ground. One of the reasons for it is difficulty of reading precise altitude. Barometer can drift when atmospheric pressure changes and can produce a lot of noise. GPS is very inaccurate when dealing with altitude. One of the options is to use some kind of rangefinder. Ultrasonic for example. Cleanflight and its derivatives supports exactly one kind of those: cheap HC-SR04 sonar. There are plans to integrate different sonars, but none of official builds supports it yet.
HC-SR04 has 4 pins:
- Vcc – 5v power supply
- Trig – input signal in this pin sends ultrasonic wave
- Echo – when ultrasonic wave bounces from solid surface and returns to sonar, logical level HIGH is sent on this pin. So, to measure distance, one must measure time between Trig and Echo
- Gnd – this one is self describing
"Problem" with HC-SR04 and modern flight controllers based on STM32 is that while this sonar uses 5V logic, usually FC's inputs are not 5V tolerant. They are 3.3V. Applying 5V to them might burn flight controller. This is why inline 1kOhm resistors have to be used on Trig and Echo lines. Like this:
Depending on flight controller hardware, HC-SR04s Trig and Echo has to be connected to different pins:
- Naze32 and Flip32 when parallel PWM radio is connected (this is the only case when resistors are not need. Those pins are 5V tolerant):
- Trig: Motor 5
- Echo: Motor 6
- Naze32 and Flip32 when PPM or Serial RX is connected
- Trig: RC7
- Echo: RC8
- Trig: RC4
- Echo: RC5
- Trig: RC4
- Echo: RC5
Since the same pins can be used by other features like SoftSerial or Parallel PWM RC, depending on hardware, some of those function can not be used with Sonar.
When hardware setup is done, enabling sonar in Cleanflight Configurator is pretty simple:
- Open Configuation page and enable SONAR
- After saving, sonar icon in top bar should go green
- Sonar readouts will be visible in Sensors tab
OK, I have sonar, what now?
Well, this is very good question. Since FC now has a way to measure its altitude above ground, altitude hold Sonar mode will be available. In theory, it should work very good and allow altitude stabilization with centimeter accuracy. Unfortunately, this is not that simple, and there are a few reasons for that:
- In perfect conditions, HC-SR04 can measure distance up to 4 meters. Conditions onboard drone are not perfect. Disturbed air and noise can lower this range to less than 1 meter
- Measurement cone is only about 45 degrees. That means, if surface (ground) is tilted above 22.5 degrees, measurement will probably fail
- Accuracy and range above grass or any not very solid surface (like concrete or asphalt) will be greatly reduced
Bottom line is: HC-SR04 onboard UAV is more less useless. The only real life usage is assistance in last phase of automated landing. Since Cleanflight does not supports automated landing, sonar is more less only a gadget.
BTW, do you want to know what flight controller software supports automated landing? iNav.