-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathEsc.cpp
More file actions
38 lines (31 loc) · 1.15 KB
/
Copy pathEsc.cpp
File metadata and controls
38 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Esc.cpp
#include "Esc.h" // Include the header file
// Constructor that initializes the ESC with a given pin
Esc::Esc(int pin)
{
_pin = pin;
_esc.attach(_pin); // Attach the ESC to the specified pin
}
// Method to initialize the ESC (sets it to 0 speed initially)
void Esc::initialize()
{
_esc.writeMicroseconds(NEUTRAL_SPEED_MS); // Start motor at 0 speed
delay(1000); // Wait for ESC initialization
}
// Method to control the ESC based on the throttle input
void Esc::control(int throttle)
{
if (abs(throttle) < MOTOR_DEAD_ZONE)
{
throttle = 0; // Ignore small values within dead zone
}
// Smooth the throttle value
smoothedMotorSpeed = smoothedMotorSpeed + MOTOR_SMOOTHING_FACTOR * (throttle - smoothedMotorSpeed);
// Map the smoothed throttle value from -255 to 255 into a PWM signal range (1000 to 2000 microseconds)
int pwmValue = map(smoothedMotorSpeed, -255, 255, MIN_SPEED_MS, MAX_SPEED_MS);
// Set the PWM signal to the ESC
_esc.writeMicroseconds(pwmValue);
// Optionally, print the PWM value for debugging
// Serial.print(" - PWM Value: ");
// Serial.println(pwmValue);
}