-
Notifications
You must be signed in to change notification settings - Fork 0
/
modeteleoperated.cpp
182 lines (150 loc) · 4.85 KB
/
modeteleoperated.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
#include "modeteleoperated.h"
#include <Timer.h>
#include <Utility.h>
#include <sysLib.h>
#include <ctime>
#include <unistd.h>
#include <stdio.h>
ModeTeleoperated::ModeTeleoperated(DriverStation *ds) : ModeBase(ds), lockarms(false), showlag(false){
OUT("Teleop Construct");
}
ModeTeleoperated::~ModeTeleoperated(){
OUT("Teleop Destroy");
}
void ModeTeleoperated::begin(){
m_ds->InOperatorControl(true);
OUT("Teleop Begin");
compressor->Start();
mainLights->setMode(ArduinoControl::Teleop);
lockarms = false;
//holdlights = false;
shootball = false;
passball = false;
drivesys->checkDead();
showlag = SD_GB("SHOW_LAG_OUTPUT");
shooter_delay = SD_GN("SHOOTER_DELAY");
timeout_started = false;
axii = op->getAxisInstance();
btns = op->getButtonsInstance();
airsys->unShootBall();
}
void ModeTeleoperated::run(){
uint32_t starttime = GetFPGATime();
op->jsBaseTick();
op->jsBaseTickAxis();
uint32_t inputtime = GetFPGATime();
// Drive
drivesys->vroomvrum();
uint32_t jagtime = GetFPGATime();
mainLights->setMode(ArduinoControl::Teleop);
// Arm open/close/latch
if(btns->button5){
openarms = true;
lockarms = false;
} else {
if(btns->button3){
if(lockarms || psensor->Get() == 0){
openarms = false;
lockarms = true;
mainLights->setMode(ArduinoControl::HasBall);
} else {
openarms = true;
mainLights->setMode(ArduinoControl::WaitCatch);
}
} else {
openarms = false;
lockarms = false;
}
}
// Piston shoot/pass/delay
if(btns->button4){
shootball = true;
passball = false;
// cancel any timeout
shooter_timeout.Stop();
timeout_started = false;
} else if(btns->button6){
passball = true;
shootball = false;
shooter_timeout.Stop();
timeout_started = false;
} else {
// Shooter timeout logic
if(axii->trigger >= 0.5){
openarms = true;
// Check if timeout has started
if(timeout_started){
// Check if timeout has passed
if(shooter_timeout.HasPeriodPassed(shooter_delay)){
OUT("Shoot!");
// Shoot ball
shootball = true;
passball = false;
}
} else {
OUT("Wait to shoot...");
// Start timeout
shooter_timeout.Reset();
shooter_timeout.Start();
timeout_started = true;
}
} else {
if(__builtin_expect(timeout_started, 0)){
OUT("Stop Wait");
shooter_timeout.Stop();
timeout_started = false;
}
//openarms = false;
shootball = false;
passball = false;
}
}
// Publish control logic
if(openarms)
airsys->openArm();
else
airsys->closeArm();
if(shootball)
airsys->shootBall();
else if(passball)
airsys->shootCenter();
else
airsys->unShootBall();
// Arm tilt
if(drivesys->isNotDead()){
if(absf(axii->rightStick.y) > 0.1){
handstilt->Set(axii->rightStick.y * axii->rightStick.y * (axii->rightStick.y > 0 ? 1 : -1));
} else {
handstilt->Set(0);
}
}
//SD_PN("Proximity Sensor", psensor->Get());
//mainLights->setFlat();
int pretcptime = GetFPGATime();
// char *tcpdata;
// sprintf(tcpdata, "Run lag %d %d %d\n", inputtime - starttime, jagtime - inputtime, pretcptime - jagtime);
// tcpc->send(tcpdata, strlen(tcpdata));
// This will crash the robot code.
uint32_t diff1 = inputtime - starttime;
uint32_t diff2 = jagtime - inputtime;
uint32_t diff3 = pretcptime - jagtime;
uint32_t diff4 = GetFPGATime() - pretcptime;
// if(diff1 > 55 || diff2 > 20000 || diff3 > 600){ // These are the low cutoffs
// if(diff1 > 200 || diff2 > 25000 || diff3 > 3700){ // These are the medium cutoffs aka the 'more than one other task have been scheduled during this time mark, or something went wrong...'
if(diff1 > 200 || diff2 > 25000 || diff3 > 3700 || diff4 > 40){
if(showlag)
printf("Run lag %d %d %d %d\n", (int)diff1, (int)diff2, (int)diff3, (int)diff4);
}
}
void ModeTeleoperated::end(){
// clear output to other things
drivesys->tchunk();
if(drivesys->isNotDead())
handstilt->Set(0.0);
compressor->Stop();
OUT("Teleop End");
m_ds->InOperatorControl(false);
}
const char *ModeTeleoperated::typeString(){
return "Teleoperated";
}