Skip to content

Commit

Permalink
it actually fucking works
Browse files Browse the repository at this point in the history
  • Loading branch information
crnicholson committed Dec 1, 2024
1 parent 7a713a7 commit be59fbb
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 252 deletions.
2 changes: 1 addition & 1 deletion Code/autopilot/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct data {
short yaw, pitch, roll;
byte year, month, day, hour, minute, second;
short txCount;
byte abortFlight;
bool abortFlight;
char callSign[7] = CALL_SIGN;
};

Expand Down
2 changes: 1 addition & 1 deletion Code/groundStation/deprecated/groundStation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <SPI.h>
#include <WiFi.h>

struct receive {
struct fromGlider {
float lat, lon, tLat, tLon, altitude, temperature, pressure, humidity, volts, hdop;
short yaw, pitch, roll;
byte hour, minute, second;
Expand Down
16 changes: 13 additions & 3 deletions Code/groundStation/groundStation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "vars.h"
#include "websockets.h"

bool dataValid;
bool dataValid, newPacketForGlider;
long rxCount;

struct receive receiveStruct;
struct fromGlider fromGliderStruct;
struct toGlider toGliderStruct;

TaskHandle_t loop2;

Expand Down Expand Up @@ -98,6 +99,15 @@ void loop() {
// This is using FreeRTOS to poll the Websockets.
void pollWebsockets(void *pvParameters) {
if (client.available()) {
client.poll();
client.poll(); // If there is data, a call back will be run in websockets.cpp. It will add all new data to toGliderStruct.
}
if (newPacketForGlider) {
newPacketForGlider = false;
#ifdef HAMMING
hammingSend();
#endif
#ifndef HAMMING
normalSend();
#endif
}
}
28 changes: 14 additions & 14 deletions Code/groundStation/lora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ void loraSetup() {
}

void normalReceive() {
while (LoRa.available() < sizeof(receiveStruct)) // Wait intil the packet is fully received.
while (LoRa.available() < sizeof(fromGliderStruct)) // Wait intil the packet is fully received.
;

packetSize = LoRa.parsePacket();
if (packetSize == sizeof(receiveStruct)) {
LoRa.readBytes((byte *)&receiveStruct, sizeof(receiveStruct));
if (packetSize == sizeof(fromGliderStruct)) {
LoRa.readBytes((byte *)&fromGliderStruct, sizeof(fromGliderStruct));

dataValid = true;

if (receiveStruct.alignmet != 100) {
if (fromGliderStruct.alignment != 100) {
dataValid = false;
while (LoRa.available())
LoRa.read();
Expand All @@ -77,24 +77,24 @@ void normalSend() {
;

LoRa.beginPacket();
LoRa.write((byte *)&sendStruct, sizeof(sendStruct));
LoRa.write((byte *)&toGliderStruct, sizeof(toGliderStruct));
LoRa.endPacket(true); // Use async send.
}

void hammingReceive() {
while (LoRa.available() < sizeof(receiveStruct) * 2) // Wait intil the packet is fully received.
while (LoRa.available() < sizeof(fromGliderStruct) * 2) // Wait intil the packet is fully received.
;

packetSize = LoRa.parsePacket(); // Parse packet.
if (sizeof(receiveStruct) * 2 == packetSize) {
if (sizeof(fromGliderStruct) * 2 == packetSize) {
shortBlink(LED);

byte encodedData[2 * sizeof(receiveStruct)];
byte encodedData[2 * sizeof(fromGliderStruct)];
LoRa.readBytes(encodedData, sizeof(encodedData));

byte *decodedData = (byte *)&receiveStruct;
byte *decodedData = (byte *)&fromGliderStruct;

for (size_t i = 0; i < sizeof(receiveStruct); ++i) {
for (size_t i = 0; i < sizeof(fromGliderStruct); ++i) {
// Decode each byte using Hamming(7,4) code.
byte highNibble = hammingDecode(encodedData[2 * i]);
byte lowNibble = hammingDecode(encodedData[2 * i + 1]);
Expand All @@ -104,7 +104,7 @@ void hammingReceive() {

dataValid = true;

if (receiveStruct.alignmet != 100) {
if (fromGliderStruct.alignment != 100) {
dataValid = false;
while (LoRa.available())
LoRa.read();
Expand All @@ -118,11 +118,11 @@ void hammingReceive() {
}

void hammingSend() {
byte *byteArray = (byte *)&sendStruct; // Convert the packet to a byte array.
byte *byteArray = (byte *)&toGliderStruct; // Convert the packet to a byte array.

byte encodedData[2 * sizeof(sendStruct)]; // Hamming(7,4) doubles the size!
byte encodedData[2 * sizeof(toGliderStruct)]; // Hamming(7,4) doubles the size!

for (size_t i = 0; i < sizeof(sendStruct); ++i) {
for (size_t i = 0; i < sizeof(toGliderStruct); ++i) {
byte highNibble = hammingEncode((byteArray[i] >> 4) & 0xF);
byte lowNibble = hammingEncode(byteArray[i] & 0xF);

Expand Down
50 changes: 25 additions & 25 deletions Code/groundStation/sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ void sdSetup() {
// }

void writeToCard() {
File dataFile = SD.open(String(receiveStruct) + ".csv", FILE_WRITE); // Create or open the file.
File dataFile = SD.open(String(fromGliderStruct) + ".csv", FILE_WRITE); // Create or open the file.

dataFile.close();

File dataFile = SD.open(String(receiveStruct) + ".csv");
File dataFile = SD.open(String(fromGliderStruct) + ".csv");

if (dataFile.read() != "l") {
dataFile.close();
File dataFile = SD.open(String(receiveStruct) + ".csv", FILE_WRITE);
File dataFile = SD.open(String(fromGliderStruct) + ".csv", FILE_WRITE);
if (dataFile) {
dataFile.println("lat,lon,altitude,tLat,tLon,temperature,pressure,humidity,volts,yaw,pitch,roll,year,month,day,hour,minute,second,abort,txCount,rxCount,uLat,uLon,uAlt,rssi,snr,callsign");
dataFile.close();
Expand All @@ -78,48 +78,48 @@ void writeToCard() {
}
}

File dataFile = SD.open(String(receiveStruct) + ".csv", FILE_WRITE);
File dataFile = SD.open(String(fromGliderStruct) + ".csv", FILE_WRITE);

if (dataFile) {
dataFile.print(receivedData.lat);
dataFile.print(fromGliderStruct.lat);
dataFile.print(",");
dataFile.print(receivedData.lon);
dataFile.print(fromGliderStruct.lon);
dataFile.print(",");
dataFile.print(receivedData.altitude);
dataFile.print(fromGliderStruct.altitude);
dataFile.print(",");
dataFile.print(receivedData.tLat);
dataFile.print(fromGliderStruct.tLat);
dataFile.print(",");
dataFile.print(receivedData.tLon);
dataFile.print(fromGliderStruct.tLon);
dataFile.print(",");
dataFile.print(receivedData.temperature);
dataFile.print(fromGliderStruct.temperature);
dataFile.print(",");
dataFile.print(receivedData.pressure);
dataFile.print(fromGliderStruct.pressure);
dataFile.print(",");
dataFile.print(receivedData.humidity);
dataFile.print(fromGliderStruct.humidity);
dataFile.print(",");
dataFile.print(receivedData.volts);
dataFile.print(fromGliderStruct.volts);
dataFile.print(",");
dataFile.print(receivedData.yaw);
dataFile.print(fromGliderStruct.yaw);
dataFile.print(",");
dataFile.print(receivedData.pitch);
dataFile.print(fromGliderStruct.pitch);
dataFile.print(",");
dataFile.print(receivedData.roll);
dataFile.print(fromGliderStruct.roll);
dataFile.print(",");
dataFile.print(receivedData.year);
dataFile.print(fromGliderStruct.year);
dataFile.print(",");
dataFile.print(receivedData.month);
dataFile.print(fromGliderStruct.month);
dataFile.print(",");
dataFile.print(receivedData.day);
dataFile.print(fromGliderStruct.day);
dataFile.print(",");
dataFile.print(receivedData.hour);
dataFile.print(fromGliderStruct.hour);
dataFile.print(",");
dataFile.print(receivedData.minute);
dataFile.print(fromGliderStruct.minute);
dataFile.print(",");
dataFile.print(receivedData.second);
dataFile.print(fromGliderStruct.second);
dataFile.print(",");
dataFile.print(receivedData.abort);
dataFile.print(fromGliderStruct.abort);
dataFile.print(",");
dataFile.print(receivedData.txCount);
dataFile.print(fromGliderStruct.txCount);
dataFile.print(",");
dataFile.print(rxCount);
dataFile.print(",");
Expand All @@ -133,7 +133,7 @@ void writeToCard() {
dataFile.print(",");
dataFile.print(snr);
dataFile.print(",");
dataFile.println(receivedData.callSign);
dataFile.println(fromGliderStruct.callSign);

dataFile.close(); // Close the file.
#ifdef DEVMODE
Expand Down
3 changes: 3 additions & 0 deletions Code/groundStation/uploader/data/KC1SFR.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lat,Lon,Alt,Target lat,Target lon,Yaw,Pitch,Roll,Time,Temperature,Pressure,Humidity,Voltage,Received abort,TX count,RX count,RSSI,SNR,Uploader lat,Uploader lon,Uploader alt,ID
42.306705,-71.336548,1500.5,42.307,-71.335,45.0,2.5,0.0,2024-11-30T14:45:12Z,23.4,1013.25,55.3,3.7,False,102,95,-70,12.5,42.3601,-71.0589,20.0,1234
42.306705,-71.336548,1500.5,42.307,-71.335,45.0,2.5,0.0,2024-11-30T14:45:13Z,23.4,1013.25,55.3,3.7,False,102,95,-70,12.5,42.3601,-71.0589,20.0,1234
15 changes: 15 additions & 0 deletions Code/groundStation/uploader/echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import asyncio
import websockets


async def echo(websocket):
async for message in websocket:
await websocket.send(message)


async def main():
async with websockets.serve(echo, "localhost", 8080):
await asyncio.Future() # run forever


asyncio.run(main())
Loading

0 comments on commit be59fbb

Please sign in to comment.