-
Notifications
You must be signed in to change notification settings - Fork 0
/
YAAB.ino
371 lines (300 loc) · 9.58 KB
/
YAAB.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
YAAB - Yet Another Autococker Board
Arduino based autococker board developed around the platform and ATMEL AVR
chips
Copyright (C) 2012 Dan Silk
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "pins.h"
#include "timers.h"
#include "adc.h"
#include "interrupts.h"
#include "settings.h"
///
/// Program Specific defines - for readability
#define bps_to_cycle_time(bps) 1000 / bps;
/// Cycle Values
/// Values specific to a marker cycle
volatile CycleValues g_CycleValues =
{
0, // Flags using in marker cycle (CycleFlags)
0, // Shots to fire in 'cycle' (Burst)
CS_Ready_To_Fire, // Marker current state
0, // Cycle counter in 0.1ms increments (max 1.6383 seconds)
0 // Current value read from the eyes
};
// Store in EEMEM later
///
/// Marker Settings
/// Things specific to the marker
volatile MarkerSettings g_Settings =
{
100, // Trigger Debounce
0, // Current Profile
0, // Shots since Service?
{ 40, 60, 550, 240 }, // Sear on, C On, C Delay, C Off
{ 10, 100, 1000 } // Eye Detect Time, Eye Ball Reflect, Eye Timeout
};
///
/// Marker Profiles
/// Customisable by the user
MarkerProfile g_Profiles[] =
{
{ "Semi", 0x1, 0x0, AT_Semi, flag_set(TA_FireOnPress) },
{ "Pump", 0x1, 0x0, AT_Pump, flag_set(TA_FireOnPress) },
{ "Auto", 0x1, 0x0, AT_Auto, flag_set(TA_FireOnPress) },
{ "Burst", 0x3, 0x0, AT_Semi, flag_set(TA_FireOnPress) },
{ "React", 0x1, 0x3, AT_Semi, flag_set(TA_FireOnPress) | flag_set(TA_FireOnRelease) },
};
MarkerProfile* g_CurrentProfile = &g_Profiles[g_Settings.currentProfile];
unsigned char g_NumProfiles = sizeof g_Profiles/sizeof(MarkerProfile);
///
/// Used to blink an LED in the loop - to make sure the program is running
//#define KEEP_ALIVE_ACTIVE
#if defined KEEP_ALIVE_ACTIVE
#define KEEP_ALIVE_PIN 5 // Pin 13
#define TRIGGER_PRESSED_PIN 4 // Pin 12
#define KEEP_ALIVE_PORT PORTB
#define KEEP_ALIVE_PORT_REG DDRB
#define KEEP_ALIVE_PULSE 1000
unsigned long lastKeepAlivePulse = 0;
#endif
///
/// Enable serial output
//#define SERIAL_DEBUG
#if defined SERIAL_DEBUG
unsigned char lastEyeState = ES_Empty_Seen;
#endif
///
///
///
void setup()
{
#if defined SERIAL_DEBUG
Serial.begin(9600);
/*
Number of profiles: 5
Size of EyeSettings: 5
Size of MarkerTiming: 6
Size of MarkerSettings: 17
Size of FiringValues: 7
Size of MarkerProfile: 9
*/
Serial.print("Number of profiles: ");
Serial.println(g_NumProfiles);
Serial.print("Size of EyeSettings: ");
Serial.println(sizeof(EyeSettings));
Serial.print("Size of MarkerTiming: ");
Serial.println(sizeof(MarkerTiming));
Serial.print("Size of MarkerSettings: ");
Serial.println(sizeof(MarkerSettings));
Serial.print("Size of CycleValues: ");
Serial.println(sizeof(CycleValues));
Serial.print("Size of MarkerProfile: ");
Serial.println(sizeof(MarkerProfile));
#endif
// Setup pin direction
set_input(CYCLE_PORT_REG, TRIGGER_PIN);
set_output(CYCLE_PORT_REG, SEAR_PIN);
set_output(CYCLE_PORT_REG, PNEU_PIN);
set_output(EYE_PORT_REG, EYE_PIN);
set_input(EYE_PORT_REG, IRED_PIN);
#if defined KEEP_ALIVE_ACTIVE
// Set pins for Keep alive LED
// Just to prove the loop is ticking over
set_output(KEEP_ALIVE_PORT_REG, KEEP_ALIVE_PIN);
set_output(KEEP_ALIVE_PORT_REG, TRIGGER_PRESSED_PIN);
#endif
/*
set_input(INPUT_PORT_REG, UP_BUTTON_PIN);
set_input(INPUT_PORT_REG, OK_BUTTON_PIN);
set_input(INPUT_PORT_REG, DN_BUTTON_PIN);
*/
// Enable internal pullups
output_high(CYCLE_PORT, TRIGGER_PIN);
/*
output_high(INPUT_PORT, UP_BUTTON_PIN);
output_high(INPUT_PORT, OK_BUTTON_PIN);
output_high(INPUT_PORT, DN_BUTTON_PIN);
*/
cli();
timer_init();
adc_init();
//trigger_init();
// read initial trigger state
if(input_value(CYCLE_PORT, TRIGGER_PIN) == LOW)
bit_set(g_CycleValues.flags, CF_Trigger_Pressed);
else
bit_clear(g_CycleValues.flags, CF_Trigger_Pressed);
sei();
#if defined SERIAL_DEBUG
Serial.println("Setup complete");
#endif
}
void loop()
{
if(input_value(CYCLE_PORT, TRIGGER_PIN) != !is_bit_set(g_CycleValues.flags, CF_Trigger_Pressed))
{
onExternalChange();
#if defined KEEP_ALIVE_ACTIVE
output_toggle(KEEP_ALIVE_PORT, TRIGGER_PRESSED_PIN);
#endif
}
#if defined SERIAL_DEBUG
if(lastEyeState != g_CycleValues.eyesState)
{
lastEyeState = g_CycleValues.eyesState;
Serial.print("Eye State: ");
Serial.println(lastEyeState, HEX);
}
#endif
#if defined KEEP_ALIVE_ACTIVE
if(millis() - lastKeepAlivePulse > KEEP_ALIVE_PULSE)
{
lastKeepAlivePulse = millis();
output_toggle(KEEP_ALIVE_PORT, KEEP_ALIVE_PIN);
}
#endif
}
// Change marker state
inline void changeState(unsigned char newState)
{
g_CycleValues.markerState = (CycleStates)newState;
g_CycleValues.cycleCount = 0;
}
// actually fire the marker
inline void startCycle()
{
startTimer();
// Shot fired
if(!is_bit_set(g_CurrentProfile->actionType, AT_Auto))
g_CycleValues.shotsToGo--;
// Increment shots fired
g_Settings.shotsSinceLastReset++;
// Set Sear High (Release hammer)
output_high(CYCLE_PORT, SEAR_PIN);
// We are now firing
changeState(CS_Sear_Firing);
}
inline void fireMarker()
{
// stop counting for debounce
bit_clear(g_CycleValues.flags, CF_Debounce_Charge);
// Determine how many shots to fire this 'cycle'
if(is_bit_set(g_CycleValues.flags, CF_Trigger_Pressed))
g_CycleValues.shotsToGo = g_CurrentProfile->shotsToFirePress;
else
g_CycleValues.shotsToGo = g_CurrentProfile->shotsToFireRelease;
startCycle();
}
///
/// Interrupts
///
///
/// External Interrupt 0 Changed
inline void onExternalChange()
{
// Toggle the trigger pressed flag
bit_toggle(g_CycleValues.flags, CF_Trigger_Pressed);
// Clear counter
g_CycleValues.cycleCount = 0;
// Do we want to check for debounce?
bool triggerPressed = is_bit_set(g_CycleValues.flags, CF_Trigger_Pressed);
if ((triggerPressed&& is_bit_set(g_CurrentProfile->triggerAction, TA_FireOnPress))
|| (!triggerPressed && is_bit_set(g_CurrentProfile->triggerAction, TA_FireOnRelease)))
{
bit_set(g_CycleValues.flags, CF_Debounce_Charge);
}
else
{
bit_clear(g_CycleValues.flags, CF_Debounce_Charge);
}
}
///
/// Timer Tick
inline void onTimerTick()
{
if(g_CycleValues.markerState == CS_Ready_To_Fire && is_bit_set(g_CycleValues.flags, CF_Debounce_Charge))
{
// increment cycle time
g_CycleValues.cycleCount++;
if(g_CycleValues.cycleCount >= g_Settings.debounceTime)
{
fireMarker();
}
}
else
{
// increment cycle time
g_CycleValues.cycleCount++;
switch(g_CycleValues.markerState)
{
case CS_Sear_Firing:
if(bit_is_set(CYCLE_PORT, SEAR_PIN) && g_CycleValues.cycleCount >= g_Settings.timings.searOn)
{
// Turn off sear
output_low(CYCLE_PORT, SEAR_PIN);
}
if(g_CycleValues.cycleCount >= g_Settings.timings.pneuDel)
{
// TODO: Something about pump!
// Turn on pneumatics
output_high(CYCLE_PORT, PNEU_PIN);
changeState(CS_Breech_Opening);
}
break;
case CS_Breech_Opening:
if(is_bit_set(g_CycleValues.flags, CF_Use_Eyes))
{
// TODO: INSERT EYE LOGIC HERE
// Start read if needed
// else compare read for empty
// then compare read for ball seen
// Change state once ball seen long enough
}
else if(g_CycleValues.cycleCount == g_Settings.timings.pneuOn)
{
// Turn off pneumatics
output_low(CYCLE_PORT, PNEU_PIN);
changeState(CS_Breech_Closing);
}
break;
case CS_Breech_Closing:
if(g_CycleValues.cycleCount >= g_Settings.timings.pneuOff)
{
if(g_CycleValues.shotsToGo > 0 || (is_bit_set(g_CurrentProfile->actionType, AT_Auto) && is_bit_set(g_CycleValues.flags, CF_Trigger_Pressed)))
{
// Fire another shot
startCycle();
}
else
{
// Ready for next shot
changeState(CS_Ready_To_Fire);
stopTimer();
}
}
break;
};
}
}
///
/// ADC Conversion Complete
inline void onADCReadComplete()
{
// Read the value ADC for a value between 0-255
if(ADCH >= g_Settings.eyeSettings.eyeBall)
g_CycleValues.eyesState = ES_Ball_Seen;
else
g_CycleValues.eyesState = ES_Empty_Seen;
}