Making an ESP32-based quadcopter from scratch
Python arduinoautopilotcontrol-theorydroneesp32flight-controlflight-controllermavlinkquadcopterquadcopter-firmwareuav
Make motor parameters apply without reboot Add callback to parameter definition to call after parameter is changed.
Oleg Kalacheva687303062d7
diff --git a/flix/motors.ino b/flix/motors.inoindex 1dee5fa..42cb0c5 100644--- a/flix/motors.ino+++ b/flix/motors.ino@@ -24,6 +24,7 @@ void setupMotors() {// configure pinsfor (int i = 0; i < 4; i++) {ledcAttach(motorPins[i], pwmFrequency, pwmResolution);+ pwmFrequency = ledcChangeFrequency(motorPins[i], pwmFrequency, pwmResolution); // when reconfiguring}sendMotors();print("Motors initialized\n");diff --git a/flix/parameters.ino b/flix/parameters.inoindex 83ecbbb..cef683f 100644--- a/flix/parameters.ino+++ b/flix/parameters.ino@@ -19,8 +19,9 @@ struct Parameter {bool integer;union { float *f; int *i; }; // pointer to the variablefloat cache; // what's stored in flash- Parameter(const char *name, float *variable) : name(name), integer(false), f(variable) {};- Parameter(const char *name, int *variable) : name(name), integer(true), i(variable) {};+ void (*callback)(); // called after parameter change+ Parameter(const char *name, float *variable, void (*callback)() = nullptr) : name(name), integer(false), f(variable), callback(callback) {};+ Parameter(const char *name, int *variable, void (*callback)() = nullptr) : name(name), integer(true), i(variable), callback(callback) {};float getValue() const { return integer ? *i : *f; };void setValue(const float value) { if (integer) *i = value; else *f = value; };};@@ -67,12 +68,12 @@ Parameter parameters[] = {{"EST_ACC_WEIGHT", &accWeight},{"EST_RATES_LPF_A", &ratesFilter.alpha},// motors- {"MOT_PIN_FL", &motorPins[MOTOR_FRONT_LEFT]},- {"MOT_PIN_FR", &motorPins[MOTOR_FRONT_RIGHT]},- {"MOT_PIN_RL", &motorPins[MOTOR_REAR_LEFT]},- {"MOT_PIN_RR", &motorPins[MOTOR_REAR_RIGHT]},- {"MOT_PWM_FREQ", &pwmFrequency},- {"MOT_PWM_RES", &pwmResolution},+ {"MOT_PIN_FL", &motorPins[MOTOR_FRONT_LEFT], setupMotors},+ {"MOT_PIN_FR", &motorPins[MOTOR_FRONT_RIGHT], setupMotors},+ {"MOT_PIN_RL", &motorPins[MOTOR_REAR_LEFT], setupMotors},+ {"MOT_PIN_RR", &motorPins[MOTOR_REAR_RIGHT], setupMotors},+ {"MOT_PWM_FREQ", &pwmFrequency, setupMotors},+ {"MOT_PWM_RES", &pwmResolution, setupMotors},{"MOT_PWM_STOP", &pwmStop},{"MOT_PWM_MIN", &pwmMin},{"MOT_PWM_MAX", &pwmMax},@@ -152,6 +153,7 @@ bool setParameter(const char *name, const float value) {if (strcasecmp(parameter.name, name) == 0) {if (parameter.integer && !isfinite(value)) return false; // can't set integer to NaN or Infparameter.setValue(value);+ if (parameter.callback) parameter.callback();return true;}}diff --git a/gazebo/Arduino.h b/gazebo/Arduino.hindex 1ca8089..ad4bef4 100644--- a/gazebo/Arduino.h+++ b/gazebo/Arduino.h@@ -165,6 +165,7 @@ void delay(uint32_t ms) {bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { return true; }bool ledcWrite(uint8_t pin, uint32_t duty) { return true; }+uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution) { return freq; }unsigned long __micros;unsigned long __resetTime = 0;