-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDriveTrain.cpp
executable file
·194 lines (181 loc) · 5.17 KB
/
DriveTrain.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
#include "DriveTrain.h"
#include <Talon.h>
#include "612.h"
#include "main.h"
const double DriveTrain::SPEED=1.0;
// all in feet
const double DriveTrain::CIRCUMROBOT = 2 * PI * ROBOTRAD;
DriveTrain::DriveTrain(main_robot* r,
uint8_t modFL,uint32_t chanFL,
uint8_t modRL,uint32_t chanRL,
uint8_t modFR,uint32_t chanFR,
uint8_t modRR,uint32_t chanRR)
:RobotDrive(new Talon(modFL,chanFL),
new Talon(modRL,chanRL),
new Talon(modFR,chanFR),
new Talon(modRR,chanRR)),
isMovingL(false),isMovingR(false),
isTurningL(false),isTurningR(false),
hasDriven(false), hasTurned(false)
{
robot = r;
encode = new EncodeDistance(ENCODER_LMODULE_A, ENCODER_LCHANNEL_A,
ENCODER_LMODULE_B, ENCODER_LCHANNEL_B,
ENCODER_RMODULE_A, ENCODER_RCHANNEL_A,
ENCODER_RMODULE_B, ENCODER_RCHANNEL_B);
}
DriveTrain::~DriveTrain()
{
delete encode;
}
void DriveTrain::autoDrive(double distance)
{
stopAuto();
neededDist = distance;
TankDrive(SPEED, SPEED);
isMovingL = true;
isMovingR = true;
encode->EncoderL->Start();
encode->EncoderR->Start();
// originUltraDist = (double)robot->sensors->getUltrasonic();
}
void DriveTrain::autoTurn(double degrees) // any degrees less than zero (0) will turn right; basically the unit circle
{
stopAuto();
double degrees2Radians = degrees * (PI/180);
double arcLength = ROBOTRAD * degrees2Radians; // checks the length of the arc in feet
neededDist = arcLength;
if (degrees > 0) {
TankDrive(-SPEED, SPEED);
isTurningL = true;
}
if (degrees < 0) {
TankDrive(SPEED, -SPEED);
isTurningR = true;
}
hasTurned = false;
encode->EncoderL->Start();
encode->EncoderR->Start();
}
void DriveTrain::teleTurn(Dir direction, double power)
{
if (isAuto())
stopAuto();
if (direction == RIGHT)
TankDrive(power,-1*power);
else if (direction == LEFT) //jank yo!
TankDrive(-1*power,power);
}
void DriveTrain::updateDrive()
{
if (isMovingL || isMovingR)
{
/* speedL = SPEED;
speedR = SPEED;
double varUltraDist = (double)robot->sensors->getUltrasonic();
if (originUltraDist-varUltraDist >= neededDist)
{
originUltraDist = -8999;
isMovingL = false;
isMovingR = false;
speedL = 0.0f;
speedR = 0.0f;
}
if (speedL = 0.0f && speedR == 0.0f)
hasDriven = true;
TankDrive(speedL, speedR);*/
speedL = SPEED; //USING ENCODERS
if (encode->getLDistance() >= neededDist)
{
// encode->EncoderL->Stop();
// encode->EncoderL->Reset();
isMovingL = false;
speedL = 0.0f;
}
speedR = SPEED;
if (encode->getLDistance() >= neededDist)
{
encode->EncoderL->Stop();
encode->EncoderL->Reset();
isMovingR = false;
speedR = 0.0f;
}
if (!isMovingL && !isMovingR)
hasDriven = true;
TankDrive(speedL, speedR);
}
}
void DriveTrain::updateTurn()
{
if (isTurningL) // NeededDist is positive
{
speedL = SPEED;
if (encode->getLDistance() <= -neededDist)
{
encode->EncoderL->Stop();
encode->EncoderL->Reset();
speedL = 0.0f;
}
speedR = SPEED;
if (encode->getRDistance() >= neededDist)
{
encode->EncoderR->Stop();
encode->EncoderR->Reset();
speedR = 0.0f;
}
if(speedL < ZEROTEST && speedR < ZEROTEST)
isTurningL = false;
if (speedL == 0.0f && speedR == 0.0f)
hasTurned = true;
TankDrive(-speedL, speedR);
}
else if (isTurningR)
{
speedL = SPEED;
if (encode->getLDistance() >= -neededDist)
{
encode->EncoderL->Stop();
encode->EncoderL->Reset();
speedL = 0.0f;
}
speedR = SPEED;
if (encode->getRDistance() <= neededDist)
{
encode->EncoderR->Stop();
encode->EncoderR->Reset();
speedR = 0.0f;
}
if(speedL < ZEROTEST && speedR < ZEROTEST)
isTurningR = false;
if (speedL == 0.0f && speedR == 0.0f)
hasTurned = true;
TankDrive(-speedL, speedR);
}
}
void DriveTrain::update()
{
updateDrive();
updateTurn();
}
/*void DriveTrain::updateHelper(void* instName)
{
DriveTrain* driveObj = (DriveTrain*)instName;
driveObj->update();
}*/
bool DriveTrain::isAuto()
{
return ((isMovingL) || (isMovingR) || (isTurningL) || (isTurningR));
}
void DriveTrain::stopAuto()
{
TankDrive(0.0f, 0.0f);
encode->EncoderL->Stop();
encode->EncoderR->Stop();
encode->EncoderL->Reset();
encode->EncoderR->Reset();
isMovingL = false;
isMovingR = false;
isTurningL = false;
isTurningR = false;
hasDriven = false;
}