1 / 20

Lab 5: Controls and feedback

Lab 5: Controls and feedback. Lab 5: Control and Feedback. You may need a resistor other than exactly 2K for better sensitivity. 22K. 2K. This embedded system uses the Photo sensor to detect the light intensity of the environment and adjusts the light emitted by the LED to maintain a

osmond
Download Presentation

Lab 5: Controls and feedback

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 5: Controls and feedback

  2. Lab 5: Control and Feedback You may need a resistor other than exactly 2K for better sensitivity 22K 2K This embedded system uses the Photo sensor to detect the light intensity of the environment and adjusts the light emitted by the LED to maintain a constant light intensity environment.

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

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

  5. 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).

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

  7. Lab 5: Controls and Feedback

  8. 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 Level SW0 (100%) Full LED intensity SW1 (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

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

  10. 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;

  11. 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);

  12. 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; • }

  13. 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; • ????/

  14. Lab 5: Controls and Feedback

  15. Lab 5: Controls and Feedback

  16. Lab 5: Controls and Feedback

  17. Lab 5: Controls and Feedback

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

  19. 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 Level SW0 (100%) Full LED intensity SW1 (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

More Related