diff --git a/docs/usage.md b/docs/usage.md index 4623bd2..d64ced0 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -179,8 +179,6 @@ Before flight you need to calibrate the accelerometer: If using non-default motor pins, set the pin numbers using the parameters: `MOTOR_PIN_FL`, `MOTOR_PIN_FR`, `MOTOR_PIN_RL`, `MOTOR_PIN_RR` (front-left, front-right, rear-left, rear-right respectively). -Certain ESP32 models (such as ESP32-S3 and ESP32-C3) support a lower maximum PWM frequency; on these boards the parameter `MOT_PWM_FREQ` should be set to 38000 Hz. - #### Brushless motors In case of using brushless motors with ESCs: diff --git a/flix/config.h b/flix/config.h index 5792384..30e8162 100644 --- a/flix/config.h +++ b/flix/config.h @@ -1,69 +1,27 @@ // Copyright (c) 2026 Oleg Kalachev // Repository: https://github.com/okalachev/flix -// Default configuration parameters +// Parameter defaults #pragma once -// IMU -#define IMU_MODEL -1 -#define IMU_BUS 0 -#define IMU_PIN_INT -1 -#define IMU_ROT_ROLL 0 -#define IMU_ROT_PITCH 0 -#define IMU_ROT_YAW -PI/2 +void setDefaults() { + // Set defaults here -// Motors -#define MOTOR_PIN_FL 12 -#define MOTOR_PIN_FR 13 -#define MOTOR_PIN_RL 14 -#define MOTOR_PIN_RR 15 -#define MOT_PWM_FREQ 78000 -#define MOT_PWM_RES 10 -#define MOT_PWM_STOP 0 -#define MOT_PWM_MIN 0 -#define MOT_PWM_MAX -1 + #if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3) + pwmFrequency = 38000; + #endif -// Control -#define PITCHRATE_P 0.05 -#define PITCHRATE_I 0.2 -#define PITCHRATE_D 0.001 -#define PITCHRATE_I_LIM 0.3 -#define ROLLRATE_P PITCHRATE_P -#define ROLLRATE_I PITCHRATE_I -#define ROLLRATE_D PITCHRATE_D -#define ROLLRATE_I_LIM PITCHRATE_I_LIM -#define YAWRATE_P 0.3 -#define YAWRATE_I 0.0 -#define YAWRATE_D 0.0 -#define YAWRATE_I_LIM 0.3 -#define ROLL_P 6 -#define ROLL_I 0 -#define ROLL_D 0 -#define PITCH_P ROLL_P -#define PITCH_I ROLL_I -#define PITCH_D ROLL_D -#define YAW_P 3 -#define RATES_D_LPF_ALPHA 0.2 // cutoff frequency ~ 40 Hz + #ifdef FLIX2 + imuModel = 4; // ICM-40609-D + imuIntPin = 10; + imuCsPin = 14; -// Power -#define VOLTAGE_PIN -1 -#define VOLTAGE_SCALE 2 + motorPins[0] = 41; + motorPins[1] = 7; + motorPins[2] = 38; + motorPins[3] = 18; -// RC -#define RC_PIN_RX -1 - -// Flix2 board configuration -#ifdef FLIX2 - #define IMU_MODEL 4 // ICM-40609-D - #define IMU_PIN_INT 10 - #define SS 14 - - #define MOTOR_PIN_RL 41 - #define MOTOR_PIN_RR 7 - #define MOTOR_PIN_FL 38 - #define MOTOR_PIN_FR 18 - #define MOT_PWM_FREQ 38000 - - #define VOLTAGE_PIN 3 -#endif + voltagePin = 3; + #endif +} diff --git a/flix/control.ino b/flix/control.ino index 2a02404..16447cb 100644 --- a/flix/control.ino +++ b/flix/control.ino @@ -3,7 +3,6 @@ // Flight control -#include "config.h" #include "vector.h" #include "quaternion.h" #include "pid.h" @@ -13,6 +12,7 @@ const int RAW = 0, ACRO = 1, STAB = 2, AUTO = 3; // flight modes int mode = STAB; bool armed = false; +int _desaturate = 1; Quaternion attitudeTarget; Vector ratesTarget; @@ -20,12 +20,12 @@ Vector ratesExtra; // feedforward rates Vector torqueTarget; // 0 - no torque, 1 - maximum torque float thrustTarget; -PID rollRatePID(ROLLRATE_P, ROLLRATE_I, ROLLRATE_D, ROLLRATE_I_LIM, RATES_D_LPF_ALPHA); -PID pitchRatePID(PITCHRATE_P, PITCHRATE_I, PITCHRATE_D, PITCHRATE_I_LIM, RATES_D_LPF_ALPHA); -PID yawRatePID(YAWRATE_P, YAWRATE_I, YAWRATE_D); -PID rollPID(ROLL_P, ROLL_I, ROLL_D); -PID pitchPID(PITCH_P, PITCH_I, PITCH_D); -PID yawPID(YAW_P); +PID rollRatePID(0.05, 0.2, 0.001, 0.3, 0.2); +PID pitchRatePID(0.05, 0.2, 0.001, 0.3, 0.2); +PID yawRatePID(0.3); +PID rollPID(6); +PID pitchPID(6); +PID yawPID(3); Vector maxRate(radians(360), radians(360), radians(360)); float tiltMax = radians(30); int flightModes[] = {STAB, STAB, STAB}; // map for rc mode switch diff --git a/flix/imu.ino b/flix/imu.ino index effc2e4..268f320 100644 --- a/flix/imu.ino +++ b/flix/imu.ino @@ -6,17 +6,16 @@ #include #include #include -#include "config.h" #include "vector.h" #include "filter.h" #include "util.h" IMU *imu; -int imuModel = IMU_MODEL; // 1 - MPU9250, 2 - ICM20948, 3 - MPU6050, 4 - ICM40609D -int imuBus = IMU_BUS; // 0 - SPI, 1 - I2C -int imuSckPin = SCK, imuMisoPin = MISO, imuMosiPin = MOSI, imuCsPin = SS, imuIntPin = IMU_PIN_INT; +int imuModel = -1; // 1 - MPU9250, 2 - ICM20948, 3 - MPU6050, 4 - ICM40609D +int imuBus = 0; // 0 - SPI, 1 - I2C +int imuSckPin = SCK, imuMisoPin = MISO, imuMosiPin = MOSI, imuCsPin = SS, imuIntPin = -1; int imuSdaPin = SDA, imuSclPin = SCL; -Vector imuRotation(IMU_ROT_ROLL, IMU_ROT_PITCH, IMU_ROT_YAW); // imu orientation as Euler angles +Vector imuRotation(0, 0, -PI/2); // imu orientation as Euler angles Vector gyro; // gyroscope output, rad/s Vector gyroBias; diff --git a/flix/motors.ino b/flix/motors.ino index 8b29abf..959a951 100644 --- a/flix/motors.ino +++ b/flix/motors.ino @@ -3,17 +3,16 @@ // PWM control for motors -#include "config.h" #include "util.h" float motors[4]; // normalized motor thrusts in range [0..1] -int motorPins[4] = {MOTOR_PIN_RL, MOTOR_PIN_RR, MOTOR_PIN_FR, MOTOR_PIN_FL}; // default pin numbers -int pwmFrequency = MOT_PWM_FREQ; -int pwmResolution = MOT_PWM_RES; -int pwmStop = MOT_PWM_STOP; -int pwmMin = MOT_PWM_MIN; -int pwmMax = MOT_PWM_MAX; // -1 means duty cycle mode +int motorPins[4] = {-1, -1, -1, -1}; // default pin numbers +int pwmFrequency = 78000; +int pwmResolution = 10; +int pwmStop = 0; +int pwmMin = 0; +int pwmMax = -1; // -1 means duty cycle mode const int MOTOR_REAR_LEFT = 0, MOTOR_REAR_RIGHT = 1, MOTOR_FRONT_RIGHT = 2, MOTOR_FRONT_LEFT = 3; diff --git a/flix/parameters.ino b/flix/parameters.ino index a9d014b..2c314e7 100644 --- a/flix/parameters.ino +++ b/flix/parameters.ino @@ -14,6 +14,8 @@ extern float rcLossTimeout, descendTime, disarmTilt; extern float voltageScale; extern LowPassFilter voltageFilter; +#include "config.h" + Preferences storage; struct Parameter { @@ -161,6 +163,7 @@ Parameter parameters[] = { void setupParameters() { print("Setup parameters\n"); + setDefaults(); storage.begin("flix"); // Read parameters from storage for (auto ¶meter : parameters) { diff --git a/flix/power.ino b/flix/power.ino index d73128a..0dcc17e 100644 --- a/flix/power.ino +++ b/flix/power.ino @@ -5,14 +5,13 @@ #include #include -#include "config.h" #include "filter.h" #include "util.h" float voltage = NAN; LowPassFilter voltageFilter(1); -int voltagePin = VOLTAGE_PIN; -float voltageScale = VOLTAGE_SCALE; +int voltagePin = -1; +float voltageScale = 2; void setupPower() { REG_CLR_BIT(RTC_CNTL_BROWN_OUT_REG, RTC_CNTL_BROWN_OUT_ENA); // disable reset on low voltage diff --git a/flix/rc.ino b/flix/rc.ino index 501536e..ec02a30 100644 --- a/flix/rc.ino +++ b/flix/rc.ino @@ -4,11 +4,10 @@ // Work with the RC receiver #include -#include "config.h" #include "util.h" SBUS rc(Serial1); -int rcRxPin = RC_PIN_RX; // -1 means disabled +int rcRxPin = -1; // -1 means disabled uint16_t channels[16]; // raw rc channels int channelZero[16]; // calibration zero values