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  Serial.println("Motor begin done");
23 };
24 
25 void 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 
46 uint16_t Motor::getSpeed(void){
47  return this->duty;
48 };
Motion.h
This component controls the ability to rotate and change position.
LEDC_MODE
#define LEDC_MODE
Definition: Motion.h:20
Motor::channel
ledc_channel_t channel
Definition: Motion.h:54
Motor::begin
void begin(void)
Initializes the motor.
Definition: Motor.cpp:10
Motor::getSpeed
uint16_t getSpeed(void)
returns the currently activ speed
Definition: Motor.cpp:46
Motor::Motor
Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel)
Definition: Motor.cpp:3
Motor::timer
ledc_timer_t timer
Definition: Motion.h:53
Motor::setSpeed
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
Motor::duty
uint16_t duty
Definition: Motion.h:56
Motor::pin
uint8_t pin
Definition: Motion.h:52