forked from arduino/BasicEducationShield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWheels.cpp
95 lines (77 loc) · 1.6 KB
/
Wheels.cpp
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
#include "BasicEducationShield.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
Wheels::Wheels(int lpin, int rpin){
this->lpin=lpin;
this->rpin=rpin;
}
void Wheels::begin(){
//Must be called in setup
top = 120;
low = 60;
still = 90;
fromL = still;
fromR = still;
toL = still;
toR = still;
left.attach(lpin);
delay(1000);
right.attach(rpin);
}
void Wheels::go(int tl, int tr){
this->tl=tl;
this->tr=tr;
int lSpeed = fromL;
int rSpeed = fromR;
for(int i=0; i<(top-low); i++){
if(lSpeed<tl) lSpeed++;
else if(lSpeed>tl) lSpeed--;
if (rSpeed<tr) rSpeed++;
else if(rSpeed>tr) rSpeed--;
left.write(lSpeed);
right.write(rSpeed);
delay(20);
if(lSpeed==tl && rSpeed==tr) i=top-low;
}
fromL=tl;
fromR=tr;
}
void Wheels::goForward(){
toL = low;
toR = top;
go(toL, toR);
}
void Wheels::goBackwards(){
toL = top;
toR = low;
go(toL, toR);
}
void Wheels::turnLeft(){
toL = top;
toR = top;
go(toL, toR);
}
void Wheels::turnRight(){
toL = low;
toR = low;
go(toL, toR);
}
void Wheels::standStill(){
toL = still;
toR = still;
go(toL, toR);
}
void Wheels::follow(int d){
int leftSpeed = constrain(ROBOT_SPEED+d, -100, 100);
int rightSpeed = constrain(ROBOT_SPEED-d, -100, 100);
leftSpeed = map(-leftSpeed, -100, 100, 40, 140);
rightSpeed = map(rightSpeed, -100, 100, 40, 140);
Serial.print(leftSpeed);
Serial.print(" ");
Serial.println(rightSpeed);
left.write(leftSpeed);
right.write(rightSpeed);
}