Loading...
Pages: [1]   Go Down
Author Topic: gps to magnetic compass  (Read 188 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to convert my current gps coordinates and the desired gps coordinates to a compass heading.  I am trying to use the equations for the great circle. When I compile I am getting this error. error: invalid operands of types 'double' and 'double' to binary 'operator%'  I'm pretty sure it does not like my modulo operator, but because atan2 returns a value between -180 and 180 I need to use it.

Code:
double lat_gps, lon_gps, lat, lon;
float desired_heading;

          desired_heading =atan2(cos(lat_gps)*sin(lat)-sin(lat_gps)*cos(lat)*sin(lon-lon_gps),sin(lon-lon_gps)*cos(lat)); 
          desired_heading = (desired_heading*(180.0/3.14)+360.0) % 360.0;
          motor_control(desired_heading);
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 279
Posts: 15312
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Two points.

First double and float are both the same size floating point variables in the arduino platform (The double implementation on the Arduino is currently exactly the same as the float, with no gain in precision).

 .
Second from the arduino reference on the Modulo function ( http://arduino.cc/en/Reference/Modulo  )

Quote
The modulo operator does not work on floats.


Lefty
Logged

New Jersey
Offline Offline
Edison Member
*
Karma: 24
Posts: 2345
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As the compiler says, the modulo operator cannot be used on floating point numbers. If you don't need the heading to have decimal places, just cast it to an int and use modulo. If you do, add an additional step and only add the 360 if the interim result is less than 0.
Logged

Pages: [1]   Go Up
Print
 
Jump to: