-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCANSAT-telemetry.ino
More file actions
265 lines (205 loc) · 5.87 KB
/
CANSAT-telemetry.ino
File metadata and controls
265 lines (205 loc) · 5.87 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <Wire.h> //importing libraries for all sensors
#include <Adafruit_BMP280.h>
#include <DFRobot_BMX160.h>
#include <SoftwareSerial.h>
SoftwareSerial XBee(34, 35);
#include <Servo.h>
#include <TimeLib.h>
#include <SD.h>
#define BMP280_ADDRESS 0x76
Adafruit_BMP280 bmp;
DFRobot_BMX160 bmx160;
char command;
bool telemetry = 1;
bool simulation = 0;
String sim_pr;
bool timeset = 0;
bool new_pressure=0;
Servo s;
float d;
int countn = 0;
float dR;
float dP;
Servo BLDC;
float start_BLDC;
int pressure[3] = {0, 0, 0}; //will be used as queue to determine S/W states acc. to pressure and altitude readings each second
float altitudes[3] = {0, 0, 0}; //new value pushed each second (with each data packet)
int cam = 32; //mini spy cam
bool vid = 0;
const int buzzer = 9;
File Kalpana_Test;
bool logged = 0;
String packet = "";
String prev_packet = "";
int simulated_pressure;
float calibration = 1013.25; //calibration for BMP altitude
int SWstate = 0;
SoftwareSerial mySerial(16, 17); //GNSS Tx and Rx, mySerial is GNSS object
int cnt = 0; // packet count
void setup() {
BLDC.attach(6); //ESC activation
delay(1);
BLDC.write(10);
delay(5000);
Serial.begin(9600);
XBee.begin(9600);
mySerial.begin(9600);
mySerial.println("$PMTK220,1000*5F");
pinMode(cam, OUTPUT);
digitalWrite(cam, HIGH);
// setTime(0, 0, 0, 24, 5, 2023);
unsigned status;
status = bmp.begin(BMP280_ADDRESS);
bmx160.begin();
//Serial.println("TEAM_ID,TIME_STAMPING,PACKET_COUNT,ALTITUDE,PRESSURE,TEMP,VOLTAGE,GNSS_TIME,GNSS_LATITUDE,GNSS_LONGITUDE,GNSS_ALTITUDE,GNSS_SATS,ACC_R,ACC_P,ACC_Y,GYRO_R,GYRO_P,GYRO_Y,FLIGHT_SOFTWARE_STATE");
// if (!status) {
// while (1);
// delay(10);
// }
//
// if (!bmx160.begin()) {
// Serial.println("NO BMX found");
// while (1);
// }
SD.begin(BUILTIN_SDCARD);
// for (int j = 0; j < 10; j++) { //calibration by taking mean of 10 BMP pressure readings
// calibration += bmp.readPressure();
// delay(50);
// }
// calibration /= 1000;
// calibration/=10;
// calibration=bmp.readPressure();
// calibration=bmp.readPressure();
// calibration/=100;
// for (int j = 0; j < 300; j++) { //calibration of bmx
// sBmx160SensorData_t Omagn, Ogyro, Oaccel;
// bmx160.getAllData(&Omagn, &Ogyro, &Oaccel);
// delay(50);
// }
s.attach(33); //servo pwm pin
pinMode(buzzer, OUTPUT);
Kalpana_Test = SD.open("Flight_2022ASI049.csv", FILE_WRITE);
if (Kalpana_Test && logged == 0) {
Kalpana_Test.println("TEAM_ID,TIME_STAMPING,PACKET_COUNT,ALTITUDE,PRESSURE,TEMP,VOLTAGE,GNSS_TIME,GNSS_LATITUDE,GNSS_LONGITUDE,GNSS_ALTITUDE,GNSS_SATS,ACC_R,ACC_P,ACC_Y,GYRO_R,GYRO_P,GYRO_Y,FLIGHT_SOFTWARE_STATE");
logged = 1;
}
Kalpana_Test.close();
}
// with GCS commands
void loop() {
if (XBee.available()) {
command = XBee.read();
if(command=='x'){
telemetry = 1;
}
}
if (command == 'c') { //calibrate
calibration = 0.00;
for (int j = 0; j < 10; j++) { //calibration by taking mean of 10 BMP pressure readings
calibration += bmp.readPressure();
delay(50);
}
calibration /= 1000;
// for (int j = 0; j < 10; j++) { //calibration of bmx
// sBmx160SensorData_t Omagn, Ogyro, Oaccel;
// bmx160.getAllData(&Omagn, &Ogyro, &Oaccel);
// delay(50);
// }
}
else if (command == 'x') { // telemetry on/off
// telemetry = 1;
setTime(0, 0, 0, 24, 5, 2023);
start_BLDC = millis();
while (telemetry == 1) {
data_packet();
delay(10);
}
}
else if (command == 'e') { // sim enable
SWstate = 1;
simulation = 1;
}
else if (command == 'a') { // sim activate
setTime(0, 0, 0, 24, 5, 2023);
packet = "";
cnt=-1;
while(simulation==1){
packet = "";
cnt++;
packet += "<2022ASI-049";
packet += ",";
Timestamp();
packet += String(cnt) + ",";
sim_bmp();
Voltage();
Quectel_L89();
BMX();
for(int i=0;i<6;i++){
packet += 0.0;
packet += ",";
}
packet+="1>\r";
if (new_pressure){
new_pressure=0;
Data_Log(); // logging packet to sd card for backup
Xbeetx();
}
if (XBee.available()) {
command = XBee.read();
if (command == 'd') { // sim disable
simulation = 0;
}
}
}
}
}
void data_packet() { //data_packet function name with GCS commands
telemetry = 1;
cnt=-1;
while (telemetry && SWstate != 7 ) { // until the CanSat makes impact, telemetry is sent
if (timeset == 0) {
setTime(0, 0, 0, 24, 5, 2023);
timeset = 1;
}
packet = "";
cnt++;
if (cnt == 7380) { //breadboard testing camera, ON at 2 hr 3 mins ..7380
s.write(180);
ON_camera();
BLDC.write(80);
}
if (cnt == 7980) { //breadboard testing camera, OFF after 10 mins reccording..7860+120
OFF_camera();
BLDC.write(0);
while (cnt < 11580) { //bb test..buzzer should play upto 3 mins only...11460
cnt++;
analogWrite(buzzer, 10000);
delay(500);
analogWrite(buzzer, 0);
delay(500);
}
}
packet += "<2022ASI-049";
packet += ",";
Timestamp();
packet += String(cnt) + ",";
BMP_280();
Voltage();
Quectel_L89();
// BMX();
for(int i=0;i<6;i++){
packet += 0.0;
packet +=",";
}
SW_state();
Data_Log(); // logging packet to sd card for backup
Xbeetx();
if (XBee.available()) {
command = XBee.read();
if (command == 'x') {
telemetry = 0 ;
}
}
// Serial.println(packet); // printing packet to serial monitor
}
}