Making an ESP32-based quadcopter from scratch
Python arduinoautopilotcontrol-theorydroneesp32flight-controlflight-controllermavlinkquadcopterquadcopter-firmwareuav
More cleanups
Oleg Kalachevf84d1e95dd9b
diff --git a/flix/cli.ino b/flix/cli.inoindex 510ccf2..fca2bf8 100644--- a/flix/cli.ino+++ b/flix/cli.ino@@ -5,10 +5,6 @@#include "pid.h"-static String command;-static String value;-static bool parsingCommand = true;-extern PID rollRatePID, pitchRatePID, yawRatePID, rollPID, pitchPID;const char* motd =@@ -34,8 +30,6 @@ const char* motd ="fullmot <n> - test motor on all signals\n""wifi - start wi-fi access point\n";-bool showMotd = true;-static const struct Param {const char* name;float* value;@@ -57,7 +51,7 @@ static const struct Param {// {"m", &mode, nullptr},};-static void doCommand()+static void doCommand(String& command, String& value){if (command == "help" || command == "motd") {Serial.println(motd);@@ -138,6 +132,11 @@ static void cliTestMotor(uint8_t n)void parseInput(){+ static bool showMotd = true;+ static String command;+ static String value;+ static bool parsingCommand = true;+if (showMotd) {Serial.println(motd);showMotd = false;@@ -148,7 +147,7 @@ void parseInput()if (c == '\n') {parsingCommand = true;if (!command.isEmpty()) {- doCommand();+ doCommand(command, value);}command.clear();value.clear();diff --git a/flix/flix.ino b/flix/flix.inoindex 9828941..d474575 100644--- a/flix/flix.ino+++ b/flix/flix.ino@@ -26,7 +26,6 @@ float dt; // time delta from previous step, sfloat loopFreq; // loop frequency, Hzuint16_t channels[16]; // raw rc channelsfloat controls[RC_CHANNELS]; // normalized controls in range [-1..1] ([0..1] for throttle)-uint32_t rcFailSafe, rcLostFrame;float motors[4]; // normalized motors thrust in range [-1..1]Vector rates; // angular rates, rad/sVector acc; // accelerometer data, m/s/sdiff --git a/flix/motors.ino b/flix/motors.inoindex d0c2cd6..3113cca 100644--- a/flix/motors.ino+++ b/flix/motors.ino@@ -1,46 +1,25 @@// Copyright (c) 2023 Oleg Kalachev <[email protected]>// Repository: https://github.com/okalachev/flix-// https://habr.com/ru/company/first/blog/664922/-// esc: https://aliexpress.com/item/4000280617058.html (KINGDUO Micro Mini 4A 1S Brushed Esc 3.6-6V)-// motor: https://aliexpress.com/item/32731613504.html (8520 3.7V)+// Motors output+// Motor: 8520 3.7V+// ESC: KINGDUO Micro Mini 4A 1S Brushed Esc 3.6-6V#define MOTOR_0_PIN 12#define MOTOR_1_PIN 13#define MOTOR_2_PIN 14#define MOTOR_3_PIN 15-// #define PWM_FREQUENCY 200-// #define PWM_FREQUENCY 50 // TODO: way low frequency considering the IMU is 1kHz-#define PWM_FREQUENCY 200 // WARNING: original 50-+#define PWM_FREQUENCY 200#define PWM_RESOLUTION 8-// #define PWM_MIN 1575-// #define PWM_MAX 2300#define PWM_NEUTRAL 1500-// #define PWM_REVERSE_MAX 700-// #define PWM_REVERSE_MIN 1425--// static const uint16_t pwmMin[] = {1600-50, 1600-50, 1600-50, 1600-50};-// static const uint16_t pwmMax[] = {2100, 2300, 2000, 2000}; // NOTE: ORIGINAL-static const uint16_t pwmMin[] = {1600, 1600, 1600, 1600}; // NOTE: success-// static const uint16_t pwmMax[] = {2000, 2000, 2000, 2000}; // NOTE: success+static const uint16_t pwmMin[] = {1600, 1600, 1600, 1600};static const uint16_t pwmMax[] = {2300, 2300, 2300, 2300};-// from esc description-// static const uint16_t pwmMin[] = {1600, 1600, 1600, 1600};-// static const uint16_t pwmMax[] = {2300, 2300, 2300, 2300};--// static const uint16_t pwmReverseMin[] = {1420+50, 1440+50, 1440+50, 1440+50};--// static const uint16_t pwmReverseMin[] = {1420, 1440, 1440, 1440};-// static const uint16_t pwmReverseMax[] = {700, 1100, 1100, 1100}; // NOTE: ???static const uint16_t pwmReverseMin[] = {1390, 1440, 1440, 1440};static const uint16_t pwmReverseMax[] = {1100, 1100, 1100, 1100};-bool useBreak; // TODO: redesign-void setupMotors() {Serial.println("Setup Motors");@@ -56,12 +35,6 @@ void setupMotors() {ledcAttachPin(MOTOR_2_PIN, 2);ledcAttachPin(MOTOR_3_PIN, 3);- // send initial break to initialize ESCs- // Serial.println("Calibrating ESCs");- // useBreak = true;- // sendMotors();- // delay(2000);- // useBreak = false;sendMotors();Serial.println("Motors initialized");}@@ -69,7 +42,7 @@ void setupMotors() {static uint16_t getPWM(float val, int n){if (val == 0) {- return PWM_NEUTRAL; // useBreak ? PWM_NEUTRAL : 0;+ return PWM_NEUTRAL;} else if (val > 0) {return mapff(val, 0, 1, pwmMin[n], pwmMax[n]);} else {diff --git a/flix/rc.ino b/flix/rc.inoindex 73f3561..bf02ecd 100644--- a/flix/rc.ino+++ b/flix/rc.ino@@ -18,8 +18,8 @@ void readRC(){bool failSafe, lostFrame;if (RC.read(channels, &failSafe, &lostFrame)) {- if (failSafe) { rcFailSafe++; return; } // TODO: NOT TESTED YET- if (lostFrame) { rcLostFrame++; return; }+ if (failSafe) { return; } // TODO:+ if (lostFrame) { return; }normalizeRC();}}diff --git a/gazebo/Arduino.h b/gazebo/Arduino.hindex 6e29617..a18bbbf 100644--- a/gazebo/Arduino.h+++ b/gazebo/Arduino.h@@ -36,9 +36,9 @@ class __FlashStringHelper;// https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/class String: public std::string {public:- long toInt() { return atol(this->c_str()); }- float toFloat() { return atof(this->c_str()); }- bool isEmpty() { return this->empty(); }+ long toInt() const { return atol(this->c_str()); }+ float toFloat() const { return atof(this->c_str()); }+ bool isEmpty() const { return this->empty(); }};class Print;