Servos Elementary Robotics with Servos

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 8 Pulse Width Modulation (PWM)
Advertisements

More fun with Timer/Counters
Analog to Digital Conversion
Analog/Digital Subsystem
Analog Comparator Positive input chooses bet. PB2 and Bandgap Reference. Negative input chooses bet. PB3 and the 8 inputs of the A/D. ACME= Analog Comparator.
1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.
L.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Robotics Research Laboratory Louisiana State University.
Servo Motors # A servo is a motor that you can position at any angle very accurately. # Servos have a limited servo range; most servos have a range of.
Applied Control Systems Robotics & Robotic Control
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Timer/counter Chapter 12
September, 2007ENGR Low-Level Robot Control Mechatronics: Motors, sensors & embedded controls.
A Few Words From Dilbert
Unit 10.2 Timer Examples. Example – Music Generation Channel 6 – Set up as a timer Output to Generate Square Waves Channel 4 – Set up as a timer Output.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
ELE2MIC Lecture 21 The AVR Sleep Modes ATMEGA128’s Analog to Digital Converter –Features –Block Diagram –Clock Source –Input Sources –Interrupts –BandGap.
Advanced uC Session Speaker : Chiraag Juvekar Jan 13, 2011 Speaker : Chiraag Juvekar Jan 13, 2011.
ELE22MIC Lecture 18 The AVR Sleep Modes The ATMEGA128’s Timer System
Counter/Timer/PWM. incoming Lab. Counter counter is a device which stores the number of times a particular event or process has occurred synchronous/asynchronous.
Microprocessoren lesson 6. PWM /motor control/servo.
STEPPER MOTORS Name: Mr.R.Anandaraj Designation: Associate. Professor Department: Electrical and Electronics Engineering Subject code :EC 6252 Year: II.
Servos The material presented is taken from a variety of sources including:
Suleyman Demirel University CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.
Embedded Systems Design: A Unified Hardware/Software Introduction 1 Chapter 4 Standard Single Purpose Processors: Peripherals.
Timers and Scheduled Interrupts
Analog to Digital Converter (ADC)
PWM: Pulse Width Modulation © 2014 Project Lead The Way, Inc.Digital Electronics.
GROUP MEMBERS: SUMEET HOODA. RAJDEEP PATRE. DHIRAJ GAONKAR. LIVE HUMAN DETECTION USING WIRELESS REMOTE CONTROLLED ROBOT.
Sensing Algorithm using IR Sensor and ADC Soong-sil University. Robotics 기 정 두 원.
1 Lab 4: D/A Converter Lab 4: D/A Converter This is a simple resistive network for a D/A converter Port 1, Port 0 are digital inputs ==> 00 (minimum),
16-Bit Timer/Counter 1 and 3
Motors & Motor Controllers
Application Case Study Christmas Lights Controller
Timers and Scheduled Interrupts
Introduction to Motors, servos and steppers
Embedded Systems Programming Interrupts and Handlers
Why are Timer Functions Important?
Embedded Systems Programming (What they do and when / how to use them)
Outline Introduction to Arduino UNO Programming environment setup GPIO
Embedded Systems Programming
Application Case Study Security Camera Controller
Timers and Event Counters
Environment Temperature Monitor
Analog Comparator An analog comparator is available on pins PE2(AIN0), PE3(AIN1) The comparator operates like any other comparator. -when (+) exceeds (-)
Microcontroller Applications
BITS EDU. CAMPUS , VARNAMA
AVR Addressing Modes Subject: Microcontoller & Interfacing
DC MOTOR SPEED CONTROL 1. Introduction
Pulse Width Modulation (PWM) Motor Feedback - Shaft Encoder
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
The Arduino Microcontroller: Atmel AVR Atmega 328
Timer/Counter Modified from Dr. Lam Phung’s Slides.
16-bit Timer/Counter1 Features
8-bit Timer/Counter0 with PWM
CBC Fundamentals Lecture is based on material from Robotic Explorations: A Hands-on Introduction to Engineering, Fred Martin, Prentice Hall, 2001.
8-bit Timer/Counter2 with PWM and Asynchronous Operation
ADC and DAC Programming in AVR
Digital Fundamentals Floyd Chapter 1 Tenth Edition
Wave Generation and Input Capturing
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
UNIT 11: RC-SERVOMOTOR CONTROL
Robotics System Lecture 11_12: DC Motor
Presentation transcript:

