0% found this document useful (0 votes)
5 views34 pages

Module-2.1 - Embedded Systems and OO Modelling

This document outlines a module on Embedded Systems and Object-Oriented Modelling at UNIBO, focusing on the top-down approach to modelling embedded software. It discusses the importance of modelling for understanding system complexities, reusability, and correctness, while detailing programming paradigms and languages such as object-oriented programming. The document also includes practical examples of modelling embedded software, including a button-LED system and a smart light controller, emphasizing the use of control loops and finite state machines.

Uploaded by

arvin.lajani3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views34 pages

Module-2.1 - Embedded Systems and OO Modelling

This document outlines a module on Embedded Systems and Object-Oriented Modelling at UNIBO, focusing on the top-down approach to modelling embedded software. It discusses the importance of modelling for understanding system complexities, reusability, and correctness, while detailing programming paradigms and languages such as object-oriented programming. The document also includes practical examples of modelling embedded software, including a button-LED system and a smart light controller, emphasizing the use of control loops and finite state machines.

Uploaded by

arvin.lajani3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

v1.

0
20241022

Embedded Systems and IoT


Ingegneria e Scienze Informatiche - UNIBO
a.a 2024/2025
Lecturer: Prof. Alessandro Ricci

[module-2.1]
EMBEDDED SYSTEMS
AND OBJECT-ORIENTED
MODELLING
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 1
SUMMARY
• This module considers aspects concerning the modelling
of embedded software, using modelling paradigm such
as OO

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 2


DESIGN AND DEVELOPMENT OF
EMBEDDED SOFTWARE
• Two main modelling approaches
– top-down approach
• starting from the domain and its modelling using
high-level paradigms, such as OO
– bottom-up approach
• starting from the hardware characteristics and
related concepts
• This module is about a top-down approach

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 3


MODELLING AND PROGRAMMING
• Programming paradigms such as OO are first of all modelling
paradigms
– they specify how we can conceptualise, represent, model a
system
– howe we can analyse a problem and define the corresponding
solution in some application domain
• Model
– representation of relevant aspects of a system, abstracting from
those that are considered not relevant
– relevant aspects concern the structure, behaviour and interaction
of the system
• Strong relationships between modelling and programming [OLE]
– scandinavian school, where part of the OO is rooted

Ole Lehrmann Madsen, Birger Møller-Pedersen. A Unified Approach to Modeling and


Programming, Model Driven Engineering Languages and Systems. Lecture Notes in Computer
Science Volume 6394, 2010, pp 1-15.

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 4


IMPORTANCE OF MODELLING
• Analysing the problem and defining solutions in a more
abstract way than implementation technologies and
languages
• Advantages
– better understanding of system working and
management of related complexities
– reusability, portability
– extensibility
– to have more rigorous approaches to verify the
correctness of the system

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 5


MODELLING WHAT
- FUNDAMENTAL DIMENSIONS
• Structure
– what are the parts composing a system
• Behaviour
– the computational behaviour of every part
• Interaction
– how parts interact

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 6


PARADIGMS AND LANGUAGES
• Modelling paradigms
– set of concepts and principles defining models
• e.g.: object-oriented paradigm
– often related to programming
• es: OOP
• Modelling languages
– provide a rigorous non ambiguous way to represent models
• e.g.: UML language
– often related to specific modelling paradigms
• e.g. UML has been strongly inspired by the OO paradigm, in
particular for what concerns modelling the structure and
interactions

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 7


OBJECT-ORIENTED PARADIGM
• Main paradigm in modelling and programming software
– effective level of abstraction to capture and represent
essential aspects of the problem and its domain
• Fostering important properties at the design and
programming level
– modularity
– encapsulation
– reuse and extensibility mechanism
• Among important aspects not directly captured by the
paradigm
– concurrency, asychronous interactions, distribution

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 8


