-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiTaskWorkV1.cpp
More file actions
502 lines (488 loc) · 18.8 KB
/
MultiTaskWorkV1.cpp
File metadata and controls
502 lines (488 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include <Arduino.h>
#include <Preferences.h>
#include <ESP32Servo.h>
Preferences preferences;
//information for the robot control
unsigned long pastRRot = 0;
unsigned long pastLRot = 0;
unsigned long totalRRot = 0;
unsigned long totalLRot = 0;
unsigned long leftCount = 0; //Encoder value from the interrupt function LEFT
unsigned long rightCount = 0; //Encoder value from the interrupt function RIGHT
//Pin Assignments
const int interruptPinR = 19;
const int interruptPinL = 18;
const int motorLS = 4;
const int motorLD = 2;
const int motorRS = 17;
const int motorRD = 16;
//Adjustable speed and movement properties
int leftDutyC = 300;
int rightDutyC = 300;
int encoderCountL = 0;
int encoderCountR = 0;
int freqSpeed = 100;
unsigned long nowTime = 0;
bool setTime = false;
// Setting PWM properties
const int freq = 30000;
const int pwmChannelL = 15;
const int resolution = 10;
const int pwmChannelR = 14;
void checkSpeed(){
if (setTime == false){ //only will happen on the initialization of the function
nowTime = millis();
pastRRot = totalRRot;
pastLRot = totalLRot;
setTime = true;
}
if ((millis() - nowTime) >= freqSpeed){
encoderCountL = (totalLRot - pastLRot);
encoderCountR = (totalRRot - pastRRot);
if ((encoderCountL - encoderCountR) >= 2){
rightDutyC++;
//Serial.println("left moves faster");
}
if ((encoderCountR - encoderCountL) >= 2){
rightDutyC--;
//Serial.println("right moves faster");
}
/*Serial.print("Right Speed Set");
Serial.println(rightDutyC);
Serial.print("left rotations:");
Serial.println(encoderCountL);
Serial.print("Right rotations:");
Serial.println(encoderCountR);
*/
setTime = false; //Reset the whole process
}
}
void moveBackward(int leftRotation, int rightRotation, int speed) {
rightDutyC = speed;
leftDutyC = speed;
digitalWrite(motorLD,HIGH); //set direction as Backward
digitalWrite(motorRD,HIGH);
Serial.println("MOVING Backward");
rightCount = (rightRotation + totalRRot);
leftCount = (leftRotation + totalLRot);
while (totalLRot <= leftCount){ //turn on the motors while rotations are less than the value
ledcWrite(pwmChannelL, leftDutyC);
ledcWrite(pwmChannelR, rightDutyC);
checkSpeed();
if (totalRRot >= rightCount){ //this will turn off the right motor if the right side reaches the rotation count first
ledcWrite(pwmChannelR, 0);
//Serial.print("Right Rotation Count:");
//Serial.println(rightCount);
//Serial.println("Motor Right Stopped First");
}
}
ledcWrite(pwmChannelL, 0); //after while loop completed the left motor is turned off
while (totalRRot < rightCount){ //then it will check to see if the right motor should stay on or off
ledcWrite(pwmChannelR, rightDutyC);
//Serial.println("Right motor continue");
}
ledcWrite(pwmChannelL, 0); //turn both motors off
ledcWrite(pwmChannelR, 0);
//Serial.print("Left Rotation Count:");
//Serial.println(leftCount);
//Serial.print("Right Rotation Count:");
//Serial.println(rightCount);
//Serial.println("Movement Completed");
}
void moveForward(int leftRotation, int rightRotation, int speed) {
rightDutyC = speed;
leftDutyC = speed;
digitalWrite(motorLD,LOW); //set direction as forward
digitalWrite(motorRD,LOW);
Serial.println("MOVING FORWARD");
rightCount = (rightRotation + totalRRot);
leftCount = (leftRotation + totalLRot);
while (totalLRot <= leftCount){ //turn on the motors while rotations are less than the value
ledcWrite(pwmChannelL, leftDutyC);
ledcWrite(pwmChannelR, rightDutyC);
checkSpeed();
//Serial.println(rightDutyC);
//Serial.print("Left Rotation Count:");
//Serial.println(totalLRot);
if (totalRRot >= rightCount){ //this will turn off the right motor if the right side reaches the rotation count first
ledcWrite(pwmChannelR, 0);
//Serial.print("Right Rotation Count:");
//Serial.println(totalRRot);
Serial.println("Motor Right Stopped First");
}
}
ledcWrite(pwmChannelL, 0); //after while loop completed the left motor is turned off
while (totalRRot < rightCount){ //then it will check to see if the right motor should stay on or off
ledcWrite(pwmChannelR, rightDutyC);
Serial.println("Right motor continue");
}
ledcWrite(pwmChannelL, 0); //turn both motors off
ledcWrite(pwmChannelR, 0);
Serial.println("Movement Completed");
}
void turnLeft(int turnRotate, int speed) {
rightDutyC = speed;
leftDutyC = speed;
digitalWrite(motorLD, HIGH); //set direction of the motors
digitalWrite(motorRD, LOW);
Serial.println("Moving LEFT");
rightCount = (turnRotate + totalRRot);
leftCount = (turnRotate + totalLRot);
while (totalLRot <= leftCount){ //turn on the motors while rotations are less than the value
ledcWrite(pwmChannelL, leftDutyC);
ledcWrite(pwmChannelR, rightDutyC);
checkSpeed();
//Serial.print("Left Rotation Count:");
//Serial.println(leftCount);
if (totalRRot >= rightCount){ //this will turn off the right motor if the right side reaches the rotation count first
ledcWrite(pwmChannelR, 0);
//Serial.print("Right Rotation Count:");
//Serial.println(rightCount);
//Serial.println("Motor Right Stopped First");
}
}
ledcWrite(pwmChannelL, 0); //after while loop completed the left motor is turned off
while (totalRRot < rightCount){ //then it will check to see if the right motor should stay on or off
ledcWrite(pwmChannelR, rightDutyC);
//Serial.println("Right motor continue");
}
ledcWrite(pwmChannelL, 0); //turn both motors off
ledcWrite(pwmChannelR, 0);
Serial.println("turn completed");
}
void turnRight(int turnRotate, int speed) {
rightDutyC = speed;
leftDutyC = speed;
digitalWrite(motorLD, LOW); //set direction of the motors
digitalWrite(motorRD, HIGH);
Serial.println("Moving RIGHT");
rightCount = (turnRotate + totalRRot);
leftCount = (turnRotate + totalLRot);
while (totalLRot <= leftCount){ //turn on the motors while rotations are less than the value
ledcWrite(pwmChannelL, leftDutyC);
ledcWrite(pwmChannelR, rightDutyC);
checkSpeed();
//Serial.print("Left Rotation Count:");
//Serial.println(leftCount);
if (totalRRot >= rightCount){ //this will turn off the right motor if the right side reaches the rotation count first
ledcWrite(pwmChannelR, 0);
//Serial.print("Right Rotation Count:");
//Serial.println(rightCount);
//Serial.println("Motor Right Stopped First");
}
}
ledcWrite(pwmChannelL, 0); //after while loop completed the left motor is turned off
while (totalRRot < rightCount){ //then it will check to see if the right motor should stay on or off
ledcWrite(pwmChannelR, rightDutyC);
//Serial.println("Right motor continue");
}
ledcWrite(pwmChannelL, 0); //turn both motors off
ledcWrite(pwmChannelR, 0);
//leftCount = 0; //Reset the rotation count values
//Serial.print("Left Rotation Count:");
//Serial.println(leftCount);
//rightCount = 0;
//Serial.print("Right Rotation Count:");
//Serial.println(rightCount);
Serial.println("turn completed");
}
void addRotR(){
totalRRot += 1;
}
void addRotL(){
totalLRot += 1;
}
//Information from Sorter
int minUs = 500;
int maxUs = 2400;
const int servoDelay = 350; //determine how long our servo will delay before proceeding to next movement
ESP32PWM pwm;
Servo servo1;
Servo servo2;
class Sorter {
int AIN; //GPIO for IR reader
int servoPin; //GPIO pin for the servo
int buttonPin;
const char* nSpace;
unsigned long startMTime; //used for the delay of the servo
char ballColor = 'N';
char ballValue = 'N';
bool start;
bool switchChanged;
unsigned long currentTime = 0;
int blackValue = 700; //change this depending on the ir value of black ball
int whiteValue = 3000; //change this depending on the ir value of white ball
Servo servo;
public:
Sorter(Servo myServo, int sPin, int bPin, int inputPin, const char* nameSpace) {
servo = myServo;
servoPin = sPin;
buttonPin = bPin;
AIN = inputPin;
pinMode(buttonPin, INPUT_PULLUP);
start = true;
nSpace = nameSpace;
}
void calibrate(){
//Move servo into starting position
servo.attach(servoPin, minUs, maxUs);
servo.write(90);
preferences.begin(nSpace, false);
//Menu startup
Serial.print("Enter 1 to set values for");
Serial.println(nSpace);
delay(3000);
//servo.detach();
if(Serial.available()){
int cereal = Serial.parseInt();
while(cereal == 1){
Serial.println("Enter 1 (Black) or 2 (White)");
while(!Serial.available()){}
int color = Serial.parseInt();
if (color == 1){
int storedBlack = preferences.getInt("black", 400);
Serial.print("Stored black value is: ");
Serial.println(storedBlack);
int currentBlack = analogRead(AIN);
Serial.print("Current black value is: ");
Serial.println(currentBlack);
Serial.println("Enter 1 to change value or anything else to continue");
while(!Serial.available()){}
int change = Serial.parseInt();
if (change == 1){
preferences.putInt("black", currentBlack);
Serial.println("Value Stored");
Serial.println("Enter 1 to end or anything to continue");
while(!Serial.available()){}
int clear = Serial.parseInt();
if(clear != 1){
cereal = 1;
color = 0;
}
if(clear == 1){
cereal = 0;
}
}
if(change != 1){
cereal = 0;
Serial.println("Value Not Stored");
Serial.println("Enter 1 to end or anything to continue");
while(!Serial.available()){}
int clear = Serial.parseInt();
if(clear != 1){
cereal = 1;
color = 0;
}
if(clear == 1){
cereal = 0;
}
}
}
if (color == 2){
int storedWhite = preferences.getInt("white", 400);
Serial.print("Stored white value is: ");
Serial.println(storedWhite);
int currentWhite = analogRead(AIN);
Serial.print("Current white value is: ");
Serial.println(currentWhite);
Serial.println("Enter 1 to change value or anything else to continue");
while(!Serial.available()){}
int change = Serial.parseInt();
if (change == 1){
preferences.putInt("white", currentWhite);
Serial.println("Value Stored");
Serial.println("Enter 1 to end or anything to continue");
while(!Serial.available()){}
int clear = Serial.parseInt();
if(clear != 1){
cereal = 1;
color = 0;
}
if(clear == 1){
cereal = 0;
}
}
if(change != 1){
cereal = 0;
Serial.println("Value Not Stored");
Serial.println("Enter 1 to end or anything to continue");
while(!Serial.available()){}
int clear = Serial.parseInt();
if(clear != 1){
cereal = 1;
color = 0;
}
if(clear == 1){
cereal = 0;
}
}
}
}
}
blackValue = preferences.getInt("black", 400);
whiteValue = preferences.getInt("white", 2000);
preferences.end();
}
void left() {
currentTime = millis();
if (start == true) {
//servo.attach(servoPin, minUs, maxUs);
servo.write(170);
startMTime = currentTime;// waits 100ms for the servo to reach the position
start = false;
}
if (currentTime >= (startMTime + servoDelay)) {
servo.write(90);
switchChanged = false;
//Serial.println("Switch changed back");
if (currentTime >= (startMTime + (2 * servoDelay))) {
ballValue = 'N';
start = true;
//servo.detach();
}
}
}
void right() {
currentTime = millis(); //update the time
if (start == true) { //will only run once on start of the function
//servo.attach(servoPin, minUs, maxUs); //attach the servo to pwm pin
servo.write(10); //tip servo right
startMTime = currentTime; //Set the start time of servo movement
//Serial.println("Im here 8");
start = false; //set value false so that this function is not run until movement restarts
}
Serial.println("Im here 7");
if (currentTime >= (startMTime + servoDelay)) { //once time for the movement has completed
servo.write(90); //move servo back to position
Serial.println("Im here 9");
switchChanged = false; //allow switch to be pressed again
if (currentTime >= (startMTime + (2 * servoDelay))){//true when double the delay time has passed since start
ballValue = 'N'; //allow detection to begin again
start = true; //reset movement
Serial.println("Im here 10");
//servo.detach();
}
}
}
void sortBallSorterRight() {
if (ballValue == 'N') {
delay(1);
ballValue = detectValue();
/*
//Serial.println(switchChanged);
if (digitalRead(buttonPin) == 1) {
switchChanged = true;
//Serial.println("button Pressed");
}
if (switchChanged) {
}
*/
}
if (ballValue == 'W') {
right();
delay(1);
}
if (ballValue == 'B') {
left();
delay(1);
}
}
void sortBallSorterLeft() { //This function is for the left sorter that tips right
if (ballValue == 'N') { //This will be default ball color value and can only read color when in this part
delay(1);
ballValue = detectValue();
/*
//Serial.println(switchChanged);
if (digitalRead(buttonPin) == 1) {
switchChanged = true;
//Serial.println("button Pressed");
}
if (switchChanged) {
}
*/
}
if (ballValue == 'B') { //Black ball
right();
delay(1);
//Serial.println("Im here 5");
}
if (ballValue == 'W') { //White ball
left();
delay(1);
//Serial.println("Im here 6");
}
}
char detectValue() {
int newIrValue = analogRead(AIN); //read analog pin
Serial.println(newIrValue);
//Serial.println(newIrValue);
if (newIrValue >= (whiteValue-1000)) { //compare value to global ball value
ballColor = 'W';
//Serial.println("White");
}
else if (newIrValue <= (blackValue+400)) {
ballColor = 'B';
//Serial.println("Black");
} else {
ballColor = 'N';
}
return ballColor;
}
};
Sorter sorter(servo1, 26, 25 , 34, "sortA"); //create sorter objects
Sorter sorter2(servo2, 33, 32, 35, "sortB");
TaskHandle_t Task1; //create tasks to run on cores
TaskHandle_t Task2;
void Task1code( void * pvParameters ){ //define task1 function
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
//analogRead(34);
//delay(1000);
sorter.sortBallSorterRight();
//Serial.println("Im here 1");
sorter2.sortBallSorterLeft();
delay(1);
}
vTaskDelete( NULL );
}
void Task2code( void * pvParameters ){ //define task2 function
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
moveForward(1000,1000,300);
//moveForward(1000,1000,300);
moveBackward(1000,1000,300);
}
vTaskDelete( NULL );
}
void setup() {
ledcSetup(pwmChannelL, freq, resolution);
ledcSetup(pwmChannelR, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(motorLS, pwmChannelL);
ledcAttachPin(motorRS, pwmChannelR);
ESP32PWM::allocateTimer(0); //servo timers
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
//ESP32PWM::allocateTimer(3);
Serial.begin(115200);
// set pin modes for the GPIO
pinMode(motorLD, OUTPUT);
pinMode(motorRD, OUTPUT);
digitalWrite(motorLD, LOW);
digitalWrite(motorLD, LOW);
// configure LED PWM functionalities
// pin modes and interrupts
pinMode(interruptPinR, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPinR), addRotR, CHANGE);
pinMode(interruptPinL, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPinL), addRotL, CHANGE);
sorter.calibrate();
sorter2.calibrate();
Serial.println("Setup Complete");
delay(1000);
xTaskCreatePinnedToCore(Task1code,"Task1", 10000, NULL, 5, &Task1, 0);
xTaskCreatePinnedToCore(Task2code,"Task2", 10000, NULL, 5, &Task2, 1);
}
void loop() {}