Remove ESC support and add MOSFET support in motors code

The new version uses MOSFETs
This commit is contained in:
Oleg Kalachev 2024-10-23 09:27:54 +03:00
parent b0b6eb9a97
commit 23f3295439
3 changed files with 17 additions and 31 deletions

View File

@ -102,7 +102,7 @@ void doCommand(String& command, String& value) {
} else if (command == "mrl") { } else if (command == "mrl") {
cliTestMotor(MOTOR_REAR_LEFT); cliTestMotor(MOTOR_REAR_LEFT);
} else if (command == "fullmot") { } else if (command == "fullmot") {
fullMotorTest(value.toInt(), false); fullMotorTest(value.toInt());
} else if (command == "reset") { } else if (command == "reset") {
attitude = Quaternion(); attitude = Quaternion();
} else { } else {

View File

@ -1,9 +1,9 @@
// Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com> // Copyright (c) 2023 Oleg Kalachev <okalachev@gmail.com>
// Repository: https://github.com/okalachev/flix // Repository: https://github.com/okalachev/flix
// Motors output control // Motors output control using MOSFETs
// In case of using ESC, use this version of the code: https://gist.github.com/okalachev/8871d3a94b6b6c0a298f41a4edd34c61.
// Motor: 8520 3.7V // Motor: 8520 3.7V
// ESC: KINGDUO Micro Mini 4A 1S Brushed Esc 3.6-6V
#define MOTOR_0_PIN 12 // rear left #define MOTOR_0_PIN 12 // rear left
#define MOTOR_1_PIN 13 // rear right #define MOTOR_1_PIN 13 // rear right
@ -12,11 +12,6 @@
#define PWM_FREQUENCY 200 #define PWM_FREQUENCY 200
#define PWM_RESOLUTION 8 #define PWM_RESOLUTION 8
#define PWM_NEUTRAL 1500
#define PWM_MIN 1600
#define PWM_MAX 2300
#define PWM_REVERSE_MIN 1400
#define PWM_REVERSE_MAX 700
void setupMotors() { void setupMotors() {
Serial.println("Setup Motors"); Serial.println("Setup Motors");
@ -31,34 +26,25 @@ void setupMotors() {
Serial.println("Motors initialized"); Serial.println("Motors initialized");
} }
uint16_t getPWM(float val, int n) { uint8_t signalToDutyCycle(float control) {
if (val == 0) { float duty = mapff(control, 0, 1, 0, (1 << PWM_RESOLUTION) - 1);
return PWM_NEUTRAL; return round(constrain(duty, 0, (1 << PWM_RESOLUTION) - 1));
} else if (val > 0) {
return mapff(val, 0, 1, PWM_MIN, PWM_MAX);
} else {
return mapff(val, 0, -1, PWM_REVERSE_MIN, PWM_REVERSE_MAX);
}
}
uint8_t pwmToDutyCycle(uint16_t pwm) {
return map(pwm, 0, 1000000 / PWM_FREQUENCY, 0, (1 << PWM_RESOLUTION) - 1);
} }
void sendMotors() { void sendMotors() {
ledcWrite(MOTOR_0_PIN, pwmToDutyCycle(getPWM(motors[0], 0))); ledcWrite(MOTOR_0_PIN, signalToDutyCycle(motors[0]));
ledcWrite(MOTOR_1_PIN, pwmToDutyCycle(getPWM(motors[1], 1))); ledcWrite(MOTOR_1_PIN, signalToDutyCycle(motors[1]));
ledcWrite(MOTOR_2_PIN, pwmToDutyCycle(getPWM(motors[2], 2))); ledcWrite(MOTOR_2_PIN, signalToDutyCycle(motors[2]));
ledcWrite(MOTOR_3_PIN, pwmToDutyCycle(getPWM(motors[3], 3))); ledcWrite(MOTOR_3_PIN, signalToDutyCycle(motors[3]));
} }
void fullMotorTest(int n, bool reverse) { void fullMotorTest(int n) {
printf("Full test for motor %d\n", n); printf("Full test for motor %d\n", n);
for (int pwm = PWM_NEUTRAL; pwm <= 2300 && pwm >= 700; pwm += reverse ? -100 : 100) { for (float signal = 0; signal <= 1; signal += 0.1) {
printf("Motor %d: %d\n", n, pwm); printf("Motor %d: %f\n", n, signal);
ledcWrite(n, pwmToDutyCycle(pwm)); ledcWrite(n, signalToDutyCycle(signal));
delay(3000); delay(3000);
} }
printf("Motor %d: %d\n", n, PWM_NEUTRAL); printf("Motor %d: %f\n", n, 0);
ledcWrite(n, pwmToDutyCycle(PWM_NEUTRAL)); ledcWrite(n, signalToDutyCycle(0));
} }

View File

@ -54,6 +54,6 @@ void handleMavlink(const void *_msg);
void setLED(bool on) {}; void setLED(bool on) {};
void calibrateGyro() { printf("Skip gyro calibrating\n"); }; void calibrateGyro() { printf("Skip gyro calibrating\n"); };
void calibrateAccel() { printf("Skip accel calibrating\n"); }; void calibrateAccel() { printf("Skip accel calibrating\n"); };
void fullMotorTest(int n, bool reverse) { printf("Skip full motor test\n"); }; void fullMotorTest(int n) { printf("Skip full motor test\n"); };
void sendMotors() {}; void sendMotors() {};
void printIMUCal() { printf("cal: N/A\n"); }; void printIMUCal() { printf("cal: N/A\n"); };