Dezibot 4
Motor.cpp
Go to the documentation of this file.
1 #include "Motion.h"
2 
3 Motor::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 
10 void 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 };
23 
24 void Motor::setSpeed(uint16_t duty){
25 
26  int difference = duty-this->getSpeed();
27  if (difference > 0){
28  for(int i = 0;i<difference;i+=difference/20){
29  this->duty += difference/20;
30  ledc_set_duty(LEDC_MODE,this->channel,duty);
31  ledc_update_duty(LEDC_MODE,this->channel);
32  delayMicroseconds(5);
33  }
34  } else {
35  for(int i = 0;i>difference;i-=abs(difference/20)){
36  this->duty -= abs(difference/20);
37  ledc_set_duty(LEDC_MODE,this->channel,duty);
38  ledc_update_duty(LEDC_MODE,this->channel);
39  delayMicroseconds(5);
40  }
41  }
42 
43 };
44 
45 uint16_t Motor::getSpeed(void){
46  return this->duty;
47 };
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:45
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:24
ledc_channel_t channel
Definition: Motion.h:54
uint8_t pin
Definition: Motion.h:52
ledc_timer_t timer
Definition: Motion.h:53