MODELLING EMBEDDED SOFTWARE
BASED ON MICRO-CONTROLLERS
• Two main macro parts
– controller
• encapsulating the control/application logic concerning the
task(s) to do
• to this purpose, it accesses/uses/observes passive resources
and devices
• active component, based on some control architecture
– controlled elements
• modelling the resources and devices managed/used by the
controller to do its job
– e.g. I/O devices, sensors, actuators
• encapsulates functionalities useful for the controller
• passive component

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 9


FEATURES
• Controller
– Integrating pro-active and re-active behaviour
– based on a control loop architecture
• simplest one: super-loop
• in a future module: event-loop
• Controlled elements
– well-defined interface
– observable state and events
– reusability, controllability
– OO model

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 10


BUTTON-LED EXAMPLE
• Modelling the button-led system
– a button functioning as switch to control a led
– the (software) controller must switch on the led when
the button is pressed and switch off it when the button
is released

Button Controller Led

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 11


AN OO MODEL OF THE BUTTON-LED
<<interface>> <<interface>>
Controller
Button Led

isPressed():boolean switchOn()
switchOff()

• Interfaces
– Button
• isPressed(): boolean
– Led
• switchOn()
• switchOff()

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 12


CONTROLLER: CONTROL LOOP
• Control loop — based on a super-loop architecture in this case

lightOn := false

loop {
/* first: sensing */
bool isPressed = button.isPressed();

/* processing and acting */


if (!lightOn && isPressed){
light.switchOn()
lightOn := true
} else if (lightOn && !isPressed){
light.switchOff()
lightOn := false
}
}
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 13
FROM MODELS TO CODE:
ARDUINO IN WIRING/C++
#ifndef __LIGHT__ /* Led.cpp */
#define __LIGHT__ #include "Led.h"
#include "Arduino.h"
<<interface>> class Light {
public: Led::Led(int pin){
Light
virtual void switchOn() = 0; this->pin = pin;
virtual void switchOff() = 0; pinMode(pin,OUTPUT);
switchOn() }; }
switchOff()
#endif void Led::switchOn(){
digitalWrite(pin,HIGH);
#ifndef __LED__ }
#define __LED__
Led
void Led::switchOff(){
int pin #include "Light.h" digitalWrite(pin,LOW);
switchOn() };
switchOff() class Led: public Light {
public:
Led(int pin);
void switchOn();
void switchOff();
protected:
int pin;
};

#endif
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 14
FROM MODELS TO CODE:
ARDUINO IN WIRING/C++
• Light interface (in Light.h)
– interface for any lighting device — in C++: an abstract class
• Led device
– concrete type of Light
– declaration of the Led class in Led.h
– definition/implementation of the Led class in Led.cpp
• Remarks
– C++ allows for separating class declaration from class definition
– to use/access Arduino base library/procedures/constants in *.cpp
files, the header “Arduino.h” must be included
• not needed in the *.ino main file

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 15


FROM MODELS TO CODE:
ARDUINO IN WIRING/C++
#ifndef __BUTTON__ #include "ButtonImpl.h"
#define __BUTTON__ #include "Arduino.h"

<<interface>> class Button { ButtonImpl::ButtonImpl(int pin){


Button this->pin = pin;
public: pinMode(pin, INPUT);
virtual bool isPressed() = 0; }
isPressed()
};
bool ButtonImpl::isPressed(){
#endif return digitalRead(pin) == HIGH;
}
ButtonImpl #ifndef __BUTTONIMPL__
int pin #define __BUTTONIMPL__

isPressed() #include "Button.h"

class ButtonImpl: public Button {


public:
ButtonImpl(int pin);
bool isPressed();

protected:
int pin;
};
#endif

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 16


FROM MODELS TO CODE:
ARDUINO IN WIRING/C++
• Button
– basic button interface
• ButtonImpl
– a class implementing buttons

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 17


FROM MODELS TO CODE:
ARDUINO IN WIRING/C++
#include "Led.h"
• Setup #include "ButtonImpl.h"

– objects creation #define LED_PIN 13


#define BUTTON_PIN 2
• Main (super-)loop Light* light;
Button* button;
boolean lightOn;

