ME 333 Assignment 7 and 8 PI Control of LED/Phototransistor Pair. Overview

Similar documents
ME 333: Introduction to Mechatronics

Exercise 5: PWM and Control Theory

Touchless Control: Hand Motion Triggered Light Timer

ME 461 Laboratory #5 Characterization and Control of PMDC Motors

Lab 5: Control and Feedback. Lab 5: Controls and feedback. Lab 5: Controls and Feedback

Lab 5 Timer Module PWM ReadMeFirst

ME 461 Laboratory #3 Analog-to-Digital Conversion

EE283 Electrical Measurement Laboratory Laboratory Exercise #7: Digital Counter

MSK4310 Demonstration

LM4: The timer unit of the MC9S12DP256B/C

University of North Carolina-Charlotte Department of Electrical and Computer Engineering ECGR 3157 Electrical Engineering Design II Fall 2013

Pulse Width Modulation

Hello, and welcome to this presentation of the FlexTimer or FTM module for Kinetis K series MCUs. In this session, you ll learn about the FTM, its

CprE 288 Introduction to Embedded Systems (Output Compare and PWM) Instructors: Dr. Phillip Jones

Microcontroller: Timers, ADC

νµθωερτψυιοπασδφγηϕκλζξχϖβνµθωερτ ψυιοπασδφγηϕκλζξχϖβνµθωερτψυιοπα σδφγηϕκλζξχϖβνµθωερτψυιοπασδφγηϕκ χϖβνµθωερτψυιοπασδφγηϕκλζξχϖβνµθ

9 Feedback and Control

ET 304A Laboratory Tutorial-Circuitmaker For Transient and Frequency Analysis

MICROCONTROLLER TUTORIAL II TIMERS

Generating DTMF Tones Using Z8 Encore! MCU

Physics 335 Lab 7 - Microcontroller PWM Waveform Generation

Tektronix Courseware. Academic Labs. Sample Labs from Popular Electrical and Electronics Engineering Curriculum

Last Time. P and N type semiconductors Diode internals Transistors NPN PNP

Analog to Digital Conversion

TECHNICAL DOCUMENT EPC SERVO AMPLIFIER MODULE Part Number L xx EPC. 100 Series (1xx) User Manual

Part (A) Using the Potentiometer and the ADC* Part (B) LEDs and Stepper Motors with Interrupts* Part (D) Breadboard PIC Running a Stepper Motor

Using the Z8 Encore! XP Timer

P a g e 1. Introduction

Understanding the Arduino to LabVIEW Interface

EE152 Final Project Report

Electronic Instrumentation ENGR-4300 Fall 2004 Section Experiment 7 Introduction to the 555 Timer, LEDs and Photodiodes

Energy Monitoring System (EMS) Abstract

LABORATORY 4. Palomar College ENGR210 Spring 2017 ASSIGNED: 3/21/17

Hello and welcome to this Renesas Interactive Course that provides an overview of the timers found on RL78 MCUs.

Chapter 6 PROGRAMMING THE TIMERS

Timer 0 Modes of Operation. Normal Mode Clear Timer on Compare Match (CTC) Fast PWM Mode Phase Corrected PWM Mode

EE 314 Spring 2003 Microprocessor Systems

GE423 Laboratory Assignment 6 Robot Sensors and Wall-Following

VORAGO Timer (TIM) subsystem application note

Control System for Lamp Luminosity. Ian Johnson, Tyler McCracken, Scott Freund EE 554 November 29, 2010

Real Analog - Circuits 1 Chapter 11: Lab Projects

For this exercise, you will need a partner, an Arduino kit (in the plastic tub), and a laptop with the Arduino programming environment.

EE 308 Spring S12 SUBSYSTEMS: PULSE WIDTH MODULATION, A/D CONVERTER, AND SYNCHRONOUS SERIAN INTERFACE

CSE 3215 Embedded Systems Laboratory Lab 5 Digital Control System

PIC Functionality. General I/O Dedicated Interrupt Change State Interrupt Input Capture Output Compare PWM ADC RS232

