/*
Matt Hobson
Spring 2006
ELEN 599
Filename: Actuation.c
Purpose: Contains all of the functions pertaining to the movement of the 
         robot's actuators (gripper, arm, elevator). 

********************************** INCLUDE ************************************/

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

//******************************** DEFINES *************************************

//ACTUATION:
#define ArmUp     0x04   //Port M pin that controls arm motor direction
#define ArmDown   0xFB  //Port M state to move arm to top of chute 
#define ElevatorUp 0x08 //Port M pin that controls elevator motor direction
#define ElevatorDown 0xF7 //Direction to send elevator down
#define GripOpen  0x00  //Port M pin that controls gripper motor direction
#define GripClose 0x10 //Port M state to close grippers 
#define ArmBrake 0x40  //Port M pin connected to arm brake input
#define ElevatorBrake 0x80  //Port M pin that controls elevator brake
#define GripperBrake 0x80   //Same as ElevatorBrake (shares same pin)
#define GripperOut 100000
#define GripPackage 10000
#define GripDiff 30000

//MOTOR SPEEDS:
#define Normal 145   //Normal speed
#define Fast 185    //Fast speed
#define XFast 250   //Extra fast
#define Slow 90     //Slow speed
#define Creep 70    //Really slow
#define High 254    //Turns PWM signal into logic high (for braking)

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

extern void arm_speed (unsigned char);
extern void elevator_speed (unsigned char);
extern void gripper_speed (unsigned char);
extern void delay (int);
//extern void atd0_interrupt_on (void);
//extern void atd0_interrupt_off (void);
//extern void ATD_Init(void);

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

unsigned long int Gencode = 0;  //Gripper encoder count
unsigned long int Eencode = 0;  //Elevator encoder count
unsigned char x = 0;            //Counter for speed control
extern unsigned long int ElapsedTicks; //Holds total # of clock ticks for IC
extern unsigned int TOcount;    //Counter for the # of Timer Overflow Flags (TOF)
extern unsigned int TOFedge1;   //Stores value of TOcount at time of rising edge
extern unsigned int TOFedge2;   //Stores value of TOcount at time of falling edge
extern unsigned int TOdiff;     //Stores the difference of the TOF edges
extern unsigned char Oflow;     //Keep track of when TOcount needs to roll over
extern unsigned int riseEdge;   //Variable to hold the rising input edge
extern unsigned int fallEdge;   //Variable to hold the falling input edge
extern unsigned char pot;  //Result of ATD for arm potentiometer

//**************************** ARM_DIR FUNCTION ********************************

void arm_dir (unsigned char pos)
{
   if(pos == ArmUp)
      {
	     PTM &= ~ArmBrake;  //Turn off the brake
	     PTM |=  ArmUp;
	  }
   if(pos == ArmDown)
      {
	     PTM &= ~ArmBrake;  //Turn off the brake
		 PTM &= ArmDown;
	  }
}

//************************** ELEVATOR_DIR FUNCTION *****************************

void elevator_dir (unsigned char floor)
{
   if(floor == ElevatorUp)
      {
         PTM &= ~ElevatorBrake;  //Turn off the brake
	     PTM |=  ElevatorUp;  //Set direction to move elevator up
      }
   if(floor == ElevatorDown)
      {
	     PTM &= ~ElevatorBrake;  //Turn off the brake
		 PTM &= ElevatorDown;  //Set direction to move elevator down
      }
}

//************************** GRIPPER_DIR FUNCTION ******************************

void gripper_dir (unsigned char move)
{
   PTM = move;
}

//**************************** ARM_BRAKE FUNCTION ******************************

void arm_brake (void)
{
   PTM |= ArmBrake;
   arm_speed(High);  //Take PWM line to 1
   delay(20);
   arm_speed(0);   
}

//************************* ELEVATOR_BRAKE FUNCTION ****************************

void elevator_brake (void)
{
   PTM = ElevatorBrake;
   elevator_speed(High);
   delay(20);  
}

//************************** GRIPPER_BRAKE FUNCTION ****************************

void gripper_brake (void)
{
   PTM = GripperBrake;
   gripper_speed(High);
   delay(20); 
   gripper_speed(0);
}

//*************************** ELEVATOR_UP FUNCTION *****************************

void elevator_up (void)
{
   elevator_dir(ElevatorUp);
   for(x=0; x<=XFast; x++)
      {
         elevator_speed(x);  //Increment the speed of the elevator motor
//	     delay(5);
      }
   while((PORTAD0 & 0x08)==0)
      {;}                    //Wait until button S2 has been released
   elevator_speed(0);        //Stop the motor
}   

//************************** ELEVATOR_DOWN FUNCTION ****************************

