-
Notifications
You must be signed in to change notification settings - Fork 0
/
TankDrive.cpp
240 lines (209 loc) · 6.1 KB
/
TankDrive.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
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
/*
TankDrive.cpp - Library to create TankDrive
Created by Scott Davidson, June 28 2016.
Released into the public domain
*/
#include <Arduino.h>
#include "TankDrive.h"
#include "Trapezoid.h"
#include "mpu.h"
#include "PID_sd.h"
TankDrive::TankDrive(int speedPinLeft, int speedPinRight, int forwardPinRight, int reversePinRight, int forwardPinLeft, int reversePinLeft)
{
// connect motor controller pins to Arduino digital pins
// motor Left
_speedPinLeft = speedPinLeft;
_forwardPinLeft = forwardPinLeft;
_reversePinLeft = reversePinLeft;
// motor two
_speedPinRight = speedPinRight;
_forwardPinRight = forwardPinRight;
_reversePinRight = reversePinRight;
// set all the motor control pins to outputs
pinMode(speedPinLeft, OUTPUT);
pinMode(speedPinRight, OUTPUT);
pinMode(forwardPinLeft, OUTPUT);
pinMode(reversePinLeft, OUTPUT);
pinMode(forwardPinRight, OUTPUT);
pinMode(reversePinRight, OUTPUT);
_usePID=1; //Will not use PID by default.
PID MotorPID = PID();
Trapezoid TrapPath = Trapezoid();
}
int TankDrive::InitializeMPU()
{
int notStable = 1;
int stableRead = 0;
int oldAngle=180;
long calibrationStart = millis();
_mpuRet = mympu_open(200);
Serial.print("MPU init: "); Serial.println(_mpuRet);
Serial.println("Starting MPU stabilization check. This can take a minute.");
Serial.println("The MPU requires 8 seconds of no movement to calibrate the Gyro with Accelerometer.");
delay(8000); //Delay 2 seconds to settle the minitbot for MPU initialization
while (notStable){
_mpuRet = mympu_update();
_currentAngle = mympu.ypr[0];
if (_currentAngle == oldAngle) {
stableRead=stableRead+1;
}
else {
stableRead=0;
}
//Serial.println(_currentAngle);
if (stableRead > 5000 and notStable){
Serial.print("Stablised in ");Serial.print((millis()-calibrationStart)/1000);Serial.print(" seconds");
Serial.print(" at ");Serial.print(_currentAngle);Serial.println(" degrees.");
notStable=0;
}
oldAngle=_currentAngle;
}
}
void TankDrive::fullStop ()
{
// now turn off motors
digitalWrite(_forwardPinLeft, LOW);
digitalWrite(_reversePinLeft, LOW);
digitalWrite(_forwardPinRight, LOW);
digitalWrite(_reversePinRight, LOW);
}
void TankDrive::doTurn(int turnDegrees)
{
_mpuRet = mympu_update();
float baseAngle = mympu.ypr[0];
int cSpeed = 120;
float cAngle = 0;
float correction=0;
float correctLeft=0;
float correctRight=0;
while(cAngle < turnDegrees) {
_mpuRet = mympu_update();
_currentAngle = mympu.ypr[0];
cAngle = baseAngle-_currentAngle;
cAngle = abs(cAngle);
digitalWrite(_speedPinLeft, cSpeed);
digitalWrite(_speedPinRight, cSpeed);
}
fullStop();
}
void TankDrive::doDrive(int driveTime)
{
_mpuRet = mympu_update();
float baseAngle = mympu.ypr[0];
long startTime = millis();
long cTime = 0;
int cSpeed = 150;
float cAngle = 0;
float correction=0;
float correctLeft=0;
float correctRight=0;
_simpleDeadband = 0.75;
_simpleKp=20;
TrapPath.setRunTime(driveTime);
TrapPath.setStartTime(startTime);
//Using Standard P to control direction
while (millis() - startTime < driveTime) {
_mpuRet = mympu_update();
_currentAngle = mympu.ypr[0];
cSpeed = TrapPath.getSetPoint(millis()-startTime);
cAngle = baseAngle-_currentAngle;
if (_usePID) {
correction = MotorPID.Compute(cAngle,baseAngle);
correctLeft = -correction;
correctRight = correction;
}
else { //use standard Kp multiplier
if (cAngle>_simpleDeadband){
correctLeft = -cAngle*_simpleKp;
correctRight = cAngle*_simpleKp;
}
else if (cAngle<(_simpleDeadband*-1)){
correctLeft = cAngle*_simpleKp;
correctRight = -cAngle*_simpleKp;
}
}
//Serial Monitor report of P follwing
Serial.print("Angle: ");Serial.print(cAngle);
Serial.print(" Speed: ");Serial.print(cSpeed);
//Serial.print(" Correct Left: ");Serial.print(correctLeft);
//Serial.print(" Correct Right: ");Serial.println(correctRight);
Serial.print(" Correct Left: ");Serial.print(constrain(cSpeed+correctLeft,5,255));
Serial.print(" Correct Right: ");Serial.println(constrain(cSpeed+correctRight,5,255));
digitalWrite(_speedPinLeft, constrain(cSpeed+correctLeft,0,255));
digitalWrite(_speedPinRight, constrain(cSpeed+correctRight,0,255));
}
fullStop();
}
void TankDrive::driveForward(int driveTime)
{
// Set motor directions to move forward
digitalWrite(_forwardPinLeft, HIGH);
digitalWrite(_reversePinLeft, LOW);
digitalWrite(_forwardPinRight, HIGH);
digitalWrite(_reversePinRight, LOW);
doDrive (driveTime);
}
void TankDrive::driveReverse(int driveTime)
{
// Set motor directions to move in reverse
digitalWrite(_forwardPinLeft, LOW);
digitalWrite(_reversePinLeft, HIGH);
digitalWrite(_forwardPinRight, LOW);
digitalWrite(_reversePinRight, HIGH);
doDrive (driveTime);
}
void TankDrive::turnLeft(int turnAngle)
{
// Set motor directions to Turn Left
digitalWrite(_forwardPinLeft, LOW);
digitalWrite(_reversePinLeft, LOW);
digitalWrite(_forwardPinRight, HIGH);
digitalWrite(_reversePinRight, LOW);
doTurn(turnAngle);
}
void TankDrive::turnRight(int turnAngle)
{
// Set motor directions to turn Right
digitalWrite(_forwardPinLeft, HIGH);
digitalWrite(_reversePinLeft, LOW);
digitalWrite(_forwardPinRight, LOW);
digitalWrite(_reversePinRight, LOW);
doTurn(turnAngle);
}
/*
int TankDrive::TimeAccel() const
{
return m_trapezoid._timeAccel;
}
*/
void TankDrive::SetTimeAccel(int timeAccel)
{
TrapPath.setAcceleration(timeAccel);
}
void TankDrive::SetMaxSpeed(int maxSpeed)
{
TrapPath.setMaxSpeed(maxSpeed);
}
void TankDrive::SetMinSpeed(int minSpeed)
{
TrapPath.setMinSpeed(minSpeed);
}
/*Trapezoid& TankDrive::Trapezoid()
{
return _trapezoid;
}
*/
Trapezoid& TankDrive::GetTrapezoid()
{
return TrapPath;
}
PID& TankDrive::GetPID()
{
return MotorPID;
}
/*void Trapezoid::SetTrapezoid(const Trapezoid& trapezoid)
{
if (trapezoid.IsValid())
m_trapezoid = trapezoid;
}
*/