Repository navigation
Expand file tree
/
Copy pathbot_move.h
More file actions
164 lines (135 loc) · 4.42 KB
/
Copy pathbot_move.h
File metadata and controls
164 lines (135 loc) · 4.42 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
#include <RedBotSoftwareSerial.h>
#include <RedBot.h>
RedBotMotors motors;
RedBotEncoder encoder = RedBotEncoder(A2, 10);
//Turn Delay
int turn = 430;
//Motor Power - May be need to change the varibles and offsets
int motorL = 200;
int motorR = motorL*0.95;
//Motor Power for Turn
int motorTL = 175;
int motorTR = motorTL*0.90;
//Forward Delay
int forward = 755;
int back = 745;
//Rotary
int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev
float wheelDiam = 2.56; // diam = 65mm / 25.4 mm/in
float wheelCirc = PI*wheelDiam; // Redbot wheel circumference = pi*D
void setup()
{
Serial.begin(9600);//begins serial monitor
pinMode(12, INPUT_PULLUP);
}
void moveForward(int forward)
{
float distance = 9;
int motorPower = 170;
for(int go=0; go<forward; go++)
{
delay(1000);
long lCount = 0;
long rCount = 0;
long targetCount;
float numRev;
// variables for tracking the left and right encoder counts
long prevlCount, prevrCount;
long lDiff, rDiff; // diff between current encoder count and previous count
// variables for setting left and right motor power
int leftPower = motorPower;
int rightPower = motorPower;
// variable used to offset motor power on right vs left to keep straight.
int offset = 5; // offset amount to compensate Right vs. Left drive
numRev = distance / wheelCirc; // calculate the target # of rotations
targetCount = numRev * countsPerRev; // calculate the target count
// debug
Serial.print("driveStraight() ");
Serial.print(distance);
Serial.print(" inches at ");
Serial.print(motorPower);
Serial.println(" power.");
Serial.print("Target: ");
Serial.print(numRev, 3);
Serial.println(" revolutions.");
Serial.println();
// print out header
Serial.print("Left\t"); // "Left" and tab
Serial.print("Right\t"); // "Right" and tab
Serial.println("Target count");
Serial.println("============================");
encoder.clearEnc(BOTH); // clear the encoder count
delay(100); // short delay before starting the motors.
motors.drive(motorPower); // start motors
while (rCount < targetCount)
{
// while the right encoder is less than the target count -- debug print
// the encoder values and wait -- this is a holding loop.
lCount = encoder.getTicks(LEFT);
rCount = encoder.getTicks(RIGHT);
Serial.print(lCount);
Serial.print("\t");
Serial.print(rCount);
Serial.print("\t");
Serial.println(targetCount);
motors.leftDrive(leftPower);
motors.rightDrive(rightPower);
// calculate the rotation "speed" as a difference in the count from previous cycle.
lDiff = (lCount - prevlCount);
rDiff = (rCount - prevrCount);
// store the current count as the "previous" count for the next cycle.
prevlCount = lCount;
prevrCount = rCount;
// if left is faster than the right, slow down the left / speed up right
if (lDiff > rDiff)
{
leftPower = leftPower - offset;
rightPower = rightPower + offset;
}
// if right is faster than the left, speed up the left / slow down right
else if (lDiff < rDiff)
{
leftPower = leftPower + offset;
rightPower = rightPower - offset;
}
delay(50); // short delay to give motors a chance to respond.
}
// now apply "brakes" to stop the motors.
motors.brake();
}
}
void turnRight(double turnR) //turns by # - turnRight(#);
{
for(int go=0; go<turnR; go++)
{
delay(1000);
motors.leftMotor(0-motorTL);
motors.rightMotor(motorTR);
delay(turn);
motors.brake();
}
}
void turnLeft(double turnL) //turns by # - turnLeft(#);
{
for(int go=0; go<turnL; go++)
{
delay(1000);
motors.leftMotor(motorTL);
motors.rightMotor(0-motorTR);
delay(turn);
motors.brake();
}
}
void moveBack(double bdis) //moves backwards by # of tile - moveBack(#);
{
for(int go=0; go<bdis; go++)
{
delay(1000);
motors.leftMotor(0-motorL);
motors.rightMotor(0-motorR);
delay(back);
motors.brake();
}
}
//sensors when on tile == 890, 921, 915-917
//sensors on dark surface 870-875, 900-910, 980-990