Add nvs in motorctl: Store and Load accel/decel conf

Changes in menu for accel/decel time are now persistent after
restarts

main: initialize nvs and pass pointer to motorctl task
motorctl:
    - add method to get default configured value
    - add name to config -> adjust logging
    - add methods to read and write msFadeAccel and msFadeDecel from nvs
This commit is contained in:
jonny_jr9
2024-02-20 16:34:42 +01:00
parent dab6a437c0
commit 268018832d
5 changed files with 178 additions and 22 deletions

View File

@@ -90,6 +90,7 @@ sabertooth2x60_config_t sabertoothConfig = {
// TODO add motor name string -> then use as log tag?
//--- configure left motor (contol) ---
motorctl_config_t configMotorControlLeft = {
.name = "left",
.msFadeAccel = 1500, // acceleration of the motor (ms it takes from 0% to 100%)
.msFadeDecel = 1000, // deceleration of the motor (ms it takes from 100% to 0%)
.currentLimitEnabled = false,
@@ -101,6 +102,7 @@ motorctl_config_t configMotorControlLeft = {
//--- configure right motor (contol) ---
motorctl_config_t configMotorControlRight = {
.name = "right",
.msFadeAccel = 1500, // acceleration of the motor (ms it takes from 0% to 100%)
.msFadeDecel = 1000, // deceleration of the motor (ms it takes from 100% to 0%)
.currentLimitEnabled = false,

View File

@@ -4,6 +4,7 @@ extern "C"
#include <esp_system.h>
#include <esp_event.h>
#include <nvs_flash.h>
#include "nvs.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
@@ -93,9 +94,12 @@ esp_err_t on_joystick_url(httpd_req_t *req)
return (httpJoystickMain->*pointerToReceiveFunc)(req);
}
//-- tag for logging --
//--- tag for logging ---
static const char * TAG = "main";
//-- handle passed to tasks for accessing nvs --
nvs_handle_t nvsHandle;
@@ -140,8 +144,8 @@ void createObjects()
// create controlled motor instances (motorctl.hpp)
// with configurations from config.cpp
motorLeft = new controlledMotor(setLeftFunc, configMotorControlLeft);
motorRight = new controlledMotor(setRightFunc, configMotorControlRight);
motorLeft = new controlledMotor(setLeftFunc, configMotorControlLeft, &nvsHandle);
motorRight = new controlledMotor(setRightFunc, configMotorControlRight, &nvsHandle);
// create speedsensor instances
// with configurations from config.cpp
@@ -207,6 +211,22 @@ extern "C" void app_main(void) {
//--- initialize encoder ---
const QueueHandle_t encoderQueue = encoder_init(&encoder_config);
//--- initialize nvs-flash --- (for persistant config values)
ESP_LOGW(TAG, "initializing nvs-flash...");
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_LOGE(TAG, "NVS truncated -> deleting flash");
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
//--- open nvs-flash ---
err = nvs_open("storage", NVS_READWRITE, &nvsHandle);
if (err != ESP_OK)
ESP_LOGE(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err));
printf("\n");