This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUNO.ino
186 lines (164 loc) · 3.88 KB
/
UNO.ino
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
#include <Wire.h> //blue wire on 2 pin connector to analog 4
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include "IntersemaBaro.h"
const int chipSelect = 10;
//%%%%%%%%%%%%%%%%%%%%%%%% ALTIMETER SETUP %%%%%%%%%%%%%%%%%%%%%%%%//
Intersema::BaroPressure_MS5607B baro(true);
// Altitude variables
float avg_alt;
float alt0;
float altitude;
float old_Alt;
float new_Alt;
unsigned long T0;
unsigned long T1;
unsigned long T;
//create state machine for deployment stages
enum states{
None,
Setup,
Launchpad,
Thrust,
Descent,
Deployment
};
//set initial state
states flight = None;
//states timer = None;
//%%%%%%%%%%%%%%%%%%%%%%%% RECOVERY PIN SETUP %%%%%%%%%%%%%%%%%%%%%%%%//
//const int SAFETY = 7;
const int SEP = 5;
const int R = 3;
//%%%%%%%%%%%%%%%%%%%%%%%% PAYLOAD PIN SETUP %%%%%%%%%%%%%%%%%%%%%%%%//
const int PL = 8;
int loop_counter = 0;
void setup() {
//begin communication
Serial.begin(9600); //set to 115200 when using GPS
pinMode(R, OUTPUT);
pinMode(SEP, OUTPUT);
//pinMode(SAFETY, INPUT);
pinMode(PL, OUTPUT);
digitalWrite(R, LOW);
digitalWrite(SEP, LOW);
digitalWrite(PL, LOW);
Serial.println("Booted up");
//altimeter
baro.init();
Serial.println("Initialized");
//set starting altitude
alt0 = 0;
altitude = 0;
avg_alt = 0;
int num_points = 50;
for (int i=0; i<num_points; i++)
{
alt0 += baro.getHeightCentiMeters()/30.48;
delay(10);
}
alt0 /= num_points; //normalize to the number of samples collected
//set initial states
flight = Setup;
//timer = None;
//initialize datalogging
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
}
else{
File dataFile = SD.open("Flight.txt", FILE_WRITE);
if (dataFile){
dataFile.println("Beginning New Flight");
dataFile.close();
Serial.println("New Datalog Created");
}
else{
Serial.println("error opening file Flight.txt");
return;
}
Serial.println("Initialization Complete");
}
}
void loop() {
////////// Altimeter //////////
altitude = baro.getHeightCentiMeters()/30.48 - alt0;
avg_alt += (altitude - avg_alt)/5;
////////// Recovery and Payload Activation //////////
//ALTMETER BASED DEPLOYMENT LOOP//
switch(flight) {
case Setup:
Serial.println("SETUP");
if(avg_alt > 10){
flight = Launchpad;
}
break;
case Launchpad:
Serial.println("LAUNCHPAD");
old_Alt = avg_alt;
if(old_Alt > 50){
T0 = millis();
flight = Thrust;
//timer = Thrust;
}
break;
case Thrust:
Serial.println("THRUST");
new_Alt = avg_alt;
T = millis();
if ((new_Alt - old_Alt)/(millis() - T) < 0){
digitalWrite(PL, HIGH);
digitalWrite(SEP, HIGH);
flight = Descent;
}
old_Alt = new_Alt;
break;
case Descent:
Serial.println("DESCENT");
if (avg_alt < 1600){
digitalWrite(R, HIGH);
Serial.println("DEPLOYMENT");
}
break;
case Deployment:
Serial.println("DEPLOYED");
Serial.println("Waiting to be recovered...");
delay(5000);
break;
}
//TIMER BASED DEPLOYMENT LOOP//
/* switch(timer){
case Thrust:
T1 = millis();
if(T1 > T0 + 20000){
digitalWrite(SEP, HIGH);
digitalWrite(PL, HIGH);
T0 = millis();
timer = Descent;
}
break;
case Descent:
T1 = millis();
if (T1 > T0 + 60000){
digitalWrite(R, HIGH);
}
}
*/
//Debug//
if (loop_counter % 20 == 0) {
Serial.println("Altitude:");
Serial.println(avg_alt);
}
loop_counter++;
//datalogging
String data = "";
data = altitude;
File dataFile = SD.open("Flight.txt", FILE_WRITE);
if (dataFile){
dataFile.println(data);
dataFile.close();
}
else{
Serial.println("error opening file Flight.txt");
}
}