1 /****************************************************************************
  2  Module
  3      Gun.c
  4 
  5  Description
  6      Contains functions for controlling gun wheel speed and aim.  Also
  7      contains functions for sequencing the ball feeding mechanism.
  8 
  9  Notes
 10      This module is for the project in ME 218B
 11 
 12  History
 13  When           Who     What/Why
 14  -------------- ---     --------
 15 
 16  02/15/10 11:19 NRM      Began Coding
 17 ****************************************************************************/
 18 
 19 
 20 /*----------------------------- Include Files -----------------------------*/
 21 #include <stdio.h>
 22 #include <mc9s12e128.h>  // derivative information
 23 #include <S12E128bits.h> // E128 bit definitions
 24 #include <timers12.h>    // Timer Module
 25 #include <bitdefs.h>     // BIT0HI, BIT0LO....definitions
 26 #include "PWM.h"
 27 #include "Gun.h"
 28 
 29 /*----------------------------- Module Defines ----------------------------*/
 30 #define _Ball_Capacity_ 10  // change according to how many balls we hold
 31 #define _FIRST_DELAY_ 300     // how many ms is the first delay for the gates
 32 #define _SECOND_DELAY_ 300     // how many ms is the 2nd delay for the gates
 33 
 34 /*---------------------------- Module Variables ---------------------------*/
 35 static int Balls_Left = _Ball_Capacity_; // variable for # of balls left
 36                                          // in holder
 37 static int CurrentGateStep = 1;
 38 
 39 
 40 /*------------------------------ Module Code ------------------------------*/
 41 /****************************************************************************
 42  Function
 43      Gun_Motor_Init
 44 
 45  Parameters
 46      None.
 47 
 48  Returns
 49      None.
 50 
 51  Description
 52      Initializes all the ports associated with the Gun System to INPUTS
 53      or OUTPUTS.  Initializes the PWM subsystem or the Gun System.
 54  Notes
 55      PWM Port for motor is initialized in PWM module.
 56 
 57  Author
 58      Nicholas Musser, 02/15/10
 59 ****************************************************************************/
 60 void Gun_Motor_Init(void)
 61 {
 62     TMRS12_Init(TMRS12_RATE_1MS);     // Initialize the S12Timer Module
 63     DDRP = DDRP | BIT4HI;  // Set Bit 4 to output for Gate #2
 64     DDRP = DDRP | BIT3HI;  // Set Bit 3 to output for Gate #1
 65 }
 66 
 67 
 68 
 69 /****************************************************************************
 70  Function
 71      Set_Speed
 72 
 73  Parameters
 74      Percentage of maximum speed desired.  eg. for 50% feed the function 50
 75 
 76  Returns
 77      NONE.
 78 
 79  Description
 80      User gives the function an integer value between 0 and 100% which
 81      represents the pecentage of maximum attainable speed.  To stop the
 82      motor give the function 0.
 83 
 84  Notes
 85      The motor is driven using Drive-Coast Mode.  This allows for higher
 86      speeds than Drive-Break Mode.
 87 
 88  Author
 89      Nicholas Musser, 02/15/10
 90 ****************************************************************************/
 91 void Set_Speed(int SPEED)
 92 {
 93     PWMDTY0 = (255*SPEED)/100;  // Set the PWM Duty Cycle to user input
 94 }
 95 
 96 
 97 
 98 /****************************************************************************
 99  Function
100      Aim_Gun_Up_Down
101 
102  Parameters
103      None.
104 
105  Returns
106      None.
107 
108  Description
109      This function allows the user to define the launch angle of the
110      gun.
111 
112  Notes
113 
114 
115  Author
116      Nicholas Musser, 02/15/10
117 ****************************************************************************/
118 void Aim_Gun_Up_Down(void)
119 {
120     //
121 }
122 
123 
124 
125 /****************************************************************************
126  Function
127      Stop_1_Open (bottom gate)
128 
129  Parameters
130      None.
131 
132  Returns
133      None.
134 
135  Description
136      Sets Bit 3 of Port P to LO/HI in order to place the door lock in the
137      OPEN position.
138 
139  Notes
140 
141 
142  Author
143      Nicholas Musser, 02/15/10
144 ****************************************************************************/
145 void Gate_1_Open(void)
146 {
147     PTP &= BIT3LO;  // Set Bit 3 of Port P HI
148     PWMDTY4 = 80;
149 }
150 
151 
152 
153 /****************************************************************************
154  Function
155      Stop_1_Closed
156 
157  Parameters
158      None.
159 
160  Returns
161      None.
162 
163  Description
164      Sets Bit 3 of Port P to LO/HI in order to place the door lock in the
165      CLOSED position.
166 
167  Notes
168 
169 
170  Author
171      Nicholas Musser, 02/15/10
172 ****************************************************************************/
173 void Gate_1_Closed(void)
174 {
175     PTP |= BIT3HI;// Set Bit 3 of Port P LO
176     PWMDTY4 = 0;
177 }
178 
179 void Gate_1_Reset(void)
180 {
181     PTP &= BIT3LO;// Set Bit 3 of Port P LO
182     PWMDTY4 = 0;
183 }
184 
185 
186 
187 
188 /****************************************************************************
189  Function
190      Stop_2_Open (top gate)
191 
192  Parameters
193      None.
194 
195  Returns
196      None.
197 
198  Description
199      Sets Bit 4 of Port P to LO/HI in order to place the door lock in the
200      OPEN position.
201 
202  Notes
203 
204 
205  Author
206      Nicholas Musser, 02/15/10
207 ****************************************************************************/
208 void Gate_2_Open(void)
209 {
210     PTP &= BIT4LO;; // Set Bit 4 of Port P HI
211     PWMDTY5 = 80;
212 }
213 
214 
215 /****************************************************************************
216  Function
217      Stop_2_Closed
218 
219  Parameters
220      None.
221 
222  Returns
223      None.
224 
225  Description
226      Sets Bit 4 of Port P to LO/HI in order to place the door lock in the
227      CLOSED position.
228 
229  Notes
230 
231 
232  Author
233      Nicholas Musser, 02/15/10
234 ****************************************************************************/
235 void Gate_2_Closed(void)
236 {
237     PTP |= BIT4HI;// Set Bit 4 of Port P LO
238     PWMDTY5 = 0;
239 }
240 
241 
242 void Gate_2_Reset(void)
243 {
244     PTP &= BIT4LO;// Set Bit 4 of Port P LO
245     PWMDTY5 = 0;
246 }
247 
248 
249 
250 
251 
252 /****************************************************************************
253  Function
254      Release_Ball
255 
256  Parameters
257      None.
258 
259  Returns
260      None.
261 
262  Description
263      Controls the OPEN/CLOSE sequence of the door locks to release one ball
264 
265  Notes
266      NONE.
267 
268  Author
269      Nicholas Musser, 02/15/10
270 ****************************************************************************/
271 void Release_Ball(void)
272 {
273     static int n;  // initial time variable
274     static int i = 0;  // variable use in timing loop
275 
276 
277     SetTimer(300);
278     while((TIM2_TFLG1 & _S12_C4F) == 0 )
279     {
280       //(void)printf("10Hz\n\r");
281     }
282 
283     TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
284 
285     SetTimer(300);
286     while((TIM2_TFLG1 & _S12_C4F) == 0 )
287     {
288       //(void)printf("10Hz\n\r");
289     }
290 
291     TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
292 
293     SetTimer(300);
294     while((TIM2_TFLG1 & _S12_C4F) == 0 )
295     {
296       //(void)printf("10Hz\n\r");
297     }
298 
299     TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
300 
301 
302     Gate_1_Open();  // OPEN Stop #1
303     (void)printf("Gate 1 is opened\n\r");
304     SetTimer(300);
305     while((TIM2_TFLG1 & _S12_C4F) == 0 )
306     {
307       //(void)printf("10Hz\n\r");
308     }
309 
310     TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
311     Gate_1_Reset();
312 
313     //KeyStroke = getchar();
314     Gate_1_Closed();  // OPEN Stop #1
315     (void)printf("Gate 1 is closed\n\r");
316     SetTimer(300);
317     while((TIM2_TFLG1 & _S12_C4F) == 0 )
318     {
319       //(void)printf("10Hz\n\r");
320     }
321 
322     TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
323     Gate_1_Reset();
324 
325 
326     //KeyStroke = getchar();
327     Gate_2_Open();  // OPEN Stop #1
328     (void)printf("Gate 2 is opened\n\r");
329     SetTimer(300);
330     while((TIM2_TFLG1 & _S12_C4F) == 0 )
331     {
332       //(void)printf("10Hz\n\r");
333     }
334 
335     TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag    0
336     Gate_2_Reset();
337 
338     //KeyStroke = getchar();
339     Gate_2_Closed();  // OPEN Stop #1
340     (void)printf("Gate 2 is closed\n\r");
341     SetTimer(300);
342     while((TIM2_TFLG1 & _S12_C4F) == 0 )
343     {
344       //(void)printf("10Hz\n\r");
345     }
346 
347     TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
348     Gate_2_Reset();
349     Balls_Left = Balls_Left -1; // reduce ball count by 1
350 
351 }
352 
353 // release ball steps
354 char Release_Ball2(void)
355 {
356     switch (CurrentGateStep)
357     {
358       case 1:
359         Gate_1_Open();  // OPEN Stop #1
360         (void)printf("Gate 1 is opened\n\r");
361         SetTimer(300);
362         CurrentGateStep++;
363         return 0;
364         break;
365       case 2:
366         if((TIM2_TFLG1 & _S12_C4F) != 0 )
367         {
368           TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
369           Gate_1_Reset();
370           CurrentGateStep++;
371           return 0;
372           break;
373         }
374       case 3:
375         Gate_1_Closed();  // CLOSE Stop #1
376         (void)printf("Gate 1 is closed\n\r");
377         SetTimer(300);
378         CurrentGateStep++;
379         return 0;
380         break;
381       case 4:
382         if((TIM2_TFLG1 & _S12_C4F) != 0 )
383         {
384           TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
385           Gate_1_Reset();
386           CurrentGateStep++;
387           return 0;
388           break;
389         }
390 
391       case 5:
392         Gate_2_Open();  // OPEN Stop #1
393         (void)printf("Gate 2 is opened\n\r");
394         SetTimer(300);
395         CurrentGateStep++;
396         return 0;
397         break;
398       case 6:
399         if((TIM2_TFLG1 & _S12_C4F) != 0 )
400         {
401           TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
402           Gate_2_Reset();
403           CurrentGateStep++;
404           return 0;
405           break;
406         }
407       case 7:
408         Gate_2_Closed();  // CLOSE Stop #1
409         (void)printf("Gate 2 is closed\n\r");
410         SetTimer(300);
411         CurrentGateStep++;
412         return 0;
413         break;
414       case 8:
415         if((TIM2_TFLG1 & _S12_C4F) != 0 )
416         {
417           TIM2_TFLG1 = _S12_C4F; //Clear the OC2 flag
418           Gate_2_Reset();
419           // Reset all the gates back to the beginning of releasing ball
420           CurrentGateStep = 1;
421           Balls_Left = Balls_Left -1; // reduce ball count by 1
422           printf("before RETURN out of Release_Ball2()\r\n");
423           return 1;  // Finish releasing ball
424           break;
425         }
426     }
427   //return 1;
428 }
429 
430 
431 /****************************************************************************
432  Function
433      Release_All_Balls
434 
435  Parameters
436      None.
437 
438  Returns
439      None.
440 
441  Description
442      Controls the OPEN/CLOSE sequence of the door locks to release all
443      balls.  This function is used at the end of play as a last ditch
444      effort
445 
446  Notes
447      NONE.
448 
449  Author
450      Nicholas Musser, 02/15/10
451 ****************************************************************************/
452 void Release_All_Balls(void)
453 {
454     while(Balls_Left > 0) // for how ever many balls are left
455     {
456         Release_Ball(); // Release one ball
457     }
458 }
459 
460 
461 
462 /****************************************************************************
463  Function
464      Gun_And_Feeder_STOP
465 
466  Parameters
467      None.
468 
469  Returns
470      None.
471 
472  Description
473      Stop the gun wheel motor and feeder system.  This would be used if
474      we receive a STOP command from the Target Commander, or the
475      game was over.
476 
477  Notes
478      NONE.
479 
480  Author
481      Nicholas Musser, 02/15/10
482 ****************************************************************************/
483 void Gun_And_Feeder_STOP(void)
484 {
485     Set_Speed(0);   // Stop wheel by setting duty cycle to 0%
486     Gate_2_Closed();  // Place both gates in closed position
487     Gate_1_Closed();
488 }
489 
490 int BallLeft(void)
491 {
492   return Balls_Left;
493 }
494 /*------------------------------ End of file ------------------------------*/
495 
496