Distributed Real-Time Control Systems. Chapter 10 Real-Time Digital Control

Similar documents
Distributed Real- Time Control Systems. Lecture 7 Real- Time Control

Interrupts Arduino, AVR, and deep dark programming secrets. What is an Interrupt?

Physics 120B: Lecture 11. Timers and Scheduled Interrupts

Laboratory 4 Usage of timers

Interrupts and timers

Topic 11: Timer ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

TIMSK=0b ; /* enables the T/C0 overflow interrupt in the T/C interrupt mask register for */

Physics 124: Lecture 12. Timer Basics. Timers and Scheduled Interrupts

Marten van Dijk, Syed Kamran Haider

Unit 14 Timers and Counters 14.1

Unit 13 Timers and Counters

EE 308: Microcontrollers

AVR Timers TIMER0. Based on:

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz III

ADC: Analog to Digital Conversion

Supplementary Materials: Fabrication of a Lab on Chip Device Using Material Extrusion (3D Printing) and Demonstration via Malaria Ab ELISA

School of Electrical, Computer and Telecommunications Engineering University of Wollongong Australia

CSCE 236 Embedded Systems, Spring 2012 Quiz/Test 2

Newbie s Guide to AVR Timers

X X X. VGA - Standard:

Newbie's Guide to AVR Timers (C) Dean Camera, 2007

Marten van Dijk Department of Electrical & Computer Engineering University of Connecticut

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz IV

RTC rtc(dst_on); U8GLIB_SH1106_128X64 u8g(u8g_i2c_opt_none U8G_I2C_OPT_DEV_0);

Software debouncing of buttons

AN HONORS UNIVERSITY IN MARYLAND UMBC. AvrX. Yousef Ebrahimi Professor Ryan Robucci

onetesla Interrupter Firmware Guide General Overview

Fundamental concept in computation Interrupt execution of a program to handle an event

INTERRUPTS in microprocessor systems

Embedded Systems Design with the Atmel AVR Microcontroller Part II

MEDIS Module 2. Microcontroller based systems for controlling industrial processes. Chapter 4: Timer and interrupts. M. Seyfarth, Version 0.

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz V

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

Introduction to Embedded Systems

Interrupts & Interrupt Service Routines (ISRs)

COMP2121: Microprocessors and Interfacing

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

Embedded Systems and Software

CN310 Microprocessor Systems Design

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

UNIVERSITY OF MANITOBA Midterm

CENG-336 Introduction to Embedded Systems Development. Timers

Processor and compiler dependent

Chapter 2. Overview of Architecture and Microcontroller-Resources

More Arduino Programming

Arduino Part 2. Introductory Medical Device Prototyping

In Class Assignment 2

Programming Timer1 as a Stopwatch

Introduction to Embedded Systems

Embedded Systems and Software

INTERRUPT, TIMER/COUNTER. KONKUK UNIV. VLSI Design Lab. LSI Design Lab

12.1. Unit 12. Exceptions & Interrupts

Use a semaphore to avoid the enqueue and dequeue functions from accessing and modifying count variable at the same time.

AGH University of Science and Technology Cracow Department of Electronics

Microprocessors & Interfacing

8254 is a programmable interval timer. Which is widely used in clock driven digital circuits. with out timer there will not be proper synchronization

The Atmel ATmega328P Microcontroller

Department of EIE / Pondicherry Engineering College. Timer/Counters. Department of EIE / Pondicherry Engineering College 1

AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Features. Introduction. AVR 8-bit Microcontrollers APPLICATION NOTE

Atmel AVR Timers and Interrupts

The 8051 microcontroller has two 16-bit timers/counters called T0 and T1.

Arduino. (Digital-to-Analog Converter D/A D2A) (Digital to Analog Conversion) 3. (Analog to Digital conversion)

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

The MC9S12 Timer Output Compare Function Making an event happen at specific time on the HC12 The MC9S12 Output Compare Function