void setup(){
light = new Led(LED_PIN);
button = new ButtonImpl(BUTTON_PIN);
lightOn = false;
}

void loop(){
bool isPressed = button->isPressed();
if (!lightOn && isPressed){
light->switchOn();
lightOn = true;
} else if (lightOn && !isPressed){
light->switchOff();
lightOn = false;
}
}
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 18
REMARKS
• Object-Oriented level of abstraction
– low-level implementation details (e.g. the specific
wiring primitives used to drive pins) are hidden, not
exposed to the controller
– better readability, portability, reusability

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 19


EXTENSION: AN EXAMPLE
• Modelling a led with light intensity
<<interface>>
Light

switchOn()
switchOff()

Led <<interface>>
int pin LightExt
switchOn()
switchOff() setIntensity(int)

LedExt

setIntensity(int)

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 20


EXTENSION: AN EXAMPLE
• Modelling a led with light intensity
#ifndef __LIGHT_EXT__ #include "LedExt.h"
#define __LIGHT_EXT__ #include "Arduino.h"
#include "Light.h"
LedExt::LedExt(int pin) : Led(pin) {
class LightExt : public Light { currentIntensity = 128;
public: isOn = false;
virtual void setIntensity(int) = 0; }
}; LedExt::LedExt(int pin, int intensity) : Led(pin) {
#endif isOn = false;
currentIntensity = intensity;
#ifndef __LED_EXT__ }
#define __LED_EXT__
#include "Led.h" void LedExt::switchOn(){
#include "LightExt.h" analogWrite(pin,currentIntensity);
isOn = true;
class LedExt: public LightExt, public Led { }
public: void LedExt::setIntensity(int value){
LedExt(int pin); currentIntensity = value;
LedExt(int pin, int intensity); if (isOn){
void switchOn(); analogWrite(pin,currentIntensity);
void switchOff(); }
void setIntensity(int v); }
private: void LedExt::switchOff(){
int currentIntensity; analogWrite(pin,0);
bool isOn; isOn = false;
}; }
#endif

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 21


FADING
#include "LedExt.h"

// the pin must by a PWM one


#define LED_PIN 9

LightExt* led;
int brightness;
int fadeAmount;

void setup(){
brightness = 0;
fadeAmount = 5;
led = new LedExt(LED_PIN,brightness);
led->switchOn();
}

void loop(){
led->setIntensity(brightness);
brightness = brightness + fadeAmount;

if (brightness == 0 || brightness == 255) {


fadeAmount = -fadeAmount ;
}

delay(20);
}

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 22


REMARKS
• The new LedExt class can be reused also in previous
examples, without changing the code

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 23


FROM A BUTTON-LED TO A SMART
LIGHT
• As a simple variant, let’s consider an environment equipped with a light that
can be switched on or off depending on the presence of people around.
Moreover, the light should be switched on even if the environment is dark
• Evolution of previous system
– Light (output)
– MovementDetector (input)
– LightDetector (input)
– SmartLightController
<<interface>>
MovementDetector

detected():boolean
SmartLight <<interface>>
Controller Light

<<interface>> switchOn()
LightDetector switchOff()

getIntensity(): double

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 24


SMART LIGHT CONTROLLER
• Control loop
lightOn := false

loop {
detected := presDetector.detected()
intens := lightSensor.getIntensity()
isLowIntens := intens < LIGHT_THRESHOLD
if (!lightOn && detected && isLowIntens){
light.switchOn()
lightOn := true
} else if (lightOn && (!isLowIntens || !detected)){
light.switchOff()
lightOn := false
}
}

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 25


CONTROL LOOP AND FINITE STATE
MACHINES
• Execution model based on a control loop (or super loop)
– at each cycle the controller read the input state and
choose the proper action to perform
• the choice of the action depends on the current
state
• the action can lead to update the current state
• Need of having proper formalisms to describe state-
based behaviours
– Finite State Machines (FSM)
• synchronous, asynchronous
– next module

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 26


