-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprg.c
201 lines (188 loc) · 5.87 KB
/
prg.c
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
#include<stdio.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<linux/i2c.h>
#include<linux/i2c-dev.h>
#include <signal.h>
#include <math.h>
//#include <chrono>
//#include "MahonyAHRS/MahonyAHRS.h"
#define DEVID 0x00
#define BUFFER_SIZE 40
#define OUT_X_L 0x28
#define OUT_X_H 0x29
#define OUT_Y_L 0x2A
#define OUT_Y_H 0x2B
#define OUT_Z_L 0x2C
#define OUT_Z_H 0x2D
#define OUT_X_L_M 0x08
#define OUT_X_H_M 0x09
#define OUT_Y_L_M 0x0A
#define OUT_Y_H_M 0x0B
#define OUT_Z_L_M 0x0C
#define OUT_Z_H_M 0x0D
//extern volatile float q0, q1, q2, q3;
FILE *myoutput = NULL;
int finished = 0;
int main_counter = 0;
int binTwosComplementToSignedDecimal(int mynum,int significantBits)
{
char binary[9];
int power = pow(2,significantBits-1);
int sum = 0;
int i, j;
binary[8] = 0;
for (i = 0; i < 8; ++i) {
if (((mynum >> i) & 1) == 0) {
binary[7-i] = '0';
} else {
binary[7-i] = '1';
}
}
//printf("%s vs %d\n", binary, mynum);
for (i=0; i<significantBits; ++i)
{
if ( i==0 && binary[i]!='0')
{
sum = power * -1;
}
else
{
sum += (binary[i]-'0')*power;//The -0 is needed
}
power /= 2;
}
return sum;
}
void close_handle(int s) {
finished = 1;
}
char readFromAddress(int file, int addr);
void writeSomeData(int file, char *buff, int n) {
if (write(file, buff, n) != n) {
perror("Failed to write\n");
}
}
int getintval(char xl, char xh) {
return (binTwosComplementToSignedDecimal(xh, 8) << 8) | binTwosComplementToSignedDecimal(xl, 8);
}
void readAccelMagn(int file, int *gyrX, int *gyrY, int *gyrZ, int *magX, int *magY, int *magZ) {
if(ioctl(file, I2C_SLAVE, 0x1D) < 0){
perror("Failed to connect to the sensor\n");
}
char xl = readFromAddress(file, OUT_X_L);
char xh = readFromAddress(file, OUT_X_H);
*gyrX = getintval(xl, xh);
xl = readFromAddress(file, OUT_Y_L);
xh = readFromAddress(file, OUT_Y_H);
*gyrY = getintval(xl, xh);
xl = readFromAddress(file, OUT_Z_L);
xh = readFromAddress(file, OUT_Z_H);
*gyrZ = getintval(xl, xh);
//printf("accel (x, y, z): (%d, %d, %d)\n", gyrX, gyrY, gyrZ);
xl = readFromAddress(file, OUT_X_L_M);
xh = readFromAddress(file, OUT_X_H_M);
*magX = getintval(xl, xh);
xl = readFromAddress(file, OUT_Y_L_M);
xh = readFromAddress(file, OUT_Y_H_M);
*magY = getintval(xl, xh);
xl = readFromAddress(file, OUT_Z_L_M);
xh = readFromAddress(file, OUT_Z_H_M);
*magZ = getintval(xl, xh);
//printf("magn (x, y, z): (%d, %d, %d)\n", gyrX, gyrY, gyrZ);
}
void readGyro(int file, int *gyrX, int *gyrY, int *gyrZ) {
if(ioctl(file, I2C_SLAVE, 0x6B) < 0){
perror("Failed to connect to the sensor\n");
}
char xl = readFromAddress(file, OUT_X_L);
char xh = readFromAddress(file, OUT_X_H);
*gyrX = getintval(xl, xh);
xl = readFromAddress(file, OUT_Y_L);
xh = readFromAddress(file, OUT_Y_H);
*gyrY = getintval(xl, xh);
xl = readFromAddress(file, OUT_Z_L);
xh = readFromAddress(file, OUT_Z_H);
*gyrZ = getintval(xl, xh);
//printf("gyro (x, y, z): (%d, %d, %d)\n", gyrX, gyrY, gyrZ);
}
void setupGyro(int file) {
if(ioctl(file, I2C_SLAVE, 0x6B) < 0){
perror("Failed to connect to the sensor\n");
}
char writeBuffer[2] = {0x20, 0x8F};
writeSomeData(file, writeBuffer, 2);
writeBuffer[0] = 0x21;
writeBuffer[1] = 0x20;
writeSomeData(file, writeBuffer, 2);
}
void setupAxelMagn(int file) {
if(ioctl(file, I2C_SLAVE, 0x1D) < 0){
perror("Failed to connect to the sensor\n");
}
char writeBuffer[2] = {0x20, 0x8F};
writeSomeData(file, writeBuffer, 2);
writeBuffer[0] = 0x21;
writeBuffer[1] = 0x18;
writeSomeData(file, writeBuffer, 2);
writeBuffer[0] = 0x24;
writeBuffer[1] = 0x64;
writeSomeData(file, writeBuffer, 2);
writeBuffer[0] = 0x25;
writeBuffer[1] = 0x20;
writeSomeData(file, writeBuffer, 2);
writeBuffer[0] = 0x26;
writeBuffer[1] = 0x00;
writeSomeData(file, writeBuffer, 2);
}
int main(){
int file;
signal(SIGINT, close_handle);
myoutput = fopen("output.txt", "w");
fprintf(myoutput, "accx, accy, accz, magx, magy, magz, gyrx, gyry, gyrz");
if((file=open("/dev/i2c-2", O_RDWR)) < 0){
perror("failed to open the bus\n");
return 1;
}
// setup
printf("setting up gyro\n");
setupGyro(file);
printf("setting up accel\n");
setupAxelMagn(file);
int counter = 0;
int gyrx = 0, gyry = 0, gyrz = 0, accx = 0, accy = 0, accz = 0, magx = 0, magy = 0, magz = 0;
int lastAx = 0, lastAy = 0, lastAz = 0, lastmx = 0, lastmy = 0, lastmz = 0;
while (finished == 0) {
readGyro(file, &gyrx, &gyry, &gyrz);
readAccelMagn(file, &accx, &accy, &accz, &magx, &magy, &magz);
//MahonyAHRSupdate((float)gyrx, (float)gyry, (float)gyrz, (float)accx, (float)accy, (float)accz, (float)magx, (float)magy, (float)magz);
if ((lastAx != accx || lastAy != accy || lastAz != accz) || (lastmx != magx || lastmy != magy || lastmz != magz)) {
fprintf(myoutput, "%d, %d, %d, %d, %d ,%d, %d, %d, %d\n", accx, accy, accz, magx, magy, magx, gyrx, gyry, gyrz);
//printf("%d, %d, %d, %d, %d ,%d, %d, %d, %d\n", accx, accy, accz, magx, magy, magx, gyrx, gyry, gyrz);
++main_counter;
lastAx = accx;
lastAy = accy;
lastAz = accz;
lastmx = magx;
lastmy = magy;
lastmz = magz;
}
}
close(file);
fclose(myoutput);
printf("total counts %d\n", main_counter);
return 0;
}
char readFromAddress(int file, int addr) {
char writeBuffer = addr;
if(write(file, &writeBuffer, 1)!=1){
perror("Failed to reset the read address\n");
return 1;
}
char readBuffer[1];
if(read(file, readBuffer, 1)!=1){
perror("Failed to read in the buffer\n");
return 1;
}
return readBuffer[0];
}