Capturing the Time of an External Event Input Capture Subsystem

The Atmel ATmega168A Microcontroller

CSCE374 Robotics Fall 2013 Notes on the irobot Create

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Lab Test III

SquareWear Programming Reference 1.0 Oct 10, 2012

The MC9S12 Timer Input Capture Function

Introduction. Unit 4. Numbers in Other Bases in C/C++ BIT FIDDLING. Microcontrollers (Arduino) Overview Digital I/O

UNIVERSITY OF MANITOBA Final Exam

Embedded Systems. October 2, 2017

Introduction to the MC9S12 Hardware Subsystems

EE Embedded Systems Design. Lessons Exceptions - Resets and Interrupts

leos little embedded Operating System User's guide Written by Leonardo Miliani

Interrupt vectors for the 68HC912B32. The interrupt vectors for the MC9S12DP256 are located in memory from 0xFF80 to 0xFFFF.

Newbie s Guide to AVR Interrupts

Microprocessors & Interfacing

Digital and Analogue Project Report

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2313

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

Interrupts, timers and counters

Turbidostat Hardware and Software Design

The short program to blink the red LED used the delay() function. This is very handy function for introducing a short time delays into programs.

CprE 288 Introduction to Embedded Systems (Timers/Input Capture) Instructors: Dr. Phillip Jones

Chapter 6 PROGRAMMING THE TIMERS

L A M P I R A N UNIVERSITAS KRISTEN MARANTHA

Interrupts. How can we synchronize with a peripheral? Polling

Embedded Real-Time Systems

EE 308 Spring Lecture 28 March 30, 2012 Review for Exam 2. Introduction to the MC9S12 Expanded Mode

I/O Systems (3): Clocks and Timers. CSE 2431: Introduction to Operating Systems

EE 308 Spring A software delay

Using Input Capture on the 9S12

What happens when an HC12 gets in unmasked interrupt:

Chapter 2 The Basic Functions

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text

Transcription:

Distributed Real-Time Control Systems Chapter 10 Real-Time Digital Control 1

Real-Time Digital Control Hardware Digital Controllers are usually designed as periodic tasks with fixed period and synchronized A/D-D/A operations. Depending on the computational (hardware/software architectures), there maybe: Latencies (delays) that degrade the performance Variations in the sampling period, also called Jitter Clock A/D Control Algorithm D/A Process 2

Computational Delay Loop wait for next sampling time y = read A/D e = ref y p = K1*ref Kp*y i = i_ant + K2*(e + e_ant) d = K3*d_ant K4*(y - y_ant) u = p+i+d y_ant = y i_ant = i d_ant = d e_ant = e write u to D/A u y time 3

Minimize Computational delays Arrange code so that delay in feedback is minimized Loop wait for next sampling time y = read A/D e = ref y p = K1*ref Kp*y i = i_ant + K2*(e + e_ant) d = K3*d_ant K4*(y - y_ant) u = p+i+d y_ant = y i_ant = i d_ant = d e_ant = e write u to D/A Loop wait for next sampling time y = read A/D e = ref y p = K1*ref Kp*y i = i_ant + K2*(e + e_ant) d = K3*d_ant K4*(y - y_ant) u = p+i+d write u to D/A y_ant = y i_ant = i d_ant = d e_ant = e 4

Approximate Analysis of Delays A delay DT can be interpreted as an additional phase Df(w) = w*dt. Controller Delay System C(s) e -st G(s) This reduces the controller phase margin PM by Df(w c ), where w c is the crossover frequency. Since the damping factor x is approximately given by x = PM/100, a delay reduces damping and increases overshoot in the step response. 5

Step Example : No Delay PID Zero-Order Zero-Order Hold1 Discrete Hold PID Controller Scope4 Transport Delay1 1 s 2+s Transfer Fcn Scope Kp = 3 Kd = 3 Td = 0.05 Ts = 0.2 output control 6

