-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.h
116 lines (98 loc) · 3.17 KB
/
drive.h
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
#include <RedBot.h>
RedBotMotors motors;
RedBotEncoder encoder = RedBotEncoder(A2, 10);
int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev
float wheelDiam = 2.75; // diam = 65mm / 25.4 mm/in
float wheelCirc = PI * wheelDiam; // Redbot wheel circumference = pi*D
void driveDistance(float distance, int leftMotorPower, int rightMotorPower)
{
long lCount = 0;
long rCount = 0;
float numRev;
numRev = (float) distance / wheelCirc;
encoder.clearEnc(BOTH); // clear the encoder count
delay(250);
motors.leftMotor(leftMotorPower);
motors.rightMotor(rightMotorPower-1);
while (lCount < numRev * countsPerRev)
{
// while the left encoder is less than the target count -- debug print
// the encoder values and wait -- this is a holding loop.
Serial.print(lCount);
Serial.print("\t");
Serial.println(rCount);
lCount = encoder.getTicks(LEFT);
rCount = encoder.getTicks(RIGHT);
}
// now apply "brakes" to stop the motors.
motors.brake();
}
void backward(float distance, int leftMotorPower, int rightMotorPower)
{
long lCount = 0;
long rCount = 0;
float numRev;
numRev = (float) distance / wheelCirc;
encoder.clearEnc(BOTH); // clear the encoder count
delay(250);
motors.leftMotor(-1*leftMotorPower);
motors.rightMotor(-1*rightMotorPower);
while (abs(lCount) < numRev * countsPerRev)
{
// while the left encoder is less than the target count -- debug print
// the encoder values and wait -- this is a holding loop.
Serial.println(lCount);
Serial.print("\t");
Serial.println(rCount);
lCount = encoder.getTicks(LEFT);
rCount = encoder.getTicks(RIGHT);
}
// now apply "brakes" to stop the motors.
motors.brake();
}
void turnRDist(float distance, int leftMotorPower, int rightMotorPower)
{
long lCount = 0;
long rCount = 0;
float numRev;
numRev = (float) distance / wheelCirc;
encoder.clearEnc(BOTH); // clear the encoder count
delay(250);
motors.leftMotor(1*leftMotorPower);
motors.rightMotor(-1*rightMotorPower);
while (abs(lCount) < numRev * countsPerRev)
{
// while the left encoder is less than the target count -- debug print
// the encoder values and wait -- this is a holding loop.
Serial.println(lCount);
Serial.print("\t");
Serial.println(rCount);
lCount = encoder.getTicks(LEFT);
rCount = encoder.getTicks(RIGHT);
}
// now apply "brakes" to stop the motors.
motors.brake();
}
void turnLDist(float distance, int leftMotorPower, int rightMotorPower)
{
long lCount = 0;
long rCount = 0;
float numRev;
numRev = (float) distance / wheelCirc;
encoder.clearEnc(BOTH); // clear the encoder count
delay(250);
motors.leftMotor(1*leftMotorPower);
motors.rightMotor(- 1*rightMotorPower);
while (abs(lCount) < numRev * countsPerRev)
{
// while the left encoder is less than the target count -- debug print
// the encoder values and wait -- this is a holding loop.
Serial.println(lCount);
Serial.print("\t");
Serial.println(rCount);
lCount = encoder.getTicks(LEFT);
rCount = encoder.getTicks(RIGHT);
}
// now apply "brakes" to stop the motors.
motors.brake();
}