-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNitron-FCB.ino
245 lines (205 loc) · 6.38 KB
/
Nitron-FCB.ino
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
/*
* Nerf Vortex Nitron - Brushless Fire Control v0.9.3
*/
#include <Servo.h>
// ESC pulse lengths in uS
const int pusherRunPulse = 1590; // normal run
const int pusherReturnPulse = 1550; // minimum run
const int pusherStopPulse = 1000; // maximum brake
const int flywheelRunPulse = 1700; // normal run
const int flywheelStopPulse = 1520; // coast
// Timers for flywheel in mS
const int flywheelSpoolTime = 50; // 0.05 seconds
const int flywheelRunTime = 250; // 0.25 seconds
// Button debounce timer, in mS
const int debounceDelay = 5;
// Shot queue depth for each mode
const uint8_t shotCount[] = {1, 3, 255};
// Arduino pin configuration
const int flywheelRpmPin = 2;
const int pusherRpmPin = 3;
const int magazinePin = 4;
const int ejectPin = 5;
const int disclPin = 6;
const int discrPin = 7;
const int positionPin = 8;
const int triggerPin = 9;
const int readyPin = 10;
const int flywheelEscPin = 11;
const int pusherEscPin = 12;
// ESCs are operated via the Servo library.
Servo flywheel;
Servo pusher;
// global variables
bool magazinePinState = 0;
bool ejectPinState = 0;
bool disclPinState = 0;
bool discrPinState = 0;
bool positionPinState = 0;
bool triggerPinState = 0;
bool readyPinState = 0;
unsigned int shotMode = 0;
unsigned int shotsRemaining = 0;
unsigned long flywheelStartTime = 0;
unsigned long flywheelTimer = 0;
int pusherPulse = 1520;
int flywheelPulse = 1520;
void setup() {
/*
* This is the setup function which should initialize
* all additional required features and functions.
*/
pinMode(flywheelRpmPin, INPUT_PULLUP);
pinMode(pusherRpmPin, INPUT_PULLUP);
pinMode(magazinePin, INPUT_PULLUP);
pinMode(ejectPin, INPUT_PULLUP);
pinMode(disclPin, INPUT_PULLUP);
pinMode(discrPin, INPUT_PULLUP);
pinMode(positionPin, INPUT_PULLUP);
pinMode(triggerPin, INPUT_PULLUP);
pinMode(readyPin, INPUT_PULLUP);
flywheel.attach(flywheelEscPin);
flywheel.writeMicroseconds(flywheelPulse);
pusher.attach(pusherEscPin);
pusher.writeMicroseconds(pusherPulse);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
/*
* This is the primary loop.
*/
magazinePinState = debounceInput(magazinePin);
ejectPinState = debounceInput(ejectPin);
disclPinState = debounceInput(disclPin);
discrPinState = debounceInput(discrPin);
positionPinState = debounceInput(positionPin);
triggerPinState = debounceInput(triggerPin);
readyPinState = debounceInput(readyPin);
pusherControl();
flywheelControl();
fireControl();
modeSelection();
if (positionPinState) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}
bool debounceInput(int pin) {
/*
* To debounce inputs, we'll use some static arrays to
* store information on which pin is being checked.
*
* We also need to invert the raw switch values since
* using INPUT_PULLUP inverts our results.
*/
static unsigned long timer[22] = {0};
static bool lastState[22] = {0};
static bool currentState[22] = {0};
bool liveValue = !digitalRead(pin);
if (liveValue != lastState[pin]) {
timer[pin] = millis();
lastState[pin] = liveValue;
}
if (millis() - timer[pin] >= debounceDelay) {
currentState[pin] = liveValue;
}
return currentState[pin];
}
void pusherControl() {
/*
* Operate the pusher motor if we need to retract it,
* or if we're sending off a shot and the flywheel is spun up.
*
* Failing to chamber a new round in time, disc eject, or magazine removal forces an automatic stop.
*/
if (ejectPinState && magazinePinState) {
if (positionPinState) {
if (shotsRemaining) {
if (disclPinState && discrPinState && flywheelStartTime && millis() - flywheelStartTime >= flywheelSpoolTime) {
pusherPulse = pusherRunPulse;
}
} else {
pusherPulse = pusherReturnPulse;
}
} else {
if (shotsRemaining) {
if (flywheelStartTime && millis() - flywheelStartTime >= flywheelSpoolTime) {
if (shotsRemaining == 1 && shotMode >= 1) { // Back off the pusher speed early for the last shot
pusherPulse = pusherReturnPulse;
} else {
pusherPulse = pusherRunPulse;
}
}
if(!disclPinState && !discrPinState) { // Should be OR, and have a delay timer
pusherPulse = pusherStopPulse;
}
} else {
pusherPulse = pusherStopPulse;
}
}
} else {
pusherPulse = pusherStopPulse;
}
pusher.writeMicroseconds(pusherPulse);
}
void flywheelControl() {
/*
* Start the flywheel on either trigger,
* Save the initial spool-up time.
* Maintain flywheel speed for a minimum run time
* Disc eject or magazine removal forces an automatic stop.
*/
if ((shotsRemaining || readyPinState) && ejectPinState && magazinePinState){
if (flywheelStartTime == 0) {
flywheelStartTime = millis(); // Save the initial spool-up time.
}
flywheelTimer = millis(); // Reset the timer.
flywheelPulse = flywheelRunPulse;
}
if (millis() - flywheelTimer >= flywheelRunTime || !ejectPinState || !magazinePinState) {
flywheelPulse = flywheelStopPulse;
flywheelStartTime = 0;
}
flywheel.writeMicroseconds(flywheelPulse);
}
void fireControl() {
/*
* Track trigger and chamber states, count off rounds as they go out.
* Trigger release, disc eject, or magazine removal forces an automatic zeroing of the queue.
*/
static bool lastTriggerPinState = 0;
static bool lastpositionPinState = 0;
if (triggerPinState && !lastTriggerPinState && ejectPinState && magazinePinState) {
shotsRemaining = shotCount[shotMode];
}
if (shotsRemaining && !lastpositionPinState && positionPinState) {
shotsRemaining--;
}
if (!triggerPinState || !ejectPinState || !magazinePinState) {
shotsRemaining = 0;
}
lastTriggerPinState = triggerPinState;
lastpositionPinState = positionPinState;
}
void modeSelection() {
/*
* Select fire mode via triggers while disk eject is pulled back.
*/
static bool lastTriggerPinState = 0;
static bool lastReadyPinState = 0;
if (!ejectPinState) {
if (triggerPinState && !lastTriggerPinState) {
if (shotMode < sizeof(shotCount)-1) {
shotMode++;
}
}
if (readyPinState && !lastReadyPinState) {
if (shotMode > 0) {
shotMode--;
}
}
}
lastTriggerPinState = triggerPinState;
lastReadyPinState = readyPinState;
}