1#ifndef J_MOTOR_DRIVER_ESP32_SERVO_H
2#define J_MOTOR_DRIVER_ESP32_SERVO_H
15 float SERVO_TICKS_PER_MICROSECOND = 1;
30 JMotorDriverEsp32Servo(
byte _pwmChannel,
byte _servoPin,
int _freq = 50,
int _resBits = 14,
int _minServoValue = 544,
int _maxServoValue = 2400,
bool _constrainRange =
true)
36 pwmChannel = _pwmChannel;
40 SERVO_TICKS_PER_MICROSECOND = (float)(1 << SERVO_RES) * SERVO_FREQ / 1000000.0;
61 if (freq == SERVO_FREQ && resBits == SERVO_RES) {
62 return SERVO_TICKS_PER_MICROSECOND;
66 SERVO_TICKS_PER_MICROSECOND = (float)(1 << SERVO_RES) * SERVO_FREQ / 1000000.0;
67 unsigned long startMicros = micros();
68 while (digitalRead(servoPin) == HIGH && micros() - startMicros <=
maxServoValue)
70 ledcDetachPin(servoPin);
71 ledcSetup(pwmChannel, SERVO_FREQ, SERVO_RES);
73 ledcAttachPin(servoPin, pwmChannel);
76 return SERVO_TICKS_PER_MICROSECOND;
83 val = constrain(_val, -1.0, 1.0);
89 return abs(_val) < 1.0;
97 pinMode(servoPin, OUTPUT);
98 ledcSetup(pwmChannel, SERVO_FREQ, SERVO_RES);
99 ledcAttachPin(servoPin, pwmChannel);
106 unsigned long startMicros = micros();
107 while (digitalRead(servoPin) == HIGH && micros() - startMicros <=
maxServoValue)
109 ledcDetachPin(servoPin);
110 pinMode(servoPin, OUTPUT);
111 digitalWrite(servoPin, LOW);
For servos and motor controllers that use servo signals (ESCs)
Definition JMotorDriverEsp32Servo.h:10
bool set(float _val)
set motor power
Definition JMotorDriverEsp32Servo.h:78
void adjustFrequency(float freq=1.0)
helper function for adjusting the frequency that the servo signal pulse is repeated at For some servo...
Definition JMotorDriverEsp32Servo.h:48
float setFrequencyAndResolution(int freq=50, int resBits=14)
set frequency that servo signal pulse is repeated at and how many bits are used internally for resolu...
Definition JMotorDriverEsp32Servo.h:59
JMotorDriverEsp32Servo(byte _pwmChannel, byte _servoPin, int _freq=50, int _resBits=14, int _minServoValue=544, int _maxServoValue=2400, bool _constrainRange=true)
constructor, sets pins, custom frequency and resolution optional
Definition JMotorDriverEsp32Servo.h:30
float getMaxRange()
high end of the range
Definition JMotorDriverEsp32Servo.h:122
float getMinRange()
low end of the range
Definition JMotorDriverEsp32Servo.h:126
bool setEnable(bool _enable)
use to enable or disable a motor, and sets up pin states
Definition JMotorDriverEsp32Servo.h:91
bool getEnable()
get the enable state of the driver
Definition JMotorDriverEsp32Servo.h:118
defines interface for servo driver with variable frequency, subclass of JMotorDriverServo and JMotorD...
Definition JMotorDriverServoAdvanced.h:8
int maxServoValue
Definition JMotorDriverServo.h:11
bool constrainRange
Definition JMotorDriverServo.h:16
int setMicroseconds
Definition JMotorDriverServo.h:15
int minServoValue
Definition JMotorDriverServo.h:10