Exercise 5: PWM and Control Theory

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

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

GE423 Laboratory Assignment 6 Robot Sensors and Wall-Following

Chapter 7: The motors of the robot

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

Sensors and Sensing Motors, Encoders and Motor Control

EE 308 Lab Spring 2009

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

CSE 3215 Embedded Systems Laboratory Lab 5 Digital Control System

Project Final Report: Directional Remote Control

FABO ACADEMY X ELECTRONIC DESIGN

Using Magnetic Sensors for Absolute Position Detection and Feedback. Kevin Claycomb University of Evansville

EE 314 Spring 2003 Microprocessor Systems

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

Hello, and welcome to this presentation of the STM32L4 comparators. It covers the main features of the ultra-lowpower comparators and some

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

Micromouse Meeting #3 Lecture #2. Power Motors Encoders

Workshops Elisava Introduction to programming and electronics (Scratch & Arduino)

The light sensor, rotation sensor, and motors may all be monitored using the view function on the RCX.

An External Command Reading White line Follower Robot

Understanding the Arduino to LabVIEW Interface

CHAPTER 4 CONTROL ALGORITHM FOR PROPOSED H-BRIDGE MULTILEVEL INVERTER

EE-110 Introduction to Engineering & Laboratory Experience Saeid Rahimi, Ph.D. Labs Introduction to Arduino

Microcontrollers and Interfacing

Sensors and Sensing Motors, Encoders and Motor Control

Mechatronics Engineering and Automation Faculty of Engineering, Ain Shams University MCT-151, Spring 2015 Lab-4: Electric Actuators

PSoC Academy: How to Create a PSoC BLE Android App Lesson 9: BLE Robot Schematic 1

AVR PWM 11 Aug In the table below you have symbols used in the text. The meaning of symbols is the same in the entire guide.

Single-Phase Grid-Tied Inverter (PWM Rectifier/Inverter)

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

Pulse-Width-Modulation Motor Speed Control with a PIC (modified from lab text by Alciatore)

Experiment#6: Speaker Control

ECE 511: FINAL PROJECT REPORT GROUP 7 MSP430 TANK

Lab 1.2 Joystick Interface

Measuring Distance Using Sound

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

Lab 2: Blinkie Lab. Objectives. Materials. Theory

Design of Joint Controller Circuit for PA10 Robot Arm

Mate Serial Communications Guide This guide is only relevant to Mate Code Revs. of 4.00 and greater

RX23T inverter ref. kit

ME 461 Laboratory #2 Timers and Pulse-Width Modulation

LAB 3 TIMER FUNCTIONS: BOLT DROP AND SQUARE WAVE

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

LINE MAZE SOLVING ROBOT

ECEN 449: Microprocessor System Design Department of Electrical and Computer Engineering Texas A&M University

Microcontroller Based Closed Loop Speed and Position Control of DC Motor

Lecture 2 Exercise 1a. Lecture 2 Exercise 1b

High Current DC Motor Driver Manual

Motor Control Demonstration Lab

Module 5. DC to AC Converters. Version 2 EE IIT, Kharagpur 1

Lab 5: Inverted Pendulum PID Control

Standard single-purpose processors: Peripherals

Serial communication inverter. Lab bench scenario. Inverter Board, A/D, D/A, PWM, Filters, Encoders. Inverter board. and Dimmer introduction

Objectives: Learn what an Arduino is and what it can do Learn what an LED is and how to use it Be able to wire and program an LED to blink

ENGR 1110: Introduction to Engineering Lab 7 Pulse Width Modulation (PWM)

Microcontrollers: Lecture 3 Interrupts, Timers. Michele Magno

2.017 DESIGN OF ELECTROMECHANICAL ROBOTIC SYSTEMS Fall 2009 Lab 4: Motor Control. October 5, 2009 Dr. Harrison H. Chin

LM4: The timer unit of the MC9S12DP256B/C

Lab 2.2 Custom slave programmable interface

MICROCONTROLLER TUTORIAL II TIMERS

UNIVERSAL PNEUMATIC TRANSDUCER FEATURES

