Dezibot 4
Loading...
Searching...
No Matches
Motion.cpp
Go to the documentation of this file.
1
12#include "Motion.h"
13
14
15// Initialize the movement component.
16
17
18void Motion::begin(void) {
19 ledc_timer_config_t motor_timer = {
20 .speed_mode = LEDC_MODE,
21 .duty_resolution = DUTY_RES,
22 .timer_num = TIMER,
23 .freq_hz = FREQUENCY,
24 .clk_cfg = LEDC_AUTO_CLK
25 };
26 ledc_timer_config(&motor_timer);
30};
31void Motion::moveTask(void * args) {
32 uint32_t runtime = (uint32_t)args;
33
36 Motion::xLastWakeTime = xTaskGetTickCount();
37 while(1){
38 if(runtime>40||runtime==0){
39 vTaskDelayUntil(&xLastWakeTime,40);
40 runtime -= 40;
41 //calc new parameters
42 //set new parameters
43 int fifocount = detection.getDataFromFIFO(buffer);
44 int rightCounter = 0;
45 int leftCounter = 0;
46 int changerate = 0;
47 for(int i = 0;i<fifocount;i++){
48 if(buffer[i].gyro.z>correctionThreshold){
49 rightCounter++;
50 } else if(buffer[i].gyro.z<-correctionThreshold){
51 leftCounter++;
52 }
53 }
54 int difference = abs(leftCounter-rightCounter);
55 if (difference>25){
56 changerate = 200;
57 } else if(difference>20){
58 changerate = 100;
59 } else if(difference >15){
60 changerate = 50;
61 } else if(difference > 10){
62 changerate = 20;
63 } else{
64 changerate = 5;
65 }
66
67 if(leftCounter>rightCounter){ //rotates anticlock
68 LEFT_MOTOR_DUTY+=changerate;
69 RIGHT_MOTOR_DUTY-=changerate;
70 } else if(leftCounter<rightCounter){
71 LEFT_MOTOR_DUTY-=changerate;
72 RIGHT_MOTOR_DUTY+=changerate;
73 }
74
77 } else {
78 vTaskDelayUntil(&xLastWakeTime,runtime);
81 vTaskDelete(xMoveTaskHandle);
82 }
83 }
84};
85
86// Move forward for a certain amount of time.
87void Motion::move(uint32_t moveForMs, uint baseValue) {
89 vTaskDelete(xMoveTaskHandle);
90 xMoveTaskHandle = NULL;
91 }
93
94 vTaskDelete(xClockwiseTaskHandle);
96 }
98 vTaskDelete(xAntiClockwiseTaskHandle);
100
101 }
102 LEFT_MOTOR_DUTY = baseValue;
103 RIGHT_MOTOR_DUTY = baseValue;
104 xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle);
105
106};
107
108void Motion::leftMotorTask(void * args) {
109 uint32_t runtime = (uint32_t)args;
110 if(xMoveTaskHandle){
111 vTaskDelete(xMoveTaskHandle);
112 xMoveTaskHandle = NULL;
113 }
115 vTaskDelete(xAntiClockwiseTaskHandle);
117 }
120 while(1){
121 if((runtime>40)||(runtime==0)){
122 vTaskDelayUntil(&xLastWakeTime,40);
123 runtime -=40;
124 } else {
125 vTaskDelayUntil(&xLastWakeTime,runtime);
127 vTaskDelete(xClockwiseTaskHandle);
128 }
129 vTaskDelayUntil(&xLastWakeTime,40);
130 }
131};
132
133// Rotate clockwise for a certain amount of time.
134void Motion::rotateClockwise(uint32_t rotateForMs,uint baseValue) {
135 LEFT_MOTOR_DUTY = baseValue;
136 RIGHT_MOTOR_DUTY = baseValue;
137 if (rotateForMs > 0){
139 vTaskDelete(xClockwiseTaskHandle);
140 }
141 xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle);
142 } else {
145 }
146};
147
148void Motion::rightMotorTask(void * args) {
149 uint32_t runtime = (uint32_t)args;
150 if(xMoveTaskHandle){
151 vTaskDelete(xMoveTaskHandle);
152 xMoveTaskHandle = NULL;
153 }
155 vTaskDelete(xClockwiseTaskHandle);
157 }
160 while(1){
161 if(runtime>40||runtime==0){
162 vTaskDelayUntil(&xLastWakeTime,40);
163 runtime -= 40;
164 } else {
165 vTaskDelayUntil(&xLastWakeTime,runtime);
167 vTaskDelete(xAntiClockwiseTaskHandle);
168 }
169 }
170};
171
172// Rotate anticlockwise for a certain amount of time.
173void Motion::rotateAntiClockwise(uint32_t rotateForMs,uint baseValue) {
174 LEFT_MOTOR_DUTY = baseValue;
175 RIGHT_MOTOR_DUTY = baseValue;
176 if(rotateForMs > 0){
178 vTaskDelete(xAntiClockwiseTaskHandle);
179 }
180 xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle);
181 } else {
184 }
185};
186
187void Motion::stop(void){
188 if(xMoveTaskHandle){
189 vTaskDelete(xMoveTaskHandle);
190 xMoveTaskHandle = NULL;
191 }
193 vTaskDelete(xAntiClockwiseTaskHandle);
195 }
197 vTaskDelete(xClockwiseTaskHandle);
199 }
202}
203
This component controls the ability to rotate and change position.
#define DUTY_RES
Definition Motion.h:24
#define TIMER
Definition Motion.h:21
#define FREQUENCY
Definition Motion.h:25
#define LEDC_MODE
Definition Motion.h:20
uint getDataFromFIFO(FIFO_Package *buffer)
will read all availible packages from fifo, after 40ms Fifo is full
void begin(void)
initialized the IMU Component. Wakes the IMU from Standby Set configuration
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
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 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
void begin(void)
Initializes the motor.
Definition Motor.cpp:10
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