CONTROL LOOP: ABOUT
EFFICIENCTY AND REACTIVITY
• Efficiency
– in the examples the loop is always cycling even if the
controller is conceptually waiting for an input
• Reactivity
– depends on how fast a cycle is completed
– if during a single cycle sensors change states multiple
times, these changes are not detected

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 27


ABOUT REACTIVITY: EXAMPLE
• “Implement a controller for making a continuous fade in and fade out
of a led, with duration DT seconds, using a button to start and stop
asynchronously the process”
• Simple solution …
running := false fadeIn(){
loop { intensity := 0
if (button.switchedOn() && delta := 1/NUM_STEPS;
!running){ for (i := 0; i < NUM_STEPS; i++){
running := true intensity := intensity + delta
light.switchOn() light.setIntensity(intensity)
} else if (button.switchedOff() sleep(DeltaTime)
&& running){ }
running := false }
light.switchOff()
} fadeOut(){...}
if (running){
fadeIn()
fadeOut()
PROBLEM:
}
}
if the button changes during the fade in/out,
the controller does not detect it…
ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 28
THE CONTROLLER AS AN ACTIVE
ENTITY
• About the conceptual nature of the controller
– can it be modelled conceptually as an object in OO modelling?
• Actually objects in OO modelling/design/programming are typically
passive entities
– featuring proper interfaces to be used by the client of the object
– they don’t encapsulate a control flow
• The controller conceptually is an active entity, with its own logical
control flow
– at the design level, it is meant to encapsulate the control strategy
of the system
• So what kind of abstraction can be used to model this kind of
entities?

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 29


CONTROLLER MODEL: AGENTS
• We will use the concept of “agent” to model controllers
– agents are first-class active entities, equipped with an
autonomous logical control flow, designed to achieve
or perform some task(s) that call for being reactive to
data and events perceived from the environment
(through sensors), and that do actions to have an
effect on the environment though some actuators
• Agent as first-class design abstraction
– reactive + pro-active behaviour
– encapsulating a control flow
– task-oriented design

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 30


AGENTS IN LITERATURE
• Concepts adopted in various contexts
– Artificial Intelligence (AI) and Distributed AI
– modelling and simulation
– software engineering (Agent-Oriented Software Engineering)
– Agents and Multi-Agent Systems Community [WOO,JEN]
sensors

ck
dba

pe
fee PERCEPTION

rce
pts
ENVIRONMENT

DECISION
act o
ion
s od
nt
tio
ac ACTION

effectors / actuators

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 31


CONTROL LOOP - TASK-BASED
DECOMPOSITION AND EXECUTION
• Complex jobs can be decomposed into tasks, that are executed by
the control loop
– the agent carries on one or multiple tasks concurrently
– the behaviour of each task can be described by a state machine
• This architecture is described in next modules

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 32


FROM INDIVIDUAL AGENTS TO
MULTI-AGENT SYSTEMS
• Complex embedded systems can be distributed and
actually include a system of interacting/cooperating
independent sub-systems
– e.g. a smart home, a smart city
– Internet of Things (IoT) perspective
• In order to model these systems we can consider multi-
agent systems, as ensemble of agents that interact and
cooperate [WOO,JEN]

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 33


BIBLIOGRAPHY
• [OLE] Ole Lehrmann Madsen, Birger Møller-Pedersen. A Unified Approach
to Modeling and Programming, Model Driven Engineering Languages and
Systems. Lecture Notes in Computer Science Volume 6394, 2010, pp 1-15.
• [GOF] Erich Gamma; Richard Helm, Ralph Johnson, John M. Vlissides
(1995). Design Patterns: Elements of Reusable Object-Oriented Software.
Addison-Wesley
• [WOO] Michael Wooldridge. An Introduction to MultiAgent Systems. John
Wiley & Sons
• [JEN] Nicholas R. Jennings. 2001. An agent-based approach for building
complex software systems. Commun. ACM 44, 4 (April 2001), 35-41.

ESIOT ISI-LT - UNIBO EMBEDDED SYSTEMS AND OO MODELLING 34

You might also like