Published by: PIONEER RESEARCH & DEVELOPMENT GROUP ( 1

Temperature Monitoring and Fan Control with Platform Manager 2

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

ECE 445 Senior Design Laboratory. Fall Individual Progress Report. Automatic Pill Dispenser

Generator Speed Controller Model GSC 1

SCHOOL OF TECHNOLOGY AND PUBLIC MANAGEMENT ENGINEERING TECHNOLOGY DEPARTMENT

Generating DTMF Tones Using Z8 Encore! MCU

Arduino Control of Tetrix Prizm Robotics. Motors and Servos Introduction to Robotics and Engineering Marist School

ATmega16A Microcontroller

Ultimate Actuator Drivebox 30A Quick start guide

UNIVERSAL INPUT TO PULSE CONVERTER MODULE

Select the single most appropriate response for each question.

COSC 3215 Embedded Systems Laboratory

Using Optical Isolation Amplifiers in Power Inverters for Voltage, Current and Temperature Sensing

Rochester Institute of Technology Real Time and Embedded Systems: Project 2a

J. La Favre Using Arduino with Raspberry Pi February 7, 2018

Computer Controlled Curve Tracer

Module: Arduino as Signal Generator

ECE251: Tuesday October 3 0

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

Digital Debug With Oscilloscopes Lab Experiment

Feedback Devices. By John Mazurkiewicz. Baldor Electric

ME 461 Laboratory #5 Characterization and Control of PMDC Motors

Jaguar Motor Controller (Stellaris Brushed DC Motor Control Module with CAN)

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

HAW-Arduino. Sensors and Arduino F. Schubert HAW - Arduino 1

Iowa State University Electrical and Computer Engineering. E E 452. Electric Machines and Power Electronic Drives

Laboratory Exercise 1 Microcontroller Board with Driver Board

Total Hours Registration through Website or for further details please visit (Refer Upcoming Events Section)

ISSN Vol.05,Issue.01, January-2017, Pages:

Lab Exercise 9: Stepper and Servo Motors

Design of double loop-locked system for brush-less DC motor based on DSP

EE 308 Spring 2006 FINAL PROJECT: INTERFACING AND MOTOR CONTROL WEEK 1 PORT EXPANSION FOR THE MC9S12

OVEN INDUSTRIES, INC. Model 5C7-362

Electric Bike BLDC Hub Motor Control Using the Z8FMC1600 MCU

Enhancing Analog Signal Generation by Digital Channel Using Pulse-Width Modulation

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

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

DC SERVO MOTOR CONTROL SYSTEM

Transcription:

Exercise 5: PWM and Control Theory Overview In the previous sessions, we have seen how to use the input capture functionality of a microcontroller to capture external events. This functionality can also be used to measure the frequency of external signals (the measurement of the button press time was a very simple example for this scenario). In this exercise, we will learn how to generate control signals for driving external devices and how to combine both features. Pulse Width Modulation (PWM) Pulse Width Modulation (PWM) is an efficient way to control analog circuits using digital signals. It is used in a wide variety of applications. As its name suggests, the control information is modulated in the width of a pulse. Figure 1 depicts three example PWM signals. Signal a) is a PWM signal with 25 % duty cycle, that is, the signal is high for one quarter of the time in one period and is low for the rest of the time. Signals b) and c) have a duty cycle of 50 % and 75 %, respectively. a) high low b) mean c) period Figure 1: PWM Signals with 25 %, 50 % and 75 % Duty Cycle and respective Mean Values. The mean value of a PWM signal (shown as dashed horizontal lines in figure 1) is an analog voltage value between 0 V and the high voltage of the digital signal, typically 5 V. This means that if the period of the PWM signal is reasonable short and the device attached to it behaves inertially, it will actually respond as if controlled by an analog signal with that analog voltage! Exercise 5.1 We will now develop an application to control the brightness of LEDs. Atmega168 provides several mechanisms to generate PWM signals. For simplicity, we will stick to Fast PWM Mode. a) What are the mean voltages corresponding to the PWM signals shown in figure 1, given a high voltage of 5 V? b) Read the manual to understand how the Fast PWM Mode works. Find out the following information from the relevant sections in chapter 15 of the manual: 1/5

