actuators, and i/o lecture 2: sensors,

67
Lecture 2: Sensors, Actuators, and I/O

Upload: others

Post on 07-Nov-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Actuators, and I/O Lecture 2: Sensors,

Lecture 2: Sensors, Actuators, and I/O

Page 2: Actuators, and I/O Lecture 2: Sensors,

“Discussion about labs

2

Page 3: Actuators, and I/O Lecture 2: Sensors,

Embedded systems are everywhere, have specific purposes, and unique challenges

Microcontrollers (MCUs) have CPU, I/O, memory on one chip

In lab you began working with an MCU and embedded hardware

In homework you previewed sensors, actuators, I/O

Review

3

Page 4: Actuators, and I/O Lecture 2: Sensors,

Today⬢ Sensors and actuators⬢ I/O⬢ Interrupts

4

Page 5: Actuators, and I/O Lecture 2: Sensors,

“Why would we as software

engineers care about circuits, analog components, how I/O

works, etc?

5

Page 6: Actuators, and I/O Lecture 2: Sensors,

Ideas

6

Page 7: Actuators, and I/O Lecture 2: Sensors,

Difference in electric potential

Measured between two points (or one point and implicit ground)

We say we measure voltage across a component

Voltage

Voltage across resistor

7

Page 8: Actuators, and I/O Lecture 2: Sensors,

CurrentRate of flow of charged particles through a circuit

Convention in circuits: imagine particles flowing from positive to negative terminal

We say we measure current through a component

Current through resistor

8

Page 9: Actuators, and I/O Lecture 2: Sensors,

Parallel and series componentsSeries Parallel

Current is the same through both componentsVoltage: ???

Voltage is the same across both componentsCurrent: ???

9

Page 10: Actuators, and I/O Lecture 2: Sensors,

Kirchoff’s lawsSum of currents flowing into a node is the same as sum of currents flowing out of the node

Sum of voltages around a closed loop is zero

Vin

V1

V2

-Vin + V1 + V2 = 0Vin = V1 + V2

II1I2

I = I1 + I210

Page 11: Actuators, and I/O Lecture 2: Sensors,

Ohm’s law: V = IR (SI units: volts, amperes, ohms)

Power law: P = IV (SI units: watts, amperes, volts)

Useful for:Computing values needed to build circuit

Figuring out the limits of what you can attach to your microcontroller

Writing down accurate math for modeling your system

Ohm’s law & power law

11

Page 12: Actuators, and I/O Lecture 2: Sensors,

Thevenin equivalent circuitAny linear electrical network containing current sources, voltage sources, and resistors can be replaced by an equivalent circuit with one voltage source and one resistor

12

Page 13: Actuators, and I/O Lecture 2: Sensors,

Thevenin for resistors in seriesV = IR

Current is the same through both componentsVin

V1

V2

Vin = V1 + V2

VinR1

R2Vin Rs = ??

13

Page 14: Actuators, and I/O Lecture 2: Sensors,

Thevenin for resistors in parallelV = IR

Vin R1R2 Vin Rp = ??

II1I2

I = I1 + I2

Voltage is the same across both components

14

Page 15: Actuators, and I/O Lecture 2: Sensors,

“Given Ohm’s law (V = IR) and the Power law (P = IV), what

is the maximum power output of your Arduino pin

(rated at 3.3 V, 7 mA)

15

Page 16: Actuators, and I/O Lecture 2: Sensors,

What is the actual minimum resistance?

16

Page 17: Actuators, and I/O Lecture 2: Sensors,

Interpreting device data sheetsYellow LED data sheet

17

Page 18: Actuators, and I/O Lecture 2: Sensors,

Digital devicesLeds are digital output devices

Things like push buttons are digital input devices

(When connected correctly) are driven by or produce a high/low signal

18

Page 19: Actuators, and I/O Lecture 2: Sensors,

“Can you give some examples

of analog (produce/are driven by a continuous signal)

