-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfly.cpp
More file actions
248 lines (196 loc) · 5.6 KB
/
fly.cpp
File metadata and controls
248 lines (196 loc) · 5.6 KB
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
#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#include <stdint.h>
#include <signal.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <curses.h>
//gcc -o week1 week_1.cpp -lwiringPi -lncurses -lm
#define frequency 25000000.0
#define CONFIG 0x1A
#define SMPLRT_DIV 0x19
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C
#define ACCEL_CONFIG2 0x1D
#define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP
#define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode
#define PWR_MGMT_2 0x6C
enum Ascale {
AFS_2G = 0,
AFS_4G,
AFS_8G,
AFS_16G
};
enum Gscale {
GFS_250DPS = 0,
GFS_500DPS,
GFS_1000DPS,
GFS_2000DPS
};
int setup_imu();
void calibrate_imu();
void read_imu();
void update_filter();
//global variables
int imu;
float x_gyro_calibration=0;
float y_gyro_calibration=0;
float z_gyro_calibration=0;
float roll_calibration=0;
float pitch_calibration=0;
float accel_z_calibration=0;
float imu_data[6]; //gyro xyz, accel xyz
long time_curr;
long time_prev;
struct timespec te;
float yaw=0;
float pitch_angle=0;
float roll_angle=0;
int main (int argc, char *argv[])
{
setup_imu();
calibrate_imu();
while(1)
{
read_imu();
update_filter();
}
}
void calibrate_imu()
{
/*
x_gyro_calibration=??
y_gyro_calibration=??
z_gyro_calibration=??
roll_calibration=??
pitch_calibration=??
accel_z_calibration=??
*/
printf("calibration complete, %f %f %f %f %f %f\n\r",x_gyro_calibration,y_gyro_calibration,z_gyro_calibration,roll_calibration,pitch_calibration,accel_z_calibration);
}
void read_imu()
{
int address=0;//todo: set address value for accel x value
float ax=0;
float az=0;
float ay=0;
int vh,vl;
//read in data
vh=wiringPiI2CReadReg8(imu,address);
vl=wiringPiI2CReadReg8(imu,address+1);
//convert 2 complement
int vw=(((vh<<8)&0xff00)|(vl&0x00ff))&0xffff;
if(vw>0x8000)
{
vw=vw ^ 0xffff;
vw=-vw-1;
}
imu_data[3]=0;// todo: convert vw from raw values to "g's"
address=0;//todo: set address value for accel y value
vh=wiringPiI2CReadReg8(imu,address);
vl=wiringPiI2CReadReg8(imu,address+1);
vw=(((vh<<8)&0xff00)|(vl&0x00ff))&0xffff;
if(vw>0x8000)
{
vw=vw ^ 0xffff;
vw=-vw-1;
}
imu_data[4]=0;//Todo: convert vw from raw valeus to "g's"
address=0;//todo: set addres value for accel z value;
vh=wiringPiI2CReadReg8(imu,address);
vl=wiringPiI2CReadReg8(imu,address+1);
vw=(((vh<<8)&0xff00)|(vl&0x00ff))&0xffff;
if(vw>0x8000)
{
vw=vw ^ 0xffff;
vw=-vw-1;
}
imu_data[5]=0;//todo: convert vw from raw values to g's
address=0;//todo: set addres value for gyro x value;
vh=wiringPiI2CReadReg8(imu,address);
vl=wiringPiI2CReadReg8(imu,address+1);
vw=(((vh<<8)&0xff00)|(vl&0x00ff))&0xffff;
if(vw>0x8000)
{
vw=vw ^ 0xffff;
vw=-vw-1;
}
imu_data[0]=x_gyro_calibration+0;////todo: convert vw from raw values to degrees/second
address=0;//todo: set addres value for gyro y value;
vh=wiringPiI2CReadReg8(imu,address);
vl=wiringPiI2CReadReg8(imu,address+1);
vw=(((vh<<8)&0xff00)|(vl&0x00ff))&0xffff;
if(vw>0x8000)
{
vw=vw ^ 0xffff;
vw=-vw-1;
}
imu_data[1]=y_gyro_calibration+0;////todo: convert vw from raw values to degrees/second
address=0;////todo: set addres value for gyro z value;
vh=wiringPiI2CReadReg8(imu,address);
vl=wiringPiI2CReadReg8(imu,address+1);
vw=(((vh<<8)&0xff00)|(vl&0x00ff))&0xffff;
if(vw>0x8000)
{
vw=vw ^ 0xffff;
vw=-vw-1;
}
imu_data[2]=z_gyro_calibration+0;////todo: convert vw from raw values to degrees/second
}
void update_filter()
{
//get current time in nanoseconds
timespec_get(&te,TIME_UTC);
time_curr=te.tv_nsec;
//compute time since last execution
float imu_diff=time_curr-time_prev;
//check for rollover
if(imu_diff<=0)
{
imu_diff+=1000000000;
}
//convert to seconds
imu_diff=imu_diff/1000000000;
time_prev=time_curr;
//comp. filter for roll, pitch here:
}
int setup_imu()
{
wiringPiSetup ();
//setup imu on I2C
imu=wiringPiI2CSetup (0x68) ; //accel/gyro address
if(imu==-1)
{
printf("-----cant connect to I2C device %d --------\n",imu);
return -1;
}
else
{
printf("connected to i2c device %d\n",imu);
printf("imu who am i is %d \n",wiringPiI2CReadReg8(imu,0x75));
uint8_t Ascale = AFS_2G; // AFS_2G, AFS_4G, AFS_8G, AFS_16G
uint8_t Gscale = GFS_500DPS; // GFS_250DPS, GFS_500DPS, GFS_1000DPS, GFS_2000DPS
//init imu
wiringPiI2CWriteReg8(imu,PWR_MGMT_1, 0x00);
printf(" \n\r");
wiringPiI2CWriteReg8(imu,PWR_MGMT_1, 0x01);
wiringPiI2CWriteReg8(imu, CONFIG, 0x00);
wiringPiI2CWriteReg8(imu, SMPLRT_DIV, 0x00); //0x04
int c=wiringPiI2CReadReg8(imu, GYRO_CONFIG);
wiringPiI2CWriteReg8(imu, GYRO_CONFIG, c & ~0xE0);
wiringPiI2CWriteReg8(imu, GYRO_CONFIG, c & ~0x18);
wiringPiI2CWriteReg8(imu, GYRO_CONFIG, c | Gscale << 3);
c=wiringPiI2CReadReg8(imu, ACCEL_CONFIG);
wiringPiI2CWriteReg8(imu, ACCEL_CONFIG, c & ~0xE0); // Clear self-test bits [7:5]
wiringPiI2CWriteReg8(imu, ACCEL_CONFIG, c & ~0x18); // Clear AFS bits [4:3]
wiringPiI2CWriteReg8(imu, ACCEL_CONFIG, c | Ascale << 3);
c=wiringPiI2CReadReg8(imu, ACCEL_CONFIG2);
wiringPiI2CWriteReg8(imu, ACCEL_CONFIG2, c & ~0x0F); //
wiringPiI2CWriteReg8(imu, ACCEL_CONFIG2, c | 0x00);
}
return 0;
}