University of North Carolina, Charlotte Department of Electrical and Computer Engineering ECGR 3157 EE Design II Fall 2009

A MORON'S GUIDE TO TIMER/COUNTERS v2.2. by

PIC Analog Voltage to PWM Duty Cycle

Course Introduction. Content 20 pages 3 questions. Learning Time 30 minutes

Electronics I. laboratory measurement guide

Roland Kammerer. 13. October 2010

Lecture 2 Exercise 1a. Lecture 2 Exercise 1b

Control System Circuits with Opamps

Mechatronics Laboratory Assignment 3 Introduction to I/O with the F28335 Motor Control Processor

EE-110 Introduction to Engineering & Laboratory Experience Saeid Rahimi, Ph.D. Lab Timer: Blinking LED Lights and Pulse Generator

3-in-1 Air Condition Solution

MAE106 Laboratory Exercises Lab # 1 - Laboratory tools

University of California at Berkeley Donald A. Glaser Physics 111A Instrumentation Laboratory

Fluxgate Magnetometer

ATmega16A Microcontroller

The University of Texas at Arlington Lecture 10 ADC and DAC

Sensors and Sensing Motors, Encoders and Motor Control

Chapter 1: DC circuit basics

When you have completed this exercise, you will be able to relate the gain and bandwidth of an op amp

1. Introduction to Analog I/O

ME 461 Laboratory #2 Timers and Pulse-Width Modulation

B.E. SEMESTER III (ELECTRICAL) SUBJECT CODE: X30902 Subject Name: Analog & Digital Electronics

EE 308 Lab Spring 2009

Lab 7: DELTA AND SIGMA-DELTA A/D CONVERTERS

Electronics Design Laboratory Lecture #4. ECEN 2270 Electronics Design Laboratory

Chapter 1: DC circuit basics

UNIVERSITY OF VICTORIA FACULTY OF ENGINEERING. SENG 466 Software for Embedded and Mechatronic Systems. Project 1 Report. May 25, 2006.

Power Factor Correction in Digital World. Abstract. 1 Introduction. 3 Advantages of Digital PFC over traditional Analog PFC.

RL78 Motor Control. YRMCKITRL78G14 Starter Kit. Renesas Electronics Europe. David Parsons Application Engineering Industrial Business Group.

ELCT 912: Advanced Embedded Systems

uc Crash Course Whats is covered in this lecture Joshua Childs Joshua Hartman A. A. Arroyo 9/7/10

Community College of Allegheny County Unit 4 Page #1. Timers and PWM Motor Control

Electronics. RC Filter, DC Supply, and 555

EE431 Lab 1 Operational Amplifiers

Chlorophyll a/b-chlorophyll a sensor for the Biophysical Oceanographic Sensor Array

LABORATORY 5 v3 OPERATIONAL AMPLIFIER

ESE 350 Microcontroller Laboratory Lab 5: Sensor-Actuator Lab

Speed Control of DC Motor Using Phase-Locked Loop

IT.MLD900 SENSORS AND TRANSDUCERS TRAINER. Signal Conditioning

Measuring Distance Using Sound

Analog-to-Digital Converter. Student's name & ID (1): Partner's name & ID (2): Your Section number & TA's name

Grundlagen Microcontroller Counter/Timer. Günther Gridling Bettina Weiss

Lab #10: Analog to Digital Converter (ADC) Week of 15 April 2019

To design/build monostable multivibrators using 555 IC and verify their operation using measurements by observing waveforms.

Op-Amp Simulation Part II

Intruder Alarm Name Mohamed Alsubaie MMU ID Supervisor Pr. Nicholas Bowring Subject Electronic Engineering Unit code 64ET3516

Electronic Instrumentation

Electronic Instrumentation ENGR-4300 Fall Project 4: Optical Communications Link

Brushed DC Motor Microcontroller PWM Speed Control with Optical Encoder and H-Bridge

DC->DC Power Converters

ELEG 205 Analog Circuits Laboratory Manual Fall 2016

Module 9C: The Voltage Comparator (Application: PWM Control via a Reference Voltage)

DC and AC Circuits. Objective. Theory. 1. Direct Current (DC) R-C Circuit

Lab 5: Inverted Pendulum PID Control

Integrators, differentiators, and simple filters

Transcription:

ME 333 Assignment 7 and 8 PI Control of LED/Phototransistor Pair Overview For this assignment, you will be controlling the light emitted from and received by an LED/phototransistor pair. There are many steps in the process, so we will be breaking it up into 4 main parts: 1. Implement a PWM output circuit that will drive the LED at a given frequency. 2. Implement a timer that will cycle through an array and update a current reference value of desired light level. 3. Set up a phototransistor circuit to read analog voltage levels and then convert these levels to digital values. 4. Implement a proportional integral controller that will take feedback from the phototransistor circuit, calculate an error from the current reference value, and adjust the LED PWM accordingly. Step 1 In class 2/11 Wire the LED in series with digital output D0 and a 330 ohm resistor. The long leg of the LED will go into D0, the short leg will go to the 330 ohm resistor, and the 330 ohm resistor will go to ground. Setup Timer2 on the NU32 to interrupt every 0.1 seconds. To set the timer to interrupt you must: Set the correct timer prescaler (TCKPS bits in T2CON) Set the input to PBCLK (TCS bit in T2CON) Turn the timer on (ON bit in T2CON) Set the period register so the timer rolls over every 0.1 seconds (PR2) Reset the Timer2 count (TMR2) to 0 Set the Timer2 interrupt priority and subpriority bits Clear the Timer2 interrupt flag Enable Timer2 interrupt Within the interrupt, invert the current value of D0 (This will toggle the output LED). This should create a very simple 5 Hz PWM signal that has a 50% duty cycle. That means that it spends half of its time high, the other half low, and that it repeats itself once per cycle. Hook up your nscope on channel A to verify your timing is correct. Exercises:

1. Using this timer based PWM, generate a 50% duty cycle PWM signal running at 100Hz, 1kHz, and 10kHz. Verify these times on the nscope and take a snapshot of each. 2. What are the values of the timer prescaler, N, and period register, PR2, you used in question 1? 3. What is the slowest 50% duty cycle PWM signal you can generate using this method, and what prescaler would you use to create it?

Overview The goal of this project is to control the light emitted by an LED. We provide a signal that specifies the brightness of the LED over time, and program the PIC to make the LED brightness follow the signal. The steps to make this work are outlined here: they are extremely similar to what must be done to control motors. The project consists of 4 parts. Parts 1 & 2 are due on Tuesday 2/18 before class. You will demo the response to part 2 question 7 in class. Include answers to all the questions (including nscope snapshots) and the source code for the answer to part 2 question 7. Parts 3 & 4 will be posted soon and will be due on Tuesday 2/25 before class Here is a diagram, including the circuits and control loop. The reference signal is stored as an array on the PIC. The project consists of several components: 1. The LED circuit. We control the brightness of the LED by turning it on and off really fast using a PWM signal. The switching is too fast for the human eye to see, so it appears as different brightness levels, depending on the duty cycle of the signal. Part 1 of the assignment had you control the light level of this LED In Part 2, you will use the Output Compare feature to control the light level, rather than manually changing it in a timer. 2. The reference generator generates the signal (aka the reference) that we want the LED to track. The signal is stored in an array on the PIC, and is played back by a timer interrupt. In Part 2, you will create this reference signal and use a timer to change the PWM duty cycle. This will cause the LED to approximately follow your signal. 3. The sensing circuit. This is so we can measure how much light is emitted, and adjust the brightness of the LED accordingly. The voltage will be measured on the PIC using the analog to digital converter (ADC), which converts voltage into a digital integer value.