peripherals?

19

Page 20: Actuators, and I/O Lecture 2: Sensors,

Your book talks about:⬢ Accelerometers (measure acceleration of displaced

mass)⬢ Anemometers (air flow for velocity)⬢ GPS (satellite for position)⬢ Gyroscopes (gimbals and modern)⬢ Microphones⬢ Engine controllers, thermometers, cameras, chemical

sensors, etc

Input components

20

Page 21: Actuators, and I/O Lecture 2: Sensors,

Other input components in your kits⬢ Photoresistor - resistance changes

based on light⬢ Potentiometer - outputs voltage based

on rotation of the dial⬢ Tilt sensor - Metal bearing completes

circuit

Image source 21

Page 22: Actuators, and I/O Lecture 2: Sensors,

Output componentsYour book talks about:⬢ LEDs⬢ Motors (DC)

Your kits have:⬢ LCD screen (controlled digitally)⬢ Servo motor (controlled by lengths of high/low pulses)⬢ Piezo speaker (electricity displaces film to make

sound)

Image source

22

Page 23: Actuators, and I/O Lecture 2: Sensors,

“How do you control a DC

motor that requires an external power source?

23

Page 24: Actuators, and I/O Lecture 2: Sensors,

Basically an electric switchVoltage applied to Gate connects Drain and Source

Come in different types (beyond the scope of this course)

The MOSFET transistor in your kits has a minimum gate voltage of 4.2V… what do we do?

Transistors

24

Page 25: Actuators, and I/O Lecture 2: Sensors,

Optocouplers⬢ Control one circuit using another, but they

are completely electrically separate!

Image source 25

Page 26: Actuators, and I/O Lecture 2: Sensors,

Motor driven using mosfet and optocoupler

26

Page 27: Actuators, and I/O Lecture 2: Sensors,

SummaryLaws of physics help us compute properties of circuits, for design, understanding, and modeling

Devices can be driven/read with digital or analog signals, can be inputs (sensors) or outputs (actuators)

27

Page 28: Actuators, and I/O Lecture 2: Sensors,

Break

28

Page 29: Actuators, and I/O Lecture 2: Sensors,

Analog ↔ Digital I/OMicrocontrollers are digital

How do we read in analog input?

How do we produce or simulate analog output?

29

Page 30: Actuators, and I/O Lecture 2: Sensors,

Modeling analog devicesUseful to have a mathematical model of sensors/actuators for verification and understanding

30

Page 31: Actuators, and I/O Lecture 2: Sensors,

Linear and Affine Models, RangeMap physical value at time t x(t) to sensor input

Approximate with linear affine function, saturate if outside range [L,H]:

31

Page 32: Actuators, and I/O Lecture 2: Sensors,

Example: photoresistor Physical value x(t) is measure of light (e.g. illuminance)

MCU input voltage depends on physical properties of the sensor and how it’s wired

First compute intermediate function v(x(t)) = a’x(t) + b’, which produces an input voltage between 0v and VCC

Meaning a’L + b’ = 0; a’H + b’ = VCC

Can solve for L and H given a’,b’,VCC32

Page 33: Actuators, and I/O Lecture 2: Sensors,

QuantizationInput to MCU is not a voltage, it is a number of some precision

For example, 10 bits precision: input is number from 0-1023

