modular programming
DESCRIPTION
Modular Programming. Where the rubber meets the Code. The three uses of static. Encapsulation. Groups. together the data and the functions that operate on the data. Publishes. an interface to the rest of the code. Hides. the details of the implementation. Encapsulation. - PowerPoint PPT PresentationTRANSCRIPT
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Modular Programming
Where the rubber meets the Code
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
The three uses of static
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
FunctionA() FunctionB()
int foo char bar
FunctionC()
Encapsulation
Groups
Publishes
Hides
together the data and the functions that operate on the data
an interface to the rest of the code
the details of the implementation
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
FunctionA() FunctionB()
int foo char bar
FunctionC()
Encapsulation
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
What goes into a header file?
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
What shouldn’t be in a header file?
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Where do you #include the header file?
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Programming Style IssuesLayout within a module
One "True" Indentation Style
Use of White Space
Use of Comments Naming Conventions
Module Level Comment#includes
library /module headersPublic header for this module
Private Constant DefinitionsPrivate Macro DefinitonsPrivate Type DefinitionsPrivate VariablesPrivate Function PrototypesCode
while (1) { MSES_HandleEvents(); }
void putKey(SERVICE_PARAM){ putchar( GET_SHARED_BYTE() ); putchar('.');}
if (NewTime() && ((GetTime() % 1050) == 0)) return 1; else return 0;
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Module Design by Interface Specification
View:The module provides Services to the rest of the code
Design Activities:
Specify the Services
Describe Functionality
Name the Services
Design the implementation
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Application Programming Interfaces (API) for the ME218c Master modules
Module: Communications to UI on PC
To avoid hanging up the master during the transmission or reception of messages, this module should implement buffered, interrupt driven transmit and receive. The communications routines for this module will need to be interrupt driven because the UI may send its message at any time.
Char InitializeUICommunications(void);Do whatever hardware and software initialization necessary to prepare for communications with the UI on SC1.
Void TellUINewUserReady(void);Should send the message to the UI that a new iButton has been inserted and read.
Unsigned char IsNameReady(void);Should check to see if a new name is ready from the UI. Return TRUE if a new name is ready, FALSE otherwise.
Unsigned char GetNewName( unsigned char NameSpace[])Should copy the name gotten from the UI into the array NameSpace. The copy operation should copy no more than 16 characters, including the terminating NULL. Should return TRUE if there was a new name ready, FALSE otherwise.
A Real Example
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Design the interfaces to modules for
Driving the platform
Gathering sensor data
Produce:
Public interface specification
What are the details that are being hidden?
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Mike, Ian, and James// mike, james, and ian// cmpe 118, spring 05// these functions or for the motor and sensory control of our final project
// motor direction defines#define forward 1#define brake 0#define backward -1
#define success 1#define failure 0
// Motor control functionschar wheel_left(char direction, char speed);char wheel_right(char direction, char speed);// given a direction and a speed between 0 and 10 it return 0 if failure // and 1 if successful.// The bot can coast by giving a direction of "forward" or "backward" with a speed of 0,// or it can brake by giving any speed with a direction of "brake".
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
// bump sensorschar bump_sensors(void);// When called it will return a char where the upper four bits are always 0 then front left,front right, back right, back left.
// line sensorschar line_sensors(void);// when called it will return a char where the upper four bits are always 0 then forward left, // forward middle, forward right, and middle;
// aiming sensorschar aim_sensors(void);// when called it will return a char where the top six bits are zero left aiming photo transistor, // and right aiming photo transistor.
// firing solenoidchar firing(char);// when passed a 1 solenoid is open and it firing, when passed a 0 is closed. Will return 0 for// failure and 1 for success.
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Thomas, Maria, and OscarDRIVINGvoid LeftMtrSpeed(uchar speed)void RightMtrSpeed(uchar speed)
IRuchar getBroadIR() //Returns IR level for broad sensoruchar getAccurateIR() //Returns IR level for narrow sensor
TAPEuchar checkTape() //Returns solid state for tape (could be boolean)
TURRETuchar TurnLeft(uchar distance) // Returns statusuchar TurnRight(uchar distance) // checks for homeswitch
BUMPERSuchar getBumpers() //Returns which bumpers are pressed
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
John and Suraj#ifndef DROID_H#define DROID_H
#include "util.h"
// types typedef enum { DIRECTION_LEFT, DIRECTION_RIGHT, DIRECTION_FORWARD,
DIRECTION_BACKWARD } Direction;
// functions/** Makes the droid search for a target (tie fighter or death-star beacon) by slowly rotating and scanning in the direction specified.
@param a_dir The direction in which to scan for a target. @return false when no target has been detected. */ boolean Droid_scan(Direction a_dir);
/** Makes the droid seek out its nearest target and orient itself such that it is facing the target. From here, the droid may proceed forward and engage the target.
@return false when no target has been detected. */ boolean Droid_seek();
/** Makes the droid engage its nearest target by driving forward, while dynamically adjusting its trajectory, until the droid is within point-blank firing range.
Next, the droid fires a foam ball at the target.
@return false when no target has been detected. */ boolean Droid_engage();
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
/** Rotates the droid, in the given direction, by the given number of steps.
@param a_dir The direction in which the droid shall rotate. @param a_numSteps The number of discrete rotational steps the
droid shall undertake in its rotation. @return false upon failure. */ boolean Droid_rotate(Direction a_dir, uint a_numSteps);
/** Drives the droid, in the given direction, by the given number of steps.
@param a_dir The direction in which the droid shall drive. @param a_numSteps The number of discrete translational steps the droid
shall undertake in its translation. @return false upon failure. */ boolean Droid_drive(Direction a_dir, uint a_numSteps);
#endif
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
#ifndef SENSORS_H#define SENSORS_H
#include "util.h"
// types typedef enum { SENSOR_PRECISION, SENSOR_LEFT, SENSOR_RIGHT, SENSOR_BUMP }
Sensor;
// functions/** Initializes all sensors on the droid. */ void Sensors_init();
/** Initializes the specified sensor on the droid. @param a_sensor The sensor we wish to initialize. */ void Sensors_initSensor(const Sensor a_sensor);
/** Returns the quantified strength of the signal being sensed by the specified sensor.
@param a_sensor The sensor whose signal strength we wish to retrieve.*/ uint Sensors_getSensorStrength(const Sensor a_sensor);
#endif
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Mike, Ian, and Dev
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Hierarchical State Machines(Harel Statecharts)
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Beaco n
A
10'Tape45 d e g .
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
F ind ingTape II
F ind ingTape I
TrackingTape
S t o p
E n t e r
F o u n d T
F o u n d T a p e
F o u n d T a p e
A Possible Top-Level State Diagram
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Work out State Diagrams to Implement Finding Tape I
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Acquiring
Drivingto
TapeStop
Enter
Beacon Acquired
Hit Tape
Finding Tape
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
TurningCW
TurningCCW
Acquired
EntryLeft Sensor Peaks
Set Slow Speed,Reverse
L R
Acquiring
Stop Turning, Post Beacon Aquired
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
Implementing Hierarchical State Machines
What do you need?
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
If current state is state oneExecute During function for state oneIf an event is active
If event is event oneExecute action function for state one : event oneDecide what the next state will be
EndifIf event is event two
Execute action function for state one : event twoDecide what the next state will be
EndifRepeat the block above as required for each of the
possible events affecting this state.If next state is different from current state
Execute exit function for state oneExecute entry function for new stateModify state variable to reflect the new state
EndifEndif Return from state machine function
State Machine Function Template
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
/**************************************************************************** Module d:\me218b\Lectures\Lecture 29\SMTemplate.c
Description This is a template file for implementing state machines.
Notes
History When Who What/Why -------------- --- -------- 02/18/99 10:19 jec built template from MasterMachine.c 02/14/99 10:34 jec Began Coding****************************************************************************//*----------------------------- Include Files -----------------------------*//* include header files for this state machine as well as any machines at the next lower level in the hierarchy that are sub-machines to this machine*/
/*----------------------------- Module Defines ----------------------------*/// define constants for the states and event for this machine
/*---------------------------- Module Functions ---------------------------*//* prototypes for private functions for this machine, things like entry & exit functions. *//*---------------------------- Module Variables ---------------------------*/// everybody needs a state variable, you may need others as wellstatic unsigned char CurrentState;
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
void RunStateMachine(unsigned char CurrentEvent ){ unsigned char NextState = CurrentState;
switch ( CurrentState ) { case STATE_ONE : // If current state is state one DuringStateOne(); //Execute During function for state one if ( CurrentEvent != NO_EVENT ) //If an event is active { switch ( CurrentEvent) { case EVENT_ONE : //If event is event one // Execute action function for state one : event one NextState = STATE_TWO;//Decide what the next state will be break; } // If next state is different from current state if ( NextState != CurrentState) { // Execute exit function for current state // Execute entry function for new state CurrentState = NextState; //Modify state variable } } return;
CMPE 118 MECHATRONICSCMPE 118 MECHATRONICS
/**************************************************************************** Function StartStateMachine
Parameters None
Returns None
Description Does any required initialization for this state machine Notes
Author J. Edward Carryer, 2/18/99, 10:38AM****************************************************************************/void StartStateMachine ( void ){ CurrentState = ENTRY_STATE;// call the (optional) entry function for the ENTRY_STATE// any other initialization necessary to re-start the state machine}