Step Example : Delay ½ T PID Zero-Order Zero-Order Hold1 Discrete Hold PID Controller Scope4 Transport Delay1 1 s 2+s Transfer Fcn Scope Kp = 3 Kd = 3 Td = 0.05 Ts = 0.2 output control 7

Influence of Delay: Output 1.4 1.2 1 0.8 Increasing Delay 0.6 0.4 0.2 0 0 1 2 3 4 5 6 7 8 Time [sec] Step response. 8

Step Example : Delay ½ T + PID Retune PID Zero-Order Zero-Order Hold1 Discrete Hold PID Controller Scope4 Transport Delay1 1 s 2+s Transfer Fcn Scope Kp = 2 Kd = 2 Td = 0.05 Ts = 0.2 9 output control 9

Jitter Jitter = spurious variations of the pre-defined sampling time. 10

Causes of Jitter Non adequate use of temporal primitives. Variation in the length of the running code due to different branching The control algorithm may change as a function of its state and external events. Preemption of the controller by higher priority processes or threads. In multi-threaded applications, the scheduler may delay the control thread due to higher priority tasks. Preemption of the real-time clock (tick) due to higher priority interrupts. Execution of higher priority tasks and/or interrupts. Networking delays: In distributed systems, where the control computation, sensor sampling, and actuator drive are processed in different physical nodes. In this case, implementation delays also include communication delays that may change due to bus traffic or message loss. 11

Influence of Jitter 2.5 Effect of Jitter in Trajectory Generation 2 1.5 output 1 0.5 0-0.5 0 1 2 3 4 5 6 7 8 9 10 t 12

Minimize Jitter Use real-time clocks. Avoid branching during control computation Minimize pre-emptions by higher priority interrupts (e.g external interrupts) 13

Arduino Timing Primitives delay(ms) - Pauses the program for the amount of time (in miliseconds) specified. Uses TIMER 0. unsigned long millis() - Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days. Uses TIMER 0. micros() - Returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes. Has a resolution of about 4 microseconds. Uses TIMER 0. delaymicroseconds(us) - Pauses the program for the amount of time (in microseconds) specified. The largest value that will produce an accurate delay is 16383. Performs accurately down to 3 microseconds. Does not use timers. 14

Control loop with timer polling unsigned long prevtime = 0; // milliseconds unsigned long sampinterval = 10; // milliseconds void setup() { / *whatever needed */} void loop() { unsigned long currenttime = millis(); if(currenttime - previoustime > sampinterval) { //... // perform the control operations here //... // save the previous time for next cycle previoustime = currenttime; } } WHAT ARE THE DISADVANTAGES OF THIS APPROACH? 15

Control loop with delay unsigned long prevtime = 0; // milliseconds unsigned long sampinterval = 10; // milliseconds void setup() { / *whatever needed */} void loop() { unsigned long starttime = millis(); //... // perform the control operations here //... } unsigned long endtime = millis(); delay(sampinterval (endtime starttime)); WHAT ARE THE DISADVANTAGES OF THIS APPROACH? 16

Real-Time Timers The ATMega has Three Hardware Timers/Counters Timer 0 (8 bit) Used for delay(), millis() and PWM analogwrite() on pins 5 and 6. Timer 1 (16 bit) Used for PWM analogwrite() on pins 9 and 10, and for the Servo library. Timer 2 (8 bit) Used for tone() and analogwrite() on pins 3 and 11. 17

Timer Overview Timers are counters that count pulses from a timebase. Using the timebase of the Arduino (16MHz) the resolution is 62.5 ns, but this can be configured by a pre-scaler. Timers can generate interrupts when they overflow or reach certain values. Timers can generate PWM signals with configurable frequency and duty cycle. Timer s operation is controlled via a set of registers. 18

