Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions WST-FC/include/communicationModules/CommunicationWebSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef COMMUNICATIONWEBSOCKET_H
#define COMMUNICATIONWEBSOCKET_H

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "DroneData.h"
#include "Configuration.h"
#include "ICommunicationInterface.h"

class CommunicationWebSocket : public ICommunicationInterface {
private:
static CommunicationWebSocket* instance;
DroneControlData *sharedData;
DroneStatus *droneStatus;

unsigned long lastUpdate{0};
unsigned long lastDataTime = 0;

AsyncWebServer server;
AsyncWebSocket ws;

public:
CommunicationWebSocket(DroneControlData *dataPtr, DroneStatus *status);

void Init() override;
void Loop() override;
void SendData(SensorsData* data) override;
void SendData(DroneControlData* data);


static void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len);

void checkConnectionTimeout();
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class ICommunicationInterface{
virtual void Init();
virtual void Loop();
virtual void SendData(SensorsData* data);
virtual void SendData(DroneControlData* data);
};
#endif
4 changes: 4 additions & 0 deletions WST-FC/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ lib_deps =
br3ttb/PID@^1.2.1
adafruit/Adafruit ADXL345@^1.3.4
adafruit/Adafruit Unified Sensor@^1.1.14
esp32async/AsyncTCP@^3.4.10
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super! Dobrze że jest to async!

esp32async/ESPAsyncWebServer@^3.10.3

[env:esp32-c3-super-mini]
platform = espressif32
Expand All @@ -38,3 +40,5 @@ lib_deps =
br3ttb/PID@^1.2.1
adafruit/Adafruit ADXL345@^1.3.4
adafruit/Adafruit Unified Sensor@^1.1.14
esp32async/AsyncTCP@^3.4.10
esp32async/ESPAsyncWebServer@^3.10.3
64 changes: 64 additions & 0 deletions WST-FC/src/communicationModules/CommunicationWebSocket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "Configuration.h"
#include "communicationModules/CommunicationWebSocket.h"
CommunicationWebSocket* CommunicationWebSocket::instance = nullptr;
CommunicationWebSocket::CommunicationWebSocket(DroneControlData *dataPtr, DroneStatus *status)
: server(80), ws("/ws")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Przenieś protokół do pliku z konfiguracją. Obecnie będziemy mieli konflikt portów niestety

{
sharedData = dataPtr;
droneStatus = status;
instance = this;
}
void CommunicationWebSocket::Init(){
ws.onEvent(onWsEvent);
server.addHandler(&ws);
server.begin();
Serial.println("WebSocket server started on port 80");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Konkatenuj zmieenną z numerem portu - użytkownik może chcieć zmienić

}
void CommunicationWebSocket::Loop(){
checkConnectionTimeout();
ws.cleanupClients();
}
void CommunicationWebSocket::SendData(SensorsData* data){
ws.textAll((char*)data, sizeof(SensorsData));
}
void CommunicationWebSocket::checkConnectionTimeout() {
if (millis() - lastDataTime > 500) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nie zakładaj sztywnego czasu. W pliku konfiguracyjnym mamy zdefiniowany rogue time

if (sharedData != nullptr) {

sharedData->throttle = 0;
sharedData->yaw = 0;
sharedData->pitch = 0;
sharedData->roll = 0;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nie możemy w locie nagle wyłączyć silników. Do tego mamy metodę z odpowiednią procedurą. Póki co jest ona pusta, ale planowo będzie odpowiadała za powolne wyłączanie silników tak aby nie zagrażały nikomu

}
}
}
void CommunicationWebSocket::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void *arg, uint8_t *data, size_t len)
{
if (type == WS_EVT_CONNECT) {
Serial.printf("[WS] Client connected! ID: %u\n", client->id());
}
else if (type == WS_EVT_DISCONNECT) {
Serial.printf("[WS] Client disconnected! ID: %u\n", client->id());

if (instance && instance->sharedData) {
instance->sharedData->throttle = 0;
instance->sharedData->yaw = 0;
instance->sharedData->pitch = 0;
instance->sharedData->roll = 0;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rozłączenie nie może powodować nagłego wyłączenia silników. Raz że mamy od tego specjalną funkcję a dwa rozłączenie może być chwilowe

}
}
else if (type == WS_EVT_DATA) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;

if (info->final && info->index == 0 && info->len == len && info->opcode == WS_BINARY) {

if (len == sizeof(DroneControlData) && instance && instance->sharedData) {

memcpy(instance->sharedData, data, sizeof(DroneControlData));

instance->lastDataTime = millis();
}
}
}
}