Dezibot 4
Motion.h
Go to the documentation of this file.
1 
12 #ifndef Motion_h
13 #define Motion_h
14 #include <stdint.h>
15 #include <Arduino.h>
16 #include <freertos/FreeRTOS.h>
17 #include <freertos/task.h>
18 #include "driver/ledc.h"
20 #define LEDC_MODE LEDC_LOW_SPEED_MODE
21 #define TIMER LEDC_TIMER_2
22 #define CHANNEL_LEFT LEDC_CHANNEL_3
23 #define CHANNEL_RIGHT LEDC_CHANNEL_4
24 #define DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
25 #define FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz
26 #define DEFAULT_BASE_VALUE 3900
27 class Motor{
28  public:
29  Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel);
30 
34  void begin(void);
35 
43  void setSpeed(uint16_t duty);
44 
50  uint16_t getSpeed(void);
51  protected:
52  uint8_t pin;
53  ledc_timer_t timer;
54  ledc_channel_t channel;
55 
56  uint16_t duty;
57 };
58 
59 class Motion{
60 protected:
61  static inline uint16_t RIGHT_MOTOR_DUTY = DEFAULT_BASE_VALUE;
62  static inline uint16_t LEFT_MOTOR_DUTY = DEFAULT_BASE_VALUE;
63  static const int MOTOR_RIGHT_PIN = 11;
64  static const int MOTOR_LEFT_PIN = 12;
65  static void moveTask(void * args);
66  static void leftMotorTask(void * args);
67  static void rightMotorTask(void * args);
68  static inline TaskHandle_t xMoveTaskHandle = NULL;
69  static inline TaskHandle_t xClockwiseTaskHandle = NULL;
70  static inline TaskHandle_t xAntiClockwiseTaskHandle = NULL;
71  static inline TickType_t xLastWakeTime;
72 
73  static inline FIFO_Package* buffer = new FIFO_Package[64];
74  static inline int correctionThreshold = 150;
75 
76 public:
77  //Instances of the motors, so they can also be used from outside to set values for the motors directly.
80 
81  //MotionDetection instance, for motion Correction and user (access with dezibot.motion.detection)
82  static inline MotionDetection detection;
83 
88  void begin(void);
89 
100  static void move(uint32_t moveForMs=0,uint baseValue=DEFAULT_BASE_VALUE);
101 
108  static void rotateClockwise(uint32_t rotateForMs=0,uint baseValue=DEFAULT_BASE_VALUE);
109 
116  static void rotateAntiClockwise(uint32_t rotateForMs=0,uint baseValue=DEFAULT_BASE_VALUE);
117 
122  static void stop(void);
123 
130  static void moveWithoutCorrection(uint32_t moveForMs=0, uint baseValue = DEFAULT_BASE_VALUE);
131 
132 };
133 
134 
135 #endif //Motion_h
Motion::moveWithoutCorrection
static void moveWithoutCorrection(uint32_t moveForMs=0, uint baseValue=DEFAULT_BASE_VALUE)
Does the same as the move function, but this function does not apply any kind of algorithm to improve...
Motion::leftMotorTask
static void leftMotorTask(void *args)
Definition: Motion.cpp:108
Motion::RIGHT_MOTOR_DUTY
static uint16_t RIGHT_MOTOR_DUTY
Definition: Motion.h:61
MotionDetection.h
This component controls the IMU (Accelerometer & Gyroscope) ICM-42670-P.
CHANNEL_LEFT
#define CHANNEL_LEFT
Definition: Motion.h:22
DEFAULT_BASE_VALUE
#define DEFAULT_BASE_VALUE
Definition: Motion.h:26
Motion::xMoveTaskHandle
static TaskHandle_t xMoveTaskHandle
Definition: Motion.h:68
Motion::stop
static void stop(void)
stops any current movement, no matter if timebased or endless
Definition: Motion.cpp:187
Motion::correctionThreshold
static int correctionThreshold
Definition: Motion.h:74
FIFO_Package
Definition: MotionDetection.h:42
CHANNEL_RIGHT
#define CHANNEL_RIGHT
Definition: Motion.h:23
Motor::channel
ledc_channel_t channel
Definition: Motion.h:54
Motor::begin
void begin(void)
Initializes the motor.
Definition: Motor.cpp:10
Motion::move
static void move(uint32_t moveForMs=0, uint baseValue=DEFAULT_BASE_VALUE)
Move forward for a certain amount of time. Call with moveForMs 0 will start movement,...
Definition: Motion.cpp:87
Motor
Definition: Motion.h:27
Motion::buffer
static FIFO_Package * buffer
Definition: Motion.h:73
Motor::getSpeed
uint16_t getSpeed(void)
returns the currently activ speed
Definition: Motor.cpp:46
Motion::rotateAntiClockwise
static void rotateAntiClockwise(uint32_t rotateForMs=0, uint baseValue=DEFAULT_BASE_VALUE)
Rotate anticlockwise for a certain amount of time. Call with moveForMs 0 will start movement,...
Definition: Motion.cpp:173
Motion::rightMotorTask
static void rightMotorTask(void *args)
Definition: Motion.cpp:148
Motion::MOTOR_RIGHT_PIN
static const int MOTOR_RIGHT_PIN
Definition: Motion.h:63
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
Motion::xLastWakeTime
static TickType_t xLastWakeTime
Definition: Motion.h:71
Motion::rotateClockwise
static void rotateClockwise(uint32_t rotateForMs=0, uint baseValue=DEFAULT_BASE_VALUE)
Rotate clockwise for a certain amount of time. Call with moveForMs 0 will start movement,...
Definition: Motion.cpp:134
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
Motion::moveTask
static void moveTask(void *args)
Definition: Motion.cpp:31
Motion::xAntiClockwiseTaskHandle
static TaskHandle_t xAntiClockwiseTaskHandle
Definition: Motion.h:70
Motion::right
static Motor right
Definition: Motion.h:79
Motor::pin
uint8_t pin
Definition: Motion.h:52
Motion::xClockwiseTaskHandle
static TaskHandle_t xClockwiseTaskHandle
Definition: Motion.h:69
TIMER
#define TIMER
Definition: Motion.h:21
Motion
Definition: Motion.h:59
Motion::LEFT_MOTOR_DUTY
static uint16_t LEFT_MOTOR_DUTY
Definition: Motion.h:62
Motion::MOTOR_LEFT_PIN
static const int MOTOR_LEFT_PIN
Definition: Motion.h:64
Motion::detection
static MotionDetection detection
Definition: Motion.h:82
Motion::left
static Motor left
Definition: Motion.h:78
MotionDetection
Definition: MotionDetection.h:51
Motion::begin
void begin(void)
Initialize the movement component.
Definition: Motion.cpp:18