Added ServoPin() class to PwmPin.h
Controls a 50-Hz servo motor using ESP32 built-in LED PWM. Allows custom range for microseconds and degrees.
This commit is contained in:
parent
61655845a9
commit
65df97c563
|
|
@ -88,3 +88,44 @@ void PwmPin::HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////
|
||||||
|
|
||||||
|
ServoPin::ServoPin(uint8_t channel, uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees){
|
||||||
|
this->channel=channel & 0x07;
|
||||||
|
this->pin=pin;
|
||||||
|
this->minMicros=minMicros;
|
||||||
|
this->maxMicros=maxMicros;
|
||||||
|
this->minDegrees=minDegrees;
|
||||||
|
microsPerDegree=(double)(maxMicros-minMicros)/(maxDegrees-minDegrees);
|
||||||
|
|
||||||
|
ledc_timer_config_t ledTimer;
|
||||||
|
ledTimer.timer_num=LEDC_TIMER_1;
|
||||||
|
ledTimer.speed_mode=LEDC_HIGH_SPEED_MODE;
|
||||||
|
ledTimer.duty_resolution=LEDC_TIMER_16_BIT;
|
||||||
|
ledTimer.freq_hz=50;
|
||||||
|
ledc_timer_config(&ledTimer);
|
||||||
|
|
||||||
|
ledChannel.gpio_num=pin;
|
||||||
|
ledChannel.speed_mode=LEDC_HIGH_SPEED_MODE;
|
||||||
|
ledChannel.channel=(ledc_channel_t)(this->channel);
|
||||||
|
ledChannel.intr_type=LEDC_INTR_DISABLE;
|
||||||
|
ledChannel.timer_sel=LEDC_TIMER_1;
|
||||||
|
ledChannel.hpoint=0;
|
||||||
|
ledChannel.duty*=micros2duty;
|
||||||
|
set(initDegrees);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServoPin::set(double degrees){
|
||||||
|
ledChannel.duty=(degrees-minDegrees)*microsPerDegree+minMicros;
|
||||||
|
|
||||||
|
if(ledChannel.duty<minMicros)
|
||||||
|
ledChannel.duty=minMicros;
|
||||||
|
else if(ledChannel.duty>maxMicros)
|
||||||
|
ledChannel.duty=maxMicros;
|
||||||
|
|
||||||
|
ledChannel.duty*=micros2duty;
|
||||||
|
ledc_channel_config(&ledChannel);
|
||||||
|
}
|
||||||
|
|
||||||
|
const double ServoPin::micros2duty=65535.0/20000.0;
|
||||||
|
|
|
||||||
|
|
@ -24,3 +24,23 @@ class PwmPin {
|
||||||
static void HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ); // converts Hue/Saturation/Brightness to R/G/B
|
static void HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ); // converts Hue/Saturation/Brightness to R/G/B
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ServoPin {
|
||||||
|
uint8_t channel; // channel must be in range [0,7] (only HighSpeed Channels will be used)
|
||||||
|
uint8_t pin;
|
||||||
|
uint16_t minMicros;
|
||||||
|
uint16_t maxMicros;
|
||||||
|
double minDegrees;
|
||||||
|
double microsPerDegree;
|
||||||
|
ledc_channel_config_t ledChannel;
|
||||||
|
|
||||||
|
static const double micros2duty;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ServoPin(uint8_t channel, uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees);
|
||||||
|
ServoPin(uint8_t channel, uint8_t pin, double initDegrees=0) : ServoPin(channel,pin,initDegrees,1000,2000,-90,90) {};
|
||||||
|
|
||||||
|
void set(double degrees); // sets the Servo to degrees, where degrees is bounded by [minDegrees,maxDegrees]
|
||||||
|
int getPin(){return pin;} // returns the pin number
|
||||||
|
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -5,21 +5,39 @@
|
||||||
#include "PwmPin.h"
|
#include "PwmPin.h"
|
||||||
|
|
||||||
PwmPin yellow(0,16);
|
PwmPin yellow(0,16);
|
||||||
PwmPin red(1,17);
|
PwmPin red(0,17);
|
||||||
|
|
||||||
|
//ServoPin servo(3,18,-90);
|
||||||
|
ServoPin servo(3,18,0,500,2200,-90,90);
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
Serial.print("\n\nTest sketch for HomeSpan Extras Library\n\n");
|
Serial.print("\n\nTest sketch for HomeSpan Extras Library\n\n");
|
||||||
|
|
||||||
Serial.println("Starting...");
|
Serial.println("Starting...");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
|
|
||||||
|
|
||||||
|
double STEP=1;
|
||||||
|
|
||||||
|
for(int i=-100*STEP;i<=100*STEP;i++){
|
||||||
|
servo.set((double)i/STEP);
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i=100*STEP;i>=-100*STEP;i--){
|
||||||
|
servo.set((double)i/STEP);
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
for(int i=0;i<100;i++){
|
for(int i=0;i<100;i++){
|
||||||
red.set(22,i);
|
red.set(22,i);
|
||||||
delay(10);
|
delay(10);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue