Dezibot 4
Loading...
Searching...
No Matches
Motor.cpp
Go to the documentation of this file.
1#include "Motion.h"
2
3Motor::Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel){
4 this->pin = pin;
5 this->channel = channel;
6 this->timer = timer;
7 this->duty = 0;
8};
9
10void Motor::begin(void){
11 pinMode(this->pin,OUTPUT);
12 ledc_channel_config_t channelConfig = {
13 .gpio_num = this->pin,
14 .speed_mode = LEDC_MODE,
15 .channel = this->channel,
16 .intr_type = LEDC_INTR_DISABLE,
17 .timer_sel = this->timer,
18 .duty = 0, // Set duty to 0%
19 .hpoint = 0
20 };
21 ledc_channel_config(&channelConfig);
22 Serial.println("Motor begin done");
23};
24
25void Motor::setSpeed(uint16_t duty){
26
27 int difference = duty-this->getSpeed();
28 if (difference > 0){
29 for(int i = 0;i<difference;i+=difference/20){
30 this->duty += difference/20;
31 ledc_set_duty(LEDC_MODE,this->channel,duty);
32 ledc_update_duty(LEDC_MODE,this->channel);
33 delayMicroseconds(5);
34 }
35 } else {
36 for(int i = 0;i>difference;i-=abs(difference/20)){
37 this->duty -= abs(difference/20);
38 ledc_set_duty(LEDC_MODE,this->channel,duty);
39 ledc_update_duty(LEDC_MODE,this->channel);
40 delayMicroseconds(5);
41 }
42 }
43
44};
45
46uint16_t Motor::getSpeed(void){
47 return this->duty;
48};
This component controls the ability to rotate and change position.
#define LEDC_MODE
Definition Motion.h:20
uint16_t getSpeed(void)
returns the currently activ speed
Definition Motor.cpp:46
Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel)
Definition Motor.cpp:3
void begin(void)
Initializes the motor.
Definition Motor.cpp:10
uint16_t duty
Definition Motion.h:56
void setSpeed(uint16_t duty)
Set the Speed by changing the pwm. To avoid current peaks, a linear ramp-up is used.
Definition Motor.cpp:25
ledc_channel_t channel
Definition Motion.h:54
uint8_t pin
Definition Motion.h:52
ledc_timer_t timer
Definition Motion.h:53