Embedded ProgrammingLearning ObjectivesMechatronics Concept MapProcedural vs. Embedded ProgrammingEvent Driven Program StructureRequirements for Events and ServicesEvent Checkers for Discrete EventsCode to Check for Discrete EventsEvents involving an analog quantityEvent Checkers for Events Involving Analog QuantitiesEvent Detection with HysteresisCode to Check for Events With Analog QuantitiesMain body of Events & Services CodeEmbedded ProgrammingB. Furman09MAY2011Learning ObjectivesDistinguish between procedural programming and embedded programmingExplain the Events and Services embedded programming frameworkExplain what an Event isExplain what a Service isExplain the key rule and its two corollariesDescribe how an event checking routine works forDiscrete quantitiesAnalog quantitiesDevelop code for:an event checking routinea servicean event driven systemMechatronics Concept MapController(Hardware & Software)System toControlSensorSignalConditioningPowerInterfaceActuatorUserInterfacePowerSourceBJ Furman 22JAN11ME 106ME 154ME 157ME 195ME 120ME 297AME 106ME 120ME 106ME 190ME 187ME 110ME 136ME 154ME 157ME 182ME 189ME 195ME 106ME 120ME 106 INTEGRATIONProcedural vs. Embedded ProgrammingProceduralME 30/CmpE 46Computation and analysis programsMostly sequentialStart … EndKnown inputs and outputsProgram is in controlPredictable operation and timingEmbeddedME 106Inputs and outputs can occur at any time, in any order and are not predictableInputs can come from multiple sourcesSensors, user inputs, or internal (timer, ADC, etc.)May handle simultaneous inputs and outputsProgram never endsEvent Driven Program StructureProgramming task divides into: Checking for eventsServicing events when they occurEventA detectable change or transition in something of interestButton press (before: not pressed, after: pressed)ADC complete flag bit setServiceAn action taken in response to an eventRequirements for Events and ServicesThe occurrence of events must be checked for continuously and oftenServices must execute quickly and must be non-blockingEx. Determine if a switch has closedBlocking code:while(digitalRead(pin) == HIGH);Event Checkers for Discrete EventsEx. Check that a switch has closedPseudocodeIF switch is closed AND switch was open last time, THENSwitchClosed event has occurredELSESwitchClosed event has not occurredNeed to keep track of the state of the switch (i.e., maintain its history)Use a state variableWill need to be declared as a static local variable in the function that checks for the eventNeed to retain the value between successive calls to the event checking functionCode to Check for Discrete EventsEvents involving an analog quantityhttp://www.soe.ucsc.edu/classes/cmpe118/Winter08/LectureNotes/EventDrivenProg.pdfA single-valued threshold will likely result in “chatter” when the quantify of interest is near the thresholdEvent Checkers for Events Involving Analog QuantitiesFiltering the signal may helpAdd hysteresis in the event checkerMake the criteria for when the event occurs a variable instead of a fixed valueInitially threshold is set to an upper valueAs soon as the signal crosses the threshold, the threshold is dropped to a lower valuePseudocode:Set threshold to high valueIF var is greater than or equal to the threshold, THENEvent has happenedSet threshold to lower valueELSEEvent has not happenedEvent Detection with Hysteresishttp://www.soe.ucsc.edu/classes/cmpe118/Winter08/LectureNotes/EventDrivenProg.pdfCode to Check for Events With Analog QuantitiesMain body of Events & Services
View Full Document