void elevator_down (void)
{
   elevator_dir(ElevatorDown);
   for(x=0; x<=XFast; x++)
      {
         elevator_speed(x);  //Increment the speed of the elevator motor
//	     delay(5);
      }
   while((PORTAD0 & 0x10)==0)
      {;}                    //Wait until button S1 has been released
   elevator_speed(0);        //Stop the motor
}      
//**************************** ARM_DOWN FUNCTION *******************************

void arm_down (void)
{
//   ATD_Init();
   arm_dir(ArmDown);
   arm_speed(Normal);
   while(pot > 10)
   {;}
   arm_brake();
   delay(100);
//   atd0_interrupt_off();
}

//***************************** ARM_UP FUNCTION ********************************

void arm_up (void)
{
//   ATD_Init();
   arm_dir(ArmUp);
   arm_speed(Normal);
   while(pot < 235)
   {;}
   arm_brake();
   delay(100);
//   atd0_interrupt_off();
}
   
//************************** CLOSE_GRIPPER FUNCTION ****************************

void close_gripper (unsigned long int Gdist)
{
   TCTL3 = 0x10;  //Capture rising edges on TC6
   gripper_dir(GripClose);
   gripper_speed(Normal);
   while(Gencode < Gdist)
   {
      while ((TFLG1 & 0x40)== 0)  //Wait for rising edge on TC6
        {;}
      Gencode++;     //Increment the number of clicks received from gripper encoder
      TFLG1 = 0x40;  //Clear flag on TC6
   }
   Gencode  = 0;     //Reset the counter
   gripper_brake();
   delay(120);
}      
   
//************************** OPEN_GRIPPER FUNCTION *****************************
 
void open_gripper (unsigned long int Gdist)
{
   TCTL3 = 0x10;  //Capture rising edges on TC6
   gripper_dir(GripOpen);
   gripper_speed(Normal);
   while(Gencode < Gdist)
   {
      while ((TFLG1 & 0x40)== 0)  //Wait for rising edge on TC6
        {;}
      Gencode++;     //Increment the number of clicks received from gripper encoder
      TFLG1 = 0x40;  //Clear flag on TC6
   }
   Gencode  = 0;     //Reset the counter
   gripper_brake();
   delay(120);
}   

//************************** GRIP_PACKAGE FUNCTION *****************************

void grip_package (unsigned long int Gdist)
{
   TCTL3 = 0x10;  //Capture rising edges on TC6
   gripper_dir(GripClose);
   gripper_speed(Normal);
   while(Gencode < Gdist)
   {
      while ((TFLG1 & 0x40)== 0)  //Wait for rising edge on TC6
        {;}
      Gencode++;     //Increment the number of clicks received from gripper encoder
      TFLG1 = 0x40;  //Clear flag on TC6
   }
   Gencode  = 0;     //Reset the counter
   gripper_brake();
   delay(120);  
/* TOcount = 0;
   Gencode = 0;
   ElapsedTicks =  0;
   gripper_dir(GripClose);
   gripper_speed(Normal);
   while(ElapsedTicks < 2000)
   {
     TCTL3 = 0x10;  //Set Input Capture on TC6 to look for rising edges only
     while ((TFLG1 & 0x40)== 0)  //Wait for rising edge
       {;}
	 Gencode++;
     TOFedge1 = TOcount; //Store value of TOcount when rising edge occurs
     riseEdge = TC1;   //Store the first latched value of the free running timer
     TFLG1 = 0x40;  //Clear flag on TC1
     TCTL3 = 0x20;  //Set Input Capture on TC6 to look for falling edges only 
     while ((TFLG1 & 0x40)== 0)  //Wait for falling edge
       {;}
     TOFedge2 = TOcount; //Store value of TOcount when falling edge occurs
     fallEdge = TC1;   //Store the second latched value of the free running timer
     TFLG1 = 0x40;  //Clear flag on TC1
   
     if (TOFedge1 > TOFedge2)  //For the special case of the input capture event
     {                         //happening when TOcount rolls over.
        TOdiff = TOFedge2 + (65536 - TOFedge1);
     }
     else   //For the normal case: TOFedge2 > TOFedge1
     {   
	    TOdiff = TOFedge2 - TOFedge1;
     }
     ElapsedTicks = (TOdiff * 65536) + fallEdge - riseEdge; //Total # of ticks
   }
*/

}           
//************************** HOME_POSITION FUNCTION ****************************

void home_position (unsigned char place)  //Places robot in the 8"x8"x12" position
{
//   atd0_interrupt_on();
   if(place == 0x00)  //Starting at home position
   {
      arm_up();
   }
   if(place == 0xFF)  //Finishing position
   {
      close_gripper(75000);
	  arm_up();
   }
//   atd0_interrupt_off();     
}

//******************************************************************************
