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

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

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

Physics 120B: Lecture 11. Timers and Scheduled Interrupts

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

Laboratory 4 Usage of timers

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

Topic 11: Timer ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

Interrupts and timers

Unit 14 Timers and Counters 14.1

Unit 13 Timers and Counters

Marten van Dijk, Syed Kamran Haider

ADC: Analog to Digital Conversion

EE 308: Microcontrollers

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

AVR Timers TIMER0. Based on:

CSCE 236 Embedded Systems, Spring 2012 Quiz/Test 2

X X X. VGA - Standard:

Newbie s Guide to AVR Timers

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

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

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

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

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

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

onetesla Interrupter Firmware Guide General Overview

INTERRUPTS in microprocessor systems

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

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

Interrupts & Interrupt Service Routines (ISRs)

Introduction to Embedded Systems

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

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

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

Software debouncing of buttons

Embedded Systems Design with the Atmel AVR Microcontroller Part II

12.1. Unit 12. Exceptions & Interrupts

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

Introduction to Embedded Systems

UNIVERSITY OF MANITOBA Midterm

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

Microprocessors & Interfacing

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

More Arduino Programming

Arduino Part 2. Introductory Medical Device Prototyping

Programming Timer1 as a Stopwatch

COMP2121: Microprocessors and Interfacing

Embedded Systems and Software

In Class Assignment 2

Embedded Systems and Software

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

Processor and compiler dependent

CN310 Microprocessor Systems Design

AGH University of Science and Technology Cracow Department of Electronics

CSCE374 Robotics Fall 2013 Notes on the irobot Create

Microprocessors & Interfacing

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

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

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

The Atmel ATmega328P Microcontroller

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

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

Turbidostat Hardware and Software Design

CENG-336 Introduction to Embedded Systems Development. Timers

Chapter 6 PROGRAMMING THE TIMERS

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

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

The Atmel ATmega168A Microcontroller

FINAL EXAM. last name first name

Embedded Real-Time Systems

The MC9S12 Timer Input Capture Function

EE Embedded Systems Design. Lessons Exceptions - Resets and Interrupts

Embedded Systems. October 2, 2017

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

AGH University of Science and Technology Cracow Department of Electronics

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.

Chapter 2. Overview of Architecture and Microcontroller-Resources

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

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

MICROPROCESSOR TECHNOLOGY

Newbie s Guide to AVR Interrupts

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

Input and Output. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna

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

Timers and Interrupts. Mark Neil - Microprocessor Course

PC Interrupt Structure and 8259 DMA Controllers

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

L A M P I R A N UNIVERSITAS KRISTEN MARANTHA

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

Interrupts, timers and counters

SquareWear Programming Reference 1.0 Oct 10, 2012

Interrupts. How can we synchronize with a peripheral? Polling

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

Digital and Analogue Project Report

Signature: 1. (10 points) Basic Microcontroller Concepts

EE4390 Microprocessors

Carl Peto. 10th August 2017 SWIFT FOR ARDUINO

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

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

Transcription:

Distributed Real- Time Control Systems Lecture 7 Real- Time Control 1

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

Periodic Tasks Starting Times Latency Service Time Period 3

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 4

Approximate Analysis of Delays A delay ΔT can be interpreted as an additional phase Δφ(ω) = ω ΔT. Controller Delay System C(s) e - st G(s) This reduces the controller phase margin PM by Δφ(ω c ), where ω c is the crossover frequency. Since the damping factor ξ is approximately given by ξ = 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 OS 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 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 13

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

Arduino Timing Primitives delay(ms) - Pauses the program for the amount of time (in miliseconds) specified. Uses TIMER 0. Homework: what is the max delay? 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. 15

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? 16

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? 17

Real- Time Timers The ATMega has Three Hardware Timers/Counters Timer 0 (8 bit) Used for delay(), millis() and analogwrite() on pins 5 and 6. Timer 1 (16 bit) Used for 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. 18

AVR Timer Hardware Diagram 19

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. 20

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 21

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

Timer Resolution All timers are initialized for a pre- scale of 64 Setting CSx0:2 to 000 will stop counting 23

Modes of Operation 24

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()! 25

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

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. 27

ATMega 328 Interrupts 28

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 } 29

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. 30