-
Notifications
You must be signed in to change notification settings - Fork 0
/
MxMotion.ts
159 lines (145 loc) · 4.02 KB
/
MxMotion.ts
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
enum AxisType
{
X = 0,
Y,
Z
}
enum EularType
{
Roll = 0,
Pitch,
Yaw
}
//% weight=8 color=#000016 icon="\uf0b2" block="MxMotion"
namespace MxMotion{
const MxMotion_ADDR = 0x23
enum MotionReg
{
Device_ID = 1,
Device_CONFIG,
ROLL_L,
ROLL_H,
PITCH_L,
PITCH_H,
YAW_L,
YAW_H,
GYRO_X_L,
GYRO_X_H,
GYRO_Y_L,
GYRO_Y_H,
GYRO_Z_L,
GYRO_Z_H,
ACCEL_X_L,
ACCEL_X_H,
ACCEL_Y_L,
ACCEL_Y_H,
ACCEL_Z_L,
ACCEL_Z_H,
}
/**
*start up the motion sensor
*/
//%block="start up the motion sensor"
//%weight=994 inlineInputMode="external" %blockID="MxMotion_init"
export function init(): void {
if(i2cRead(MotionReg.Device_ID) == 0x44){
i2cWrite(MotionReg.Device_CONFIG, 0x02); // reset
basic.pause(500);
i2cWrite(MotionReg.Device_CONFIG, 0x01); // enable
}
}
/**
*read Motion eular data from sensor
*@param axis [0-2] set the data of axis; eg: 0, 1
*/
//%block="read eular angle %axis from sensor"
//%weight=94 %blockID="MxMotion_eular"
export function readEular(axis: EularType): number {
let out = 0
switch(axis){
case 0:
out = i2cRead(MotionReg.ROLL_H) << 8 | i2cRead(MotionReg.ROLL_L)
if(out > 32767){
out -= 65536
}
break
case 1:
out = i2cRead(MotionReg.PITCH_H) << 8 | i2cRead(MotionReg.PITCH_L)
if(out > 32767){
out -= 65536
}
break
case 2:
out = i2cRead(MotionReg.YAW_H) << 8 | i2cRead(MotionReg.YAW_L)
if(out > 32767){
out -= 65536
}
break
default:
break
}
return out
}
/**
*read Motion accel data from sensor
*@param axis [0-2] set the data of axis; eg: 0, 1
*/
//%block="read accel data %axis from sensor(mm/s^2)"
//%weight=94 %blockID="MxMotion_accel"
export function readAccel(axis: AxisType): number {
let out = 0
switch(axis){
case 0:
out = i2cRead(MotionReg.ACCEL_X_H) << 8 | i2cRead(MotionReg.ACCEL_X_L)
break
case 1:
out = i2cRead(MotionReg.ACCEL_Y_H) << 8 | i2cRead(MotionReg.ACCEL_Y_L)
break
case 2:
out = i2cRead(MotionReg.ACCEL_Z_H) << 8 | i2cRead(MotionReg.ACCEL_Z_L)
break
default:
break
}
if(out > 32767){
out -= 65536
}
return out
}
/**
*read Motion gyro data from sensor
*@param axis [0-2] set the data of axis; eg: 0, 1
*/
//%block="read gyro data %axis from sensor(dgree/s)"
//%weight=94 %blockID="MxMotion_gyro"
export function readGyro(axis: AxisType): number {
let out = 0
switch(axis){
case 0:
out = i2cRead(MotionReg.GYRO_X_H) << 8 | i2cRead(MotionReg.GYRO_X_L)
break
case 1:
out = i2cRead(MotionReg.GYRO_Y_H) << 8 | i2cRead(MotionReg.GYRO_Y_L)
break
case 2:
out = i2cRead(MotionReg.GYRO_Z_H) << 8 | i2cRead(MotionReg.GYRO_Z_L)
break
default:
break
}
if(out > 32767){
out -= 65536
}
return out
}
function i2cWrite(reg: number, value: number): void {
let buf = pins.createBuffer(2)
buf[0] = reg
buf[1] = value
pins.i2cWriteBuffer(MxMotion_ADDR, buf)
}
function i2cRead(reg: number): number {
pins.i2cWriteNumber(MxMotion_ADDR, reg, NumberFormat.UInt8LE)
return pins.i2cReadNumber(MxMotion_ADDR, NumberFormat.UInt8LE, false)
}
}