Dezibot 4
Motion.cpp
Go to the documentation of this file.
1 
12 #include "Motion.h"
13 
14 
15 // Initialize the movement component.
16 
17 
18 void 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);
29  detection.begin();
30 };
31 void 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.
87 void Motion::move(uint32_t moveForMs, uint baseValue) {
88  if(xMoveTaskHandle){
89  vTaskDelete(xMoveTaskHandle);
90  xMoveTaskHandle = NULL;
91  }
93 
94  vTaskDelete(xClockwiseTaskHandle);
95  xClockwiseTaskHandle = NULL;
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 
108 void 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.
134 void 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 
148 void Motion::rightMotorTask(void * args) {
149  uint32_t runtime = (uint32_t)args;
150  if(xMoveTaskHandle){
151  vTaskDelete(xMoveTaskHandle);
152  xMoveTaskHandle = NULL;
153  }
155  vTaskDelete(xClockwiseTaskHandle);
156  xClockwiseTaskHandle = NULL;
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.
173 void 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 
187 void Motion::stop(void){
188  if(xMoveTaskHandle){
189  vTaskDelete(xMoveTaskHandle);
190  xMoveTaskHandle = NULL;
191  }
193  vTaskDelete(xAntiClockwiseTaskHandle);
195  }
197  vTaskDelete(xClockwiseTaskHandle);
198  xClockwiseTaskHandle = NULL;
199  }
202 }
203 
Motion::leftMotorTask
static void leftMotorTask(void *args)
Definition: Motion.cpp:108
DUTY_RES
#define DUTY_RES
Definition: Motion.h:24
Motion::RIGHT_MOTOR_DUTY
static uint16_t RIGHT_MOTOR_DUTY
Definition: Motion.h:61
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.h
This component controls the ability to rotate and change position.
Motion::correctionThreshold
static int correctionThreshold
Definition: Motion.h:74
LEDC_MODE
#define LEDC_MODE
Definition: Motion.h:20
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
Motion::buffer
static FIFO_Package * buffer
Definition: Motion.h:73
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
MotionDetection::getDataFromFIFO
uint getDataFromFIFO(FIFO_Package *buffer)
will read all availible packages from fifo, after 40ms Fifo is full
Definition: MotionDetection.cpp:245
Motion::rightMotorTask
static void rightMotorTask(void *args)
Definition: Motion.cpp:148
FREQUENCY
#define FREQUENCY
Definition: Motion.h:25
MotionDetection::begin
void begin(void)
initialized the IMU Component. Wakes the IMU from Standby Set configuration
Definition: MotionDetection.cpp:8
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
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
Motion::xClockwiseTaskHandle
static TaskHandle_t xClockwiseTaskHandle
Definition: Motion.h:69
TIMER
#define TIMER
Definition: Motion.h:21
Motion::LEFT_MOTOR_DUTY
static uint16_t LEFT_MOTOR_DUTY
Definition: Motion.h:62
Motion::detection
static MotionDetection detection
Definition: Motion.h:82
Motion::left
static Motor left
Definition: Motion.h:78
Motion::begin
void begin(void)
Initialize the movement component.
Definition: Motion.cpp:18