Servos Elementary Robotics with Servos Embedded Systems Programming Servos Elementary Robotics with Servos Pulse-Width-Modulation - Revisited 1

Recall - Pulse Width Modulation PWM means controlling the relative widths of the positive and negative parts of an output waveform, whilst keeping the pulse frequency constant. PWM thus controls the amount of energy in the waveform (used to regulate the speed of a motor, the brightness of a light etc.). The proportion of time the signal is on is called the ‘duty cycle’: A duty cycle of 100% means that maximum energy is provided, and a duty cycle of 0% means that minimum energy is provided to the controlled device. A square wave has a 50% duty cycle. Duty Cycle 25% 50% 75% 2 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Recall - Pulse Width Modulation on ATmega1281 using Timer/Counter 0 All three Timer/Counters on the ATmega1281 are capable of producing a PWM output pulse. Timer/Counter 0 is used for illustration. Direct output of pulse waveform onto the OC0A pin (bit 7 of Port B) ‘Output Compare’ Interrupt can be generated at the switchover point in the pulse ‘Overflow’ Interrupt can be generated at start of new pulse 3 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Servos - what is a servo A servo is an actuator that provides precision rotational movement To achieve this, a servo comprises: A motor A gearbox A positional feedback system A control circuit The gearbox provides an overall ratio of about 1,000:1 to 5000:1 And enables a very small, but fast motor running on about 4-6 Volts at a few milliamps, to generate very high torque, but at much slower rotational speed. Positional feedback is provided via a shaft encoder. Programming of position is achieved by adjusting the width of the control pulse sent to the servo. A control circuit converts the pulse-width into a ‘position’ value, and uses the positional feedback to move the motor to the correct position and hold it there. 4 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Applications of Servos Vs Motors Generally - Motors are used to provide motion Servos are used to provide positional control Servos provide precision positioning – such as in machinery, robotics. Miniature servos are used in: Electronic cameras etc – for focus control, zoom etc. Toys and models (cars, boats, aeroplanes, helicopters etc.) Servos are better than motors for accuracy But are: More expensive Slower because of heavy gearing More complex to control 5 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Shaft-encoder provides rotational position feedback Servo - Architecture Gearbox Output shaft Shaft-encoder provides rotational position feedback Optical type illustrated, but can also be magnetic, or based on a potentiometer Motor Input signal is a pulse, the pulse-width indicates the desired position of the servo Servo control circuit Control signal drives motor in either direction Rotational-position feedback 6 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Servo - Control The servo position is indicated by a repeated pulse: The width of the pulse is decoded by the servo control circuit, and converted into a ‘target’ position The servo motor is then powered on until the positional feedback indicates that the target position has been reached. The servo motor is continually powered to hold this position – until the pulse-width changes, indicating a new position. The pulse-width is typically in the range 500 microseconds to 2500 microseconds, and servo rotational angle is typically 180 degrees. Thus: Pulse of 500µS indicates fully anticlockwise, Pulse of 2500µS indicates fully clockwise, Pulse of 1500µS indicates centre position. The pulse is repeated at a typical rate of 18 milliseconds, thus the timing looks like: Pulse (P) Equivalent servo position 500µS 2500µS Interval (I) = 18 ms typical 7 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

SERVO - Typical characteristics 'Standard' SERVO - Such as - (WWW.GWS.COM.TW Part number S03N STD) 3-Wire system (no feedback) Red 5V, Black 0V, White\Yellow 3-5V control pulse Pulse width to control position 0.5ms – 2.5ms ("1.5ms neutral") Pulse rate, approximately every 18ms Torque 3.4Kg / cm Rotation 180 degrees +/- 10 degrees Current draw standby about 8mA, maximum about 550mA General purpose servo Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich 8

