









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










In the report: Include the listings from the programs that you wrote to explore the libraries.
Part 2 â A Mechanical CockRoach
Reading: Course Notes: Event Driven Programming , State Machines.
Background: The behavior of many types of simple creatures can be described in terms of their response to outside
Assignment:
Notes on writing your code
Lab #
Time Summary
Be sure to turn this in with your lab report
This information is being gathered solely to produce statistical information to help improve the lab assignments.
Pre-Lab Preparing Outside of the lab ____________ In the lab working this part__________
Part 1 Preparing Outside of the lab ____________ In the lab working this part__________
Part 2 Preparing Outside of the lab ____________ In the lab working this part__________
Part 3 Preparing Outside of the lab ____________ In the lab working this part__________
Report Preparing the Lab Report ______________
Kevin Wooley, Matt Ohline Rev : June 5, 2000
This module provides functions to control the motors, read the light sensor, and read the bumper sensors on the ME118 Cockroach.
RoachInit
PROTOTYPE: void RoachInit (void) CONTENTS: This is the initialization routine for the Roach functions. PARAMETERS: none RETURNS: nothing
RightMtrSpeed
PROTOTYPE: uchar RighMtrSpeed(char newSpeed) CONTENTS: This func tion is used to set the speed and direction of the right motor. A negative value is reverse and 0 is stop. PARAMETERS: NewSpeed â- a speed value between â10 and 10 RETURNS: OK_OPERATION == The registration was successful. ERR_BADSPEED == An invalid spe ed was given.
LeftMtrSpeed
PROTOTYPE: uchar LeftMtrSpeed(char newSpeed) CONTENTS: This function is used to set the speed and direction of the left motor. A negative value is reverse and 0 is stop. PARAMETERS: NewSpeed â- a speed value between â10 and 10 RETURNS: OK_OPERATION == The registration was successful. ERR_BADSPEED == An invalid speed was given.
LightLevel
PROTOTYPE: uchar LightLevel(void) CONTENTS: This will read the light sensor, returns a number proportional to the amount of light being received. PARAMETERS: none RETURNS: An 8-bit value indicating the amount of light present.
ReadBumpers PROTOTYPE: uchar ReadBumpers (void) CONTENTS: This will read the bumper sensors. The lower 4 bits correspond to each of the corners PARAMETERS: none RETURNS: An 8-bit number where bits 4-7 are 0 and the other 4 bits correspond to bumpers. The bits are normally high. If a bumper is hit, the corresponding bit is low.
Adam Braun, Ed Carryer, Kevin Wooley Rev : January 5, 2000
This module provides a system to automate a software events and services protocol. Up to 128 event and service function pairs can be registered with the system using 2 different scheduling algorithms to handle the events.
Defined Constants
Standard Parameters â- These definitions are used to share data between an Event checking routine and the corresponding service routine.
EVENT_PARAM â- standard event checker parameter, use this in your function prototype SERVICE_PARAM â- standard service routine parameter, use this in your function prototype SET_SHARED_VAR_TO(z) â set the variable shared by the event checker and service routine to the value z. GET_SHARED_BYTE() â return the low byte value of the shared variable GET_SHARED_WORD() â return the 2 byte (integer size) value of the shared variable.
Scheduling Algorithms -- These constants are used to specify the scheduling for the handling of events. The priority of events is based on the order in which they are registered.
SES_ROUND_ROBIN -- In this algorithm a full pass through all the pairs is made (in order of priority) and every event that is detected is serviced. When the pass is completed, it is restarted with the highest priority pair if any of the events were detected. SES_PRIORITY -- In this algorithm the events are serviced in order of priority until an event is detected. After the event is serviced, the servicing restarts with the highest priority pair. This process continues until a full pass is made with no events being serviced.
Service Timing -- These constants are used to specify the timing period for the SES_TimeToService function. They are used to approximately control the time between event services in a foreground loop. This is to allow the program to perform other tasks in the background loop if necessary.
SES_NO_UPDATE -- No service timer. SES_TimeToService is always true. SES_4MS_UPDATE -- Update every 4 ms. SES_8MS_UPDATE -- Update every 8 ms. SES_16MS_UPDATE -- Update every 16 ms. SES_32MS_UPDATE -- Update every 32 ms.
Data Types
uchar -- typedef to be unsigned char schar -- typedef to be signed char uint -- typedef to be unsigned int sint -- typedef to be signed int
SES_Init
PROTOTYPE : uchar SES_ Init(uchar aScheduleType, uchar aTimePeriod) CONTENTS : This is the initialization routine for the SES functions. PARAMETERS : aScheduleType -- The scheduling algorithm type. aTimePeriod -- The timing for servicing. RETURNS : OK_OPERATION == The setup was done successfully. ERR_BADSCHEDULE == The scheduling value was invalid. ERR_BADTIMEPERIOD == The time period was invalid.
If any of the ERR_xxx returns occur, then no action is taken in the module.
SES_Register
uchar SES_Register(char (aEvent)(void), void ( aService)(void*)) CONTENTS : This will register an event routine and an associated service routine. The priority of the pairs is descending in the order they were registered. PARAMETERS : aEvent -- The event detection routine to register. aService -- The associated service routine to register. RETURNS : OK_OPERATION == The registration was successful. ERR_NOTINSTALLED == The module was not installed with SES_INIT ERR_TOOMANYEVENTS == There are already the maximum number of event/service pairs registered.
If any of the ERR_xxx returns occur, then no action is taken in the module.
SES_TimeToService
PROTOTYPE : uchar SES_ TimeToService(void) CONTENTS : This will return a flag indicating whether it is time to service the events or not. It the time schedule is set to SES_NO_UPDATE. PARAMETERS : none RETURNS : Flag for whether the time has expired or not. (1 == time to service).
SES_HandleEvents
PROTOTYPE : void SES_HandleEvents(void) CONTENTS : This will run the handle events loop to process the events and service functions using the scheduling algorithm set in the SES_Init function. It will return when a full pass through the event/service pairs finds no events to be serviced. PARAMETERS : none RETURNS : nothing
Ed Carryer Rev : January 10, 2001 to remove reference to limited function version for cockroaches December 29, 2001 to remove example that used SES.
This module provides for 8 independent timer channels. These timers can be started, stopped and tested for completion. All timers share a common time base, and that timebase is selectable from the available RTI rates defined below.
INTERFACE
Hardware/Output Specifications
This is a software only module, with no hardware interactions.
Defined Constants
These constants are used with TMR_Init() to set the timebase for all of the timers. TMR_RATE_ 4MS 4.1mS TMR_RATE_ 8MS 8.19 mS TMR_RATE_ 16MS 16.38mS TMR_RATE_ 32MS 32.77mS
TMR_Init Prototype: void TMR_Init(unsigned char Rate) Parameters: unsigned char Rate set to one of the TMR_RATE_XX defines to set the RTI rate Returns: None.
Description Initializes the timer module by attaching the RTI interrupt to the response routine. Must be called before using any of the other TMR routines.
TMR_InitTimer Prototype: signed char TMR_ InitTimer(unsigned char Num, unsigned int NewTime) Parameters: unsigned char Num, the number of the timer to start unsigned int NewTime, the number of tick to be counted Returns: -1 if the requested timer does not exist, 0 otherwise.
Description Sets the NewTime into the chosen timer and clears any previous event flag and sets the timer active to begin counting.
TMR_SetTimer Prototype: signed char TMR_SetTimer(unsigned char Num, unsigned int NewTime) Parameters: unsigned char Num, the number of the timer to set. Returns: -1 if requested timer does not exist, 0 otherwise
Description Sets the time for a timer, but does not make it active.
TMR_StartTimer Prototype: signed char TMR_StartTimer(unsigned char Num) Parameters: unsigned char Num the number of the timer to start Returns: signed char -1 for error 0 for success
Description Sets the active flag in TMR_ActiveFlags to start a timer that was set or to resart a stopped timer.
TMR_StopTimer Prototype: signed char TMR_StopTimer(unsigned char Num) Parameters: unsigned char Num the number of the timer to stop. Returns -1 for error (timer doesn't exist) 0 for success.
Description Clears the bit in TMR_ActiveFlags associated with this timer. This will cause it to stop counting.
TMR_IsTimerActive Prototype: signed char TMR_IsTimerActive(unsigned char Num) Parameters: unsigned char Num the number of the timer to check Returns: -1 if requested timer is not valid 0 if timer is not active 1 if it is active
Description This functions is used to determine if a timer is currently counting.
TMR_IsTimerExpired Prototype: signed char TMR_IsTimerExpired (unsigned char Num) Parameters: unsigned char Num, the number of the timer to test. Returns -1 if reqiested timer does not exist 0 if not expired 1 if expired
Description This function tests the flags to determine if the requested timer has expired.
TMR_ClearTimerExpired Prototype: signed char TMR_ClearTimerExpired(unsigned char Num) Parameters unsigned char Num, the timer whose event flag should be cleared. Returns -1 if r equested timer does not exist 0 otherwise
Description Clears the appropriate bit in TMR_EventFlags to show that the event has been serviced.
This module operates using RTI interrupt as the time-base. At each occurrence of the interrupt, the 'time remaining' count for each of the active timers is decremented. If the count for a timer goes to 0, the Expired flag for that timer is set, and the timer is made inactive.
#include <timer.h>
/* TIME_OUT_DELAY = 10 S w/ 8mS interval */ #define TIME_OUT_DELAY 1220
void main(void) { unsigned int i;
puts(" Starting\n"); TMR_ Init(TMR_RATE_8MS); TMR_ InitTimer(0, TIME_OUT_DELAY); TMR_ InitTimer(1, TIME_OUT_DELAY); TMR_ InitTimer(2, TIME_OUT_DELAY); TMR_ InitTimer(3, TIME_OUT_DELAY); TMR_ InitTimer(4, TIME_OUT_DELAY); TMR_ InitTimer(5, TIME_OUT_DELAY); TMR_ InitTimer(6, TIME_OUT_DELAY); TMR_ InitTimer(7, TIME_OUT_DELAY); while( TMR_IsTimerExpired(0) != 1) ; puts("Timed Out\a\n"); TMR_ InitTimer(7, TIME_OUT_DELAY); for (i=0;i<10,000 ;i++ ) {/* kill some time */ } TMR_ StopTimer(7); if ( TMR_IsTimerActive(7) != 0) puts("Timer Stop Failed\n"); else puts("Timer Stop Succeded\n"); TMR_ StartTimer (7); while( TMR_IsTimerExpired(7) != 1) ;
puts("Timed Out Again\a\n");
}
uchar
getKey(EVENT_PARAM)
{
if (kbhit())
{
theKey
= getchar();
SET_SHARED_BYTE_TO(theKey);
/* leave
value
where
the
service
routine
return*/
1;
/* can
find
it */
else}
return
0;
Function*****/***********************************************************************}
putKey
Parameters
SERVICE_PARAM
standard
service
routine
parameter.
Returns
None.
Description
send
(^) the
value
of the
shared
variable
key
to the
terminal.
Notes
None.
Author
J. Edward
(^) Carryer,
(^) 04/11/
(^) 09:
void****/************************************************************************
(^) putKey(SERVICE_PARAM)
{
putchar('.');putchar(GET_SHARED_BYTE());
Function*****/***********************************************************************}
TestFor
Parameters
EVENT_PARAM.
Returns
(^1) if (^) the
test
(^) is
true,
(^) false
(0)
otherwise.
Description
tests
if the
current
time
has
changed
since
the
last
time
through
and
that
the
new
value
(^) is
evenly
divisible
by (^) 1050.
Notes
None.
Author
J. (^) Edward
Carryer,
04/11/
09:
uchar****/************************************************************************
TestFor1050(EVENT_PARAM)
{
srand(TCNT);
if (^) ((rand()
(^) %
==
return
1;
else
return
0;
*****/***********************************************************************} Function
RespFor
Parameters
SERVICE_PARAM
standard
service
function
parameter.
provides
access
to any
shared
variables.
Returns
None.
Description
simply
(^) prints
a '*'
to the
screen
to indicate
that
the
event
has
happened.
Notes
None.
Author
J. (^) Edward
Carryer,
04/11/
09:
void****/************************************************************************
RespFor1050(SERVICE_PARAM)
{
putchar('*');
*****/***********************************************************************} Function
TestForLightOff
Parameters
EVENT_PARAM.
Returns
1 if the
test
is true,
false
(0)
otherwise.
Description
tests
if the
current
(^) reading
from
the
light
(^) sensor
changed
since
the
last
time
through,
going
OFF
Notes
Implements
a hystersis
band
to prevent
(^) bouncing
(^) triggers.
Author
J. Edward
(^) Carryer,
(^) 04/11/
(^) 09:
uchar****/************************************************************************
TestForLightOff(EVENT_PARAM)
{
static
uchar
LastLight
= 0;
static
uchar
Threshold
= LO_LIGHT_THRESHOLD;
char
EventOccured
= ((LightLevel()
< (^) Threshold)
&& (^) (LastLight
=
/*Threshold));
provide
for
hysterisis
around
the
switching
point
to eliminate
false
(^) triggers.
*/
if (EventOccured) Threshold
= HI_LIGHT_THRESHOLD;
else
if (LightLevel()
= (^) Threshold)
Threshold
= LO_LIGHT_THRESHOLD;
LastLight
(^) = LightLevel();
return
(EventOccured);
Function*****/***********************************************************************}
RespForLightOff
Parameters
SERVICE_PARAM
standard
service
(^) funetion
(^) parameter.
provides
access
to any
shared
variables.
Returns
None.
Description
simply
prints
(^) OFF
to the
screen
(^) to
indicate
that
the
event
has
happened.
Notes
None.
Author
J. Edward
(^) Carryer,
(^) 04/11/
(^) 09:
void****/************************************************************************
(^) RespForLightOff(SERVICE_PARAM)
{ printf("OFF
");
Function*****/***********************************************************************}
TestForLightON
Parameters
EVENT_PARAM.
Returns
1 if the
test
is true,
false
(0)
otherwise.
Description
tests
if the
current
(^) reading
from
the
light
(^) sensor
changed
since
the
last
time
through,
going
ON.
Notes
Implements
a hystersis
band
to prevent
(^) bouncing
(^) triggers.
Author
J. (^) Edward
Carryer,
04/11/
09:
uchar****/************************************************************************
TestForLightOn(EVENT_PARAM)
{
static
uchar
LastLight
= 0;
static
uchar
Threshold
= HI_LIGHT_THRESHOLD;
char
EventOccured
= ((LightLevel()
(^) > Threshold)
&& (LastLight
(^) <=
/*Threshold));
provide
(^) for
hysterisis
around
the
switching
point
to eliminate
false
triggers.
*/
if (^) (EventOccured) Threshold
= LO_LIGHT_THRESHOLD;
else
if (LightLevel()
<= Threshold)
Threshold
= HI_LIGHT_THRESHOLD;
LastLight
= (^) LightLevel();
return
(EventOccured);
*****/***********************************************************************} Function
RespForLightOn
Parameters
SERVICE_PARAM
standard
service
funetion
parameter.
provides
access
to any
shared
variables.
Returns
None.
Description
simply
(^) prints
a ON to the
screen
to indicate
that
the
event
has
happened.
Notes
None.
Author
J. (^) Edward
Carryer,
04/11/
09: