480 likes | 608 Views
Wireless Embedded InterNet working Foundations of Ubiquitous Sensor Networks Triggers and Sensing. David E. Culler University of California, Berkeley. An Analog World. Everything in the physical world is an analog signal Sound, light, temperature, gravitational force
E N D
Wireless Embedded InterNetworkingFoundations of Ubiquitous Sensor NetworksTriggers and Sensing David E. Culler University of California, Berkeley WEI - L05 sense
An Analog World • Everything in the physical world is an analog signal • Sound, light, temperature, gravitational force • Need to convert into electrical signals • Transducers: converts one type of energy to another • Electro-mechanical, Photonic, Electrical, … • Examples • Microphone/speaker • Thermocouples • Accelerometers • And digitize • Then manipulate WEI - L05 sense
An Analog World • Transducers • Allow us to convert physical phenomena to a voltage potential in a well-defined way. I V R ohm ? WEI - L05 sense
Water Level Float Sensor Flow Sensor Rain Sensor switch Temperature Switch Pressure Switch Magnetic Reed Contact Switch Tilt Sensor PhotoInterrupter Simplest Analog Device • Often think of it as an actuator, rather than a sensor • But that’s because of the circuit we put it in • It is binary (two states) but why is it not digital? WEI - L05 sense
Vacc To Sample a switch, make it digital • Many sensor are switches • Two “states” but not digital • Open => no current • Closed => no voltage drop • Cap charges to Vacc when open • Cap discharges to GND when closed VD VtH VtL D switch GND WEI - L05 sense
EPIC I/F Board - Digital Inputs WEI - L05 sense
Getting Input into the MCU WEI - L05 sense
MCU – a system on a chip WEI - L05 sense
Programmed IO WEI - L05 sense
Interrupts MCU Hardware WEI - L05 sense
Getting a hold of the event event void Boot.booted() { atomic { P2IE &= ~PIN7; /* Disable interrupt */ P2IFG &= ~PIN7; /* Clear interrupt flag */ P2DIR &= ~PIN7; /* Configure as input */ P2IES |= PIN7; /* Select Hi->Lo */ P2IE |= PIN7; /* Enable interrupts */ } } async event void HplSignalPort2.fired() { if ( P2IFG & PIN7 ) { P2IFG &= ~PIN7; post fired(); } } Software Transport Kernel Network Driver Code Link Handler dispatch HplSignal Phy Memory Mapped IO registers Radio Timers Ports ADC Hardware Flash MCU Hardware Interrupt WEI - L05 sense
Making Sense of Physical Information • Digital representation of physical phenomenon • Transducer => Signal Conditioning => ADC => • Conversion to physical units • Calibration and correction • Here: 0 / 1, True / False • Associating meaning to the reading • Open / Closed • Empty / Full • In Position / Not • Depends on the specific device taking the reading • The Context of the device WEI - L05 sense
Engineering Units Physical Phenomena Voltage ADC Counts Sensor ADC Software Analog to Digital • What we want • How we have to get there Physical Phenomena Engineering Units WEI - L05 sense
Vacc Rcomp VA Resistive Sensor Rsensor GND Ratiometric sensor • Va = Vacc* Rsens / (Rcomp+ Rsens) • use Vref = Vacc • D = M * Rsens / (Rcomp+ Rsens) WEI - L05 sense
Sampling Basics • How do we represent an analog signal? • As a time series of discrete values On the MCU: read the ADC data register periodically V Counts WEI - L05 sense
Range Too Big Range Too Small Ideal Range Sampling Basics • What do the sample values represent? • Some fraction within the range of values What range to use? WEI - L05 sense
Sampling Basics • Resolution • Number of discrete values that represent a range of analog values • MSP430: 12-bit ADC • 4096 values • Range / 4096 = Step Larger range less information • Quantization Error • How far off discrete value is from actual • ½ LSB Range / 8192 Larger range larger error WEI - L05 sense
Sampling Basics • Converting: ADC counts Voltage • Converting: Voltage Engineering Units WEI - L05 sense
Sampling Basics • Converting values in 16-bit MCUs vtemp = adccount/4095 * 1.5; tempc = (vtemp-0.986)/0.00355; tempc = 0 • Fixed point operations • Need to worry about underflow and overflow • Avoid divide and (to a lesser degree) multiply • Floating point operations • They can be costly on the node, but not ridiculous • Pay attention to overall all contribution to error command uint16_t TempInt.get() { uint16_t tval = (uint32_t)760*(uint32_t)val/4096 – 468; return tval;} WEI - L05 sense
Sampling Basics • What sample rate do we need? • Too little: we can’t reconstruct the signal we care about • Too much: waste computation, energy, resources • Example: • 2-bytes per sample, 4 kHz 8 kB / second • But the mote only has 10 kB of RAM… WEI - L05 sense
Shannon-Nyquist Sampling Theorem • If a continuous-time signal contains no frequencies higher than , it can be completely determined by discrete samples taken at a rate: • Example: • Humans can process audio signals 20 Hz – 20 KHz • Audio CDs: sampled at 44.1 KHz • Need to ensure there is no appreciable energy above 2x sample. WEI - L05 sense
Sampling Basics • Aliasing • Different frequencies are indistinguishable when they are sampled. • Condition the input signal using a low-pass filter • Removes high-frequency components • (a.k.a. anti-aliasing filter) WEI - L05 sense
Direct Samples Dithered Samples Sampling Basics • Dithering • Quantization errors can result in large-scale patterns that don’t accurately describe the analog signal • Introduce random (white) noise to randomize the quantization error. WEI - L05 sense
Analog-to-Digital Basics • So, how do you convert analog signals to a discrete values? • A software view: • Set some control registers : • Specify where the input is coming from (which pin) • Specify the range (min and max) • Specify characteristics of the input signal (settling time) • Enable interrupt and set a bit to start a conversion • When interrupt occurs, read sample from data register • Wait for a sample period • Repeat step 1 WEI - L05 sense
Block Diagram (MSP430) WEI - L05 sense
ADC Features WEI - L05 sense
ADCs: Resources or Computation • OS provides a convenient and safe abstraction of physical resources • Operating systems deal with devices, not ADCs. • TinyOS has strived to provide uniform, easy-to-use common abstraction of the ADC. • Should it? • ADC and how sampling is performed in “on the datapath” of the application. WEI - L05 sense
ADC Core • Input • Analog signal • Output • 12-bit digital value of input relative to voltage references • Linear conversion WEI - L05 sense
SAR ADC • SAR = Successive-Approximation-Register • Binary search to find closest digital value WEI - L05 sense
SAR ADC • SAR = Successive-Approximation-Register • Binary search to find closest digital value 1 Sample Multiple cycles WEI - L05 sense
SAR ADC 1 Sample Multiple cycles WEI - L05 sense
Timing driven by: TimerA TimerB Manually using ADC12SC bit Signal selection using SHSx Polarity selection using ISSH Sample and Conversion Timing WEI - L05 sense
Voltage Reference • Voltage Reference Generator • 1.5V or 2.5V • REFON bit in ADCCTL0 • Consumes energy when on • 17ms settling time • External references allow arbitrary reference voltage • Want to sample Vcc, what Vref to use? WEI - L05 sense
Sample Timing Considerations • Port 6 inputs default to high impedance • When sample starts, input is enabled • But capacitance causes a low-pass filter effect Must wait for the input signal to converge WEI - L05 sense
How it looks in code: ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; Software Configuration WEI - L05 sense
12 possible inputs 8 external pins (Port 6) 1 Vref+ (external) 1 Vref- (external) 1 Thermistor 1 Voltage supply External pins may function as Digital I/O orADC. P6SEL register What sort of a MUX is this? Inputs and Multiplexer WEI - L05 sense
16 sample buffer Each buffer configures sample parameters Voltage reference Input channel End-of-sequence CSTARTADDx indicates where to write next sample Conversion Memory WEI - L05 sense
Single-Channel Single-Conversion Single channel sampled and converted once Must set ENC (Enable Conversion) bit each time Sequence-of-Channels Sequence of channels sampled and converted once Stops when reaching ADC12MCTLx with EOS bit Repeat-Single-Channel Single channel sampled and converted continuously New sample occurs with each trigger (ADC12SC, TimerA, TimerB) Repeat-Sequence-of-Channels Sequence of channels sampled and converted repeatedly Sequence re-starts when reaching ADC12MCTLx with EOS bit Conversion Modes WEI - L05 sense
How it looks in code: Configuration ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; ADC12MCTL0 = EOS | SREF_1 | INCH_11; Reading ADC data m_reading = ADC12MEM0; Software Configuration WEI - L05 sense
A Software Perspective command void Read.read() { ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; ADC12MCTL0 = EOS | SREF_1 | INCH_11; call Timer.startOneShot( 17 ); } event void Timer.fired() { ADC12CTL0 |= ENC; ADC12IE = 1; ADC12CTL0 |= ADC12SC; } task void signalReadDone() { signal Read.readDone( SUCCESS, m_reading ); } async event void HplSignalAdc12.fired() { ADC12CTL0 &= ~ENC; ADC12CTL0 = 0; ADC12IE = 0; ADC12IFG = 0; m_reading = ADC12MEM0; post signalReadDone(); } WEI - L05 sense
A Software Perspective command void Read.read() { ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; ADC12MCTL0 = EOS | SREF_1 | INCH_11; call Timer.startOneShot( 17 ); } event void Timer.fired() { ADC12CTL0 |= ENC; ADC12IE = 1; ADC12CTL0 |= ADC12SC; } task void signalReadDone() { signal Read.readDone( SUCCESS, m_reading ); } async event void HplSignalAdc12.fired() { ADC12CTL0 &= ~ENC; ADC12CTL0 = 0; ADC12IE = 0; ADC12IFG = 0; m_reading = ADC12MEM0; post signalReadDone(); } WEI - L05 sense
A Software Perspective command void Read.read() { ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; ADC12MCTL0 = EOS | SREF_1 | INCH_11; call Timer.startOneShot( 17 ); } event void Timer.fired() { ADC12CTL0 |= ENC; ADC12IE = 1; ADC12CTL0 |= ADC12SC; } task void signalReadDone() { signal Read.readDone( SUCCESS, m_reading ); } async event void HplSignalAdc12.fired() { ADC12CTL0 &= ~ENC; ADC12CTL0 = 0; ADC12IE = 0; ADC12IFG = 0; m_reading = ADC12MEM0; post signalReadDone(); } WEI - L05 sense
A Software Perspective command void Read.read() { ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; ADC12MCTL0 = EOS | SREF_1 | INCH_11; call Timer.startOneShot( 17 ); } event void Timer.fired() { ADC12CTL0 |= ENC; ADC12IE = 1; ADC12CTL0 |= ADC12SC; } task void signalReadDone() { signal Read.readDone( SUCCESS, m_reading ); } async event void HplSignalAdc12.fired() { ADC12CTL0 &= ~ENC; ADC12CTL0 = 0; ADC12IE = 0; ADC12IFG = 0; m_reading = ADC12MEM0; post signalReadDone(); } WEI - L05 sense
A Software Perspective command void Read.read() { ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; ADC12MCTL0 = EOS | SREF_1 | INCH_11; call Timer.startOneShot( 17 ); } event void Timer.fired() { ADC12CTL0 |= ENC; ADC12IE = 1; ADC12CTL0 |= ADC12SC; } task void signalReadDone() { signal Read.readDone( SUCCESS, m_reading ); } async event void HplSignalAdc12.fired() { ADC12CTL0 &= ~ENC; ADC12CTL0 = 0; ADC12IE = 0; ADC12IFG = 0; m_reading = ADC12MEM0; post signalReadDone(); } WEI - L05 sense
Interrupts and Tasks command void Read.read() { ADC12CTL0 = SHT0_2 | REF1_5V | REFON | ADC12ON; ADC12CTL1 = SHP; ADC12MCTL0 = EOS | SREF_1 | INCH_11; call Timer.startOneShot( 17 ); } event void Timer.fired() { ADC12CTL0 |= ENC; ADC12IE = 1; ADC12CTL0 |= ADC12SC; } task void signalReadDone() { signal Read.readDone( SUCCESS, m_reading ); } async event void HplSignalAdc12.fired() { ADC12CTL0 &= ~ENC; ADC12CTL0 = 0; ADC12IE = 0; ADC12IFG = 0; m_reading = ADC12MEM0; post signalReadDone(); } Application Kernel Driver MCU ADC WEI - L05 sense
Interrupts and Tasks • Tasks are run-to-completion • Used to signal application events • Break up computation in the application • Interrupts • Generated by the hardware • Preempt execution of tasks • Interrupts and tasks can schedule new tasks Task Task Task Handler Interrupt Hardware WEI - L05 sense
TinyOS Generic Components • Multiple instances of a component • Type polymorphism • Compile-time configuration • All of the above WEI - L05 sense
TinyOS Parameterized Interface • Logically related array of interfaces • Improved code by handling all interfaces collectively • Compile time sizing across module boundaries • Basis fo discovery WEI - L05 sense