forked from anidev/612-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
override_controls.cpp
260 lines (236 loc) · 7.5 KB
/
override_controls.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* override_controls.cpp
*
* Copyright (c) 2011, 2012 Chantilly Robotics <chantilly612@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Controls for overriding automatic shooting
*/
#include <cstdio>
#include <Timer.h>
#include "612.h"
#include "vision/vision_processing.h"
#include "override_controls.h"
#include "ports.h"
#include "visionalg.h"
#include "vision_alt.h"
#include "launch_counter.h"
#include "pid_controller.h"
#include "two_jags.h"
#include "trajectory.h"
#include "winch.h"
#include "shooter.h"
#include "buttons.h"
#include "vision_thread.h"
target KEY_TARGET = target::make_target(((98.0 + 11.0)/12) - robot_height, 12.0, 0);
target FENDER_TARGET = target::make_target(((98.0 + 11.0) / 12) - robot_height, 38.5/12, 0);
trajectory KEY_TRAJECTORY = calculate_trajectory_entryangle(KEY_TARGET, DEFAULT_ENTRYANGLE_BACKBOARD);
trajectory FENDER_TRAJECTORY = calculate_trajectory_entryangle(FENDER_TARGET, DEFAULT_ENTRYANGLE_BACKBOARD);
trajectory cur_trajectory;
void do_turret_rotation();
void do_bridge_arm();
void do_turret_winch(bool, bool, bool, bool);
void do_launcher_wheel(bool, bool, bool, bool, bool);
void do_rollers();
const int TURRET_ROTATION = 1;
const int SHOOT_SPIN = 2;
//const int ACQUIRE_TARGET = 3;
const int SHOOT_KEY = 4;
const int SHOOT_FENDER = 5;
const int ROLLERS_UP = 6;
const int ROLLERS_DOWN = 7;
const int WINCH_OVERRIDE = 8;
const int SHOOTER_OVERRIDE = 9;
const int BRIDGE_ARM_DOWN = 10;
const int BRIDGE_ARM_UP = 11;
//don't ask me WHY this won't work with buttons& OR with GenericHID&
joysmooth& joy = gunner_joystick;
void gunner_override_controls() {
// bool acquire = joy.GetRawButton(ACQUIRE_TARGET); // moved to states
bool acquire = false;
// legacy
if (acquire) {
target* t = ascertain_primary_target();
// bool have_trajectory = false; // unused
if (t) {
//have target
cur_trajectory = projected_trajectory(t);
}
else {
cur_trajectory.velocity = 0.0;
cur_trajectory.angle = 0.0;
}
}
do_turret_rotation();
do_bridge_arm();
bool key = joy.GetRawButton(SHOOT_KEY);
bool fender = joy.GetRawButton(SHOOT_FENDER);
do_turret_winch(key, fender, joy.GetRawButton(WINCH_OVERRIDE), acquire);
do_launcher_wheel(key, fender, joy.GetRawButton(SHOOTER_OVERRIDE), acquire, joy.GetRawButton(SHOOT_SPIN));
do_rollers();
}
void do_turret_rotation() {
if (joy.GetRawButton(TURRET_ROTATION)) {
shooter_turret.Susan().manual_control(gunner_joystick.GetX());
}
else {
shooter_turret.Susan().manual_control(0.0);
}
}
void do_bridge_arm() {
if(joy.GetRawButton(BRIDGE_ARM_UP)){//up
bridge_arm_up();
}
else if(joy.GetRawButton(BRIDGE_ARM_DOWN)){//down
bridge_arm_down();
}
else {
bridge_arm_neutral();
}
}
void bridge_arm_up() {
bridge_arm.set_direction(bridge_arm_t::UP);
}
void bridge_arm_down() {
bridge_arm.set_direction(bridge_arm_t::DOWN);
}
void bridge_arm_neutral() {
bridge_arm.set_direction(bridge_arm_t::NEUTRAL);
}
void do_turret_winch(bool key, bool fender, bool override, bool acquire) {
static float winch_z = 0.0;
/*
if(gunner_joystick.GetRawButton(WINCH_UP)) {
shooter_turret.Winch().disable();
shooter_turret.Winch().manual_control(winch::UP);
}
else if(gunner_joystick.GetRawButton(WINCH_DOWN)) {
shooter_turret.Winch().disable();
shooter_turret.Winch().manual_control(winch::DOWN);
}
else if(!shooter_turret.Winch().is_enabled()) {
shooter_turret.Winch().manual_control(winch::OFF);
shooter_turret.Winch().enable();
}
*/
float new_winch_z = winch_z;
if (key) {
// hard coding
//TODO: Remove
new_winch_z = deg2rad(74.4); // 61.0
//new_winch_z = KEY_TRAJECTORY.angle;
}
else if (fender) {
// hard coding
//TODO: Remove
new_winch_z = deg2rad(86.3);
//new_winch_z = FENDER_TRAJECTORY.angle;
}
else if (override) {
new_winch_z = deg2rad((-gunner_joystick.GetZ()+1)*22.5+45);
}
else if (acquire) {
if (cur_trajectory.velocity != 0) {
new_winch_z = cur_trajectory.angle;
}
}
if(new_winch_z != winch_z) {
winch_z = new_winch_z;
// std::printf("
shooter_turret.Winch().set_angle(winch_z);
}
/* if(gunner_joystick.GetRawButton(8)) {
shooter_turret.Winch().manual_control(winch::UP);
}
else if(gunner_joystick.GetRawButton(9)) {
shooter_turret.Winch().manual_control(winch::DOWN);
}
else {
shooter_turret.Winch().manual_control(winch::OFF);
}*/
}
void do_launcher_wheel(bool key, bool fender, bool override, bool acquire, bool spin) {
static float shoot_freq = 0.0;
static bool enabled = false;
float new_shoot_freq = shoot_freq;
if (key) {
/* hard coding */
//TODO: Remove
new_shoot_freq = 62.7; // 75.0
//new_shoot_freq = shooter::ballspeed_to_rps(KEY_TRAJECTORY.velocity, KEY_TRAJECTORY.angle);
}
else if (fender) {
/* hard coding */
//TODO: Remove
new_shoot_freq = 55.0;
//new_shoot_freq = shooter::ballspeed_to_rps(FENDER_TRAJECTORY.velocity, FENDER_TRAJECTORY.angle);
}
else if (override) {
new_shoot_freq = (-gunner_joystick.GetZ()+1)*15+45;
}
else if (acquire) {
if (cur_trajectory.velocity != 0.0) {
new_shoot_freq = shooter::ballspeed_to_rps(cur_trajectory.velocity, cur_trajectory.angle);
}
}
if (new_shoot_freq != shoot_freq) {
shoot_freq = new_shoot_freq;
shooter_turret.Shooter().set_freq(shoot_freq);
}
if (!enabled) {
if (spin) {
enabled = true;
shooter_turret.Shooter().enable();
}
}
else if (!spin) {
enabled = false;
shooter_turret.Shooter().disable();
}
}
void do_rollers() {
if (joy.GetRawButton(ROLLERS_UP)) {
rollers_up();
}
else if (joy.GetRawButton(ROLLERS_DOWN)) {
rollers_down();
}
else {
rollers_off();
}
}
void rollers_up() {
rollers.set_direction(roller_t::UP);
}
void rollers_down() {
rollers.set_direction(roller_t::DOWN);
}
void rollers_off() {
rollers.set_direction(roller_t::OFF);
}
void shoot_key() {
do_launcher_wheel(true, false, false, false, false);
do_turret_winch(true, false, false, false);
}
void shoot_fender() {
do_launcher_wheel(false, true, false, false, false);
do_turret_winch(false, true, false, false);
}
void shoot_spin() {
do_launcher_wheel(false, false, false, false, true);
}
void turret_acquire() {
do_launcher_wheel(false, false, false, true, false);
do_turret_winch(false, false, false, true);
}