Move all global variable declarations to the appropriate subsystems

As it makes the subsystems code easier to understand.
Declare the most used variables in main sketch file as forward declarations.
Make all control input zero by default (except controlMode).
Minor changes.
This commit is contained in:
Oleg Kalachev
2026-01-03 13:28:18 +03:00
parent a5e3dfcf69
commit c8e5e08b03
10 changed files with 42 additions and 36 deletions

View File

@@ -11,7 +11,7 @@ extern const int MOTOR_REAR_LEFT, MOTOR_REAR_RIGHT, MOTOR_FRONT_RIGHT, MOTOR_FRO
extern const int RAW, ACRO, STAB, AUTO; extern const int RAW, ACRO, STAB, AUTO;
extern float t, dt, loopRate; extern float t, dt, loopRate;
extern uint16_t channels[16]; extern uint16_t channels[16];
extern float controlRoll, controlPitch, controlThrottle, controlYaw, controlMode; extern float controlTime;
extern int mode; extern int mode;
extern bool armed; extern bool armed;
@@ -132,6 +132,7 @@ void doCommand(String str, bool echo = false) {
} }
print("\nroll: %g pitch: %g yaw: %g throttle: %g mode: %g\n", print("\nroll: %g pitch: %g yaw: %g throttle: %g mode: %g\n",
controlRoll, controlPitch, controlYaw, controlThrottle, controlMode); controlRoll, controlPitch, controlYaw, controlThrottle, controlMode);
print("time: %.1f\n", controlTime);
print("mode: %s\n", getModeName()); print("mode: %s\n", getModeName());
print("armed: %d\n", armed); print("armed: %d\n", armed);
} else if (command == "wifi") { } else if (command == "wifi") {

View File

@@ -8,6 +8,10 @@
#include "lpf.h" #include "lpf.h"
#include "util.h" #include "util.h"
Vector rates; // estimated angular rates, rad/s
Quaternion attitude; // estimated attitude
bool landed;
float accWeight = 0.003; float accWeight = 0.003;
LowPassFilter<Vector> ratesFilter(0.2); // cutoff frequency ~ 40 Hz LowPassFilter<Vector> ratesFilter(0.2); // cutoff frequency ~ 40 Hz

View File

@@ -9,16 +9,13 @@
#define WIFI_ENABLED 1 #define WIFI_ENABLED 1
float t = NAN; // current step time, s extern float t, dt;
float dt; // time delta from previous step, s extern float controlRoll, controlPitch, controlYaw, controlThrottle, controlMode;
float controlRoll, controlPitch, controlYaw, controlThrottle; // pilot's inputs, range [-1, 1] extern Vector gyro, acc;
float controlMode = NAN; extern Vector rates;
Vector gyro; // gyroscope data extern Quaternion attitude;
Vector acc; // accelerometer data, m/s/s extern bool landed;
Vector rates; // filtered angular rates, rad/s extern float motors[4];
Quaternion attitude; // estimated attitude
bool landed; // are we landed and stationary
float motors[4]; // normalized motors thrust in range [0..1]
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);

View File

