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();
38  while(1){
39  if(runtime>40||runtime==0){
40  vTaskDelayUntil(&xLastWakeTime,40);
41  runtime -= 40;
42  //calc new parameters
43  //set new parameters
44  int fifocount = detection.getDataFromFIFO(buffer);
45  int rightCounter = 0;
46  int leftCounter = 0;
47  int changerate = 0;
48  for(int i = 0;i<fifocount;i++){
49  if(buffer[i].gyro.z>correctionThreshold){
50  rightCounter++;
51  } else if(buffer[i].gyro.z<-correctionThreshold){
52  leftCounter++;
53  }
54  }
55  int difference = abs(leftCounter-rightCounter);
56  if (difference>25){
57  changerate = 200;
58  } else if(difference>20){
59  changerate = 100;
60  } else if(difference >15){
61  changerate = 50;
62  } else if(difference > 10){
63  changerate = 20;
64  } else{
65  changerate = 5;
66  }
67 
68  if(leftCounter>rightCounter){ //rotates anticlock
69  LEFT_MOTOR_DUTY+=changerate;
70  RIGHT_MOTOR_DUTY-=changerate;
71  } else if(leftCounter<rightCounter){
72  LEFT_MOTOR_DUTY-=changerate;
73  RIGHT_MOTOR_DUTY+=changerate;
74  }
75 
78  } else {
79  vTaskDelayUntil(&xLastWakeTime,runtime);
83  vTaskDelete(xMoveTaskHandle);
84  }
85  }
86 };
87 
88 // Move forward for a certain amount of time.
89 void Motion::move(uint32_t moveForMs, uint baseValue) {
90  if(xMoveTaskHandle){
91  vTaskDelete(xMoveTaskHandle);
92  xMoveTaskHandle = NULL;
93  }
95 
96  vTaskDelete(xClockwiseTaskHandle);
97  xClockwiseTaskHandle = NULL;
98  }
100  vTaskDelete(xAntiClockwiseTaskHandle);
102 
103  }
104  LEFT_MOTOR_DUTY = baseValue;
105  RIGHT_MOTOR_DUTY = baseValue;
106  xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle);
107 
108 };
109 
110 void Motion::leftMotorTask(void * args) {
111  uint32_t runtime = (uint32_t)args;
112  if(xMoveTaskHandle){
113  vTaskDelete(xMoveTaskHandle);
114  xMoveTaskHandle = NULL;
115  }
117  vTaskDelete(xAntiClockwiseTaskHandle);
119  }
122  while(1){
123  if((runtime>40)||(runtime==0)){
124  vTaskDelayUntil(&xLastWakeTime,40);
125  runtime -=40;
126  } else {
127  vTaskDelayUntil(&xLastWakeTime,runtime);
129  vTaskDelete(xClockwiseTaskHandle);
130  }
131  vTaskDelayUntil(&xLastWakeTime,40);
132  }
133 };
134 
135 // Rotate clockwise for a certain amount of time.
136 void Motion::rotateClockwise(uint32_t rotateForMs,uint baseValue) {
137  LEFT_MOTOR_DUTY = baseValue;
138  RIGHT_MOTOR_DUTY = baseValue;
139  if (rotateForMs > 0){
141  vTaskDelete(xClockwiseTaskHandle);
142  }
143  xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle);
144  } else {
147  }
148 };
149 
150 void Motion::rightMotorTask(void * args) {
151  uint32_t runtime = (uint32_t)args;
152  if(xMoveTaskHandle){
153  vTaskDelete(xMoveTaskHandle);
154  xMoveTaskHandle = NULL;
155  }
157  vTaskDelete(xClockwiseTaskHandle);
158  xClockwiseTaskHandle = NULL;
159  }
162  while(1){
163  if(runtime>40||runtime==0){
164  vTaskDelayUntil(&xLastWakeTime,40);
165  runtime -= 40;
166  } else {
167  vTaskDelayUntil(&xLastWakeTime,runtime);
169  vTaskDelete(xAntiClockwiseTaskHandle);
170  }
171  }
172 };
173 
174 // Rotate anticlockwise for a certain amount of time.
175 void Motion::rotateAntiClockwise(uint32_t rotateForMs,uint baseValue) {
176  LEFT_MOTOR_DUTY = baseValue;
177  RIGHT_MOTOR_DUTY = baseValue;
178  if(rotateForMs > 0){
180  vTaskDelete(xAntiClockwiseTaskHandle);
181  }
182  xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle);
183  } else {
186  }
187 };
188 
189 void Motion::stop(void){
190  if(xMoveTaskHandle){
191  vTaskDelete(xMoveTaskHandle);
192  xMoveTaskHandle = NULL;
193  }
195  vTaskDelete(xAntiClockwiseTaskHandle);
197  }
199  vTaskDelete(xClockwiseTaskHandle);
200  xClockwiseTaskHandle = NULL;
201  }
205 }
206 
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
void stopFIFO(void)
stops writing results to FIFO-Storage needs to be called before Data Registers (such as GYRO_DATA_*,...
uint getDataFromFIFO(FIFO_Package *buffer)
will read all availible packages from fifo, after 40ms Fifo is full Only works when startFIFO was cal...
void startFIFO(void)
starts writing the measurment results to the FIFO-Storage
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:175
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:110
static void stop(void)
stops any current movement, no matter if timebased or endless
Definition: Motion.cpp:189
static void rightMotorTask(void *args)
Definition: Motion.cpp:150
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:89
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:136
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:24