💫 Initial plans for device configuration service

This commit is contained in:
Rune Harlyk
2024-05-30 22:10:11 +02:00
committed by Rune Harlyk
parent 83a9007b51
commit d1567fa2dd
17 changed files with 15236 additions and 14556 deletions
+30
View File
@@ -0,0 +1,30 @@
import { type IMU } from '$lib/types/models';
import { writable } from 'svelte/store';
let imu_data = {
x: <number[]>[],
y: <number[]>[],
z: <number[]>[],
temp: <number[]>[]
};
const maxIMUData = 100;
function createIMU() {
const { subscribe, update } = writable(imu_data);
return {
subscribe,
addData: (content: IMU) => {
update((imu_data) => ({
...imu_data,
x: [...imu_data.x, content.x].slice(-maxIMUData),
y: [...imu_data.y, content.y].slice(-maxIMUData),
z: [...imu_data.z, content.z].slice(-maxIMUData),
temp: [...imu_data.temp, content.temp].slice(-maxIMUData)
}));
}
};
}
export const imu = createIMU();
+7
View File
@@ -135,3 +135,10 @@ export type StaticSystemInformation = {
}; };
export type SystemInformation = Analytics & StaticSystemInformation; export type SystemInformation = Analytics & StaticSystemInformation;
export type IMU = {
x: number;
y: number;
z: number;
temp: number;
};
+1 -14
View File
@@ -40,10 +40,6 @@
socket.on('close', handleClose); socket.on('close', handleClose);
socket.on('error', handleError); socket.on('error', handleError);
socket.on('rssi', handleNetworkStatus); socket.on('rssi', handleNetworkStatus);
socket.on('infoToast', handleInfoToast);
socket.on('successToast', handleSuccessToast);
socket.on('warningToast', handleWarningToast);
socket.on('errorToast', handleErrorToast);
socket.on('mode', (data:ModesEnum) => mode.set(data)); socket.on('mode', (data:ModesEnum) => mode.set(data));
socket.on('angles', (angles:number[]) => { if (angles.length) servoAngles.set(angles)}); socket.on('angles', (angles:number[]) => { if (angles.length) servoAngles.set(angles)});
if ($page.data.features.analytics) socket.on('analytics', handleAnalytics); if ($page.data.features.analytics) socket.on('analytics', handleAnalytics);
@@ -56,10 +52,6 @@
socket.off('open', handleOpen); socket.off('open', handleOpen);
socket.off('close', handleClose); socket.off('close', handleClose);
socket.off('rssi', handleNetworkStatus); socket.off('rssi', handleNetworkStatus);
socket.off('infoToast', handleInfoToast);
socket.off('successToast', handleSuccessToast);
socket.off('warningToast', handleWarningToast);
socket.off('errorToast', handleErrorToast);
socket.off('battery', handleBattery); socket.off('battery', handleBattery);
socket.off('otastatus', handleOAT); socket.off('otastatus', handleOAT);
}; };
@@ -83,12 +75,7 @@
const handleError = (data: any) => console.error(data); const handleError = (data: any) => console.error(data);
const handleInfoToast = (data: string) => notifications.info(data, 5000); const handleAnalytics = (data: Analytics) => analytics.addData(data);
const handleWarningToast = (data: string) => notifications.warning(data, 5000);
const handleErrorToast = (data: string) => notifications.error(data, 5000);
const handleSuccessToast = (data: string) => notifications.success(data, 5000);
const handleAnalytics = (data: Analytics) => analytics.addData(data);
const handleNetworkStatus = (data: number) => telemetry.setRSSI(data); const handleNetworkStatus = (data: number) => telemetry.setRSSI(data);
+7
View File
@@ -6,6 +6,7 @@
import MdiController from '~icons/mdi/controller'; import MdiController from '~icons/mdi/controller';
import Devices from '~icons/mdi/devices' import Devices from '~icons/mdi/devices'
import Camera from '~icons/mdi/camera-outline'; import Camera from '~icons/mdi/camera-outline';
import Rotate3d from '~icons/mdi/rotate-3d';
import Health from '~icons/mdi/stethoscope'; import Health from '~icons/mdi/stethoscope';
import Folder from '~icons/mdi/folder-outline'; import Folder from '~icons/mdi/folder-outline';
import Update from '~icons/mdi/reload'; import Update from '~icons/mdi/reload';
@@ -63,6 +64,12 @@
icon: Camera, icon: Camera,
href: '/peripherals/camera', href: '/peripherals/camera',
feature: $page.data.features.camera, feature: $page.data.features.camera,
},
{
title: 'IMU',
icon: Rotate3d,
href: '/peripherals/imu',
feature: $page.data.features.imu,
} }
] ]
}, },
@@ -0,0 +1,7 @@
<script lang="ts">
import IMU from './imu.svelte';
</script>
<div class="mx-0 my-1 flex flex-col space-y-4 sm:mx-8 sm:my-8">
<IMU />
</div>
+7
View File
@@ -0,0 +1,7 @@
import type { PageLoad } from './$types';
export const load = (async () => {
return {
title: 'IMU'
};
}) satisfies PageLoad;
+143
View File
@@ -0,0 +1,143 @@
<script lang="ts">
import SettingsCard from "$lib/components/SettingsCard.svelte";
import Rotate3d from '~icons/mdi/rotate-3d';
import IMUSetting from './imuSetting.svelte';
import { imu } from '$lib/stores/imu';
import { Chart, registerables } from 'chart.js';
import { cubicOut } from "svelte/easing";
import { slide } from "svelte/transition";
import { onDestroy, onMount } from "svelte";
import { daisyColor } from "$lib/DaisyUiHelper";
import { socket } from "$lib/stores";
import type { IMU } from "$lib/types/models";
Chart.register(...registerables);
let heapChartElement: HTMLCanvasElement;
let heapChart: Chart;
const handleImu = (data: IMU) => imu.addData(data);
onMount(() => {
socket.on('imu', handleImu);
heapChart = new Chart(heapChartElement, {
type: 'line',
data: {
// labels: $imu.x,
datasets: [
{
label: 'x',
borderColor: daisyColor('--p'),
backgroundColor: daisyColor('--p', 50),
borderWidth: 2,
data: $imu.x,
yAxisID: 'x'
},
{
label: 'y',
borderColor: daisyColor('--s'),
backgroundColor: daisyColor('--s', 50),
borderWidth: 2,
data: $imu.y,
yAxisID: 'y'
},
{
label: 'z',
borderColor: daisyColor('--a'),
backgroundColor: daisyColor('--a', 50),
borderWidth: 2,
data: $imu.z,
yAxisID: 'z'
}
]
},
options: {
maintainAspectRatio: false,
responsive: true,
plugins: {
legend: {
display: true
},
tooltip: {
mode: 'index',
intersect: false
}
},
elements: {
point: {
radius: 1
}
},
scales: {
x: {
grid: {
color: daisyColor('--bc', 10)
},
ticks: {
color: daisyColor('--bc')
},
display: false
},
y: {
type: 'linear',
title: {
display: true,
text: 'Angle [°]',
color: daisyColor('--bc'),
font: {
size: 16,
weight: 'bold'
}
},
position: 'left',
min: 0,
max: 10,
grid: { color: daisyColor('--bc', 10) },
ticks: { color: daisyColor('--bc') },
border: { color: daisyColor('--bc', 10) }
}
}
}
});
setInterval(() => {
updateData(), 200;
});
})
onDestroy(() => {
socket.off('imu', handleImu);
})
const updateData = () => {
// heapChart.data.labels = $imu.x;
heapChart.options.scales!.y!.min = Math.min(Math.min(...$imu.x), Math.min(...$imu.y), Math.min(...$imu.z));
heapChart.options.scales!.y!.max = Math.max(Math.max(...$imu.x), Math.max(...$imu.y), Math.max(...$imu.z));
heapChart.data.datasets[0].data = $imu.x;
heapChart.data.datasets[1].data = $imu.y;
heapChart.data.datasets[2].data = $imu.z;
heapChart.update('none');
}
</script>
<SettingsCard collapsible={false}>
<Rotate3d slot="icon" class="lex-shrink-0 mr-2 h-6 w-6 self-end" />
<span slot="title">IMU</span>
<!-- <div class="flex flex-col">
{#if $imu.x.length > 0}
{#each Object.entries($imu) as [key, value]}
<div>{key}: {value[value.length-1]}</div>
{/each}
{/if}
</div> -->
<div class="w-full overflow-x-auto">
<div
class="flex w-full flex-col space-y-1 h-60"
transition:slide|local={{ duration: 300, easing: cubicOut }}
>
<canvas bind:this={heapChartElement} />
</div>
</div>
<!-- <IMUSetting /> -->
</SettingsCard>
+1
View File
@@ -8,3 +8,4 @@ build_flags =
-D FT_UPLOAD_FIRMWARE=0 -D FT_UPLOAD_FIRMWARE=0
-D FT_DOWNLOAD_FIRMWARE=0 -D FT_DOWNLOAD_FIRMWARE=0
-D FT_ANALYTICS=1 -D FT_ANALYTICS=1
-D FT_IMU=1
@@ -0,0 +1,109 @@
#pragma once
#include <EventEndpoint.h>
#include <FSPersistence.h>
#include <HttpEndpoint.h>
#include <SecurityManager.h>
#include <StatefulService.h>
#include <SPI.h>
#include <Wire.h>
#define DEVICE_CONFIG_FILE "/config/deviceConfig.json"
#define EVENT_CONFIGURATION_SETTINGS "ConfigurationSettings"
#define CONFIGURATION_SETTINGS_PATH "/api/configuration"
/*
* OLED Settings
*/
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_RESET -1
/*
* I2C software connection
*/
#define SDA 14
#define SCL 15
/*
* Ultra sonic sensors
*/
#define USS_LEFT 12
#define USS_RIGHT 13
#define USS_MAX_DISTANCE 200
class PinConfig {
public:
int pin;
String mode;
String type;
String role;
PinConfig(int p, String m, String t, String r) : pin(p), mode(m), type(t), role(r) {}
};
class DeviceConfiguration {
public:
int sda = SDA;
int scl = SCL;
std::vector<PinConfig> pins;
static void read(DeviceConfiguration &settings, JsonObject &root) {
root["sda"] = settings.sda;
root["scl"] = settings.scl;
}
static StateUpdateResult update(JsonObject &root, DeviceConfiguration &settings) {
settings.sda = root["sda"] | SDA;
settings.scl = root["scl"] | SCL;
return StateUpdateResult::CHANGED;
};
};
class DeviceConfigurationService : public StatefulService<DeviceConfiguration> {
public:
DeviceConfigurationService(PsychicHttpServer *server, FS *fs,
SecurityManager *securityManager,
EventSocket *socket)
: _server(server),
_securityManager(securityManager),
_httpEndpoint(DeviceConfiguration::read, DeviceConfiguration::update,
this, server, CONFIGURATION_SETTINGS_PATH,
securityManager, AuthenticationPredicates::IS_ADMIN),
_eventEndpoint(DeviceConfiguration::read, DeviceConfiguration::update,
this, socket, EVENT_CONFIGURATION_SETTINGS),
_fsPersistence(DeviceConfiguration::read, DeviceConfiguration::update,
this, fs, DEVICE_CONFIG_FILE)
{
addUpdateHandler([&](const String &originId) { updatePins(); },
false);
};
void begin() {
_httpEndpoint.begin();
_eventEndpoint.begin();
_fsPersistence.readFromFS();
updatePins();
};
void updatePins() {
if (i2c_active){
Wire.end();
}
if (_state.sda != -1 && _state.scl != -1) {
Wire.begin(_state.sda, _state.scl);
i2c_active = true;
}
}
private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;
HttpEndpoint<DeviceConfiguration> _httpEndpoint;
EventEndpoint<DeviceConfiguration> _eventEndpoint;
FSPersistence<DeviceConfiguration> _fsPersistence;
bool i2c_active = false;
};
+12 -3
View File
@@ -64,8 +64,10 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server,
_factoryResetService(server, &ESPFS, &_securitySettingsService), _factoryResetService(server, &ESPFS, &_securitySettingsService),
_systemStatus(server, &_securitySettingsService), _systemStatus(server, &_securitySettingsService),
_fileExplorer(server, &_securitySettingsService), _fileExplorer(server, &_securitySettingsService),
_motionService(_server, &_socket, &_securitySettingsService, _motionService(_server, &_socket, &_securitySettingsService,&_taskManager),
&_taskManager) { _deviceConfiguration(server, &ESPFS, &_securitySettingsService, &_socket),
_imuService(&_socket)
{
} }
void ESP32SvelteKit::begin() { void ESP32SvelteKit::begin() {
@@ -89,7 +91,7 @@ void ESP32SvelteKit::begin() {
void ESP32SvelteKit::setupServer() { void ESP32SvelteKit::setupServer() {
_server->config.max_uri_handlers = _numberEndpoints; _server->config.max_uri_handlers = _numberEndpoints;
_server->listen(80); _server->listen(100);
#ifdef EMBED_WWW #ifdef EMBED_WWW
ESP_LOGV("ESP32SvelteKit", ESP_LOGV("ESP32SvelteKit",
@@ -204,6 +206,10 @@ void ESP32SvelteKit::startServices() {
#if FT_ENABLED(FT_CAMERA) #if FT_ENABLED(FT_CAMERA)
_cameraService.begin(); _cameraService.begin();
_cameraSettingsService.begin(); _cameraSettingsService.begin();
#endif
_deviceConfiguration.begin();
#if FT_ENABLED(FT_IMU)
_imuService.begin();
#endif #endif
} }
@@ -216,6 +222,9 @@ void IRAM_ATTR ESP32SvelteKit::_loop() {
#endif #endif
#if FT_ENABLED(FT_ANALYTICS) #if FT_ENABLED(FT_ANALYTICS)
_analyticsService.loop(); _analyticsService.loop();
#endif
#if FT_ENABLED(FT_IMU)
_imuService.loop();
#endif #endif
_motionService.loop(); _motionService.loop();
vTaskDelay(20 / portTICK_PERIOD_MS); vTaskDelay(20 / portTICK_PERIOD_MS);
@@ -25,11 +25,13 @@
#include <BatteryService.h> #include <BatteryService.h>
#include <FileExplorerService.h> #include <FileExplorerService.h>
#include <DownloadFirmwareService.h> #include <DownloadFirmwareService.h>
#include <DeviceConfigurationService.h>
#include <ESPFS.h> #include <ESPFS.h>
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <EventSocket.h> #include <EventSocket.h>
#include <FactoryResetService.h> #include <FactoryResetService.h>
#include <FeaturesService.h> #include <FeaturesService.h>
#include <IMUService.h>
#include <MqttSettingsService.h> #include <MqttSettingsService.h>
#include <MqttStatus.h> #include <MqttStatus.h>
#include <MotionService.h> #include <MotionService.h>
@@ -166,17 +168,30 @@ public:
{ {
return &_motionService; return &_motionService;
} }
#if FT_ENABLED(FT_CAMERA) #if FT_ENABLED(FT_CAMERA)
CameraService *getCameraService() CameraService *getCameraService()
{ {
return &_cameraService; return &_cameraService;
} }
CameraSettingsService *getCameraSettingsService() CameraSettingsService *getCameraSettingsService()
{ {
return &_cameraSettingsService; return &_cameraSettingsService;
} }
#endif #endif
DeviceConfigurationService *getDeviceConfigurationService()
{
return &_deviceConfiguration;
}
#if FT_ENABLED(FT_IMU)
IMUService *getIMUService()
{
return &_imuService;
}
#endif
void factoryReset() void factoryReset()
{ {
_factoryResetService.factoryReset(); _factoryResetService.factoryReset();
@@ -239,6 +254,10 @@ private:
CameraService _cameraService; CameraService _cameraService;
CameraSettingsService _cameraSettingsService; CameraSettingsService _cameraSettingsService;
#endif #endif
DeviceConfigurationService _deviceConfiguration;
#if FT_ENABLED(FT_IMU)
IMUService _imuService;
#endif
String _appName = APP_NAME; String _appName = APP_NAME;
+5
View File
@@ -62,4 +62,9 @@
#define FT_CAMERA 0 #define FT_CAMERA 0
#endif #endif
// ESP32 IMU on by default
#ifndef FT_IMU
#define FT_IMU 1
#endif
#endif #endif
@@ -31,6 +31,7 @@ void FeaturesService::begin() {
root["battery"] = FT_BATTERY; root["battery"] = FT_BATTERY;
root["analytics"] = FT_ANALYTICS; root["analytics"] = FT_ANALYTICS;
root["camera"] = FT_CAMERA; root["camera"] = FT_CAMERA;
root["imu"] = FT_IMU;
root["firmware_version"] = APP_VERSION; root["firmware_version"] = APP_VERSION;
root["firmware_name"] = APP_NAME; root["firmware_name"] = APP_NAME;
root["firmware_built_target"] = BUILD_TARGET; root["firmware_built_target"] = BUILD_TARGET;
+54 -19
View File
@@ -1,55 +1,90 @@
#pragma once #pragma once
#include <MPU6050_light.h> #include <MPU6050_light.h>
#include <ArduinoJson.h>
#include <EventSocket.h>
#define IMU_INTERVAL 2000 #define IMU_INTERVAL 200
#define MAX_ESP_IMU_SIZE 250
#define EVENT_IMU "imu"
class IMUService class IMUService
{ {
public: public:
IMUService():_imu(Wire){}; IMUService(EventSocket *socket) : _socket(socket), _imu(Wire) {};
void begin() void begin()
{ {
byte status = _imu.begin(); byte status = _imu.begin();
imu_success = status == 0;
if(status != 0) { if(status != 0) {
ESP_LOGE("IMUService", "MPU initialize failed"); ESP_LOGE("IMUService", "MPU initialize failed: %d", status);
vTaskDelete(NULL);
return;
} }
xTaskCreatePinnedToCore(this->_loopImpl, "IMU Service", 4096, this, tskIDLE_PRIORITY, NULL, ESP32SVELTEKIT_RUNNING_CORE); _socket->registerEvent(EVENT_IMU);
calibrate();
}; };
bool isIMUSuccess() {
return imu_success;
}
float getTemp() { float getTemp() {
return _imu.getTemp(); return imu_success ? _imu.getTemp() : -1;
} }
float getAngleX() { float getAngleX() {
return _imu.getAngleX(); return imu_success ? _imu.getAngleX() : -1;
} }
float getAngleY() { float getAngleY() {
return _imu.getAngleX(); return imu_success ? _imu.getAngleX() : -1;
} }
float getAngleZ() { float getAngleZ() {
return _imu.getAngleZ(); return imu_success ? _imu.getAngleZ() : -1;
} }
protected: void calibrate() {
static void _loopImpl(void *_this) { static_cast<IMUService *>(_this)->_loop(); } if (imu_success) {
void _loop() _imu.calcOffsets(true, true);
}
}
double round2(double value) {
return (int)(value * 100 + 0.5) / 100.0;
}
void loop()
{ {
vTaskDelay(100); unsigned long currentMillis = millis();
_imu.calcOffsets(true,true);
TickType_t xLastWakeTime = xTaskGetTickCount(); if (currentMillis - _lastUpdate >= _updateInterval)
while (1)
{ {
_imu.update(); _lastUpdate = currentMillis;
vTaskDelayUntil(&xLastWakeTime, IMU_INTERVAL / portTICK_PERIOD_MS); updateImu();
} }
}; };
protected:
JsonDocument doc;
void updateImu() {
_imu.update();
doc.clear();
doc["x"] = round2(getAngleX());
doc["y"] = round2(getAngleY());
doc["z"] = round2(getAngleZ());
doc["temp"] = round2(getTemp());
serializeJson(doc, message);
_socket->emit(EVENT_IMU, message);
}
private: private:
MPU6050 _imu; MPU6050 _imu;
EventSocket *_socket;
unsigned long _lastUpdate {0};
unsigned long _updateInterval {IMU_INTERVAL};
bool imu_success {false};
char message[MAX_ESP_IMU_SIZE];
}; };
@@ -0,0 +1,30 @@
#include <Adafruit_PWMServoDriver.h>
#include <DeviceConfigurationService.h>
class ServoController : public Adafruit_PWMServoDriver {
public:
ServoController(DeviceConfigurationService deviceConfigurationService)
: Adafruit_PWMServoDriver(), _config(deviceConfigurationService) {
begin();
}
void configure() {
setOscillatorFrequency(_config.servo_oscillator_frequency());
setPWMFreq(_config.servo_pwm_frequency());
}
void deactivate() {
isActive = false;
sleep();
}
void activate() {
isActive = true;
sleep();
}
bool isActive{false};
private:
DeviceConfigurationService _config;
};
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -39,15 +39,15 @@ lib_deps =
ArduinoJson@>=7.0.0 ArduinoJson@>=7.0.0
https://github.com/theelims/PsychicMqttClient.git#0.1.1 https://github.com/theelims/PsychicMqttClient.git#0.1.1
teckel12/NewPing@^1.9.7 teckel12/NewPing@^1.9.7
rfetick/MPU6050_light@^1.1.0
adafruit/Adafruit SSD1306@^2.5.7 adafruit/Adafruit SSD1306@^2.5.7
adafruit/Adafruit GFX Library@^1.11.5 adafruit/Adafruit GFX Library@^1.11.5
adafruit/Adafruit BusIO@^1.9.3 adafruit/Adafruit BusIO@^1.9.3
rfetick/MPU6050_light@^1.1.0 adafruit/Adafruit PWM Servo Driver Library@^2.4.1
;adafruit/Adafruit HMC5883 Unified@^1.2.1
SPI SPI
; thomasfredericks/Bounce2@ ^2.7.0 ; thomasfredericks/Bounce2@ ^2.7.0
; adafruit/Adafruit PWM Servo Driver Library@^2.4.1
; adafruit/Adafruit ADS1X15@^2.4.0 ; adafruit/Adafruit ADS1X15@^2.4.0
; adafruit/Adafruit HMC5883 Unified@^1.2.1
; adafruit/Adafruit Unified Sensor@^1.1.11 ; adafruit/Adafruit Unified Sensor@^1.1.11
; plageoj/UrlEncode@ ^1.0.1 ; plageoj/UrlEncode@ ^1.0.1
; board_build.partitions = config/no_oat.csv ; board_build.partitions = config/no_oat.csv