The sensing circuit includes a low pass RC filter. In Part 2, you will see how this RC filter converts the on/off signal from the PWM into a voltage that corresponds to the brightness of the led In Part 3, you will hook the output of this signal up to an analog input pin on the PIC and read the values of this voltage. 4. The control loop. In the timer where you change the reference signal, you will also read the sensor value. Using the reference and sensed value, you will implement a control law that uses the error between what you want the LED brightness to be and what your sensor reads to change the PWM signal, thus making the LED follow the reference signal. In part 1, the timer you created can be thought of as a primitive control loop. Each time the ISR triggered, you turned the LED either on or off, this controlled the LED In part 2, you will use the control loop to change the PWM signal to the LED, according to a reference signal. This is known as open loop control because you are sending a control signal to the LED, but are not using any sensor data. In part 3, you will read sensor data in addition to outputting the reference signal. You will also be able to output the reference and sensed signal on the computer and compare them. You will see that the sensed value does not match the reference signal. In part 4, you will implement the controller. Your data will show that the controller makes the sensor reading match the reference signal. Mission Accomplished! Overview of System, including the control loop Step 2 In class 2/13 1. The sample code we have provided generates a 1kHz PWM signal on OC1 (which is output on pin D0, the pin your LED should be connected to). The OC1RS tick count is entered by the user over serial. View the output of this PWM signal on the nscope (channel A). Play around with different numbers and observe how the wave changes. Approximately how many ticks yield a 25% duty cycle? A 75% duty cycle? 2. Wire up the phototransistor circuit, EXCLUDING THE CAPACITOR. Use a 10kOhm resistor to ground. Place the photo transistor so it is pointed directly at the LED. View the output of the phototransistor on the nscope (Channel B). What do you observe?

Approximate orientation of the LEDs 3. Add a 1uF capacitor in parallel with the 10k resistor. How does the signal change? Try a few different duty cycles. 4. We have provided an array named waveform. We will use this array to set the values of the OC1RS register. This array is designed to be played back at 10 Hz. It is initialized by the makewaveform function, called from main(). Setup Timer 2 to operate at 10 Hz. In the Timer 2 ISR, cycle through the waveform array and set the OC1RS register according to the value at the current position in the waveform. Observe the output at the LED and at the phototransistor. Are they the same? Exercises: 4. Generate a 20KHz PWM signal on pin OC1. Use two different combinations of prescaler and PR3 register. Which combination gives you more precise control over the duty cycle? 5. What are the advantages of using the built in PWM function rather than manually performing PWM in a timer, as in Part 1? 6. Setup Timer 2 to run at 1 khz. Write a function that fills the duty cycle array with the appropriate values to create a 1 Hz square wave, sampled at 1 khz. Include the ability to specify the minimum and maximum value for the square wave. 7. Generate a square wave with a minimum of roughly 1V and a maximum of roughly 2V. Observe on the nscope. Do the minimum and maximum match with what you specified? What about the frequency?

Overview In parts 1 and 2 of this project we looked at open loop control of the phototransistor sensor voltage (the output for this system). In parts 3 and 4 we will be closing the loop with a feedback control PI algorithm. Don t worry, it s a lot easier than it sounds! Parts 3 & 4 will be due on Tuesday 2/25 before class. Answer all of the exercise questions, and include plots or code when asked. You will be asked to demo Exercise 13 (the complete project tracking a 1V to 2V square wave) in class on 2/25. Part 3 You will have one main objective. That is to read in the sensor voltage from the phototransistor and convert it to a digital value from 0 1023. You will do this using the PIC s built in analog to digital converter (ADC). Don t worry about the specifics for now, we have given you sample code functions setupadc() and readadc() to both setup and read from the ADC peripheral. setupadc() can be called in main when you setup your timers and output compare. readadc() can be called within your 1kHz control loop. It will return an integer value 0 1023 which corresponds to 0 3.3V analog voltages. It is predefined to use AN0 which is also pin B0. Both of these functions are posted up on the wiki. In class 2/18 Connect the ADC (B0) to the output voltage of the phototransistor. Read the ADC in the 1kHz control loop and output it over serial along with the reference waveform. Your output should be formatted as two numbers with a space separating them followed by \r\n. Do not include additional text. Plot in matlab using uart_plot.m. Call it by running uart_plot(2,1000, comport ); (the 2 means that you are plotting two numbers, 1000 means that it is plotting 1000 data points on the x axis in between updates). Comport is the name of the com port. Press q to stop plotting. If you are having trouble plotting with matlab, you can also view the data coming in from the serial port by opening up your terminal emulator program. You may want to slow down the rate at which the PIC sends data to the terminal in this case. (Maybe to 10 Hz or so).

