Magnetic Variation

wooly

Member
Licensed User
Longtime User
Hi, new user here. Apologies if this is a dupe, I thought I had posted this earlier but it doesn't appear to have posted.

I am writing a programme in Basic4Android and would like to be able to calculate Magnetic Variation. I thought I had found the solution in NAV1,3 library, but this was for Basic4PPC.

Is there a way to calculate Magnetic Variation in Basic4Android?

Wooly
 

CharlesR

Member
Licensed User
Longtime User
Seems like you have two options.
- Use an online or offline database that maps the variation based on your location.
- Ask the user to move with the device held straight and compare the GPS direction to the compass direction.

You should also note that using a database will give you only variation, while Erel's comparison method will result in Compass Error which is the sum of variation and deviation. (Deviation is the element of compass error caused by local magnetic anomalies for example if the device is used in a vehicle)
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Is there a way to calculate Magnetic Variation in Basic4Android?

The algorithm is in the c file of Nav1.3 .
If you can translate it to basic - then you have it.

I thought to do it but didn't feel that anyone will use it. You are the first.
 
Upvote 0

wooly

Member
Licensed User
Longtime User
Thanks for the replies, I may give Davids a try. It's a long time since I played with C so it could be interesting!!

Wooly
 
Upvote 0

wooly

Member
Licensed User
Longtime User
Thank You David, you definitely went "above and beyond" in creating the library.:sign0098:

VERY much appreciated.

Wooly
 
Upvote 0

wooly

Member
Licensed User
Longtime User
After your excellent work David, may I ask a question or two.

First, a little background, I am new to B4A, and haven't really programmed much for many years. I am writing a programme, initially for my own use to calculate pointing angles for a specific satellite So far I can get my position from the GPS then have the programme calculate the angles. The last piece is to calculate the variation

I have tried following the info from the Basic4PPC library as well as the .xml file from the new library.

The programme I came up with (please don't laugh) is below:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

Dim mg As Magnetics

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim v(3) As Double      'v(0)-Magnetic Declination (OR Variation) 
                       'v(1)-Magnetic Inclination 
                       'v(2)-Grid Variation -
   
   Dim Lat As Double      'Input Latitude
   Dim Lon As Double      'Input Longitude
   Dim Alt As Double      'Input Altitude

   Dim dv As Double      'Used in Variation Calculation
   Dim sign As Int         'Used in Sign of Variation Calculation

   Dim txtLat As EditText   'Get Altitude
   Dim txtLon As EditText   'Get Longitude
   Dim txtAlt As EditText   'Get Altitude
      
   Dim Label1 As Label      'Descriptive Text
   Dim Label2 As Label      'Descriptive Text
   Dim Label3 As Label      'Descriptive Text
   Dim Label5 As Label      'Descriptive Text
      
   Dim lblVarDig As Label   'Dispalay Decimal Variation
   Dim lblVarDMS As Label   'Dispalay DMS Variation
   Dim lblInc As Label      'Dispalay Inclination
   Dim lblGV As Label      'Dispalay Grid Variation

   Dim btnCalc As Button   'Starts Calculation

End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("magtest")
mg.Initialize

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub calc_Click 

v() = mg.magneticdata(txtLat.text,txtLon.text,txtAlt.text)  'ERROR LINE

dv = Abs(v(0)) 

sign = v(0) /dv 

lblVarDig.text = Round2(v(0),2) & Chr(176) ' digital 
lblVarDMS.text = Floor(dv) & Chr(176) & Round((dv-Floor(dv))*60) & "'" ' DMS 
lblInc.text = v(1) ' Inclination 

lblGV.text = v(2) ' Grid Variation 

End Sub

The error I get, and can't fix is :

Compiling code. Error
Error compiling program.
Error description: Missing parameter.
Occurred on line: 60
v() = mg.magneticdata(txtLat.text,txtLon.text,txtAlt.text)
Word: (

If you have the time could you point me in the right direction?

Also where should WMMC.COF be located, currently I have it in C:\Program Files (x86)\Anywhere Software\Basic4android\

Wooly
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Very simple. Unlike b4ppc, here you don't use V() but just V in the following:
B4X:
v = mg.magneticdata(txtLat.text,txtLon.text,txtAlt.text)

Also where should WMMC.COF be located

It should be located in the device that runs the application, in a directory "Magnetics" which is directly in the External storage ("sdcard" in my devices, but some devices use other names). You can transfer it using USB cable or with Bridgeplus application.
The reason I put it there is that it needs replacement in time, so if the library gets it from DirAssets - it cannot be replaced by the user.

It is a nice program, nothing to lough about. Keep on the good work !
 
Last edited:
Upvote 0

wooly

Member
Licensed User
Longtime User
Hi David, thanks for the reply. I got most of the differences, ROUND -> ROUND2
INT -> FLOOR etc, but I'm not sure I would have found the v() - v!

OK on the location of the WMMC.COF file, make perfect sense.

Again thanks for all of your work and assistance.


Wooly
 
Upvote 0
Top