Post on 16-Jan-2016
1
Lab 5: Controls and feedback
2
Lab 5: Control and Feedback
This embedded system uses the Photo sensor to detect the light intensityof the environment and adjusts the light emitted by the LED to maintain aconstant light intensity environment.
You may need a resistor other than exactly 2K for better sensitivity
22K2K
3
ATMEGA16L
VTG
10K150
SW0
VTG
10K150
SW1
PB0
PB1
PC0
RXD
TXD
To Computer Serial Port
14
15
1
2
22LED0
VCC
VTG
10K
150
VTG
10K150
SW2
PC123
PB23
VTG
10K150
SW3
PB34
VTG
10K150
SW4
PB45
LED1
VCC
VTG
10K
150
LED2
VCC
VTG
10K
LED3
VCC
VTG
10K
LED4
VCC
VTG
10K
150 150 150
PC224
PC325
PC426
OC2(PD7)
21
130Sensor
200K
LED
ADC0(PA0)
40
VTG
V_Feedback
R2
R1
4
Lab 5: Controls and Feedback
• PID control– P: Proportional
• The error signal (error = desired – current) multiplied by a constant and fed out to the drive. (proportional = gain * error).
– I: Integral• The integral term is the sum of past errors, so adding the
past errors will eventually drive the output closer to the desired output
– D: Derivative• Differentiator uses the derivative (rate of change) to predict
the future behavior.
5
Lab 5: Controls and Feedback
In this lab, the plant’s function is to generate a desired level of ambient light. The sensor measures ambient light with a Cadmium Sulfide (CdS) photocell. The sensor measures the plant’s performance.
controllerPWM
r(t) u(t) y(t)plant h(t) LED
1
+-e(t)
6
Lab 5: Controls and Feedback
• The choice of a controller depends on the application’s requirements.
• This lab builds an application that uses the duty cycle of the PWM signal to dynamically adjust ambient light levels.
• The controller increases the duty cycle of the PWM in order to apply more power to the LED and thereby increase its intensity. The PWM is the control, u(t), that is fed into the plant (that is, LED).
7
Lab 5: Controls and Feedback
• Changes to the LED’s intensity can occur as fast as the mcu computes and updates the PWM duty cycle register (OCR2). For this application one updates every 100 milliseconds is more than sufficient. One milli-second is very slow compared to how fast ambient light fills a room.
• At speeds of human perception 100 milliseconds is fast but detectable. These timing considerations drive the requirement for only needing to use a proportional controller. Hint: A maximum change of 2% to 4% to the duty cycle every 50 - 100 ms implements a nice smooth transition of the LED’s intensity
8
Lab 5: Controls and Feedback
ATMEGA16L
VTG
10K150
SW0
VTG
10K150
SW1
PB0
PB1
PC0
RXD
TXD
To Computer Serial Port
14
15
1
2
22LED0
VCC
VTG
10K
150
VTG
10K150
SW2
PC123
PB23
VTG
10K150
SW3
PB34
VTG
10K150
SW4
PB45
LED1
VCC
VTG
10K
150
LED2
VCC
VTG
10K
LED3
VCC
VTG
10K
LED4
VCC
VTG
10K
150 150 150
PC224
PC325
PC426
OC2(PD7)
21
130Sensor
200K
LED
ADC0(PA0)
40
VTG
V_Feedback
R2
R1
9
Lab 5: Controls and Feedback
• SW0-SW4 are used to set the target level for the control loop.
• LED0-LED4 are used to indicate to the user which switch has been pressed.
• The percentage of the LED intensity is sent to the computer terminal through the serial port.
• Switch Target LevelSW0 (100%) Full LED intensitySW1 (75%) no_LED +((full_LED - no_LED)*3/4)SW2 (50%) no_LED +((full_LED - no_LED)/2)SW3 (25%) no_LED +((full_LED - no_LED)*1/4)SW4 (0%) No LED intensity
10
Lab 5: Controls and Feedback
• Timer2(PWM)• Timer2 is used to setup the Pulse Width
Modulation (PWM) for the LED. A clock rate of 460KHz and a non-inverted PWM can be selected.
• The pulse width is determined by the 8-bit value in the OCR2 register. 0xFF is maximum pulse width and the initialization value of 0x80 has a pulse width of 1/2 cycle.
• You can choose any other values to suit your design.
11
Lab 5: Controls and Feedback• Initialization code for Timer2:• // Timer/Counter 2 initialization• // Clock source: System Clock• // Clock value: 460.000 kHz• // Mode: Phase correct PWM top=FFh• // OC2 output: Non-Inverted PWM• TCCR2=0x62;• ASSR=0x00;• TCNT2=0x00;• OCR2=0x80;• The output for Timer2 PWM mode is PD7. Bit 7 of Port D is set up to be an output.• Initialization code for Port D:• // Port D initialization• // Set up PD7 as the output driver to the external LED• PORTD=0x00;• DDRD=0x80;
12
Lab 5: Controls and Feedback• Code to find the light with no LED and full LED intensity:• // Find ambient light with no LED output.• OCR2 = 0x00;• delay_ms( 2000 );• no_LED = read_adc( 0x0 );•• // Find ambient light with full LED output.• OCR2 = 0xFF;• delay_ms( 2000 );• full_LED = read_adc( 0x0 );• // Initialize variables for 50%.• PORTC = 0x1B; //11011 turns on LED2• target_reading = no_LED +((full_LED - no_LED)/2);•
13
Lab 5: Controls and Feedback• #include <mega16.h>• // Standard Input/Output functions• #include <stdio.h> • #include <math.h>• bit update = 0; // Update the duty cycle.• // Timer 1 overflow interrupt service routine• interrupt [TIM1_OVF] void timer1_ovf_isr(void)• {• // Reinitialize Timer 1 value• // Update every 100ms.• TCNT1H=0xE9;• TCNT1L=0x8A; •• update = 1;• }
14
Lab 5: Controls and Feedback• if( update )• {• // Read channel 0 adc.• adc_input = read_adc( 0x0 );• • // Print the adc value.• // printf( "Target Reading = %X\r", target_reading );• // printf( "ADC Input = %X\r", adc_input ); • • // Find the difference between the adc input and• // the sensor reading.• error = adc_input - target_reading;• • ????/
15
Lab 5: Controls and Feedback
+5 V
Sensor
Ground
R1
Vout
Delta V as a Function of R1
0
0.2
0.4
0.6
0.8
1
1.2
0 20000 40000 60000 80000 100000 120000 140000 160000 180000 200000
R1 Value (Ohms)
Del
ta V
(V
)
16
Lab 5: Controls and Feedback
Theoretical Max Delta V as a Function of R1
0
0.2
0.4
0.6
0.8
1
1.2
0 500 1000 1500 2000 2500 3000 3500 4000 4500
R1 Resistance (Ohms)
Del
ta V
(V
)
17
Lab 5: Controls and Feedback
Response Time - Step Change from 30% LED Intensity to 80% LED Intensity
720
740
760
780
800
820
840
860
0 5 10 15 20 25
Time (s)
LE
D In
ten
sit
y (
AD
C In
teg
er
Un
its
)
iCom
iMeas
18
Lab 5: Controls and Feedback
R11kOhm
R21kOhm
50%
VCC
5V
Detector_Output
R3150 Ohm
LED1
PWM_from_AVR
R41kOhm
R51kOhm
R61kOhm
R71kOhm
VCC
5V
3_VDC_to_Aref
19
Lab 5: Controls and Feedback
• If no switches are pushed, do the PID code.– Measure the LED intensity (iMeas)– Compare to commanded intensity (iCom)– Error = iCom – iMeas– Limit error to +/- 10– New value of duty = old value of duty + error * 1.2.– Limit duty to range of 0 to 255– Write duty to PWM register– Output iCom, iMeas and duty on UART– Wait 50 ms to give ~ 10 Hz control time
• If the switch is pushed, then change duty to new value as determined by switch number .. . . . .. etc
20
Software minimum requirements:
(1) SW0-SW4 are used to set the target level for the control loop.according to the following table
(2) LED0-LED4 are used to indicate to the user which switch has been pressed.
(3) The percentage of the LED intensity is sent to the computer terminal through the serial port.
Switch Target LevelSW0 (100%) Full LED intensitySW1 (75%) no_LED +((full_LED - no_LED)*3/4)SW2 (50%) no_LED +((full_LED - no_LED)/2)SW3 (25%) no_LED +((full_LED - no_LED)*1/4)SW4 (0%) No LED intensity