Exercises: 8. Do the ADC readings match what you specified in the waveform array? Why or why not? 9. Manually convert the voltage displayed on the scope into ADC ticks (show your calculations). Do the ADC readings and the values you calculated match (your calculation is an estimate so it may not be exact)? 10. In part 4 you will have to implement a function called setduty(u) which takes an integer control effort (u) and outputs a value for controlling the duty cycle of the PWM via the OC1RS register. In this function, the control effort input can be a positive or negative value, and will typically range from around FULL_DUTY/2 to +FULL_DUTY/2 (it may try to go over or under these values if your Kp and Ki are high). The OC1RS register, however, can only ever have a value of 0 to FULL_DUTY! For this exercise the, write a function int setduty(int u) that takes in a computed control effort, adds an offset of FULL_DUTY/2 to make sure the output won t have a negative value, and limits the variable it returns to be less than FULL_DUTY and greater than zero. Turn in the function you wrote. Part 4 This is where everything comes together. In this part, you will design the feedback controller. This feedback controller will live in the 1kHz control loop you have previously created. The basic algorithm will work like this: 1. Read in the current ADC value from the phototransistor sensor. 2. Calculate the error (e) as the difference between the current reference value (r) and value of the ADC you just read. 3. Calculate the proportional error term using Kp constant. 4. Calculate the integral error (eint) as the sum of the current error (e) and all of the previous error. Multiply the integral error by the integral error constant Ki to get the integral error term. 5. Add the proportional and integral error terms together to form the control effort variable (u). 6. Set the duty cycle of the PWM proportional to the control effort (u). Pseudocode of a PI controller might look like this: adcvalue = readadc(); e = referencevalue adcvalue; eint = e + eint; u = Kp*e + Ki*eint; OC1RS = setduty(u);

In class 2/20 Download the.hex file provided for class (on the wiki) and load it on the NU32. This.hex file contains the completed project, and you can use it to play around with Kp and Ki gains to see what the end goal of this project should look like. Use the matlab function pid_plot.m (on the wiki) to interact with the NU32. Try different Kp and Ki values and see how the controller performs. The pid_plot function takes 3 arguments, the COM port you are using, the Kp value for the controller, and the Ki value for the controller. Using the reference waveform you generated in exercise 7, the function you wrote in exercise 10, the sample code given in part 3, and the pseudocode given above, implement a proportional controller with a Kp of 1. That is, set the constant Ki to 0 to begin with. Output the sensor reading, the reference signal, and the control effort over serial, plot in matlab. You can use the c function NU32_WriteUART1Async (provided on the wiki) to send data to matlab from the NU32. This will replace NU32_WriteUART1 for this case only (when writing to matlab). For further information about writing to matlab from the NU32, see comm.txt (also posted on the wiki). Note about conversions: Remember that the reference signal and the signal coming from the ADC should have the same ranges (same units) before you use them in your control algorithm. That is, you should keep them in terms of ADC counts, which range from 0 1023. This range corresponds to 0 3.3V if you are talking about voltage. Exercises: 11. What happens to the sensor reading if Kp is much too large? What if it is much too small? 12. Now add the integral control term. It is best to start with a relatively small value and work your way up. Adjust the control constants Kp and Ki so that the ADC sensor reading tracks the reference waveform from exercise 7 (make sure to modify the waveform so it is in ADC counts, not duty cycle). 13. Include a screenshot of the matlab plot. How close are you to tracking the desired signal (a 1V to 2V square wave @ 1Hz)? Also include the final values you have chosen for Kp and Ki, and attach your final code. 14. For extra credit, try your controller with a sine wave reference. You can generate a sin wave using the included makesinwave() function. This function is designed to create a 2 Hz sin wave and put it in the array waveform. Waveform should have a length of 1000,

and should be played back at 1 khz. An example output tracking a sin wave reference