diff --git a/docs/usage.md b/docs/usage.md index d6d2669..77d7b6b 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -143,8 +143,6 @@ If using brushless motors and ESCs: 1. Set the appropriate PWM using the parameters: `MOT_PWM_STOP`, `MOT_PWM_MIN`, and `MOT_PWM_MAX` (1000, 1000, and 2000 is typical). 2. Decrease the PWM frequency using the `MOT_PWM_FREQ` parameter (400 is typical). -Reboot the drone to apply the changes. - > [!CAUTION] > **Remove the props when configuring the motors!** If improperly configured, you may not be able to stop them. diff --git a/flix/cli.ino b/flix/cli.ino index bd67c30..3250d0e 100644 --- a/flix/cli.ino +++ b/flix/cli.ino @@ -94,7 +94,7 @@ void doCommand(String str, bool echo = false) { } else if (command == "p") { bool success = setParameter(arg0.c_str(), arg1.toFloat()); if (success) { - print("%s = %g\n", arg0.c_str(), arg1.toFloat()); + print("%s = %g\n", arg0.c_str(), getParameter(arg0.c_str())); } else { print("Parameter not found: %s\n", arg0.c_str()); } diff --git a/flix/motors.ino b/flix/motors.ino index 1dee5fa..cb0ee23 100644 --- a/flix/motors.ino +++ b/flix/motors.ino @@ -24,6 +24,7 @@ void setupMotors() { // configure pins for (int i = 0; i < 4; i++) { ledcAttach(motorPins[i], pwmFrequency, pwmResolution); + pwmFrequency = ledcChangeFrequency(motorPins[i], pwmFrequency, pwmResolution); // if re-initializing } sendMotors(); print("Motors initialized\n"); diff --git a/flix/parameters.ino b/flix/parameters.ino index 77aa21a..b009c1a 100644 --- a/flix/parameters.ino +++ b/flix/parameters.ino @@ -120,6 +120,11 @@ void setupParameters() { } } +void afterParameterChange(String name, const float value) { + if (name == "MOT_PWM_FREQ" || name == "MOT_PWM_RES") setupMotors(); + if (name == "MOT_PIN_FL" || name == "MOT_PIN_FR" || name == "MOT_PIN_RL" || name == "MOT_PIN_RR") setupMotors(); +} + int parametersCount() { return sizeof(parameters) / sizeof(parameters[0]); } @@ -148,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 Inf parameter.setValue(value); + afterParameterChange(name, value); return true; } } diff --git a/gazebo/Arduino.h b/gazebo/Arduino.h index 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;