From ae8931cb251e8f10dccdb4be86c0a0491dbb2d87 Mon Sep 17 00:00:00 2001 From: Oleg Kalachev Date: Mon, 10 Aug 2026 03:45:28 +0300 Subject: [PATCH] Move most parameter defaults to config.h --- flix/config.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ flix/control.ino | 32 ++++------------------------ flix/imu.ino | 9 ++++---- flix/motors.ino | 13 ++++++------ flix/power.ino | 5 +++-- flix/rc.ino | 3 ++- 6 files changed, 75 insertions(+), 41 deletions(-) create mode 100644 flix/config.h diff --git a/flix/config.h b/flix/config.h new file mode 100644 index 0000000..4d5333f --- /dev/null +++ b/flix/config.h @@ -0,0 +1,54 @@ +// Copyright (c) 2026 Oleg Kalachev +// Repository: https://github.com/okalachev/flix + +// Default configuration parameters + +#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 + +// 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 + +// 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 + +// Power +#define VOLTAGE_PIN -1 +#define VOLTAGE_SCALE 2 + +// RC +#define RC_PIN_RX -1 diff --git a/flix/control.ino b/flix/control.ino index 057e940..2a02404 100644 --- a/flix/control.ino +++ b/flix/control.ino @@ -3,37 +3,13 @@ // Flight control +#include "config.h" #include "vector.h" #include "quaternion.h" #include "pid.h" #include "filter.h" #include "util.h" -#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 PITCHRATE_MAX radians(360) -#define ROLLRATE_MAX radians(360) -#define YAWRATE_MAX radians(300) -#define TILT_MAX radians(30) -#define RATES_D_LPF_ALPHA 0.2 // cutoff frequency ~ 40 Hz - const int RAW = 0, ACRO = 1, STAB = 2, AUTO = 3; // flight modes int mode = STAB; bool armed = false; @@ -49,9 +25,9 @@ PID pitchRatePID(PITCHRATE_P, PITCHRATE_I, PITCHRATE_D, PITCHRATE_I_LIM, RATES_D 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, 0, 0); -Vector maxRate(ROLLRATE_MAX, PITCHRATE_MAX, YAWRATE_MAX); -float tiltMax = TILT_MAX; +PID yawPID(YAW_P); +Vector maxRate(radians(360), radians(360), radians(360)); +float tiltMax = radians(30); int flightModes[] = {STAB, STAB, STAB}; // map for rc mode switch extern const int MOTOR_REAR_LEFT, MOTOR_REAR_RIGHT, MOTOR_FRONT_RIGHT, MOTOR_FRONT_LEFT; diff --git a/flix/imu.ino b/flix/imu.ino index bd00814..effc2e4 100644 --- a/flix/imu.ino +++ b/flix/imu.ino @@ -6,16 +6,17 @@ #include #include #include +#include "config.h" #include "vector.h" #include "filter.h" #include "util.h" IMU *imu; -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 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 imuSdaPin = SDA, imuSclPin = SCL; -Vector imuRotation(0, 0, PI / 2); // imu orientation as Euler angles +Vector imuRotation(IMU_ROT_ROLL, IMU_ROT_PITCH, IMU_ROT_YAW); // imu orientation as Euler angles Vector gyro; // gyroscope output, rad/s Vector gyroBias; diff --git a/flix/motors.ino b/flix/motors.ino index 959a951..8b29abf 100644 --- a/flix/motors.ino +++ b/flix/motors.ino @@ -3,16 +3,17 @@ // PWM control for motors +#include "config.h" #include "util.h" float motors[4]; // normalized motor thrusts in range [0..1] -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 +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 const int MOTOR_REAR_LEFT = 0, MOTOR_REAR_RIGHT = 1, MOTOR_FRONT_RIGHT = 2, MOTOR_FRONT_LEFT = 3; diff --git a/flix/power.ino b/flix/power.ino index 0dcc17e..d73128a 100644 --- a/flix/power.ino +++ b/flix/power.ino @@ -5,13 +5,14 @@ #include #include +#include "config.h" #include "filter.h" #include "util.h" float voltage = NAN; LowPassFilter voltageFilter(1); -int voltagePin = -1; -float voltageScale = 2; +int voltagePin = VOLTAGE_PIN; +float voltageScale = VOLTAGE_SCALE; 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 ec02a30..501536e 100644 --- a/flix/rc.ino +++ b/flix/rc.ino @@ -4,10 +4,11 @@ // Work with the RC receiver #include +#include "config.h" #include "util.h" SBUS rc(Serial1); -int rcRxPin = -1; // -1 means disabled +int rcRxPin = RC_PIN_RX; // -1 means disabled uint16_t channels[16]; // raw rc channels int channelZero[16]; // calibration zero values