From b7a4568f07a9d82e4f27a2d1acfe2f69c47910c3 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Thu, 2 May 2024 22:29:11 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Removes=20notification=20event?= =?UTF-8?q?=20service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/lib/components/Visualization.svelte | 2 +- app/src/lib/stores/analytics.ts | 2 +- app/src/lib/stores/model-store.ts | 4 +- esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp | 1 + esp32/lib/ESP32-sveltekit/MotionService.h | 73 +++++++++++++------ .../ESP32-sveltekit/NotificationEvents.cpp | 61 ---------------- .../lib/ESP32-sveltekit/NotificationEvents.h | 45 ------------ esp32/src/ActuatorStateService.cpp | 4 +- esp32/src/ActuatorStateService.h | 6 +- 9 files changed, 59 insertions(+), 139 deletions(-) delete mode 100644 esp32/lib/ESP32-sveltekit/NotificationEvents.cpp delete mode 100644 esp32/lib/ESP32-sveltekit/NotificationEvents.h diff --git a/app/src/lib/components/Visualization.svelte b/app/src/lib/components/Visualization.svelte index b1c6500..bab7b18 100644 --- a/app/src/lib/components/Visualization.svelte +++ b/app/src/lib/components/Visualization.svelte @@ -73,7 +73,7 @@ const updateAngles = (name: string, angle: number) => { modelTargetAngles[$jointNames.indexOf(name)] = angle * (180 / Math.PI); - Throttler.throttle(() => servoAnglesOut.set(modelTargetAngles), 100) + Throttler.throttle(() => servoAnglesOut.set(modelTargetAngles.map(num => Math.round(num))), 100) }; const createScene = async () => { diff --git a/app/src/lib/stores/analytics.ts b/app/src/lib/stores/analytics.ts index d931276..f00f8b2 100644 --- a/app/src/lib/stores/analytics.ts +++ b/app/src/lib/stores/analytics.ts @@ -15,7 +15,7 @@ let analytics_data = { cpu_usage: [] }; -const maxAnalyticsData = 1000; // roughly 33 Minutes of data at 1 update per 2 seconds +const maxAnalyticsData = 100; function createAnalytics() { const { subscribe, update } = writable(analytics_data); diff --git a/app/src/lib/stores/model-store.ts b/app/src/lib/stores/model-store.ts index 7b41f9f..ee02291 100644 --- a/app/src/lib/stores/model-store.ts +++ b/app/src/lib/stores/model-store.ts @@ -26,6 +26,6 @@ export const outControllerData = writable(new Array([0, 0, 0, 0, 0, 70, 0])); export const input: Writable = writable({ left: { x: 0, y: 0 }, right: { x: 0, y: 0 }, - height: 70, - speed: 0 + height: 50, + speed: 50 }); diff --git a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp index 49c9e96..276a78a 100644 --- a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp +++ b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp @@ -195,6 +195,7 @@ void IRAM_ATTR ESP32SvelteKit::_loop() { #if FT_ENABLED(FT_ANALYTICS) _analyticsService.loop(); #endif + _motionService.loop(); vTaskDelay(20 / portTICK_PERIOD_MS); } } diff --git a/esp32/lib/ESP32-sveltekit/MotionService.h b/esp32/lib/ESP32-sveltekit/MotionService.h index 4a17ca4..5258b9b 100644 --- a/esp32/lib/ESP32-sveltekit/MotionService.h +++ b/esp32/lib/ESP32-sveltekit/MotionService.h @@ -10,8 +10,6 @@ #define INPUT_EVENT "input" #define MODE_EVENT "mode" -#define MOTION_INTERVAL 100 - enum class MOTION_STATE { IDLE, @@ -40,18 +38,7 @@ class MotionService _socket->onEvent(ANGLES_EVENT, [&](JsonObject &root, int originId) { anglesEvent(root, originId); }); _socket->onSubscribe(ANGLES_EVENT, - std::bind(&MotionService::syncState, this, std::placeholders::_1, std::placeholders::_2)); - } - - void syncState(const String &originId, bool sync = false) - { - DynamicJsonDocument jsonDocument{200}; - char output[200]; - JsonObject root = jsonDocument.to(); - root["angles"] = angles; - serializeJson(root, output); - ESP_LOGV("MotionState", "Syncing state: %s", output); - _socket->emit(ANGLES_EVENT, output, originId.c_str()); + std::bind(&MotionService::syncAngles, this, std::placeholders::_1, std::placeholders::_2)); } void anglesEvent(JsonObject &root, int originId) @@ -61,19 +48,12 @@ class MotionService { angles[i] = array[i]; } - char output[100]; - serializeJson(array, output); - sprintf(output, "[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]", angles[0], angles[1], angles[2], angles[3], angles[4], - angles[5], angles[6], angles[7], angles[8], angles[9], angles[10], angles[11]); - _socket->emit(ANGLES_EVENT, output, String(originId).c_str()); + syncAngles(String(originId)); } void handleInput(JsonObject &root, int originId) { - String jsonString; JsonArray array = root["data"].as(); - serializeJson(array, jsonString); - ESP_LOGI("MotionService", "%s", jsonString.c_str()); for (int i = 0; i < 7; i++) { input[i] = array[i]; @@ -90,14 +70,63 @@ class MotionService _socket->emit(MODE_EVENT, output, String(originId).c_str()); } + void syncAngles(const String &originId = "", bool sync = false) { + char output[100]; + sprintf(output, "[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]", angles[0], angles[1], angles[2], angles[3], angles[4], + angles[5], angles[6], angles[7], angles[8], angles[9], angles[10], angles[11]); + _socket->emit(ANGLES_EVENT, output, String(originId).c_str()); + + } + + int lerp(int start, int end, float t) { + return (1 - t) * start + t * end; + } + + bool updateMotion() { + bool updated = false; + switch (motionState) { + case MOTION_STATE::IDLE: + break; + case MOTION_STATE::REST: + for (int i = 0; i < 12; i++) { + int16_t new_angle = lerp(angles[i], rest_angles[i], 0.5); + if (new_angle != angles[i]) { + angles[i] = new_angle; + updated = true; + } + } + break; + case MOTION_STATE::WALK: + angles[1] += dir; + if (angles[1] >= 90) dir = -1; + if (angles[1] <= 0) dir = 1; + updated = true; + break; + } + return updated; + } + + void loop() { + if (auto currentMillis = millis(); !_lastUpdate || (currentMillis - _lastUpdate) >= MotionInterval) { + _lastUpdate = currentMillis; + if (updateMotion()) syncAngles(); + } + } + private: PsychicHttpServer *_server; EventSocket *_socket; SecurityManager *_securityManager; TaskManager *_taskManager; + + constexpr static int MotionInterval = 100; + int8_t input[7] = {0, 0, 0, 0, 0, 0, 0}; int16_t angles[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + int16_t rest_angles[12] = {0, 90, -145, 0, 90, -145, 0, 90, -145, 0, 90, -145}; MOTION_STATE motionState = MOTION_STATE::IDLE; + unsigned long _lastUpdate; + int dir = 2; }; #endif diff --git a/esp32/lib/ESP32-sveltekit/NotificationEvents.cpp b/esp32/lib/ESP32-sveltekit/NotificationEvents.cpp deleted file mode 100644 index aa018f0..0000000 --- a/esp32/lib/ESP32-sveltekit/NotificationEvents.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -/** - * ESP32 SvelteKit - * - * A simple, secure and extensible framework for IoT projects for ESP32 platforms - * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. - * https://github.com/theelims/ESP32-sveltekit - * - * Copyright (C) 2023 theelims - * - * All Rights Reserved. This software may be modified and distributed under - * the terms of the LGPL v3 license. See the LICENSE file for details. - **/ - -#include - -NotificationEvents::NotificationEvents(PsychicHttpServer *server) : _server(server) -{ -} - -void NotificationEvents::begin() -{ - _eventSource.onOpen([&](PsychicEventSourceClient *client) { // client->send("hello", NULL, millis(), 1000); - Serial.printf("New client connected to Event Source: #%u connected from %s\n", client->socket(), client->remoteIP().toString()); - }); - _eventSource.onClose([&](PsychicEventSourceClient *client) { // client->send("hello", NULL, millis(), 1000); - Serial.printf("Client closed connection to Event Source: #%u connected from %s\n", client->socket(), client->remoteIP().toString()); - }); - _server->on(EVENT_NOTIFICATION_SERVICE_PATH, &_eventSource); - - ESP_LOGV("NotificationEvents", "Registered Event Source endpoint: %s", EVENT_NOTIFICATION_SERVICE_PATH); -} - -void NotificationEvents::pushNotification(String message, pushEvent event, int id) -{ - String eventType; - switch (event) - { - case (PUSHERROR): - eventType = "errorToast"; - break; - case (PUSHWARNING): - eventType = "warningToast"; - break; - case (PUSHINFO): - eventType = "infoToast"; - break; - case (PUSHSUCCESS): - eventType = "successToast"; - break; - default: - return; - } - _eventSource.send(message.c_str(), eventType.c_str(), id); -} - -void NotificationEvents::send(String message, String event, int id) -{ - _eventSource.send(message.c_str(), event.c_str(), id); -} diff --git a/esp32/lib/ESP32-sveltekit/NotificationEvents.h b/esp32/lib/ESP32-sveltekit/NotificationEvents.h deleted file mode 100644 index d33193e..0000000 --- a/esp32/lib/ESP32-sveltekit/NotificationEvents.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -/** - * ESP32 SvelteKit - * - * A simple, secure and extensible framework for IoT projects for ESP32 platforms - * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. - * https://github.com/theelims/ESP32-sveltekit - * - * Copyright (C) 2023 theelims - * - * All Rights Reserved. This software may be modified and distributed under - * the terms of the LGPL v3 license. See the LICENSE file for details. - **/ - -#include - -#include -#include - -#define EVENT_NOTIFICATION_SERVICE_PATH "/events" - -enum pushEvent -{ - PUSHERROR, - PUSHWARNING, - PUSHINFO, - PUSHSUCCESS -}; - -class NotificationEvents -{ -protected: - PsychicHttpServer *_server; - PsychicEventSource _eventSource; - -public: - NotificationEvents(PsychicHttpServer *server); - - void begin(); - - void pushNotification(String message, pushEvent event, int id = 0); - - void send(String message, String event, int id = 0); -}; diff --git a/esp32/src/ActuatorStateService.cpp b/esp32/src/ActuatorStateService.cpp index 35f46e6..e39a583 100644 --- a/esp32/src/ActuatorStateService.cpp +++ b/esp32/src/ActuatorStateService.cpp @@ -16,7 +16,6 @@ ActuatorStateService::ActuatorStateService( PsychicHttpServer *server, - NotificationEvents *notificationEvents, SecurityManager *securityManager ) : _httpEndpoint( ActuatorState::read, @@ -33,8 +32,7 @@ ActuatorStateService::ActuatorStateService( server, ACTUATOR_SETTINGS_SOCKET_PATH, securityManager, - AuthenticationPredicates::IS_AUTHENTICATED), - _notificationEvents(notificationEvents) + AuthenticationPredicates::IS_AUTHENTICATED) { // Setup actuator hardware diff --git a/esp32/src/ActuatorStateService.h b/esp32/src/ActuatorStateService.h index 39ad5c9..37071d2 100644 --- a/esp32/src/ActuatorStateService.h +++ b/esp32/src/ActuatorStateService.h @@ -17,7 +17,6 @@ #include #include -#include #define ACTUATOR_SETTINGS_ENDPOINT_PATH "/rest/actuators" #define ACTUATOR_SETTINGS_SOCKET_PATH "/ws" @@ -101,10 +100,9 @@ public: class ActuatorStateService : public StatefulService { public: - ActuatorStateService(PsychicHttpServer *server, NotificationEvents *notificationEvents, SecurityManager *securityManager); + ActuatorStateService(PsychicHttpServer *server, SecurityManager *securityManager); void begin(); protected: - NotificationEvents *_notificationEvents; static void _loopImpl(void *_this) { static_cast(_this)->_loop(); } void _loop() { @@ -125,7 +123,7 @@ class ActuatorStateService : public StatefulService { doc["mode"] = (int)_state.motionState; serializeJson(doc, message); - _notificationEvents->send(message, "motion", millis()); + // _notificationEvents->send(message, "motion", millis()); vTaskDelayUntil(&xLastWakeTime, MOTION_INTERVAL / portTICK_PERIOD_MS); } };