Which pins can be used for PWM output when using Timer1? Which configuration do we need on those pins (e.g., do we need to configure them as output pins)? If we use the WGM 14, how does the PWM generator work for Compare Channel A? How can the period and the duty cycle be controlled? What is the difference between inverted and non-inverted PWM? How can we configure the microcontroller to generate such signals? c) Develop a pwm_init() function that takes the period in milliseconds as input parameter and initializes the timer to generate a PWM signal with 50 % duty cycle on OC1A using WGM 14. It is acceptable if the range for the input parameter is between 0 ms and 1,000 ms. Use the LEDs to visualize the PWM output. d) Develop a pwm_set() function that can be used to change the duty cycle of the PWM signal. It should receive the new duty cycle value (between 0 and 100) as input parameter. The PWM period should not be changed by the function. e) As you have seen in the previous steps, the blinking of the LEDs is visible for large period values. Reduce the period of the PWM signal at a duty cycle of 50 % to find the value where the blinking is no longer visible. Which frequency does the period value correspond to? f) Develop an application that receives commands on the serial port and adjusts the duty cycle of PWM signal accordingly. For example, the duty cycle of PWM signal is increased upon receiving a + character and decreased upon receiving a - character. Use a period of about 10 ms. Make sure that you can control the brightness of LEDs by configuring the duty cycle. Driving a DC Motor We will now combine the functionality of input capture and PWM signal generation in an illustrating example. Imagine you have a model car that is driven by a battery-powered DC motor. We want the car to drive at a constant speed, even if there is load on the motor because the car is currently driving uphill or the battery power is low. Of course we can not always guarantee that the motor will reach the designated speed, but in this exercise we will at least try to do so. We will use a PWM signal to drive the motor. The goal is to measure how fast the motor drives and compare this with the desired speed. If the motor is too slow, we will increase the PWM duty cycle, otherwise we will decrease it. This nebavior actually describes a simple controller that tries to minimize the difference ( error ) between the desired speed and the actual speed. Unfortunately this exercise does not feature a real model car, but you are of course free to apply your results to a setup of your own ;) For completing the tasks, you receive an extra printed circuit board with a DC motor and a light barrier. A disc is attached to the motor axis. Some parts of the disc are transparent, some are not. We will use the disc in combination with the light barrier to measure the current motor speed. Note that you should never directly attach a component with a high power consumption (like a motor) to a microcontroller, because the high current that occurs while running it would destroy the controller. To circumvent this issue, we use special electronic components on the printed circuit board that allow us to directly attach it to a PWM signal generated by the microcontroller. Exercise 5.2 a) Attach the DC motor board to STK500 so that you can feed a PWM signal to the motor. Do not forget to also connect the power supply pins on the motor board (+5V, GND), they are needed for the electronic components on the board to work properly. Which pin connections do you need? 2/5

b) Use the program from exercise 5.1 f) to test the motor at different speeds. Find out the approximate minimum duty cycle at which the motor runs and the maximum duty cycle from where increasing the value does not have any visible effect (listen to the sound of the motor). When finding the minimum duty cycle, is there a difference between the motor already turning at a higher speed or turning it on from stand? c) What happens if you hold the motor so that the axis is arranged vertically? Determining the Speed of a Motor There are multiple methods that can be used to measure the speed of a motor. One method is to attach a magnet to the motor axis and use a sensor that detects the magnetic field to measure the number of rounds per minute. The approach we are using is an optical way of measuring the motor speed. There is a small light barrier mounted on the motor board. In addition to that, a disk is mounted on the motor axis. Half of the disk is opaque and the other half is transparent. When the opaque area of the disk is in between the light barrier, the light is blocked. Otherwise, the light can be detected by the receiving part of the light barrier. This optical information is transformed to a voltage by the electrical circuit on the motor board, which can be finally evaluated by the microcontroller. We will use input capture interrupts for this purpose. Note that you can not see the light that the light barrier produces, as it is in the infrared spectrum. Exercise 5.3 a) Which resolution (in degrees) can we achieve from the light barrier when using the following encoder disks and if... i)... only the rising or falling edges are considered? ii)... rising and falling edges are considered? b) Although not of interest in the current setup, can you imagine a way to measure the direction in which the motor is turning? c) Attach the signal called Schmitt-Trigger OUT to the input capture pin of the microcontroller. Make sure that jumper JP2 is set to the position 1-2 (not 2-3). Extend your program from exercise 5.1 f) so that measures the time between two successive input capture events (it is up to you whether you want to recognize the falling and/or rising edge(s)). From that on, calculate the time per round (depending on the number of edged per round) and the rounds per minute. Make sure that the rounds per minute is calculated correctly, you will need it for the subsequent exercises. Do all the calculations in the ISR and store the most recent result in a global variable. In the main program, periodically dump the current values to the debug console. Note that you should disable interrupts in the main program before accessing the global variables to ensure that the ISR does not write to the values in parallel. Since we are using input capture, this does not reduce the precision as long as interrupts are only disabled for a short amount of time. 3/5

