Using HC-SR04 sonar for altitude hold

Hi, I am new to programming in arudino and I was given a project which I need to implement an altitude hold function using HC-SR04 sonar on my quad copter. This sensor have to receive signal via the receiver to the arduino uno board which will send the signal to the naza m lite flight controller in order to control the motor on the quad copter. I have some difficulty in writing the code to the arudino uno board to enable altitude hold function. The receiver that I am using is futaba R2106GF and the transmitter is futaba T6J.

Receiver_control_esc.ino (1.49 KB)

Receiver_control_esc.ino (1.49 KB)

I have some difficulty in writing the code to the arudino uno board to enable altitude hold function.

If you can't explain what difficulty (or difficulties) you are having, you can't reasonably expect us to help you.

The difficulty that I have is in writing a code to allow the arudino uno board to store the value of the specific altitude and to not allow the quad copter to increase or decrease it altitude when it is in altitude hold mode

The difficulty that I have is in writing a code to allow the arudino uno board to store the value of the specific altitude

Seriously?

   int altitudeToHold = getCurrentAltitude();

Now, all you have to do is write the getCurrentAltitude() function to get the current altitude, by whatever means you have to determine that.

Which ultrasonic sensor do you have? Typically, ultrasonic sensors have very short ranges that they work in. Unless you plan to fly level 10 cm off the ground, an ultrasonic sensor is probably not the correct device to use.

and to not allow the quad copter to increase or decrease it altitude when it is in altitude hold mode

How will it know that it is "in altitude hold mode"? You can use the same getCurrentAltitude() function to get the current altitude, and compare that to the hold altitude. Adjust the rotor speed(s) as required, if you are too high or too low. How often you sample the altitude, and how much you adjust the rotor speed(s), are things that PID is often used to manage.

The sonar that I am using is HC-SR04

As pointed out by PaulS ,surely the basics are obvious…

Store the wanted height in feet/meters etc in a variable - say targetHeight.

Periodically measure the actual height with the SR04 and compare with targetHeight

If less, go higher and vice versa.

The dynamics may be tricky due to momentum of the 'copter - think of a PID type loop to stabilise this.

Allan

GideonHeng:
The sonar that I am using is HC-SR04

This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm.

That's from Sparkfun's site. Now, the latter part of that statement is rather stupid. Up to 3mm accuracy? WTF? That implies that. without care, you'll get less accuracy, like +/- 3 nanometers.

Still, that sensor might be good for up to 15 feet. Is that how high you plan to fly? Anything higher than that, and the sensor will be useless.

Thanks for the advice and help. I must apologise for not giving detailed explanation of the difficulties

I am planning to fly ard 50cm while altitude mode will be at 20cm

20cm? - about 8 inches.....

Your system will have to respond pretty fast if flying over anything bumpy........ like my lawn!

Allan

Okay

Yup it is 20 cm which is 8 inches

You might want to see this thread started by Panda01 who has the exact same requirement: Hover at 20cm.

https://forum.arduino.cc/index.php?topic=500188.0

...and their IP addresses are identical.
What are the chances of that?

#schoolsback