JMotor
Loading...
Searching...
No Matches
JMotorDriverEsp32Servo.h
Go to the documentation of this file.
1#ifndef J_MOTOR_DRIVER_ESP32_SERVO_H
2#define J_MOTOR_DRIVER_ESP32_SERVO_H
4#include <Arduino.h>
5
11private:
12 bool enabled = false;
13 byte servoPin;
14 byte pwmChannel;
15 float SERVO_TICKS_PER_MICROSECOND = 1;
16 int SERVO_FREQ = 50;
17 int SERVO_RES = 14;
18
19public:
30 JMotorDriverEsp32Servo(byte _pwmChannel, byte _servoPin, int _freq = 50, int _resBits = 14, int _minServoValue = 544, int _maxServoValue = 2400, bool _constrainRange = true)
31 {
32 minServoValue = _minServoValue;
33 maxServoValue = _maxServoValue;
34 enabled = false;
35 servoPin = _servoPin;
36 pwmChannel = _pwmChannel;
37 constrainRange = _constrainRange;
38 SERVO_FREQ = _freq;
39 SERVO_RES = _resBits;
40 SERVO_TICKS_PER_MICROSECOND = (float)(1 << SERVO_RES) * SERVO_FREQ / 1000000.0;
41 }
42
48 void adjustFrequency(float freq = 1.0)
49 {
50 setFrequencyAndResolution(freq * 50.0);
51 }
52
59 float setFrequencyAndResolution(int freq = 50, int resBits = 14)
60 {
61 if (freq == SERVO_FREQ && resBits == SERVO_RES) {
62 return SERVO_TICKS_PER_MICROSECOND; // already set
63 }
64 SERVO_FREQ = freq;
65 SERVO_RES = resBits;
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)
69 ; // wait for pulse to go low to avoid cutting it short and causing the servo to twitch
70 ledcDetachPin(servoPin);
71 ledcSetup(pwmChannel, SERVO_FREQ, SERVO_RES);
72 if (enabled) {
73 ledcAttachPin(servoPin, pwmChannel);
74 ledcWrite(pwmChannel, SERVO_TICKS_PER_MICROSECOND * setMicroseconds);
75 }
76 return SERVO_TICKS_PER_MICROSECOND;
77 }
78 bool set(float _val)
79 {
80 if (enabled) {
81 float val;
83 val = constrain(_val, -1.0, 1.0);
84 else
85 val = _val;
87 ledcWrite(pwmChannel, SERVO_TICKS_PER_MICROSECOND * setMicroseconds);
88 }
89 return abs(_val) < 1.0;
90 }
91 bool setEnable(bool _enable)
92 {
93 if (_enable) {
94 if (!enabled) {
95 // actually enable
96 enabled = true;
97 pinMode(servoPin, OUTPUT);
98 ledcSetup(pwmChannel, SERVO_FREQ, SERVO_RES);
99 ledcAttachPin(servoPin, pwmChannel);
100 return true;
101 }
102 } else { // disable
103 if (enabled) {
104 // actually disable
105 enabled = false;
106 unsigned long startMicros = micros();
107 while (digitalRead(servoPin) == HIGH && micros() - startMicros <= maxServoValue)
108 ; // wait for pulse to go low to avoid cutting it short and causing the servo to twitch
109 ledcDetachPin(servoPin);
110 pinMode(servoPin, OUTPUT);
111 digitalWrite(servoPin, LOW);
112 return true;
113 }
114 }
115 return false;
116 }
117
119 {
120 return enabled;
121 }
123 {
124 return 1.0;
125 }
127 {
128 return -1.0;
129 }
130};
131#endif
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