get_bearing understanding

I dont know if this is best place to ask these fairly beginner questions.  I have been spending the last few weeks reading though and understanding the autopilot code as best I can, and also learning lots about micro controller programming as I go along, but I have once again become slightly confused with a part of the code  Its more of a theory question.

long get_bearing(struct Location *loc1, struct Location *loc2)
long off_x = loc2->lng - loc1->lng;
long off_y = (loc2->lat - loc1->lat) * scaleLongUp;
long bearing =  9000 + atan2(-off_y, off_x) * 5729.57795;
if (bearing < 0) bearing += 36000;
return bearing;
I cant seem to find where the 9000, and 5729.57795 numbers come from, I looked up things about GPS maths and great circle maths etc,, but cant seem to get where these are coming from (am I missing something)

Further The scaleLongUp I know is used to reverse the longitude scaling, but why do we need to use this?  Is it from another part of code where scaleLongDown was used?

And lastly what is the get_bearing2 used for?


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

Join diydrones

Email me when people reply –


  • Developer
    Hi Alex,
    9000 is 90° expanded to an integer * 100.
    I use scaling to stretch out the longitude based on the cosine of the latitude. This turns the local area into a normal grid so that atan2 can do it's job. The output is an angle in radians which is rotated 90°. I just scale to degrees (5729.57795 ), add 90 and wrap the value if it's higher than 360.

    I use this method because it's really, really fast. If I were in a plane and needed to get a bearing between two cities, it would be off a bit, but for a few miles it's perfectly accurate. The truely accurate methods are about 8-10x more computationally expensive.

    get_bearing2 is an older method that was giving me problems. I just haven't removed it yet.
This reply was deleted.


DIY Robocars via Twitter
15 hours ago
DIY Drones via Twitter
RT @chr1sa: My talk on PX4 and FAA certification is coming up at 1:45 PST today on the PX4 Dev Summit livestream. Includes some cool new st…
DIY Drones via Twitter
RT @seesharp: I'm tuned into the PX4 / Dronecode free live conference. Great stuff. Microsoft AirSim talk in 10 minutes.…
Jul 6
DIY Robocars via Twitter
RT @masato_ka: 距離センサを3つとESP32を付けたラジコンカーをDonkeyCarライクにNNで自動走行。3層FC極小モデルをTensorFlow Lite for microcontrollerで動かしてる。機体は借り物でRumiCarって言います。Tenso…
Jul 5
DIY Robocars via Twitter
RT @SmallpixelCar: My car was able to go all the way autonomously until the crosswalk. It was only 100 yards from the target. What should b…
Jul 4
Liam left a comment on Agricultural UAVs
I'm Liam from T-MOTOR. I would like to reach out to see if there is any possibility for us to work together.
We are a propulsion system manufacturer who offers motors, propellers and ESCs for all kinds of drone applications which vary from secur…"
Jun 30
DIY Robocars via Twitter
RT @SmallpixelCar: Smart move. The car used the shadow to guide it through the bridge. This was never in the training samples. But it learn…
Jun 30
DIY Robocars via Twitter
RT @SmallpixelCar: Getting closer to the target. Single camera. Untrained road.
Jun 29
Richard Cox left a comment on Australia
"Anyone in the DIYDRONES Australian subgroup based in Alice Springs, NT?
I am experimenting with Ardupilot (standard Arduplane), Pixhawk 4 FC in a 4-ch
RC "AXN Floater Jet" foamy plane..."
Jun 29
Omar Sykes left a comment on Australia
"Hi everyone, I am looking for someone who is good at drone building, repair and software in Adelaide. Please give me a call on 0477 319 219."
Jun 29
DIY Robocars via Twitter
RT @RoboticMasters: #donkeycar
Jun 29
DIY Robocars via Twitter
Jun 29
DIY Robocars via Twitter
RT @RoboticMasters: Donkey car, car car car car car car; Donkey car, car car car car car car; Donkey Car. Anyone like our tiny tiny donkey…
Jun 29
DIY Robocars via Twitter
RT @SmallpixelCar: After improving DBSCAN speed, I can get 11 frame per second on @NVIDIAEmbedded Jerson Xavier MAXN mode and the autonomou…
Jun 26
DIY Robocars via Twitter
RT @Heavy02011: Join us at next Virtual Race League: ⁦@diyrobocars⁩ Race #4 - Parking Lot Nerds, August 1st
Jun 25
DIY Robocars via Twitter
RT @SmallpixelCar: #CUDA implementation should be easy, because most of the time is on distance calculations berween two points and if the…
Jun 25