Dezibot 4
Loading...
Searching...
No Matches
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
27class 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
59class Motion{
60protected:
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
76public:
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)
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
#define TIMER
Definition Motion.h:21
#define DEFAULT_BASE_VALUE
Definition Motion.h:26
#define CHANNEL_LEFT
Definition Motion.h:22
#define CHANNEL_RIGHT
Definition Motion.h:23
This component controls the IMU (Accelerometer & Gyroscope) ICM-42670-P.
static TickType_t xLastWakeTime
Definition Motion.h:71
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
static uint16_t RIGHT_MOTOR_DUTY
Definition Motion.h:61
static const int MOTOR_LEFT_PIN
Definition Motion.h:64
static const int MOTOR_RIGHT_PIN
Definition Motion.h:63
void begin(void)
Initialize the movement component.
Definition Motion.cpp:18
static int correctionThreshold
Definition Motion.h:74
static Motor left
Definition Motion.h:78
static void leftMotorTask(void *args)
Definition Motion.cpp:108
static void stop(void)
stops any current movement, no matter if timebased or endless
Definition Motion.cpp:187
static void rightMotorTask(void *args)
Definition Motion.cpp:148
static FIFO_Package * buffer
Definition Motion.h:73
static MotionDetection detection
Definition Motion.h:82
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
static TaskHandle_t xAntiClockwiseTaskHandle
Definition Motion.h:70
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...
static TaskHandle_t xMoveTaskHandle
Definition Motion.h:68
static uint16_t LEFT_MOTOR_DUTY
Definition Motion.h:62
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
static TaskHandle_t xClockwiseTaskHandle
Definition Motion.h:69
static void moveTask(void *args)
Definition Motion.cpp:31
static Motor right
Definition Motion.h:79
Definition Motion.h:27
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