Hints To use PWM signal to drive the DC motor, use the rather small period value (e.g. 100 µs), otherwise the light barrier signal may become noisy and distort your input capture events. You might need to use small prescaler as well to achieve better resolution. If you happen to encounter problems with floating point computations for small numbers, you might want to try using fixed-point numbers instead of floating-point numbers: for intermediate calculations, you can scale the values up by a proper scaling factor, use integer arithmetic and then scale them down again afterwards. Listing 1 shows how the simplified original code to compute the time interval between two capture events may look like using floating-point numbers, where t 1 and t 2 are the timestamps of two successive captured events, T is the MAX value of the timer and p is the associated real-time interval for T. To avoid using floating-point numbers, we could use fixed-point arithmetic as shown in listing 2. Listing 1: Floating-point based calculation of motor speed 1 f l o a t calcroundsperminute ( f l o a t t1, t2, p ) 2 { 3 / Compute the f r a c t i o n in one timer p e r i o d. Note that the i n t e r v a l / 4 / v a r i a b l e must be o f type f l o a t, o t h e r w i s e the r e s u l t i s always 0! / 5 f l o a t i n t e r v a l = ( t2 t1 )/T; 6 7 / Compute the r e a l time i n us / 8 i n t e r v a l = p ; 9 10 / Return rounds per minute / 11 return i n t e r v a l /1000... ; 12 } Listing 2: Fix-point based calculation of motor speed 1 f l o a t calcroundsperminute ( f l o a t t1, t2, p ) 2 { 3 / S c a l e numbers up. The value 1000 i s j u s t f o r demonstration, you / 4 / should pick up a value that i s l a r g e enough f o r your a p p l i c a t i o n. / 5 uint32 t s1 = t1 1000; 6 uint32 t s2 = t2 1000; 7 8 / Compute the f r a c t i o n in one timer p e r i o d. Now the a b s o l u t e / 9 / value o f the r e s u l t can be s a f e l y s t o r e d in an i n t e g e r. / 10 uint32 t i n t e r v a l = ( s2 s1 )/T; 11 12 / Compute the r e a l time i n us / 13 i n t e r v a l = i n t e r v a l p ; 14 15 / S c a l e down an r e t u r n rounds per minute / 16 return i n t e r v a l /1000... ; 17 } 4/5

Control Theory One of the requirements for our (so far virtual) model car was that the speed of the motor should be constant. Until now, we have seen that the motor speed may change even if we do not change the duty cycle of the PWM signal. Hence, we need to dynamically adapt the PWM frequency given the current speed of the motor. This is a typical problem from the control systems area. We will now implement a simple yet effective controller to achieve the desired task. One of the essential features of a controller is a feedback loop, which means that the current state of the system is fed back to controller after the controller has set a new control value. The controller we will use is a PI controller. PI means proportional and integral controller. It basically works as follows: the controller has thee input values. The first one is the so called system deviation d, that is the difference of the measured value (so-called actual value) v m to the desired value (so-called setpoint value) v s. The second and third inputs are the proportial coefficient c p and the integral coefficient c i. The output s is the new setpoint for the system. A pseudo-code algorithm for a PI controller is shown in listing 3. Listing 3: PI controller pseudocode 1 input(c p); input(c i) // R e t r i e v e c o n t r o l parameters 2 t old time() 3 v 0 4 5 forever { 6 input(v m); input(v s) // R e t r i e v e a c t u a l value and s e t p o i n t value 7 8 t cur time() // C a l c u l a t e how much time has passed 9 t t cur t old 10 t old t cur 11 12 d v m v s // PI c o n t r o l l e r 13 v v + c i d 14 s c p d + v t 15 16 output(s) // Output c o n t r o l value 17 } Motor Speed Control Exercise 5.4 a) Extend your previous program(s) so that it controls the speed of the DC motor using a PI controller. Output the current rounds per minute to the debug cosole (about every second). It should still be possible to input the new desired value (in rounds per minute) over the serial interface. Start with control parameters c p = 0.1 and c i = 0. Describe the behavior of the motor. b) Now set c p = c i = 0.1. What happens? c) Optimize the control parameters so that the motor reacts as fast as possible when you change the desired value. d) In contrast to your previous experience from exercise 5.2 c), what happens if you hold the motor so that the axis is arranged vertically? 5/5