-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimu-demo-common.spin2h
322 lines (252 loc) · 9.3 KB
/
imu-demo-common.spin2h
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{
----------------------------------------------------------------------------------------------------
Filename: imu-demo-common.spin2h
Description: IMU data visualization, common code (QVGA, 8bpp)
Author: Jesse Burt
Started: Sep 3, 2022
Updated: Oct 22, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
CON
CENTERX = vga.WIDTH/2
CENTERY = vga.HEIGHT/2
OBJ
fnt: "font.5x8"
math: "math.int"
VAR
long _gdiv
long _imu_name[3]
long _gscl1k_sine
word _ascl, _gscl, _mscl
byte _offscreen[vga.WIDTH*vga.HEIGHT]
PUB demo() | disp_data
update_settings()
disp_data := @accel_ray
vga.fgcolor(WHITE)
repeat
case_fast ser.getchar_noblock()
"C":
ser.strln(@"calibrating...")
calibrate()
"c":
vga.clear()
"a":
vga.char_attrs(0)
disp_data := @accel_ray ' point to chosen display mode
vga.clear() '
"g":
vga.char_attrs(0)
disp_data := @gyro_ray '
vga.clear() '
"m":
vga.char_attrs(vga.DRAWBG)
disp_data := @mag_plot '
vga.clear() '
disp_data()
vga.show()
PUB accel_ray() | ax, ay, az, sx, sy, sz
vga.clear()
vga.pos_xy(0, 1)
vga.printf(@"Accelerometer (raw, %s)", _imu_name)
imu.accel_g(@ax, @ay, @az)
sx := ax / 8192
sy := ay / 8192
sz := az / 8192
vga.pos_xy(0, 2)
vga.printf(@"Scale: %dg\n\r", _ascl)
vga.fgcolor(REDMAX)
vga.printf(@"X: %3.3d.%03.3d\n\r", (ax/1_000000), abs(ax//1_000000)/1000)
vga.fgcolor(WHITE)
vga.fgcolor(GREENMAX)
vga.printf(@"Y: %3.3d.%03.3d\n\r", (ay/1_000000), abs(ay//1_000000)/1000)
vga.fgcolor(WHITE)
vga.fgcolor(BLUEMAX)
vga.printf(@"Z: %3.3d.%03.3d\n\r", (az/1_000000), abs(az//1_000000)/1000)
vga.fgcolor(WHITE)
vga.line(CENTERX, CENTERY, CENTERX, CENTERY+sx, REDMAX)
vga.line(CENTERX, CENTERY, CENTERX+sy, CENTERY, GREENMAX)
vga.line(CENTERX, CENTERY, CENTERX+sz, CENTERY+sz, BLUEMAX)
vga.circle(CENTERX, CENTERY+sx, 5, REDMAX)
vga.circle(CENTERX+sy, CENTERY, 5, GREENMAX)
vga.circle(CENTERX+sz, CENTERY+sz, 5, BLUEMAX)
PUB gyro_ray() | gx, gy, gz, xsx, xsy, ysx, ysy, zsx, zsy, xdiv, ydiv, zdiv, xr, yr, zr
vga.clear()
vga.fgcolor(WHITE)
vga.pos_xy(0, 1)
vga.printf(@"Gyroscope (raw, %s)", _imu_name)
xdiv := vga.HEIGHT * 4
ydiv := vga.HEIGHT * 4
zdiv := vga.HEIGHT * 4
imu.gyro_dps(@gx, @gy, @gz)
gx /= 1000 ' micro-dps to milli-dps for display
gy /= 1000
gz /= 1000
{ scale gyro full-scale range to sine full-cycle (gscl_1k * 2) / 4096 }
xsx := CENTERX + math.cos(gx/_gscl1k_sine) / xdiv
xsy := CENTERY + math.sin(gx/_gscl1k_sine) / xdiv/4
xr := xsy / 16 ' change circle radius depending on position
ysx := CENTERX + math.cos(gy/_gscl1k_sine) / ydiv/4
ysy := CENTERY + math.sin(gy/_gscl1k_sine) / ydiv
yr := ysx / 16
zsx := CENTERX + math.cos(gz/_gscl1k_sine) / zdiv
zsy := CENTERY + math.sin(gz/_gscl1k_sine) / zdiv
vga.pos_xy(0, 2)
vga.printf(@"Scale: %ddps\n\r", _gscl)
vga.fgcolor(REDMAX)
vga.printf(@"X: %3.3d.%03.3d\n\r", gx/1000, abs(gx//1000))
vga.fgcolor(GREENMAX)
vga.printf(@"Y: %3.3d.%03.3d\n\r", gy/1000, abs(gy//1000))
vga.fgcolor(BLUEMAX)
vga.printf(@"Z: %3.3d.%03.3d", gz/1000, abs(gz//1000))
{ draw lines from screen center outward to elliptical path for each axis }
vga.line(CENTERX, CENTERY, xsx, xsy, REDMAX)
vga.line(CENTERX, CENTERY, ysx, ysy, GREENMAX)
vga.line(CENTERX, CENTERY, zsx, zsy, BLUEMAX)
vga.circle(xsx, xsy, xr, REDMAX)
vga.circle(ysx, ysy, yr, GREENMAX)
vga.circle(zsx, zsy, 5, BLUEMAX)
CON
MAGPLOTSCALE = 10000
PUB mag_plot() | mx, my, mz, sx, ex, sy, ey, x, y, grads, gradstep, sclx, scly, sclz, cx, cy
cx := 239
cy := CENTERY
sx := cx-80 ' Coords for graph "window"
ex := cx+80
sy := cy-80
ey := cy+80
grads := 8
gradstep := 160/grads
vga.fgcolor(WHITE)
vga.pos_xy(0, 1)
vga.printf(@"Magnetometer (gauss, %s)", _imu_name)
imu.mag_gauss(@mx, @my, @mz)
sclx := mx/MAGPLOTSCALE
scly := my/MAGPLOTSCALE
sclz := mz/MAGPLOTSCALE
draw_cgraph(cx, cy, 160, 160, 8, 8)
vga.plot(sx+1 #> (cx + sclx) <# ex-1, sy+1 #> (cy + scly) <# ey-1, BLUEMAX) ' X/Y
vga.plot(sx+1 #> (cx + sclx) <# ex-1, sy+1 #> (cy + sclz) <# ey-1, REDMAX) ' X/Z
vga.plot(sx+1 #> (cx + scly) <# ex-1, sy+1 #> (cy + sclz) <# ey-1, GREENMAX) ' Y/Z
vga.char_attrs(vga.DRAWBG)
vga.pos_xy(0, 2) ' Raw magnetometer data
vga.printf(@"Scale: %dGs\n\r", _mscl)
vga.printf(@"X: %3.3d.%03.3d\n\r\r", (mx/1_000000), abs(mx//1_000000)/1000)
vga.printf(@"Y: %3.3d.%03.3d\n\r\r", (my/1_000000), abs(my//1_000000)/1000)
vga.printf(@"Z: %3.3d.%03.3d\n\r\r", (mz/1_000000), abs(mz//1_000000)/1000)
vga.bgcolor(BLUEMAX) ' Color legend
vga.strln(@"X/Y")
vga.bgcolor(REDMAX)
vga.strln(@"X/Z")
vga.bgcolor(GREENMAX)
vga.fgcolor(BLACK)
vga.strln(@"Y/Z")
vga.bgcolor(BLACK)
vga.fgcolor(WHITE)
PRI draw_graph(sx, sy, ex, ey, hdivs, vdivs) | hgstep, vgstep, x, y, w, h, cx, cy
' Upper-left-anchored graph
w := ex-sx
h := ey-sy
cx := sx+sx
cy := sy+sy
hgstep := w/hdivs
vgstep := h/vdivs
repeat x from sx to ex step hgstep ' Draw dotted graduations centered on screen
repeat y from sy to ey step 2
case x
cx:
vga.plot(x, y, GREY50)
other:
vga.plot(x, y, GREY25-5)
repeat y from sy to ey step vgstep
repeat x from sx to ex step 2
case y
cy:
vga.plot(x, y, GREY50)
other:
vga.plot(x, y, GREY25-5)
PRI draw_cgraph(cx, cy, w, h, hdivs, vdivs) | hgstep, vgstep, x, y, sx, sy, ex, ey
' Center-anchored graph
sx := cx-(w/2)
sy := cy-(h/2)
ex := cx+(w/2)
ey := cy+(h/2)
hgstep := w/hdivs
vgstep := h/vdivs
repeat x from sx to ex step hgstep ' Draw dotted graduations centered on screen
repeat y from sy to ey step 2
case x
cx:
vga.plot(x, y, GREY50)
other:
vga.plot(x, y, GREY25-5)
repeat y from sy to ey step vgstep
repeat x from sx to ex step 2
case y
cy:
vga.plot(x, y, GREY50)
other:
vga.plot(x, y, GREY25-5)
PRI calibrate() | x, y, z
ser.strln(@"accel")
imu.calibrate_accel()
ser.strln(@"gyro")
imu.calibrate_gyro()
ser.strln(@"mag")
imu.calibrate_mag()
ser.strln(@"done")
longfill(@x, 0, 3)
imu.accel_bias(@x, @y, @z)
ser.printf(@"accel bias: %d, %d, %d\n\r", x, y, z)
longfill(@x, 0, 3)
imu.gyro_bias(@x, @y, @z)
ser.printf(@"gyro bias: %d, %d, %d\n\r", x, y, z)
longfill(@x, 0, 3)
imu.mag_bias(@x, @y, @z)
ser.printf(@"mag bias: %d, %d, %d\n\r", x, y, z)
PUB setup()
ser.start()
waitms(30)
ser.clear()
ser.strln(@"Serial terminal started")
vga.start()
vga.draw_to(@_offscreen)
ser.strln(@"VGA 8bpp driver started")
vga.set_font(1,1)
vga.clear()
vga.fgcolor(WHITE)
vga.pos_xy(0, 0)
_ascl := imu.CAL_XL_SCL
_gscl := imu.CAL_G_SCL
_gscl1k_sine := ((_gscl * 1000) * 2) / 4096 ' scale gyro full-scale range to sine full-cycle
_mscl := imu.CAL_M_SCL
CON
' Named constants for colors
BLACK = 0
REDMIN = 0
REDMAX = 63
GREENMIN = 64
GREENMAX = 127
BLUEMIN = 128
BLUEMAX = 191
GREY0 = 192
GREY25 = 192+16
GREY50 = 192+32
GREY75 = 192+48
WHITE = 255
DAT
{
Copyright 2022 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.
}