forked from Team612/612-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriveTrain.cpp
executable file
·164 lines (155 loc) · 4.32 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
#include "DriveTrain.h"
#include <Talon.h>
#include "612.h"
#include "main.h"
const double DriveTrain::PI = 3.141592653;
const double DriveTrain::CIRCUMROBOT = 2 * PI * ROBOTRAD;
bool DriveTrain::isMovingL = false;
bool DriveTrain::isMovingR = false;
bool DriveTrain::isTurningL = false;
bool DriveTrain::isTurningR = false;
const float DriveTrain::SPEED = 1.0f;
DriveTrain::DriveTrain(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))
{
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);
robot -> update -> addFunctions(&updateHelper, (void*) this);
printf("Drivetrain has been updated\n");
}
DriveTrain::~DriveTrain()
{
delete encode;
}
void DriveTrain::autoDrive(double distance)
{
NeededDist = distance;
TankDrive(SPEED, SPEED);
isMovingL = true;
isMovingR = true;
encode->EncoderL->Start();
encode->EncoderR->Start();
}
void DriveTrain::autoTurn(double degrees) // any degrees less than zero (0) will turn right; basically the unit circle
{
double degrees2Radians = degrees * (PI/180);
double arcLength = CIRCUMROBOT * (degrees2Radians/(2 * PI)); // 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;
}
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)
{
TankDrive(-1*power,power);
}
}
void DriveTrain::update()
{
float speedL;
float speedR;
if (isMovingL || isMovingR)
{
speedL = SPEED;
if (encode->getLDistance() >= NeededDist)
{
encode->EncoderL->Stop();
encode->EncoderL->Reset();
isMovingL = false;
speedL = 0.0f;
}
speedR = SPEED;
if (encode->getRDistance() >= NeededDist)
{
encode->EncoderR->Stop();
encode->EncoderR->Reset();
isMovingR = false;
speedR = 0.0f;
}
TankDrive(speedL, speedR);
}
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;
}
isTurningL = false;
TankDrive(-speedL, speedR);
}
if (isTurningR) // NeededDist is negative
{
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;
}
isTurningR = false;
TankDrive(-speedL, speedR);
}
}
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;
}