Boboter
Loading...
Searching...
No Matches
motors.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <cstdint>
11#include <soc/gpio_num.h>
12#include <driver/ledc.h>
13#include "include/log_sources.h"
14
15class Robot;
16
17namespace Device {
18 class Motors {
19 private:
20 static constexpr const char* TAG = "Device::Motors";
21 static constexpr log_source LOG_SOURCE = LOG_SOURCE_DEVICE_MOTORS;
22
23 static constexpr gpio_num_t LEFT_SPEED_PIN = GPIO_NUM_32;
24 static constexpr gpio_num_t LEFT_DIRECTION_PIN = GPIO_NUM_33;
25 static constexpr gpio_num_t RIGHT_SPEED_PIN = GPIO_NUM_2;
26 static constexpr gpio_num_t RIGHT_DIRECTION_PIN = GPIO_NUM_15;
27
28 static constexpr ledc_timer_t LEDC_TIMER = LEDC_TIMER_0;
29 static constexpr ledc_channel_t LEFT_LEDC_CHANNEL = LEDC_CHANNEL_0;
30 static constexpr ledc_channel_t RIGHT_LEDC_CHANNEL = LEDC_CHANNEL_1;
31
32 Robot& robot;
33 bool inverse_direction[2];
34
35 public:
36 static constexpr uint8_t NUM_MOTORS = 2;
37 static constexpr uint16_t MAX_MOTOR_SPEED = 820; // Limited to 80% PWM cycle (2^10 * 0.8) to preserve motors
38 static constexpr uint16_t DEFAULT_RAMP_TIME_MS = 1000;
39 static constexpr uint16_t MIN_RAMP_TIME_MS = 100;
40
41 enum class motor_id_t : uint8_t {
42 LEFT = 0,
44 };
45
46 enum class motor_direction_t : uint8_t {
49 };
50
51 public:
52 explicit Motors(Robot& robot);
53 ~Motors();
54
58 void initialize();
59
60 // Wrapper Functions
67 void stop(motor_id_t motor_id, uint16_t ramp_time = DEFAULT_RAMP_TIME_MS) const;
68
74 void hard_stop(motor_id_t motor_id) const;
75
84 void set_speed(motor_id_t motor_id, uint16_t speed, uint16_t ramp_time = DEFAULT_RAMP_TIME_MS) const;
85
93 void set_direction(motor_id_t motor_id, motor_direction_t direction, uint16_t ramp_time = DEFAULT_RAMP_TIME_MS) const;
94
98 static void enable_deep_sleep_hold();
99
100 // Hardware Functions
109 void _set_speed(motor_id_t motor_id, uint16_t speed) const;
110
118 void _set_direction(motor_id_t motor_id, motor_direction_t direction) const;
119
120 private:
130 [[nodiscard]] motor_direction_t _get_actual_direction(motor_id_t motor_id, motor_direction_t apparent_direction) const;
131 };
132}
void _set_direction(motor_id_t motor_id, motor_direction_t direction) const
Physically sets the direction of the given motor.
Definition motors_hardware.cpp:25
static constexpr uint8_t NUM_MOTORS
Definition motors.h:36
Motors(Robot &robot)
Definition motors.cpp:13
motor_id_t
Definition motors.h:41
@ RIGHT
Definition motors.h:43
@ LEFT
Definition motors.h:42
motor_direction_t
Definition motors.h:46
@ BACKWARD
Definition motors.h:48
@ FORWARD
Definition motors.h:47
void _set_speed(motor_id_t motor_id, uint16_t speed) const
Physically sets the speed of the given motor.
Definition motors_hardware.cpp:14
void set_speed(motor_id_t motor_id, uint16_t speed, uint16_t ramp_time=DEFAULT_RAMP_TIME_MS) const
Sets the speed of the given motor toward which it should accelerate.
Definition motors.cpp:92
void hard_stop(motor_id_t motor_id) const
Stops the given motor abruptly without ramping.
Definition motors.cpp:78
~Motors()
Definition motors.cpp:20
static constexpr uint16_t DEFAULT_RAMP_TIME_MS
Definition motors.h:38
static constexpr uint16_t MAX_MOTOR_SPEED
Definition motors.h:37
void stop(motor_id_t motor_id, uint16_t ramp_time=DEFAULT_RAMP_TIME_MS) const
Stops the given motor by gradually reducing speed for a smooth stop.
Definition motors.cpp:63
void initialize()
Sets up the necessary GPIO pins and LEDC channels.
Definition motors.cpp:27
static void enable_deep_sleep_hold()
Sets up the motor speed pins to stay low when the ESP32 goes into deep sleep mode.
Definition motors.cpp:118
void set_direction(motor_id_t motor_id, motor_direction_t direction, uint16_t ramp_time=DEFAULT_RAMP_TIME_MS) const
Sets the virtual direction of the given motor.
Definition motors.cpp:106
static constexpr uint16_t MIN_RAMP_TIME_MS
Definition motors.h:39
Class containing all the components of the robot.
Definition robot.h:32
log_source
Definition log_sources.h:12
@ LOG_SOURCE_DEVICE_MOTORS
Definition log_sources.h:24
Definition battery.cpp:14