AUTOMATIC CONTROL SYSTEMS Ali Karimpour Associate Professor Ferdowsi University of Mashhad Reference: Microcontroller Based Applied Digital Control Dogan Ibrahim, John Wiley & Sons Ltd, 2006
Liquid Level Digital Control System: A Case Study Topics to be covered include: Design of controller in time domain. 2
A typical liquid level control system 3
A typical liquid level control system Schematic of the system 4
System Model 5
Series Compensation Structure جبران سازي سري Ziegler-Nichols Design for PID Controller This procedure is only valid for open loop stable plants. Open-Loop Tuning Closed-Loop Tuning According to Ziegler and Nichols, the open-loop transfer function of a system can be approximated with time delay and single-order system, i.e. 6
Ziegler-Nichols Open-Loop Case طراحی زیگلر نیکولز حالت حلقه باز For open-loop tuning, we first find the plant parameters by applying a step input to the open-loop system. The plant parameters K, TD and T1 are then found from the result of the step test as shown in Figure. 7
Identification of System Model 5*200/256=3.906 v 2.5*3.906=9.76 v 200 8
Microcontroller program to send a step to D/A For program the Microcontroller we just need to: 1- Define PORTB as 200. 2- Enable DAC 3- Disable DAC That s all 9
Identification of System Model Gs is transfer function of tank+amplifier 10
Controller Design and Implementation 11 Controller Implementation s K K s e s u s D i p s K e s se K s su i p t K e dt t de K dt t du i p t K e T T t e t e K T T t u t u i p T t e K t e TK K T t u t u p i p 1 1 t e z K t e TK K t u z t u p i p 1 1 z TK K z e z u z D i p
Series Compensation Structure جبران سازي سري Continues block diagram Digital block diagram +z.o.h. 12
Sampling period Digital block diagram +z.o.h. First of all we need sampling period Time constant is 31 so we must consider T<3.1, we choose 0.1 13
System 14
Complete system 15
16
Microcontroller program to implement the controller Chip selection Project information Port configuration External interrupts LCD configuration ADC configuration Timers configuration Main program Controller output calculation Code WizardAVR Code WizardAVR Code WizardAVR Code WizardAVR Code WizardAVR Code WizardAVR Code WizardAVR By yourself By yourself 17
Chip selection Chip selection Tools Code WizardAVR Code WizardAVR 18
Project information /***************************************************** This program was produced by the CodeWizardAVR V2.05.0 Evaluation Automatic Program Generator Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Level Control Version : 1 Date : 2012/12/27 Author : Karimpour Company : Ferdowsi University of Mashhad Comments: This is a simple example for class. Chip type : ATmega16 Program type : Application AVR Core Clock frequency: 4.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *****************************************************/ 19 #include <mega16.h>
Port configuration 20
Port configuration void mainvoid // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTB=0x00; DDRB=0xFF; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0 PORTC=0x00; DDRC=0x01; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; 21
External interrupt configuration // void External mainvoid Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isrvoid.. // Place External your Interrupts code here initialization // INT0: On // INT0 Mode: Low level // External Interrupt 1 service routine // INT1: On interrupt [EXT_INT1] void ext_int1_isrvoid // INT1 Mode: Low level // INT2: Off // Place your code here GICR =0xC0; MCUCR=0x00; MCUCSR=0x00; GIFR=0xC0; // Global enable interrupts #asm"sei 22
LCD configuration // Alphanumeric LCD Module functions #include <alcd.h> 23 void mainvoid.. // Alphanumeric LCD initialization // Connections specified in the // Project Configure C Compiler Libraries Alphanumeric LCD menu: // RS - PORTC Bit 1 // RD - PORTC Bit 2 // EN - PORTC Bit 3 // D4 - PORTC Bit 4 // D5 - PORTC Bit 5 // D6 - PORTC Bit 6 // D7 - PORTC Bit 7 // Characters/line: 16 lcd_init16; while 1 // Place your code here
ADC configuration #include <delay.h> void mainvoid #define ADC_VREF_TYPE 0x40... // Read ADC the initialization AD conversion result unsigned // ADC Clock char frequency: read_adcunsigned 125.000 char khz adc_input // ADC Voltage Reference: AVCC pin ADMUX=adc_input // ADC Auto Trigger Source: ADC_VREF_TYPE Free Running & 0xff; // ADMUX=ADC_VREF_TYPE Delay needed for the stabilization & 0xff; of the ADC input voltage delay_us10; ADCSRA=0xA5; // SFIOR&=0x1F; Start the AD conversion ADCSRA =0x40; while 1 // Wait for the AD conversion to complete while // Place ADCSRA your code & 0x10==0; here ADCSRA =0x10; return ADCH; 24
Timers configuration // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isrvoid // Place your code here void mainvoid // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 15.625 khz // Mode: Normal top=0xff // OC0 output: Disconnected TCCR0=0x04; TCNT0=0x00; OCR0=0x00; // Timers/Counters Interrupts initialization TIMSK=0x01; // Global enable interrupts #asm"sei" 25
Total Program from CodeWizardAVR #include <mega16.h> #include <delay.h> #include <alcd.h> interrupt [EXT_INT0] void ext_int0_isrvoid // Place your code here interrupt [EXT_INT1] void ext_int1_isrvoid // Place your code here // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isrvoid // Place your code here #define ADC_VREF_TYPE 0x40 unsigned int read_adcunsigned char adc_input ADMUX=adc_input ADC_VREF_TYPE & 0xff; delay_us10; ADCSRA =0x40; while ADCSRA & 0x10==0; ADCSRA =0x10; return ADCW; 26
Total Program from CodeWizardAVR void mainvoid // Declare your local variables here PORTA=0x00; DDRA=0x00; PORTB=0x00; DDRB=0xFF; PORTC=0x00; DDRC=0x01; PORTD=0x00; DDRD=0x00; TCCR0=0x04; TCNT0=0x00; OCR0=0x00; TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; GICR =0xC0; MCUCR=0x00; MCUCSR=0x00; GIFR=0xC0; TIMSK=0x01; UCSRB=0x00; ACSR=0x80; SFIOR=0x00; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0xA5; SFIOR&=0x1F; SPCR=0x00; TWCR=0x00; lcd_init16; // Global enable interrupts #asm"sei" while 1 // Place your code here 27
// Reinitialize Timer 0 value TCNT0=0x64; time_set++; if time_set ==10 time_set = 0; yk // Controller = read_adc0; update must be here yk = yk *5000 / 1024; // convert ADC ek=ref-yk; uk = pk_1+kp+ki*ti* ek; uk = uk * 256/5000 ; if uk >255 uk_im = 255; else uk_im = unsigned char uk ; PORTB = uk_im ; PORTC.0 = 1 ; // write to D/A converter PORTC.0 = 0 ; pk_1 = uk-kp*ek ; u t u t T K e t T K TK e t p p Setting Timer i 28 // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isrvoid // Place your code here void Ts=0.1 mainvoid // Timer Timer/Counter counts 256 0 cycle. initialization // Clock source: System Clock // Clock Clock of value: timer 15.625 is 15625 khz Hz. // Mode: Normal top=0xff // Every OC0 156 output: counts Disconnected = 0.01 Sec. TCCR0=0x04; So 10 times of counter = 0.1 Sec. TCNT0=0x64; TCNT0=0x00; OCR0=0x00; So 256-100=156. // Start Timers/Counters of timer is 100 or Interrupts 0x64 in hex initialization TIMSK=0x01; // Global enable interrupts #asm"sei"
External interrupt program // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isrvoid // ref++; Place your code here itoaref,ch; lcd_clear; lcd_putsch; // External Interrupt 1 service routine interrupt [EXT_INT1] void ext_int1_isrvoid // ref--; Place your code here itoaref,ch; lcd_clear; lcd_putsch; 29
Parameter Definition and Initial Values #include <stdlib.h> unsigned int time_set = 0; int ref= 2280; float kp, ki, pk_1,ek,yk, uk, T; unsigned char uk_im ; char ch[20];. kp =279.0; T= 0.1 ; ki=42.3; pk_1 = 0; PORTC.0 = 0 ; // disable write D/A itoaref,ch; lcd_putsch;while 1 // Place your code here 30
Total Program of System /***************************************************** This program was produced by the CodeWizardAVR V2.05.0 Evaluation Automatic Program Generator Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Level Control Version : 1 Date : 2012/12/30 Author : Freeware, for evaluation and non-commercial use only Company : Ferdowsi University of Mashhad Comments: This is a simple example for lecture#16. Chip type : ATmega16 Program type : Application AVR Core Clock frequency: 4.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *****************************************************/ 31
Total Program of System #include <mega16.h> #include <delay.h> #include <alcd.h> #include <stdlib.h> unsigned int time_set = 0; int ref= 2280; float kp, ki, pk_1,ek,yk, uk, T; unsigned char uk_im ; char ch[20]; // Alphanumeric LCD Module functions #define ADC_VREF_TYPE 0x40 // Read the AD conversion result unsigned int read_adcunsigned char adc_input ADMUX=adc_input ADC_VREF_TYPE & 0xff; // Delay needed for the stabilization of the ADC input voltage delay_us10; // Start the AD conversion ADCSRA =0x40; // Wait for the AD conversion to complete while ADCSRA & 0x10==0; ADCSRA =0x10; return ADCW; 32
Total Program of System // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isrvoid // Place your code here ref--; itoaref,ch; lcd_clear; lcd_putsch; // External Interrupt 1 service routine interrupt [EXT_INT1] void ext_int1_isrvoid // Place your code here ref++; itoaref,ch; lcd_clear; lcd_putsch; // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isrvoid // Reinitialize Timer 0 value TCNT0=0x64; time_set++; if time_set ==10 time_set = 0; yk = read_adc0; yk = yk *5000 / 1024; // convert ADC ek=ref-yk; uk = pk_1+kp+ki*t* ek; uk = uk * 256/5000 ; if uk >255 uk_im = 255; else uk_im = unsigned char uk ; PORTB = uk_im ; PORTC.0 = 1 ; PORTC.0 = 0 ; pk_1 = uk-kp*ek ; // write to D/A converter 33
Total Program of System void mainvoid // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTB=0x00; DDRB=0xFF; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0 PORTC=0x00; DDRC=0x01; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; 34
Total Program of System // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 15.625 khz // Mode: Normal top=0xff // OC0 output: Disconnected TCCR0=0x04; TCNT0=0x00; OCR0=0x00; // External Interrupts initialization // INT0: On // INT0 Mode: Low level // INT1: On // INT1 Mode: Low level // INT2: Off GICR =0xC0; MCUCR=0x00; MCUCSR=0x00; GIFR=0xC0; // Timers/Counters Interrupts initialization TIMSK=0x01; // ADC initialization // ADC Clock frequency: 125.000 khz // ADC Voltage Reference: AVCC pin // ADC Auto Trigger Source: Free Running ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0xA5; SFIOR&=0x1F; // Alphanumeric LCD initialization // Connections specified in the // Project Configure C Compiler Libraries Alphanumeric LCD menu: // RS - PORTC Bit 1 // RD - PORTC Bit 2 // EN - PORTC Bit 3 // D4 - PORTC Bit 4 // D5 - PORTC Bit 5 // D6 - PORTC Bit 6 // D7 - PORTC Bit 7 // Characters/line: 16 lcd_init16; 35
Total Program of System // Global enable interrupts #asm"sei" kp =279.0; T= 0.1 ; ki=42.3; pk_1 = 0; PORTC.0 = 0 ; // disable write D/A itoaref,ch; lcd_putsch; while 1 // Place your code here 36