/*
Matt Hobson
Amy Thompson
Adeola Arije
ELEN 598
Spring, 2006
File Name: TrackControl.c
File Purpose: Controls the functions related to the operation of the BOEbot
              track.  The program continuously reads the results of the ATD
			  conversions from the track-mounted Force sensors.  Whenever the 
			  threshold voltage is exceded (robot on the force sensor), the 
			  program commands the S12 to generate the signal to actuate 
			  the corresponding servo, thus raising the gate for the robot to
			  pass through.  The gate will remain open for a predefined length
			  of time, then it will close again.  The program uses a completely
			  interrupt-driven control scheme to allow the processor to 
			  multitask.  Therefore, it should never miss the event of one of
			  the two robots activating one of the force sensors.  
			  
*/

//******************************* INCLUDE FILES ********************************

#include <mc9s12dp256.h>
#include <stdio.h>

//******************************** PROTOTYPES **********************************

extern void _HC12Setup (void);    //Startup file
extern void SCI0_Init (void);     //SCI serial communications with terminal
extern void Timer_Init (void);    //Initialization of Timer system
extern void ServoPWM_Init (void); //Initialization of PWM system
extern void ATD_Init (void);      //Initialization of ATD system
extern void ATD_complete (void);  //ISR for ATD0
extern void gate0(void);          //
extern void gate1(void);          //
extern void gate2(void);          //
extern void gate3(void);          //
extern void OC0_ISR (void);       //ISR for Timer channel 0
extern void OC1_ISR (void);       //ISR for Timer channel 1
extern void OC2_ISR (void);       //ISR for Timer channel 2
extern void OC3_ISR (void);       //ISR for Timer channel 3
extern void gateStart (void);
extern void gateDown (unsigned char);
extern void gateUp (unsigned char);

//******************************** DEFINITIONS *********************************

#define Threshold 1  //The voltage threshold of the force sensors (~19.5mV)

//********************************* VARIABLES **********************************

extern unsigned char force0;  //Results of the ATD conversion for the force
extern unsigned char force1;  //sensors
extern unsigned char force2;
extern unsigned char force3;

//******************************* MAIN FUNCTION ********************************

void main (void)
{
   _HC12Setup();     //Call the initialization code when running in flash
   
   //Initialize the MON12 Interrupt Vectors:   
   
   *(void (**)())0x3FD2 = ATD_complete;  //RAM-based MON12 interrupt: ADC0
   *(void (**)())0x3FE8 = OC3_ISR;  //RAM-based MON12 interrupt: TC3
   *(void (**)())0x3FEA = OC2_ISR;  //RAM-based MON12 interrupt: TC2  
   *(void (**)())0x3FEC = OC1_ISR;  //RAM-based MON12 interrupt: TC1
   *(void (**)())0x3FEE = OC0_ISR;  //RAM-based MON12 interrupt: TC0
      
   SCI0_Init();      //Initialize the SCI0 for communication from flash
   Timer_Init();     //Initialize the Timer system for control of gate timings
   ServoPWM_Init();  //Initialize PWM system to generate 50Hz for servo control
   ATD_Init();       //Initialize ATD system for monitoring of force sensors 
   CLI();            //Enable maskable interrupts
   
   gateStart();  //Test all the gates
  
   while(1)
   {
      if(force0 > Threshold)
	   {
	     gate0();
	   }
	  if(force1 > Threshold)
	   {
		 gate1();
	   }
      if(force2 > Threshold)
	   {
		 gate2();
	   }	  
      if(force3 > Threshold)
	   {
		 gate3();
	   } 
   } //End of while     
} //End of main  
