BallDrop.h
#ifndef BALLDROP_H
#define BALLDROP_H
//prototype
void BallDrop(void);
/* This function opens the ball chute then closes it after a delay*/
void BallDropInit(void);
/*This function closes the ball chute door*/
void CollectBall(void);
/*Call this function when at dispenser. It will drive the robot forward and
back in order to collect five balls.*/
#endif
BallDrop.c
#include <hidef.h> /* common defines and macros */
#include <mc9s12e128.h> /* derivative information */
#include <stdio.h>
#include <stdlib.h>
#include <S12E128bits.h> /*E128 bit definitions */
#include <bitdefs.h> /* BIT0HI, BIT0LO definitions */
#include <TIMERS12.h>
#include "Startup.h"
#include "BallDrop.h"
#include "LineFollow.h"
//#define TEST
//prototypes for internal functions
void opendoor(void);
void closedoor(void);
//constants - length of delays
const unsigned int RUN_TIME = 185; //length of time to run motor
const unsigned int WAIT_TIME = 1500;//length of delay between open/close
//this module assumes
//E128 pin U6 --> Green signal wire --> Green output wire --> Motor green wire
//E128 pin U7 --> White signal wire --> Yellow output wire --> Motor blue wire
//~~~~~~~~~~~ CollectBall ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*Drives robot up to dispenser, waits for a ball, then backs up.
Does this five times*/
void CollectBall(void)
{
int i;
for(i=1;i<6;i++)
{
LineFollow(30);
delay(1300);
BackUp(30,300);
}
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~BallDrop~~~~~~~~~~~~~~~~~~~~~
/*Releases all balls in ball tube*/
void BallDrop(void)
{
opendoor();
//delay between open/close
delay(WAIT_TIME);
closedoor();
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ opendoor ~~~~~~~~~~~~~~~~~~
//opens ball door
void opendoor(void)
{
//pulse motor backwards - set IN2 high
PTU = (PTU | BIT7HI);
//delay
delay(RUN_TIME);
//turn motor off
PTU = (PTU & ~BIT7HI);
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ closedoor ~~~~~~~~~~~~~~~~~~
//closes ball door
void closedoor(void)
{
//pulse motor forwards - set IN1 high
PTU = (PTU | BIT6HI);
//delay
delay(RUN_TIME);
//turn motor off
PTU = (PTU & ~BIT6HI);
return;
}
//~~~~~~~~~~~~~~~~~~~~ BallDropInit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//This function
// 1) Initializes the output ports (U7 and U6) to a value of zero
// 2) closes the ball door (default position)
void BallDropInit(void)
{
//set ports
char mask = (BIT7HI | BIT6HI);//U7 and U6 are swag motor outputs
PTU = (PTU & ~mask);//set M2 and M3 initially to 0
//close prize door
closedoor();
return;
}
//~_~_~_~_~_~_~_~_~__~_~_-Test Harness-_~_~_~_~_~_~_~_~_~
#ifdef TEST
/*Initializes motor then calls drop balls repeatedly with 1 sec
delay between calls*/
void main(void)
{
//initialize timer
TMRS12_Init(TMRS12_RATE_2MS);
//initialize module
BallDropInit();
puts("\r\nInitialized...");
while(1)
{
delay(1000);
puts("\r\nDropping Balls");
BallDrop();
}
return;
}
#endif