forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.magnetometer.common.spinh
157 lines (113 loc) · 4.99 KB
/
sensor.magnetometer.common.spinh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
----------------------------------------------------------------------------------------------------
Filename: sensor.magnetometer.common.spinh
Description: Routines common to all sensor.magnetometer device drivers
Author: Jesse Burt
Started: Aug 31, 2022
Updated: Jun 19, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
NOTE: This file must be included by the compiler using the
#include preprocessor directive, _outside_ of any existing
block designators (CON, VAR, OBJ, PUB, PRI, DAT)
}
VAR
long _mres[MAG_DOF]
long _mbias[MAG_DOF]
PUB calibrate_mag() | axis, orig_scl, orig_dr, tmpx, tmpy, tmpz, tmp[MAG_DOF], samples
' Calibrate the magnetometer
longfill(@axis, 0, 10) ' initialize vars to 0
{ save current settings }
orig_scl := mag_scale(-2)
orig_dr := mag_data_rate(-2)
mag_set_bias(0, 0, 0) ' clear existing bias
{ set sensor to driver-specific scale and data rate }
mag_scale(CAL_M_SCL)
mag_data_rate(CAL_M_DR)
samples := CAL_M_DR ' samples = DR, for 1 sec time
{ accumulate and average approx. 1sec worth of samples }
repeat samples
repeat until mag_data_rdy()
mag_data(@tmpx, @tmpy, @tmpz)
tmp[X_AXIS] += tmpx
tmp[Y_AXIS] += tmpy
tmp[Z_AXIS] += tmpz
{ calculate averages }
repeat axis from X_AXIS to Z_AXIS
tmp[axis] /= samples
{ update offsets }
mag_set_bias(tmp[X_AXIS], tmp[Y_AXIS], tmp[Z_AXIS])
{ restore existing settings }
mag_scale(orig_scl)
mag_data_rate(orig_dr)
PUB mag_gauss(mx, my, mz) | tmp[MAG_DOF]
' magnetometer data scaled to micro-gauss
mag_data(@tmp[X_AXIS], @tmp[Y_AXIS], @tmp[Z_AXIS])
long[mx] := magx_word2gauss(tmp[X_AXIS])
long[my] := magy_word2gauss(tmp[Y_AXIS])
long[mz] := magz_word2gauss(tmp[Z_AXIS])
PUB mag_tesla(ptr_x, ptr_y, ptr_z) | tmp[MAG_DOF]
' magnetometer data scaled to micro-Teslas
mag_data(@tmp[X_AXIS], @tmp[Y_AXIS], @tmp[Z_AXIS])
long[ptr_x] := magx_word2tesla(tmp[X_AXIS])
long[ptr_y] := magy_word2tesla(tmp[Y_AXIS])
long[ptr_z] := magz_word2tesla(tmp[Z_AXIS])
PUB magx_word2gauss(mag_word): mag_gauss
' Convert magnetometer X-axis ADC word to Gauss
return (mag_word * _mres[X_AXIS])
PUB magy_word2gauss(mag_word): mag_gauss
' Convert magnetometer Y-axis ADC word to Gauss
return (mag_word * _mres[Y_AXIS])
PUB magz_word2gauss(mag_word): mag_gauss
' Convert magnetometer Z-axis ADC word to Gauss
return (mag_word * _mres[Z_AXIS])
PUB magx_word2tesla(mag_word): mag_tesla
' Convert magnetometer X-axis ADC word to Teslas
return (mag_word * _mres[X_AXIS]) / 10_000
PUB magy_word2tesla(mag_word): mag_tesla
' Convert magnetometer Y-axis ADC word to Teslas
return (mag_word * _mres[Y_AXIS]) / 10_000
PUB magz_word2tesla(mag_word): mag_tesla
' Convert magnetometer Z-axis ADC word to Teslas
return (mag_word * _mres[Z_AXIS]) / 10_000
OBJ
fltmath: "math.float.nocog"
CON
DPR = 180.0 / PI ' convert radians to degrees
PUB heading = yaw
PUB yaw(): a | x, y, z, xy, rads
' Get yaw/heading of magnetometer
' Returns: angle in 1/10_000's degrees (e.g,, 359_1234 = 359.1234deg)
mag_data(@x, @y, @z)
x := fltmath.ffloat(x)
y := fltmath.ffloat(y)
if ( fltmath.fcmp(x, 0.0) == 0 )
if ( fltmath.fcmp(y, 0.0) == -1 )
xy := 90.0
elseif ( (fltmath.fcmp(y, 0.0) == 0) or (fltmath.fcmp(y, 0.0) == 1) )
xy := 0.0
else
rads := fltmath.atan2(y, x)
xy := fltmath.fmul(rads, DPR)
if ( fltmath.fcmp(xy, 360.0) == 1 ) '> 360?
xy := fltmath.fsub(xy, 360.0)
if ( fltmath.fcmp(xy, 0.0) == -1 )
xy := fltmath.fadd(xy, 360.0)
xy := fltmath.fmul(xy, 10_000.0)
return fltmath.ftrunc(xy)
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}