For our example,f(v(x(t)) = 1023 * v(x(t)) / VCC

33

Page 34: Actuators, and I/O Lecture 2: Sensors,

“If VCC = 3.3v and our

precision is 10 bits, what is the smallest change in

voltage that we can detect?

34

Page 35: Actuators, and I/O Lecture 2: Sensors,

“What is the smallest change in lux we can detect for our

example?

35

Page 36: Actuators, and I/O Lecture 2: Sensors,

Error of measured value

Comes from:

Quantization

Sensor imperfections

What is the noise of our photoresistor?

Noise

36

Page 37: Actuators, and I/O Lecture 2: Sensors,

Analog to Digital ConverterAnalog signal gets discretized to some number

Done via an Analog to Digital Converter (ADC)

MCUs have ADC built inDifferent implementations - idea is to compare to reference voltages

Why it matters to embedded software developers: cost, timing, precision

Find this in the data sheet!From the SAMD 21 family datasheet

37

Page 38: Actuators, and I/O Lecture 2: Sensors,

ADC sharingMutex

Up to individual task to check for ADC to be free and then ask for sample

Time-triggered

ADC runs in background and converts for all possible sources, storing the latest for each in a buffer

38

Page 39: Actuators, and I/O Lecture 2: Sensors,

“What tradeoffs do you see

between mutex and time-triggered ADC sharing?

39

Page 40: Actuators, and I/O Lecture 2: Sensors,

ADC sharingMutex Time-triggered

40

Page 41: Actuators, and I/O Lecture 2: Sensors,

What about outputs?Digital to analog converter - pin produces voltage from 0 to VCC

Pulse Width Modulation - simulates analog output

41

Page 42: Actuators, and I/O Lecture 2: Sensors,

Digital to Analog Converter (DAC)Divide voltage based on digital number

Actuator driven by DAC has similar quantization error to ADC

Some MCUs don’t have DACs

Expensive

Fewer applications

Unlike ADC, cannot shareImage source42

Page 43: Actuators, and I/O Lecture 2: Sensors,

Pulse Width Modulation (PWM)Rapidly switch digital pin on and off

Creates perception of analog output

Increasing/decreasing duty cycle increases/decreases perception of power output level

Many microcontrollers provide PWM peripherals

Image source 43

Page 44: Actuators, and I/O Lecture 2: Sensors,

Digital Signal Processing (DSP)ADC/DAC/PWM combined with computational power of an MCU has enabled the explosion of digital applications

⬢ Audio, video, robotics, medical…

MCU lets you take in an analog signal, do computations on it, and produce a new analog signal

DSP is a cool area but (mostly) beyond the scope of this course 44

Page 45: Actuators, and I/O Lecture 2: Sensors,

These things are not perfectQuantization, non-linearity, error in components all contribute to imprecision

DSP can help alleviate some sources of error

Design and models that take sources of error into account are vital for some applications

45

Page 46: Actuators, and I/O Lecture 2: Sensors,

Example of very simple DSP: debouncing

What do we do here?Image source

46

Page 47: Actuators, and I/O Lecture 2: Sensors,

Break

47

Page 48: Actuators, and I/O Lecture 2: Sensors,

“How did we read inputs from sensors in the first lab? What

are the upsides and downsides to this method?

48

Page 49: Actuators, and I/O Lecture 2: Sensors,

Sensors

49

Page 50: Actuators, and I/O Lecture 2: Sensors,

Polling vs interruptsPolling: reading input periodically, keep track of changes

Interrupt: be alerted when input changes/does something we are watching forReal life examples: push notification vs checking texting app, rice cooker alerting you vs manually checking doneness, pet begging for food instead of you checking their bowl...

50"Alarm Clock 2" by Alan Cleaver is licensed under CC BY 2.0

Page 51: Actuators, and I/O Lecture 2: Sensors,

Types of interruptsSoftware interrupts - function is called or some bits are set in a specific memory location to tell the software to go to an interrupt service routine (ISR) Hardware interrupts - external trigger (voltage change on pin) tells software to go to an ISR

Exceptions - internal trigger (like writing to a protected memory location) triggers a fault

51

Page 52: Actuators, and I/O Lecture 2: Sensors,

Interrupt process1. Program executing normally2. Interrupt triggered3. Processor saves program state4. Processor enters ISR5. Processor restores program state6. Program resumes executing

52

Page 53: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory

PC

Stack

SP

53

Page 54: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory

PC

Stack

SP

Interrupt happens

54

Page 55: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory

PC

Stack

SP

Old PC

Program state (local variables, etc)*

*architecture/MCU-dependent. Sometimes it is up to the programmer to save specific state such as registers

55

Page 56: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory

PC

Stack

SP

Old PC

Program state (local variables, etc)

Interrupt type 1: code memory location 1Interrupt type 2: code memory location 2….

Interrupt vector table

56

Page 57: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory Stack

SP

Old PC

Program state (local variables, etc)

Interrupt type 1: code memory location 1Interrupt type 2: code memory location 2….

Interrupt vector table

PC

57

Page 58: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory Stack

SP

Old PC

Program state (local variables, etc)

PC

58

Page 59: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory Stack

SPOld PC

PC

59

Page 60: Actuators, and I/O Lecture 2: Sensors,

Main function

ISR

Code in memory Stack

SP

PC

60

Page 61: Actuators, and I/O Lecture 2: Sensors,

What if multiple interrupts happen?Often interrupts are prioritized

Higher priority interrupt is allowed to interrupt lower priority interrupt

Ties broken by position in vector interrupt table

Programmer configures this when setting up code

61

Page 62: Actuators, and I/O Lecture 2: Sensors,

Implementing ISRsOften only one handler for some type of interrupt

Example on SAMD21 (your Arduino MCU): external interrupts have one ISR

Why?

Check flags that are set by MCU to see which pin/peripheral triggered the interrupt -- you will see this in lab

62

Page 63: Actuators, and I/O Lecture 2: Sensors,

“What is the difference

between an interrupt and a subroutine call?

63

Page 64: Actuators, and I/O Lecture 2: Sensors,

Subroutine call: you know exactly when in the code you call it

Interrupt: can happen at any point, even “inside” of a command

Even x = x + 1 is made up of multiple machine instructions (load x from memory, increment x, write value back to memory)

Atomic instructions: values being read/changed in atomic instruction cannot be read/changed by anyone else 64

Interrupts can happen at any time

Page 65: Actuators, and I/O Lecture 2: Sensors,

Homework problem 10.7

65

char flag = 0;volatile char* display;volatile short sensor1, sensor2;

void ISR() { if (flag) { sensor1 = readSensor1(); } else { sensor2 = readSensor2(); }}

int main() { // ... set up interrupts ... // ... enable interrupts ... while(1) { if (flag) { if isFaulty2(sensor2) { display = "Sensor2 Faulty"; } } else { if isFaulty1(sensor1) { display = "Sensor1 Faulty"; } } flag = !flag; }}

(a) Is it possible for the ISR to update the value of sensor1 while the main function is checking whether sensor1 is faulty? Why or why not?

(b) Suppose a spurious error occurs that causes sensor1 or sensor2 to be a faulty value for one measurement. Is it possible for that this code would not report “Sensor1 faulty” or “Sensor2 faulty”?

(c) Assuming the interrupt source for ISR() is timer-driven, what conditions would cause this code to never check whether the sensors are faulty?

(d) Suppose that instead being interrupt driven, ISR and main are executed concurrently, each in its own thread. Assume a microkernel that can interrupt any thread at any time and switch contexts to execute another thread. In this scenario, is it possible for the ISR to update the value of sensor1 while the main function is checking whether sensor1 is faulty? Why or why not?

Page 66: Actuators, and I/O Lecture 2: Sensors,

Why do we care?Interrupts are powerful and used widely in embedded programming

Understanding how interrupts affect program state/variables aids in design and debugging

Interrupts complicate modeling and timing analysis of a system (more on this later)

66

Page 67: Actuators, and I/O Lecture 2: Sensors,

SummaryPhysical devices (sensors and actuators) obey the laws of physics

Can be controlled by digital or analog signalsCan model this conversion using math

Conversion between digital/analog: ADC, DAC, PWM

Interrupts allow us to detect changes in inputs instead of just polling for them

67