-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
299 lines (223 loc) · 5.9 KB
/
main.c
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
//Main c program for offense
//Matthew Page
//Started on March 1, 2014
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
//OUTPUTS
#define ENABLE PD2 //Enables signal to board
#define THROW PD3 //Activate solenoid
#define BUMP0 PD4 //First bumper control
#define BUMP1 PD5 //Second bumper control
#define STEP PD6 //Clk signal to stepper, only pin possible
#define DIRECT PD7 //Sets direction, 0=CCW; 1=CW
//INPUTS
#define SenseCCW (PIND & (1 << PD0)) // Counter clockwise limit sensor from driver board.
#define SenseCW (PIND & (1 << PD1)) // Clockwise limit sensor from driver board.
#define Defence (PINC & (1<<PC0)) // Define the defense input given by the board
#define Play1 (PINC & (1 << PC1)) //Play selector for pin c 1
#define Play2 (PINC & (1 << PC2)) //Play selector for pin c 2
#define CCW 0
#define CW 1
//define variables
volatile int step_count;
unsigned short int num_step;
unsigned char turn;
unsigned int steps_taken;
//initialize functions
void Steps(unsigned int Steps, unsigned char Dir);
void Delay(unsigned int Delay);
ISR (TIMER0_COMPA_vect){
step_count++; //increment the counter
}
//////////**************** OFFENSE **************///////////
void aim1(void){
Steps(53, CCW);
return;
}
void aim2(void){
Steps(70, CW);
return;
}
void aim3(void){
Steps(80, CCW);
return;
}
void unaim1(void){
Steps(53, CW);
return;
}
void unaim2(void){
Steps(70, CCW);
return;
}
void unaim3(void){
Steps(80, CW);
return;
}
void calibrateR(void){ //calibrates the right hand side
while(SenseCW == 0){
Steps(1, CCW);
}
return;
}
void calibrateL(void){ //calibrates the lefthand side
steps_taken = 0;
while(SenseCCW == 0){
Steps(1, CW);
steps_taken++;
}
return;
}
void centre(unsigned int middle){
middle = steps_taken/2;
Steps(middle, CCW);
return;
}
void Steps(unsigned int Steps, unsigned char Dir){
step_count = 0;
if(Dir == CW){
PORTD |= (CW<<DIRECT);
} else if(Dir == CCW){
PORTD &= ~(CW<<DIRECT);
}
TCCR0A |= (1<<WGM01) | (1<<COM0A0); //Set CTC and output toggle
TCCR0B |= (1<<CS02) |(1<<CS00); //1024 prescale
OCR0A = 20; // Sets output compare value,
TIMSK0 |= (1<<OCIE0A); //Set ISR compare vector
sei(); //Enable interrupts
while(step_count < Steps){}
return;
}
void bumpers(void){ //Simple turn bumpers on
PORTD &= ~(1<<BUMP0);
PORTD |= (1<<BUMP1);
return;
}
void fire(void){
Delay(1000);
PORTD ^= (1<<THROW);
Delay(400);
PORTD ^= (1<<THROW);
Delay(1000); //Take out while loop and this line for discrete fnct
return;
}
//////////**************** DEFENSE **************///////////
void Steps2(unsigned int Steps, unsigned char Dir){
step_count = 0;
if(Dir == CW){
PORTD |= (CW<<DIRECT);
} else if(Dir == CCW){
PORTD &= ~(CW<<DIRECT);
}
TCCR0A |= (1<<WGM01) | (1<<COM0A0); //Set CTC and output toggle
TCCR0B |= (1<<CS01) |(1<<CS00); //64 prescale
OCR0A = 12; //focra = 5000Hz
TIMSK0 |= (1<<OCIE0A); //Set ISR compare vector
sei(); //Enable interrupts
while(step_count < Steps){}
return;
}
void d_calibrateR(void){ //calibrates the right hand side
while(SenseCW == 0){
Steps(1, CCW);
}
return;
}
void d_calibrateL(void){ //calibrates the lefthand side
steps_taken = 0;
while(SenseCCW == 0){
Steps(1, CW);
steps_taken++;
}
return;
}
void d_centre(unsigned int middle){
middle = steps_taken/2;
Steps2(middle, CCW);
return;
}
void block1(void){
Steps2(6000, CW);
return;
}
void unblock1(void){
Steps2(6000,CCW);
return;
}
void block2(void){
Steps2(8000, CCW);
return;
}
void unblock2(void){
Steps2(8000, CW);
return;
}
void Delay(unsigned int Delay){ //can change to char later when second delay not needed
int i;
for(i = 0; i < Delay; i++) {
TCCR0A = 0;
TCCR0B = (1<<CS02) | (1<<CS00); // CLK/1024, No Waveform Generation
OCR0B = 7; //Approx 2mS
TCNT0 = 0;
TIFR0 = (1 << OCF0B);
while ( !(TIFR0 & (1<<OCF0B)));
}
TCCR0B = 0; // Turn Timer Off
i = 0;
}
void play_1(void){
calibrateR();
Delay(500);
calibrateL();
Delay(500);
centre(steps_taken);
Delay(500);
aim1();
fire();
unaim1();
aim2();
fire();
aim3();
fire();
return;
}
void defense(void){
d_calibrateR();
Delay(500);
d_calibrateL();
Delay(500);
d_centre(steps_taken);
Delay(500);
block1();
Delay(500);
unblock1();
Delay(500);
block2();
Delay(500);
unblock2();
Delay(500);
return;
}
int main(void){
DDRD |= (1<<THROW) | (1<<BUMP0) | (1<<BUMP1); //Initializing outputs
DDRD |= (1<<ENABLE) | (1<<STEP) | (1<<DIRECT);
DDRD &= ~(1<<PD1) | ~(1<<PD0); //Initialize inputs
PORTD |= (1<<THROW); //Solenoid needs to be down
DDRC |= (1<<PC3); //Setting up an output so i can choose the plays
DDRC &= ~(1<<PC1) | ~(1<<PC2) | ~(1<<PC0); //Define input pins for plays and offence/defence
PORTC |= (1<<PC3); //Always output a 1
//#define Play1 (PINC & (1 << PC1))
do{
if(Defence == 0){
do{
defense();
}
while(1);
}else if((Play1 == 0) & (Play2 == 0) & (Defence == 1)){
do{
play_1();
} while(1);
}
} while(1);
}