@@ -12,9 +12,12 @@
MPU9250 imu(SPI); MPU9250 imu(SPI);
Vector imuRotation(0, 0, -PI / 2); // imu orientation as Euler angles Vector imuRotation(0, 0, -PI / 2); // imu orientation as Euler angles
Vector gyro; // gyroscope output, rad/s
Vector gyroBias;
Vector acc; // accelerometer output, m/s/s
Vector accBias; Vector accBias;
Vector accScale(1, 1, 1); Vector accScale(1, 1, 1);
Vector gyroBias;
void setupIMU() { void setupIMU() {
print("Setup IMU\n"); print("Setup IMU\n");

View File

@@ -12,12 +12,11 @@
#define MAVLINK_RATE_SLOW 1 #define MAVLINK_RATE_SLOW 1
#define MAVLINK_RATE_FAST 10 #define MAVLINK_RATE_FAST 10
extern float controlTime;
bool mavlinkConnected = false; bool mavlinkConnected = false;
String mavlinkPrintBuffer; String mavlinkPrintBuffer;
extern float controlTime;
extern float controlRoll, controlPitch, controlThrottle, controlYaw, controlMode;
void processMavlink() { void processMavlink() {
sendMavlink(); sendMavlink();
receiveMavlink(); receiveMavlink();

View File

@@ -17,7 +17,8 @@
#define PWM_MIN 0 #define PWM_MIN 0
#define PWM_MAX 1000000 / PWM_FREQUENCY #define PWM_MAX 1000000 / PWM_FREQUENCY
// Motors array indexes: float motors[4]; // normalized motor thrusts in range [0..1]
const int MOTOR_REAR_LEFT = 0; const int MOTOR_REAR_LEFT = 0;
const int MOTOR_REAR_RIGHT = 1; const int MOTOR_REAR_RIGHT = 1;
const int MOTOR_FRONT_RIGHT = 2; const int MOTOR_FRONT_RIGHT = 2;

View File

@@ -6,13 +6,16 @@
#include <SBUS.h> #include <SBUS.h>
#include "util.h" #include "util.h"
SBUS rc(Serial2); // NOTE: Use RC(Serial2, 16, 17) if you use the old UART2 pins SBUS rc(Serial2);
uint16_t channels[16]; // raw rc channels uint16_t channels[16]; // raw rc channels
float controlTime; // time of the last controls update
float channelZero[16]; // calibration zero values float channelZero[16]; // calibration zero values
float channelMax[16]; // calibration max values float channelMax[16]; // calibration max values
float controlRoll, controlPitch, controlYaw, controlThrottle; // pilot's inputs, range [-1, 1]
float controlMode = NAN; //
float controlTime; // time of the last controls update (0 when no RC)
// Channels mapping (using float to store in parameters): // Channels mapping (using float to store in parameters):
float rollChannel = NAN, pitchChannel = NAN, throttleChannel = NAN, yawChannel = NAN, modeChannel = NAN; float rollChannel = NAN, pitchChannel = NAN, throttleChannel = NAN, yawChannel = NAN, modeChannel = NAN;
@@ -38,11 +41,11 @@ void normalizeRC() {
controls[i] = mapf(channels[i], channelZero[i], channelMax[i], 0, 1); controls[i] = mapf(channels[i], channelZero[i], channelMax[i], 0, 1);
} }
// Update control values // Update control values
controlRoll = rollChannel >= 0 ? controls[(int)rollChannel] : NAN; controlRoll = rollChannel >= 0 ? controls[(int)rollChannel] : 0;
controlPitch = pitchChannel >= 0 ? controls[(int)pitchChannel] : NAN; controlPitch = pitchChannel >= 0 ? controls[(int)pitchChannel] : 0;
controlYaw = yawChannel >= 0 ? controls[(int)yawChannel] : NAN; controlYaw = yawChannel >= 0 ? controls[(int)yawChannel] : 0;
controlThrottle = throttleChannel >= 0 ? controls[(int)throttleChannel] : NAN; controlThrottle = throttleChannel >= 0 ? controls[(int)throttleChannel] : 0;
controlMode = modeChannel >= 0 ? controls[(int)modeChannel] : NAN; controlMode = modeChannel >= 0 ? controls[(int)modeChannel] : NAN; // mode switch should not have affect if not set
} }
void calibrateRC() { void calibrateRC() {

View File

@@ -3,6 +3,8 @@
// Time related functions // Time related functions
float t = NAN; // current time, s
float dt; // time delta with the previous step, s
float loopRate; // Hz float loopRate; // Hz
void step() { void step() {

View File

@@ -35,7 +35,6 @@ public:
z = NAN; z = NAN;
} }
float norm() const { float norm() const {
return sqrt(x * x + y * y + z * z); return sqrt(x * x + y * y + z * z);
} }

View File

@@ -12,17 +12,15 @@
#define WIFI_ENABLED 1 #define WIFI_ENABLED 1
float t = NAN; extern float t, dt;
float dt; extern float controlRoll, controlPitch, controlYaw, controlThrottle, controlMode;
float motors[4]; extern Vector rates;
float controlRoll, controlPitch, controlYaw, controlThrottle = NAN; extern Quaternion attitude;
float controlMode = NAN; extern bool landed;
Vector acc; extern float motors[4];
Vector gyro;
Vector rates; Vector gyro, acc, imuRotation;
Quaternion attitude; Vector accBias, gyroBias, accScale(1, 1, 1);
bool landed;
Vector imuRotation;
// declarations // declarations
void step(); void step();
@@ -75,4 +73,3 @@ void calibrateAccel() { print("Skip accel calibrating\n"); };
void printIMUCalibration() { print("cal: N/A\n"); }; void printIMUCalibration() { print("cal: N/A\n"); };
void printIMUInfo() {}; void printIMUInfo() {};
void printWiFiInfo() {}; void printWiFiInfo() {};
Vector accBias, gyroBias, accScale(1, 1, 1);