Skip to content

Commit

Permalink
Format as an Arduino library (#4)
Browse files Browse the repository at this point in the history
* move files around

* rename header file

* Add linting

* fix CI

* fix CI

* add includes
  • Loading branch information
nhz2 authored Feb 8, 2023
1 parent 753d24b commit 2a63a37
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 15 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: compile tests
working-directory: ${{github.workspace}}/test_codegen
working-directory: ${{github.workspace}}/extras
run: g++ geomag_test.cpp -std=c++14
- name: run tests
working-directory: ${{github.workspace}}/test_codegen
run: ./a.out
working-directory: ${{github.workspace}}/extras
run: ./a.out
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ XYZgeomag uses single precision floating points. It's designed to minimize ram u

## Using XYZgeomag

Just download [geomag.hpp](https://raw.githubusercontent.com/nhz2/XYZgeomag/master/geomag.hpp) and include it.
Just download [XYZgeomag.hpp](https://raw.githubusercontent.com/nhz2/XYZgeomag/master/src/XYZgeomag.hpp) and include it.
Here is an example Arduino sketch:

~~~cpp
#include "geomag.hpp"
#include "XYZgeomag.hpp"
void setup() {
// put your setup code here, to run once:
pinMode(1,INPUT);
Expand Down Expand Up @@ -78,7 +78,7 @@ You can also convert the magnetic field to
the [seven magnetic elements](https://www.ncei.noaa.gov/products/world-magnetic-model)
in units of nanotesla and degrees.
~~~cpp
#include "geomag.hpp"
#include "XYZgeomag.hpp"
void setup() {
// put your setup code here, to run once:
pinMode(1,INPUT);
Expand Down Expand Up @@ -119,16 +119,16 @@ void loop() {

To add new coefficents, download the new `.COF` file from [https://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml](https://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml)

Add the .COF file to the `test_codegen` directory.
Add the .COF file to the `extras` directory.

Then run for example
`python wmmcodeupdate.py -f WMM2015.COF -f WMM2015v2.COF -f WMM2020.COF -o ../geomag.hpp -n 12` from the `test_codegen` directory.
`python wmmcodeupdate.py -f WMM2015.COF -f WMM2015v2.COF -f WMM2020.COF -o ../src/XYZgeomag.hpp -n 12` from the `extras` directory.

In this example, `WMM2015.COF` , `WMM2015v2.COF`, and `WMM2020.COF` are the `.COF` files to use in `geomag.hpp`.
In this example, `WMM2015.COF` , `WMM2015v2.COF`, and `WMM2020.COF` are the `.COF` files to use in `src/XYZgeomag.hpp`.

## Run Tests

In the `test_codegen` directory.
In the `extras` directory.

Compile `geomag_test.cpp` for example with the command `g++ geomag_test.cpp -std=c++14`

Expand Down
34 changes: 34 additions & 0 deletions examples/Declination/Declination.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <XYZgeomag.hpp>

void setup() {
// put your setup code here, to run once:
pinMode(1,INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
int val= digitalRead(1);
float lat = val + 43.0f; // latitude in degrees
float lon = val + 75.0f; // longitude in degrees
float height = val + 305; // height above WGS84 ellipsoid in meters
geomag::Vector position = geomag::geodetic2ecef(lat,lon,height);
geomag::Vector mag_field = geomag::GeoMag(2022.5,position,geomag::WMM2020);
geomag::Elements out = geomag::magField2Elements(mag_field, lat, lon);
Serial.print(out.north);
Serial.println(" nT north");
Serial.print(out.east);
Serial.println(" nT east");
Serial.print(out.down);
Serial.println(" nT down");
Serial.print(out.horizontal);
Serial.println(" nT horizontal");
Serial.print(out.total);
Serial.println(" nT total");
Serial.print(out.inclination);
Serial.println(" deg inclination");
Serial.print(out.declination);
Serial.println(" deg declination");
Serial.println();
delay(2000);
}
33 changes: 33 additions & 0 deletions examples/Geocentric/Geocentric.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <XYZgeomag.hpp>

void setup() {
// put your setup code here, to run once:
pinMode(1,INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
int val= digitalRead(1);
geomag::Vector in;
in.x=val+1128529.6885767058f;
in.y=val+0.0;
in.z=val+6358023.736329913f;
geomag::Vector out;
int starttime=micros();
int starttimemil=millis();
out=geomag::GeoMag(2022.5,in,geomag::WMM2020);
int endtime=micros();
int endtimemil=millis();
Serial.print(out.x*1E9);
Serial.println(" nT x");
Serial.print(out.y*1E9);
Serial.println(" nT y");
Serial.print(out.z*1E9);
Serial.println(" nT z");
Serial.print("time in micro seconds: ");
Serial.println(endtime-starttime);
Serial.print("time in milli seconds: ");
Serial.println(endtimemil-starttimemil);
delay(2000);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test_codegen/geomag_test.cpp → extras/geomag_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include "../geomag.hpp"
#include "../src/XYZgeomag.hpp"


TEST_CASE( "geodetic 2 ecef test 0 model", "[Geodetic]" ) {
Expand Down
2 changes: 1 addition & 1 deletion test_codegen/wmmcodeupdate.py → extras/wmmcodeupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def parseescof(infilename, maxdegree):
""")
parser.add_argument('-f',action='append',help="""the .COF files that contains the
WMM coefficents, download from https://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml.""")
parser.add_argument('-o',type=str,default='geomag.hpp',help='the c++ header filename to write the coefficents and model')
parser.add_argument('-o',type=str,default='../src/XYZgeomag.hpp',help='the c++ header filename to write the coefficents and model')
parser.add_argument('-n',type=int,default=12,help='maximum number of degrees to use')
arg=parser.parse_args()

Expand Down
4 changes: 2 additions & 2 deletions test_codegen/wmmtestgen.py → extras/wmmtestgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def main(header_to_test,modelnames,dates,heights,lats,lons,bns,bes,bds,bhs,bfs,b
margin_deg(float or int): Acceptable error in bincs and bdecs (deg)"""
xs,ys,zs=pm.geodetic2ecef(lats,lons,heights*1000)
bxs,bys,bzs=pm.enu2uvw(bes*1E-9,bns*1E-9,-bds*1E-9,lats,lons)
file_name=header_to_test[:-4]+'_test.cpp'
file_name='geomag_test.cpp'
with open(file_name,'w') as f:
outstr="""
// %s Generated by python script wmmtestgen.py
Expand Down Expand Up @@ -150,7 +150,7 @@ def main(header_to_test,modelnames,dates,heights,lats,lons,bns,bes,bds,bhs,bfs,b
f.write(outstr)

if __name__ == '__main__':
header_to_test='geomag.hpp'
header_to_test='src/XYZgeomag.hpp'
dates=([2015]*6+[2017.5]*6)*2 + [2020]*6 + [2022.5]*6
modelnames= ['WMM2015']*12 + ['WMM2015v2']*12 + ['WMM2020']*12
heights=([0]*3+[100]*3+[0]*3+[100]*3)*3
Expand Down
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=XYZgeomag
version=2.0.0
author=Nathan Zimmerberg <[email protected]>
maintainer=Nathan Zimmerberg <[email protected]>
sentence=Calculate the magnetic field on earth using the World Magnetic Model(WMM).
paragraph=Given geocentric cartesian coordinates, the main function geomag::GeoMag calculates the magnetic field around earth in the International Terrestrial Reference System(ITRS) and uses units of decimal year, meter, and tesla.
category=Data Processing
url=https://github.com/nhz2/XYZgeomag
architectures=*
includes=XYZgeomag.hpp
2 changes: 1 addition & 1 deletion geomag.hpp → src/XYZgeomag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SOFTWARE.
*/


// ../geomag.hpp Generated by python script wmmcodeupdate.py
// ../src/XYZgeomag.hpp Generated by python script wmmcodeupdate.py
/** \file
* \author Nathan Zimmerberg ([email protected])
* \date 07 AUG 2021
Expand Down

0 comments on commit 2a63a37

Please sign in to comment.