-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_sd_card.c
162 lines (123 loc) · 3.61 KB
/
read_sd_card.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct gpsData{
float latitude;
float longitude;
float speed;
float angle;
float altitude;
char misc;
///////////////////
// code for misc //
///////////////////
// bit 0 -> lat
// bit 1 -> lon
}data_gps;
struct xyzData{
float x;
float y;
float z;
};
struct dataPoint {
struct xyzData acc; // m/s^2 //might be able to get higher resolution by raw data instead
struct xyzData gyro; // rad/s
float prs; // Pa
float tmp; // C
}data_telemetry;
struct basic{
unsigned int time; // ms
unsigned int code;
///////////////////
// code for code //
///////////////////
// bit 0 -> dataPoint -> 1 Data
// bit 1 -> 0 -> no gps fix
// bit 2 -> Address -> 0 if booster, 1 if sustainer
// bit 3 -> event 0 -> 1 if launch happened
// bit 4 -> event 1 -> 1 if separation/sustainer happened
// bit 5 -> event 2 -> 1 if drouge shoot happened
// bit 6 -> event 3 -> 1 if main shoot happened
// bit 7 -> 1 if sending battery info // maybe implement
// INT IS 16 BITS YOU CAN ADD MORE CODES
// bool mybit = ((code >> 3) & 0x01) //stores bit 3 of "code" in mybit
}time_code;
FILE *fp_read;
FILE *fp_write;
int main(int argc, char *argv[]){
struct dataPoint data_telemetry;
char filename_read [250];
char buffer[700];
char little_buf[100];
strcpy(filename_read,argv[1]);
fp_read = fopen(filename_read,"r");
if(fp_read == NULL){
perror("Couldn't find the file");
exit(1);
}
perror("\nFile opened\n"); // not really an error just don't want to print to standard out
printf("Time,Accel_X,Accel_Y,Accel_Z,Gyro_X,Gyro_Y,Gyro_Z,Pressure,Temperature,");
printf("GPS_Fix,Latitude,Longitude,GPS_Speed,GPS_Angle,GPS_Altitude,Events\n");
while(1){
if( 0 == fread(&time_code, sizeof(struct basic),1,fp_read)){
break;
}
printf("%d,",time_code.time);
if(!(time_code.code & (1 << 0))){ // if reading dataPoint
if( 0 == fread(&data_telemetry, sizeof(struct dataPoint),1,fp_read)){
break;
}
printf("%f,",data_telemetry.acc.x);
printf("%f,",data_telemetry.acc.y);
printf("%f,",data_telemetry.acc.z);
printf("%f,",data_telemetry.gyro.x);
printf("%f,",data_telemetry.gyro.y);
printf("%f,",data_telemetry.gyro.z);
printf("%f,",data_telemetry.prs);
printf("%f,",data_telemetry.tmp);
printf("%i," ,((time_code.code >> 1 )) & 0x01); // GPS_fix
}
else{
if( 0 == fread(&data_gps, sizeof(struct gpsData),1,fp_read)){ // if reading gpsData
break;
}
printf(",,,,,,,,%i," ,((time_code.code >> 1 )) & 0x01); // GPS_fix
printf("%f", data_gps.latitude);
if((data_gps.misc >> 0 ) & 0x01){
printf("S,");
}
else{
printf("N,");
}
printf("%f", data_gps.longitude);
if((data_gps.misc >> 1 ) & 0x01){
printf("E,");
}
else{
printf("W,");
}
printf("%f,", data_gps.speed);
printf("%f,", data_gps.angle);
printf("%f,", data_gps.angle);
}
//print events
if((time_code.code & (1 << 6)))
printf("Main_chute");
else if((time_code.code & (1 << 5)))
printf("drouge_chute");
else if((time_code.code & (1 << 4))){
if((time_code.code & (1 << 2)))
printf("sustainer");
else ("seperation");
}
else if((time_code.code & (1 << 3)))
printf("launch");
printf("\n");
}
fclose(fp_read);
return 0;
}
//fwrite (buffer , sizeof(char), sizeof(buffer), fp_write);
// If you want to read raw integers, you need to deal with endian issues.
// One technique is to store in network endian order which means you use
// htonl before writing and ntohl after reading.