-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDMP_Driver.h
202 lines (174 loc) · 5 KB
/
DMP_Driver.h
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
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <string.h>
#include <math.h>
#include "../DMP/inv_mpu.h"
#include "../DMP/inv_mpu_dmp_motion_driver.h"
#include "../UART/UART.h"
#ifndef __DMP_DRIVER_H
#define __DMP_DRIVER_H
//deleted handle_input
/* Data requested by client. */
#define PRINT_ACCEL (0x01)
#define PRINT_GYRO (0x02)
#define PRINT_QUAT (0x04)
#define ACCEL_ON (0x01)
#define GYRO_ON (0x02)
#define MOTION (0)
#define NO_MOTION (1)
/* Starting sampling rate. */
#define DEFAULT_MPU_HZ (100)
#define FLASH_SIZE (512)
#define FLASH_MEM_START ((void*)0x1800)
struct rx_s {
unsigned char header[3];
unsigned char cmd;
};
struct hal_s {
unsigned char sensors;
unsigned char dmp_on;
unsigned char wait_for_tap;
volatile unsigned char new_gyro;
unsigned short report;
unsigned short dmp_features;
unsigned char motion_int_mode;
struct rx_s rx;
};
static struct hal_s hal = {0};
/* USB RX binary semaphore. Actually, it's just a flag. Not included in struct
* because it's declared extern elsewhere.
*/
volatile unsigned char rx_new;
/* The sensors can be mounted onto the board in any orientation. The mounting
* matrix seen below tells the MPL how to rotate the raw data from thei
* driver(s).
* TODO: The following matrices refer to the configuration on an internal test
* board at Invensense. If needed, please modify the matrices to match the
* chip-to-body matrix for your particular set up.
*/
static signed char gyro_orientation[9] = {-1, 0, 0,
0,-1, 0,
0, 0, 1};
enum packet_type_e {
PACKET_TYPE_ACCEL,
PACKET_TYPE_GYRO,
PACKET_TYPE_QUAT,
PACKET_TYPE_TAP,
PACKET_TYPE_ANDROID_ORIENT,
PACKET_TYPE_PEDO,
PACKET_TYPE_MISC
};
/* Send data to the Python client application.
* Data is formatted as follows:
* packet[0] = $
* packet[1] = packet type (see packet_type_e)
* packet[2+] = data
*/
void send_packet(char packet_type, void *data);
/* These next two functions converts the orientation matrix (see
* gyro_orientation) to a scalar representation for use by the DMP.
* NOTE: These functions are borrowed from Invensense's MPL.
*/
static inline unsigned short inv_row_2_scale(const signed char *row)
{
unsigned short b;
if (row[0] > 0)
b = 0;
else if (row[0] < 0)
b = 4;
else if (row[1] > 0)
b = 1;
else if (row[1] < 0)
b = 5;
else if (row[2] > 0)
b = 2;
else if (row[2] < 0)
b = 6;
else
b = 7; // error
return b;
}
static inline unsigned short inv_orientation_matrix_to_scalar(
const int8_t *mtx)
{
unsigned short scalar;
/*
XYZ 010_001_000 Identity Matrix
XZY 001_010_000
YXZ 010_000_001
YZX 000_010_001
ZXY 001_000_010
ZYX 000_001_010
*/
scalar = inv_row_2_scale(mtx);
scalar |= inv_row_2_scale(mtx + 3) << 3;
scalar |= inv_row_2_scale(mtx + 6) << 6;
return scalar;
}
/* Handle sensor on/off combinations. */
static void setup_gyro(void)
{
unsigned char mask = 0;
if (hal.sensors & ACCEL_ON)
mask |= INV_XYZ_ACCEL;
if (hal.sensors & GYRO_ON)
mask |= INV_XYZ_GYRO;
/* If you need a power transition, this function should be called with a
* mask of the sensors still enabled. The driver turns off any sensors
* excluded from this mask.
*/
mpu_set_sensors(mask);
if (!hal.dmp_on)
mpu_configure_fifo(mask);
}
static void tap_cb(unsigned char direction, unsigned char count)
{
char data[2];
data[0] = (char)direction;
data[1] = (char)count;
send_packet(PACKET_TYPE_TAP, data);
}
static void android_orient_cb(unsigned char orientation)
{
send_packet(PACKET_TYPE_ANDROID_ORIENT, &orientation);
}
static inline void run_self_test(void)
{
int result;
char test_packet[4] = {0};
long gyro[3], accel[3];
result = mpu_run_self_test(gyro, accel);
if (result == 0x7) {
/* Test passed. We can trust the gyro data here, so let's push it down
* to the DMP.
*/
float sens;
unsigned short accel_sens;
mpu_get_gyro_sens(&sens);
gyro[0] = (long)(gyro[0] * sens);
gyro[1] = (long)(gyro[1] * sens);
gyro[2] = (long)(gyro[2] * sens);
dmp_set_gyro_bias(gyro);
mpu_get_accel_sens(&accel_sens);
accel[0] *= accel_sens;
accel[1] *= accel_sens;
accel[2] *= accel_sens;
dmp_set_accel_bias(accel);
}
/* Report results. */
test_packet[0] = 't';
test_packet[1] = result;
send_packet(PACKET_TYPE_MISC, test_packet);
}
/* Every time new gyro data is available, this function is called in an
* ISR context. In this example, it sets a flag protecting the FIFO read
* function.
*/
static void gyro_data_ready_cb(void)
{
hal.new_gyro = 1;
//DEBUG_PRINTLN("GERO DATA READY CB called.\ntest end.\n");
}
void DMPInitial();
#endif