Timer Registers (TCCRnA) Timer Counter Control Register A - Determines the operating mode (TCCRnB) Timer Counter Control Register B - Determines the prescale value (TCNTn) Timer Counter Register - Contains the timer count (OCRnA) Output Compare Register A - Interrupt can be triggered on this count (OCRnB) Output Compare Register B - Interrupt can be triggered on this count (TIMSKn) Timer/Counter Interrupt Mask Register - Sets the conditions for triggering an interrupt (TIFRn) Timer/Counter 0 Interrupt Flag Register - Indicates if the trigger condition has occurred n timer number 19

Timer Resolution CSxm m th bit of the Clock Select bits in the TCCRnB register All timers are initialized for a pre-scale of 64 Setting CSx0:2 to 000 will stop counting 20

AVR Timer Hardware Diagram 21

Timer Control Registers Timer Counter Control Register A Timer Counter Control Register B 22

Changing Timer 1 frequency const byte mask= B11111000; // mask bits that are not prescale int prescale = 1; //fastest possible void setup() { TCCR1B = (TCCR1B & mask) prescale; } void loop() { analogwrite(9,128); //check waveform analogwrite(10,128); //with osciloscope } Warning: Changing frequency of Timer 0 will change the behaviour of delay() and millis()! 23

Modes of Operation 24

Normal Mode Counting direction always up. Counter goes from BOTTOM (0x00) to MAX (0xFF or 0xFFFF) Overflow flag (TOV) is set when the timer overflows (same clock cycle as timer goes from MAX to BOTTOM). Via Timer Overflow Interrupt, TOV flag can be cleared automatically. 25

Clear Timer on Compare (CTC) OCRnA defines the TOP of the timer. Counter is cleared to 0x00 when counter matches TOP. Wave generation: OCnA can toggle its logical level on each compare match (COMnA = 1). 26

Fast PWM mode Counts from 0x00 to 0xFF (mode 3) or OCRnA (mode 7). Output compare pin (OCnx) is cleared/set at counter match with OCRnx, via COMnx = 2/3. 27

Phase Correct PWM mode Shifts the PWM signal so that it is symmetric around the half period point. 28

Using Timer Interrupts TIMSKn Bits 0,1 and 2 enable Timer/Counter interrupts: BIT 0: Overflow BIT 1: Output Compare Match A BIT 2: Output Compare Match B. Interrupts will be generated when: BIT 0: a timer overflow occurs BIT 1: a Compare Match occurs between the Timer/Counter and the data in OCRnA Output Compare Register A. BIT 2: a Compare Match occurs between the Timer/Counter and the data in OCRnB Output Compare Register B. 29

Timer Interrupt Registers Timer/Counter Interrupt Mask Register Timer/Counter 0 Interrupt Flag Register 30

ATMega 328 Interrupts 31

Control loop with interrupts Setting interrupt on Timer 1 at 1Hz. #include <avr/io.h> #include <avr/interrupt.h> void setup() { cli(); //stop interrupts TCCR1A = 0;// clear register TCCR1B = 0;// clear register TCNT1 = 0;//reset counter OCR1A = 15624; //must be <65536 // = (16*10^6) / (1*1024) 1 TCCR1B = (1 << WGM12); //CTC On // Set prescaler for 1024 TCCR1B = (1<<CS12) (1<<CS10); // enable timer compare interrupt TIMSK1 = (1 << OCIE1A); sei(); //allow interrupts } volatile bool flag; ISR(TIMER1_COMPA_vect){ //... //put here control operations // flag = 1; //notify main loop } void loop() { if(flag) { //post-interrupt operations flag = 0; } //other operations } 32

Guidelines for ISR s Enable global interrupts ISR s should be as fast as possible: Do not use delay() Do not call functions. Use direct register access if possible. Use flags to communicate with the main loop. Shared variables should be declared volatile. Shared data that cannot be accessed atomically needs to be protected by disabling interrupts during the access. Only byte-sized data can be accessed atomically. Anything larger than a byte needs to be protected. 33