3D Robotics


GPS altitude hold is good for +-5m, but what if you want more precision than that, closer to the ground. ArduCopter team member Randy Mackay. He explains what it takes (this is also in the manual):


    To give it a go do this:
        1. get yourself one of these Sonars and attach to the bottom of your quad
            http://www.sparkfun.com/products/8504  <-- the cheaper LV ($28)
            http://www.sparkfun.com/products/9495  <-- the slightly more XL ($50)
        2. attach it up to AN1 (the Analog2Digital port on the very back
right of the oilpan near the CLI switch.  AN5 is the outmost, so count
in from there to AN1).
        3. comment out this line in ArduCopterNG.pde:
                //#define UseBMP
        4. make sure this isn't commented out just below in ArduCopterNG.pde
                #define isRANGEFINDER
        5. if you're using the LV sonar you will need to change this line in ArduCopterNG.pde:
                 AP_RangeFinder_MaxsonarXL  AP_RangeFinder_down;
            to be like this:
                 AP_RangeFinder_MaxsonarLV  AP_RangeFinder_down;
        6. you'll need to reset your EEPROM values or go into the CLI menu and select 'o' and set your Sonar PIDs to 0.8;0.3;0.7
     I had to make one change though that affects the existing functionality
... if you're UseBMP (i.e. using baro for altitude hold) it's now
possible to do altitude hold even though you're not doing GPS hold (I.e.
if you don't have a GPS lock).  I think this is ok.
     So the Barometer altitude control and sonar altitude control continue
to be quite separate functionality that can't be used at the same time. 
Clearly we need a nice transition from one to the other when you go out
of sonar range.  anyway still, thinking about how to do that.

We may be moving the sonar input port to the Pitot port on the IMU board. Note that the ArduPirates  are now handling all public releases of the ArduCopter NG code, they'll be implementing this in their own way. There's already a version of that in their code here.
E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones


  • @Randy. Hey I just tried it and those PID values work perfectly. My arducopter holds altitude with an accuracy of about 1 inch :). Pretty awesome I'd say lol. What value do you recommend for the safety zone in centimeters?
  • Developer


         great! my bad in the first place for not having the wiki + code consistent.  Thanks and good luck!



  • @Randy. Lol I just checked the system settings in my code and its still set on AN1. No wonder my sonar wasnt working. My arducopter wasn't even using it hehe. My bad. I'll change it and try another flight.
  • Developer
    hmmm...weird.  let me give the latest code a test today and make sure it's ok still.
  • I have the full arducopter kit. But with the PID values you suggested, the copter ascended pretty rapidly.
  • Developer



         Be careful about your throttle position at the moment you turn on the altitude hold.  The code uses this value as a base value so if you have it too low or two high, no matter how hard the code works, it won't be able to keep the copter from climbing or descending (the code will not modify your throttle more than about 10% above or below this initial value).


         I guess you're not using standard ArduCopter frame right?  That's fine but you'll need to experiment a bit to get the PID values right.  If you're slowly decending, then it seems to me your PID values could be too low.  I'd try increasing them all by 10%.....but actually i suspect the issue is with your throttle at the moment you turn on the autopilot as mentioned above. 



    P.S. I've updated the wiki with some of info here so people who follow in your footsteps have an easier go at it.

  • Well this is my problem as of now. First I flew and then enabled the sonar without having the PID values adjusted and the quad ascended into the air and I had to turn off sonar hold and bring it back down. Then I updated the PID values using the CLI and then when I would switch to sonar hold, my quad copter slowly descended until I take it out of sonar hold mode. So I don't know what to change to fix this.


    Oh. And I have already tested the sonar with an arduino and the readings are dead on.

  • Developer

    Taylor - no it will just determine how aggressively the throttle is modified to try and maintain the altitude.


    the way you control is the same as if you're using Altitude hold with the barometer:

        1. get the quad flying at a some what stable altitude

        2. flick on the autopilot

     it should hold it's altitude now.  move the stick up or down to make it ascend/descend.  Note that in this mode there is a "deadzone" around where you had the throttle when you flicked on the autopilot (this is on purpose to make it easier to tell it to stay level).

  • Thanks Randy. So if I increase the P term value, will that make my copter ascend more and if I decrease the P term value will that make the copter descend?  Also, how do I use the test code file? Do I just open that up in arduino IDE and load it to the APM? Or do I just put that file into my library folder for the range finder and then watch the serial output?
  • Developer

    @taylor - thanks, nice catch.  I've updated the code to match the wiki and it should be using AN5 (for the moment although I plan to change it to use the Telemetry port in the next couple of days).  You can actually change which of the ports at the back it uses (from AN0~AN5), by changing this line in System.pde.




    Sorry, I missed your previous comment before about the PID values.  If you want to update these:

         1. set the APM's CLI switch to the back (CLI switch is the white switch at the back, right of the Oilpan)

         2. connect with Arduino's Serial Monitor @115200

         3. when the CLI menu appears select "o"

         4. when it prompts you for new PIDs, enter in three number separated by semi colons (;).  For example the standard PIDs are P=0.8 I=0.3 D=0.7 so you would enter:


             These values should work for most people I'd think.  but you could try increasing/decreasing P (the first term) if you like.


    If you're having problems with the sonar though, I'd first try out the AP_RangeFinder libraries example sketch:


    If you do choose to use the example sketch, you should ensure the code is modified to use the rangefinder you have by uncommenting/commenting out the appropriate line in the code.  I.e. the default code in the example pde issing the LV rangefinder.

    // create the range finder object
    //AP_RangeFinder_SharpGP2Y aRF;
    //AP_RangeFinder_MaxsonarXL aRF;
    AP_RangeFinder_MaxsonarLV aRF;
This reply was deleted.