Servo - Control circuit Control circuit usually contains a microcontroller, or a custom logic chip 9 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Servo – Gears and positional feedback Shaft encoder Servo output (to servo horn) Gear train Motor output shaft 10 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

moves the servo to its central position Pulse Width Modulation - Servo position control (1) A 1500 microsecond pulse moves the servo to its central position 11 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Pulse Width Modulation - Servo position control (2) 750 microseconds pulse 1500 microseconds pulse 2250 microseconds pulse 12 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example1 p1 The simplest way to produce a PWM waveform is to use one of the programmable timers. The alternative being to set port bits high and low alternatively from direct software control In the C example below, the 16-bit timer/counter 3 is used, and the PWM output is produced directly on an output pin Port E bit 3 (alternate function OC3A (Output Compare match Output 3A)). PORT D Switches are used to control servo rotation. // Define values representing each input switch #define Switch_0_Pressed 0b00000001 #define Switch_1_Pressed 0b00000010 #define Switch_2_Pressed 0b00000100 #define Switch_3_Pressed 0b00001000 #define Switch_4_Pressed 0b00010000 #define Switch_5_Pressed 0b00100000 #define Switch_6_Pressed 0b01000000 #define Switch_7_Pressed 0b10000000 13 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example1 p2 int main( void ) { InitialiseGeneral(); InitialiseTimer3_FastPWM_Single(); unsigned char SwitchesValue; while(1) { SwitchesValue = ~PIND; // Read value on switches switch(SwitchesValue) { // Pulse width ranges from 750uS to 2250uS case Switch_0_Pressed: OCR3A = 750; // Minimum value 750uS _delay_ms(80); // Switch debounce delay break; ... case Switch_7_Pressed: OCR3A = 750 + (214 * 7); // 2248 (i.e. approx 2250) } 14 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example1 p3 void InitialiseGeneral() { DDRD = 0x00; // Configure PortD direction for Input (switches) DDRE = 0xFF; // Port E bit 3 must be set as OUTPUT to provide the // PWM pulse on OC3A // Port E bit 7 Input Capture 3 Pin (ICP3) must be set // as OUTPUT to prevent noise values entering ICR3 // (ICR3 is used as TOP value for PWM counter) PORTE = 0x00; // Set all bits initially sei(); // Enable interrupts, set Global Interrupt Enable (I) bit } 15 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example1 p4 void InitialiseTimer3_FastPWM_Single() { // TCCR3A – Timer/Counter 3 Control Register A // Bit 7:6 – COMnA1:0: Compare Output Mode for Channel A // Bit 5:4 – COMnB1:0: Compare Output Mode for Channel B // Bit 3:2 – COMnC1:0: Compare Output Mode for Channel C // Bit 1:0 – WGMn1:0: Waveform WGM(3..0) 1110 Fast PWM ICR3 is TOP TCCR3A = 0b10000010; // Fast PWM non inverting, ICR3 used as TOP // TCCR3B – Timer/Counter 3 Control Register B // Bit 7 – ICNCn: Input Capture Noise Canceler // Bit 6 – ICESn: Input Capture Edge Select // Bit 5 – Reserved Bit // Bit 4:3 – WGMn3:2: Waveform Generation Mode // Bit 2:0 – CSn2:0: Clock Select TCCR3B = 0b00011001; // Fast PWM, Use Prescaler '1' // TCCR3C – Timer/Counter 3 Control Register C // Bit 7 – FOCnA: Force Output Compare for Channel A // Bit 6 – FOCnB: Force Output Compare for Channel B // Bit 5 – FOCnC: Force Output Compare for Channel C TCCR3C = 0b00000000; 16 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example1 p5 // Set Timer/Counter3 Input Capture Register (16 bit) ICR3 // For the SERVO, the pulses should occur every 18ms, i.e. 18000uS // 1MHz clock, each pulse takes 1us, so need to count 18000 clock pulses // Decimal 18000 = 0x4650 (This value defines where a single cycle ends) // The actual pulse width is much shorter than the whole cycle. ICR3H = 0x46; // 16-bit access (write high byte first, read low byte first) ICR3L = 0x50; // Set Timer/Counter count/value registers (16 bit) TCNT1H and TCNT1L TCNT3H = 0; // 16-bit access (write high byte first, read low byte first) TCNT3L = 0; // Initialise Channel A servo to mid-range position // Set Output Compare Registers (16 bit) OCR3AH and OCR3AL // Pulse width ranges from 750uS to 2250uS, Neutral pulse width = 1500uS OCR3A = 1500; // TIMSK3 – Timer/Counter 3 Interrupt Mask Register TIMSK3 = 0b00000000; // No ints used, PWM pulses appear on OC3A (PE3) // TIFR3 – Timer/Counter3 Interrupt Flag Register TIFR3 = 0b00101111; // Clear all interrupt flags } 17 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example2 p1 In the C example below, the 16-bit timer/counter 3 is used, and the PWM output is produced directly on an output pin Port E bit 3 (alternate function OC3A (Output Compare match Output 3A)). A Potentiometer on PORT F bit 2 (Analogue input) determines the servo position. int main(void) { InitialiseGeneral(); InitialiseTimer3_FastPWM_Single(); Initialise_ADC(); while(1) } 18 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example2 p2 void InitialiseGeneral() { DDRB = 0xFF; // Configure PortB direction for Output (LEDs) PORTB = 0xFF; // LEDs initially Off DDRE = 0xFF; // Port E bit 3 must be set as OUTPUT to provide // the PWM pulse on OC3A // Port E bit 7 Input Capture 3 Pin (ICP3) must be // set as OUTPUT to prevent noise entering ICR3 // (ICR3 used as TOP value for PWM counter) PORTE = 0x00; // Clear all bits initially // PORT F bit 2 is set automatically for Analogue input when the // ADC module is configured sei(); // Set Global Interrupt Enable (I) bit } Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich 19

