FlightAware Discussions

How to determine bearings of all planes in tracking range?

I’m trying to build an LED compass using the aircraft.json, so compare all the planes lat/lon with the receivers to give a number between 1 and 360 to give the bearing, which could then be picked up by the Arduino to interpret. Ideally, I’d also add altitude to the data, too change the LED colour.

I’ve seen a few code calculations for working out bearings, but most return just one aircraft.
How could I get an array of bearings for the planes around, in a format that could be fed to an Arduino?

The aircraft’s heading is transmitted as part of the ADS-B message, so you only need to ‘read’ it.

Not sure I understand what value there is in combining multiple headings though.

Bearing isn’t the same as heading - heading is the direction the aircraft is pointing, whereas bearing is the direction from some point (eg a receiver) to the aircraft. That isn’t something that is transmitted, and would need to be calculated from the position of the aircraft and receiver.

The aircraft positions are contained within the aircraft.json file generated by dump1090. There are many ways to read and process data from jsons so it really depends on how you want to implement it, and what your arduino code is expecting as an input. I doubt you will find something pre-written that does exactly what you want, but you might be able to adapt something to generate the information you need. For example, my heatmap scripts use jq to read jsons and awk to do calculations (including bearing) on the data. You could definitely do everything you want in python which would probably be somewhat neater and more flexible.

Yes, the question was how to work out the calculations for multiple bearings from the central receiver position.
I found some code that Websanity created - Add Audio Cue to Piaware - #13 by websanity - that worked out the closest aircraft and then worked out the bearing from there.
I adapted the foreach loop:

 <?php
// Fetch local data file
$json = file_get_contents('http://xxx.xxx.xxx.xxx:8080/data/aircraft.json');
$j_aircraft_data = json_decode($json,true);


// Home location and a constant
$lat_home=33.339734;
$lon_home=10.156381;
$p = 0.017453292519943295;    // Math.PI / 180

foreach ($j_aircraft_data['aircraft'] as $index=>$aircraft) {

        if (isset($aircraft['hex']) && isset($aircraft['lat']) && isset($aircra$

                $lat1=$aircraft["lat"];
                $lon1=$aircraft["lon"];
                $alt1=$aircraft["alt_baro"];
                //$lat1=50.339734;      // TEST hardcode
                //$lon1=-0.156381;


                // Bearing
                $r_lat1=deg2rad($lat1);
                $r_lon1=deg2rad($lon1);
                $r_lat_home=deg2rad($lat_home);
                $r_lon_home=deg2rad($lon_home);
                $r_dLon=$r_lon1-$r_lon_home;
                $x = cos($r_lat_home)*sin($r_lat1) - sin($r_lat_home)*cos($r_la$
                $y = sin($r_dLon) * cos($r_lat1);
                $brng=(atan2($y, $x)) / $p;     // i.e.  * 180 / PI;
                $nearest_bearing1=round(($brng + 360) % 360);

                //Altitude
                if ($alt1 >= 0 && $alt1 < 2000){
                        $col = "rgb(255,0,0)";
                 }elseif ($alt1 >= 2001 && $alt1 < 4000){
                        $col = "rgb(255,125,0)";
                }elseif ($alt1 >= 4001 && $alt1 < 6000){
                        $col = "rgb(255,215,0)";
                }elseif ($alt1 >= 6001 && $alt1 < 10000){
                        $col = "rgb(0,255,0)";
                }elseif ($alt1 >= 10001 && $alt1 < 20000){
                        $col = "rgb(0,0,255)";
                }elseif ($alt1 >= 20001 && $alt1 < 40000){
                        $col = "rgb(255,0,255)";
                }elseif ($alt1 >= 40001 && $alt1 < 60000){
                        $col = "rgb(255,255,255)";
                }

                        echo("LED[".$nearest_bearing1."]=");
                        echo $col;
                        echo "<br>";

        }       // aircraft data set
}       // Loop all aircraft entries


?>

And now the result is:

LED[161]=rgb(0,0,255)
LED[135]=rgb(255,0,255)

I’ve also asked the question on the Arduino forum about what’s the best format to send to it, but so far I’ve not heard much useful back.

1 Like

Hi folks,

Hope that code is working for you, good adaptation [feyipuk]

I haven’t touched my code for ages; I don’t know if I said but I have PHP and MySQL on the Pi and instead of driving LEDS - which sounds great! - driving a webpage just on local IP address so I can see on phone.

You could always go to somewhere like: https://www.noip.com to create a real web address, setup port forwarding on your router, then on the PI put dynamic no-IP client then make your PI accessible on a real web page (which I’ve done for another project for a friend)
Maybe I should do that and use that to fetch the data from the Pi for processing on a proper web server although the whole point is to see what you can hear from your house.

I kind of lost heart with doing further work when all Aviation “paused”; maybe when it picks up I’ll do some more - glad to see others still working away on their projects!

Gez

1 Like

Thanks, the code is working great. I ran some math on it to work out which LEDs would need to be on if I had 600 LEDS in a rectangle with different numbers on each side:

                if ($nearest_bearing1 >= 0 && $nearest_bearing1 <= 90){
                        $leds600 = (ceil($nearest_bearing1*2));
                 }elseif ($nearest_bearing1 >= 91 && $nearest_bearing1 <= 180){
                        $leds600 = (ceil(($nearest_bearing1*1.33333333333)+60));
                }elseif ($nearest_bearing1 >= 181 && $nearest_bearing1 <= 270){
                        $leds600 = (ceil(($nearest_bearing1*2)-60));
                }elseif ($nearest_bearing1 >= 271 && $nearest_bearing1 <= 360){
                        $leds600 = (ceil(($nearest_bearing1*1.33333333333)+120));

but I never worked out how to send the output to the LEDs, so now I just have the pages running on another Pi (with a noip redirect) to see how many planes there are near my house. Tried a few other work arounds, such as SQL logging; ended up moving my Pi installation to an SSD as there was so much data I ended up stopping, as it was unwieldy.

The recent situations in Belarus and getting people back from Portugal restarted my interest in going back over old plots, working out how to use KML, etc, so not looked at local running in a while.
Thanks again for the bearing code.

1 Like

I was wondering that

But kind of interesting

You may want to add a correction for magnetic variation if you are in an area where it is significant.