-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRocket Code
More file actions
215 lines (176 loc) · 6.49 KB
/
Rocket Code
File metadata and controls
215 lines (176 loc) · 6.49 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
/*
ROCKET TRACKER: ULTIMATE FLIGHT EDITION (FINAL PIN FIX)
- GPS Pins reverted to 33/34
- Break-Wire on Pin 6 (Unplug to Pop)
*/
#include <RadioLib.h>
#include <SPI.h>
#include <TinyGPSPlus.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <FS.h>
#include <LittleFS.h>
// --- HELTEC PINS ---
#define TFT_CS 38
#define TFT_RST 39
#define TFT_DC 40
#define TFT_SCLK 41
#define TFT_MOSI 42
#define TFT_BL 21
#define BAT_ADC_PIN 1
#define BAT_CTRL_PIN 2
#define VEXT_PIN 3
// *** REVERTED TO YOUR WORKING PINS ***
#define GPS_RX_PIN 33
#define GPS_TX_PIN 34
#define LED_PIN 18
// --- SENSOR PIN ---
#define POP_PIN 6 // Connect to GND for "Safe". Disconnect for "POP".
// --- LORA PINS ---
#define LORA_NSS 8
#define LORA_RST 12
#define LORA_BUSY 13
#define LORA_DIO1 14
// --- OBJECTS ---
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
SX1262 radio = new Module(LORA_NSS, LORA_DIO1, LORA_RST, LORA_BUSY);
TinyGPSPlus localGps;
HardwareSerial GPS_Serial(1);
// --- VARIABLES ---
int txInterval = 500;
unsigned long lastTx = 0;
float batteryVoltage = 0.0;
float maxAlt = -1000.0;
bool popTriggered = false;
// --- BATTERY READ ---
// --- FIXED BATTERY FUNCTION ---
float readBattery() {
// 1. Turn ON the switch (Try HIGH for V1.1)
digitalWrite(BAT_CTRL_PIN, HIGH);
delay(20); // Give it time to stabilize
// 2. Read
uint16_t raw = analogRead(BAT_ADC_PIN);
// 3. Turn OFF (Save power)
digitalWrite(BAT_CTRL_PIN, LOW);
// 4. Math Fix: Removed the "/ 2.4"
// Formula: (Raw / 4095) * RefVoltage(3.3) * Divider(4.9)
return (raw / 4095.0) * 3.3 * 4.90;
}
void setup() {
Serial.begin(115200);
// 1. Hardware Init
pinMode(VEXT_PIN, OUTPUT); digitalWrite(VEXT_PIN, HIGH);
pinMode(TFT_BL, OUTPUT); digitalWrite(TFT_BL, HIGH);
pinMode(LED_PIN, OUTPUT);
pinMode(BAT_CTRL_PIN, OUTPUT); digitalWrite(BAT_CTRL_PIN, HIGH);
pinMode(POP_PIN, INPUT_PULLUP);
// 2. Init Storage
if(!LittleFS.begin(true)){ Serial.println("FS Fail"); }
File file = LittleFS.open("/flight.csv", "w");
if(file) { file.println("Time,Lat,Lon,Alt,Speed,Drop"); file.close(); }
// 3. Init Screen
tft.initR(INITR_MINI160x80_PLUGIN);
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
tft.setTextWrap(false);
tft.setTextColor(ST77XX_MAGENTA); tft.setCursor(0, 0); tft.print("ROCKET TRACKER");
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(0, 20); tft.print("GPS:");
tft.setCursor(0, 35); tft.print("Lat:");
tft.setCursor(0, 45); tft.print("Lon:");
tft.setCursor(0, 60); tft.print("Alt:");
tft.setCursor(0, 70); tft.print("Spd:");
// 4. Init GPS & LoRa
// FIX: Using 33/34
GPS_Serial.begin(115200, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
int state = radio.begin(915.0);
if (state == RADIOLIB_ERR_NONE) {
radio.setSpreadingFactor(10); radio.setBandwidth(250.0); radio.setCodingRate(7); radio.setOutputPower(22);
tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK); tft.setCursor(100, 0); tft.print("SYS OK");
} else {
tft.setTextColor(ST77XX_RED, ST77XX_BLACK); tft.setCursor(100, 0); tft.print("RAD FAIL");
}
}
// Helper declared before use
void updateValues(String lastMsg, float speed);
void loop() {
// --- SERIAL COMMANDS ---
if(Serial.available()){
String cmd = Serial.readStringUntil('\n'); cmd.trim();
if(cmd == "dump"){
Serial.println("--- LOG START ---");
File file = LittleFS.open("/flight.csv", "r");
if(file){ while(file.available()) Serial.write(file.read()); file.close(); }
Serial.println("--- LOG END ---");
}
if(cmd == "force drop"){ popTriggered = true; Serial.println(">> FORCE DROP SENT!"); }
if(cmd == "reset"){ popTriggered = false; Serial.println(">> RESET OK."); }
}
while (GPS_Serial.available() > 0) localGps.encode(GPS_Serial.read());
// --- CHECK BREAK WIRE ---
if (digitalRead(POP_PIN) == HIGH) {
popTriggered = true;
}
if (millis() - lastTx > txInterval) {
lastTx = millis();
digitalWrite(LED_PIN, HIGH);
batteryVoltage = readBattery();
float speed = localGps.speed.mph();
float currentAlt = localGps.altitude.feet();
if (currentAlt > maxAlt) maxAlt = currentAlt;
if (maxAlt > 300 && (maxAlt - currentAlt) > 150) popTriggered = true;
// --- BUILD PACKET ---
// Insert your callsign, then delete this comment String msg = "Your_Callsign_Here,";
if (localGps.location.isValid()) {
msg += String(localGps.location.lat(), 5) + ",";
msg += String(localGps.location.lng(), 5) + ",";
msg += String(currentAlt, 0);
} else {
msg += "0.0,0.0,0";
}
msg += "," + String(speed, 1);
msg += "," + String(batteryVoltage, 2);
msg += "," + String(popTriggered ? 1 : 0);
// --- SEND & LOG ---
radio.transmit(msg);
Serial.println(msg);
File file = LittleFS.open("/flight.csv", "a");
if(file){ file.println(String(millis()) + "," + msg); file.close(); }
updateValues(msg, speed);
digitalWrite(LED_PIN, LOW);
}
}
void updateValues(String lastMsg, float speed) {
// 1. Satellites
if (localGps.location.isValid()) tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);
else tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
tft.setCursor(35, 20); tft.print(localGps.satellites.value()); tft.print(" ");
// 2. Lat/Lon
tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
tft.setCursor(30, 35);
if(localGps.location.isValid()) tft.print(localGps.location.lat(), 4);
else tft.print("Search...");
tft.setCursor(30, 45);
if(localGps.location.isValid()) tft.print(localGps.location.lng(), 4);
// 3. Altitude
tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK);
tft.setCursor(30, 60);
if(localGps.location.isValid()) { tft.print(localGps.altitude.feet(), 0); tft.print("ft "); }
else { tft.print("0ft "); }
// 4. SPEED
tft.setTextColor(ST77XX_ORANGE, ST77XX_BLACK);
tft.setCursor(30, 70); tft.print(speed, 1); tft.print("mph ");
// 5. BATTERY
if (batteryVoltage > 3.7) tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);
else if (batteryVoltage > 3.5) tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
else tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
tft.setCursor(110, 20); tft.print(batteryVoltage, 1); tft.print("V");
// 6. POP INDICATOR
if (popTriggered) {
tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
tft.setCursor(100, 70); tft.print("POP!");
} else {
tft.setTextColor(ST77XX_BLACK, ST77XX_BLACK);
tft.setCursor(100, 70); tft.print("POP!");
}
}