Programming – Generating PWM pulse on ATmega1281 – example2 p3 void InitialiseTimer3_FastPWM_Single() { // TCCR3A – Timer/Counter 3 Control Register A // Bit 7:6 – COMnA1:0: Compare Output Mode for Channel A // Bit 5:4 – COMnB1:0: Compare Output Mode for Channel B // Bit 3:2 – COMnC1:0: Compare Output Mode for Channel C // Bit 1:0 – WGMn1:0: Waveform WGM(3..0) 1110 Fast PWM ICR3 is TOP TCCR3A = 0b10000010; // Fast PWM non inverting, ICR3 used as TOP // TCCR3B – Timer/Counter 3 Control Register B // Bit 7 – ICNCn: Input Capture Noise Canceler // Bit 6 – ICESn: Input Capture Edge Select // Bit 5 – Reserved Bit // Bit 4:3 – WGMn3:2: Waveform Generation Mode // Bit 2:0 – CSn2:0: Clock Select TCCR3B = 0b00011001; // Fast PWM, Use Prescaler '1' // TCCR3C – Timer/Counter 3 Control Register C // Bit 7 – FOCnA: Force Output Compare for Channel A // Bit 6 – FOCnB: Force Output Compare for Channel B // Bit 5 – FOCnC: Force Output Compare for Channel C TCCR3C = 0b00000000; Timer 3 configuration is same as in example 1 20 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example2 p4 // Set Timer/Counter3 Input Capture Register (16 bit) ICR3 // For the SERVO, the pulses should occur every 18ms, i.e. 18000uS // 1MHz clock, each pulse takes 1us, so need to count 18000 clock pulses // Decimal 18000 = 0x4650 (This value defines where a single cycle ends) // The actual pulse width is much shorter than the whole cycle. ICR3H = 0x46; // 16-bit access (write high byte first, read low byte first) ICR3L = 0x50; // Set Timer/Counter count/value registers (16 bit) TCNT1H and TCNT1L TCNT3H = 0; // 16-bit access (write high byte first, read low byte first) TCNT3L = 0; // Initialise Channel A servo to mid-range position // Set Output Compare Registers (16 bit) OCR3AH and OCR3AL // Pulse width ranges from 750uS to 2250uS, Neutral pulse width = 1500uS OCR3A = 1500; // TIMSK3 – Timer/Counter 3 Interrupt Mask Register TIMSK3 = 0b00000000; // No ints used, PWM pulses appear on OC3A (PE3) // TIFR3 – Timer/Counter3 Interrupt Flag Register TIFR3 = 0b00101111; // Clear all interrupt flags } Timer 3 configuration is same as in example 1 21 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example2 p5 void Initialise_ADC() { // ADMUX – ADC Multiplexer Selection Register // bit7,6 Reference voltage selection (00 AREF,01 AVCC) // bit 5 ADC Left adjust the 10-bit result // Bits 4:0 – MUX4:0: Analog Channel (00010 = ADC2) ADMUX = 0b01100010; // AVCC REF, Left-adjust output, Channel 2 // ADCSRA – ADC Control and Status Register A // bit 7 ADC ENable), bit 6 Start Conversion bit 5 Auto Trigger Enable // bit 4 ADC Interrupt Flag, bit 3 ADC Interrupt Enable // bit 2,1,0 prescaler 000=2, 010=4, 011=8,100=16,101=32,110=64,111=128 ADCSRA = 0b10101101; // Enabled, Auto trig, Int enabled, Prescale = 32 // ADCSRB – ADC Control and Status Register B // Bit 3 MUX5: Analog Channel and Gain Selection Bit (0 for ADC0-7) // Bit 2:0 Auto Trigger Source (active when ADATE bit in ADCSRA is set) // 0 0 0 Free Running mode ADCSRB &= 0b11110000; // clear bits 3,2,1,0 (Free running mode) 22 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example2 p6 // void Initialise_ADC() – continued // DIDR0 – Digital Input Disable Register 0 // Bit 7:0 – ADC7D:ADC0D: ADC7:0 Digital Input Disable DIDR0 = 0b00000100; // Disable digital input on bit 2 // DIDR2 – Digital Input Disable Register 2 // Bit 7:0 – ADC15D:ADC8D: ADC15:8 Digital Input Disable DIDR2 = 0b11111111; // Disable digital input on all bits // Start the ADC Conversion (start first sample, uses 'free run' mode) //bit 6 ADCSRA (ADC Start Conversion) = 1 (START) // Read ADSCSR and OR with this value to set flag ADCSRA |= 0b01000000; // start ADC conversion } 23 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Programming – Generating PWM pulse on ATmega1281 – example2 p7 ISR(ADC_vect) // ADC Interrupt Handler { unsigned char ADCH_temp = ADCH; // Convert ADC value into Servo Pulse width for PWM channel A // Adjust the Most-significant 8-bits of the input value to a suitable // position pulse-width value // The ADC input (values 0 - 255) represent pulse-width value in the // 1500us-wide range 750us to 2250us // A simple approximation is used: // Multiply by 6, to get a value in range 0 - 1536, then add the 750 offset. OCR3A = (ADCH_temp * 6) + 750; PORTB = ~ADCH_temp; // Display the ADC value onto the LEDs } 24 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Application example – A simple, two-servo robot arm (1) 25 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Application example – A simple, two-servo robot arm (2) 26 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Application example – A simple, two-servo robot arm (3) View from above Servo #2 Servo #1 27 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Application example – A simple, two-servo robot arm (4) Connection to Atmel microcontroller (Port D) Servo power and control cables (yellow wire carries the control pulse) 28 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich

Application example – A simple, two-servo robot arm (5) 29 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich