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.
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.
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.