Electronics Design Laboratory Lecture #8ecee.colorado.edu/ecen2270/lectures/Lecture08.pdf• Either...
Transcript of Electronics Design Laboratory Lecture #8ecee.colorado.edu/ecen2270/lectures/Lecture08.pdf• Either...
Notes• Starting Experiment 4 this week – “Robot Speed and Position Control”• This lab requires you to have BOTH wheels running by the Lab 4 demo
date• Tuesday this week
– Turn in prelab 4.A and complete 4.A
– Redo Labs 2B and 3 on the second wheel
• Thursday this week: – Have speed control working on both wheels
– Start 4.B: connecting Arduino microcontroller board to your speed control circuitry and coding the microcontroller
• Lab 4 demo is Thursday the week after (see Lab calendar)
Electronics Design Laboratory 2ECEN 2270
Electronics Design Laboratory 3ECEN 2270
Speed Sensor and Filter
GND
5VDC
0-5V Speed Output proportional to actual motor speed
Vspeed
ForwardController
Reverse Controller
0-10V
0-10V
0-5V Speed Output proportional to desired motor speed
+-Speed errorForward
NOT Forward
Experiment 1 & 2
Experiment 3a
Experiment 3b
Experiment 4
Board Layout
Electronics Design Laboratory 4ECEN 2270
GND GND GND GND 10V10V5V5V
GND 10V
10VGND
5V Linear Regulator
Speed Control (2 per wheel)Speed Sensor
(1 per wheel)
Encoder 1Encoder 2
PWM 1 Vref 1PWM 2 Vref 2
CWCC
10VGND
10VGND
B1 - LeftB2 - Left
B1 - RightB2 - Right
DC1DC2
DC1DC2
10VGND
5VGND5V
Encoder 1 PowerEncoder 2 Power
10V from power supply
5V Generated by Arduino
Electronics Design Laboratory 5ECEN 2270
Arduino Power System
VIN
5V3.3V
PWRIN
USBVCC
• Three power inputs: USB (provides 5VDC), VIN pin (7-12VDC) and a barrel jack (9-12VDC)
Outputs, generated by Arduino onboard power system
7-12V, External Supply. Bench-top, batteries, etc.
9-12V, Center Positive Barrel Jack
USB 5V. Up to 500mA
5V D-
D+GND
Electronics Design Laboratory 6ECEN 2270
Arduino Power System
PWRIN
GND
5V
Linear Regulator
47uF 47uF 100nF
GND
3.3V
10kΩ
10kΩ
Linear Regulator
1uF
3.3V
LP2985-33DBVR5V
GND100nF
5V
• Need to select which power input to use, and generate 5VDC and 3.3VDC
Decoupling Capacitors
3.3V Linear Regulator
5V Linear Regulator
MC33269D-5.0
VIN
PWRINUSBVCC
VIN
VINUSBVCC
Max(VIN,PWRIN)
High if VIN or PWRIN is supplied
Either USBVCC or 5V
uC power
Electronics Design Laboratory 7ECEN 2270
+ -
Motor Drivers
Encoders
5VDC
10VDC
Arduino Power System• Either batteries or a DC Power Supply
will generate 10VDC, which we will connect to the VIN pin.
• Make sure to use decoupling capacitors on the 10VDC line.
• Not using decoupling caps will waste a bunch of your time since motors are noisy and may cause trouble for your microcontroller if the power supply is not clean.
Arduino UNO Systems
Electronics Design Laboratory 8ECEN 2270
USB-B port
ATmega328P 8-bit uC
ATmega8u2 USB To Serial
Clock Generator
Status LEDsDigital IO(GPIO)
Reset Button
Analog IO
16MHz Clock
Power System
• The Arduino is built around an 8-bit microcontroller• Communication with the ATmega328P is achieved through a USB interface• Since the ATmega328P does not ‘talk’ USB, a serial to USB conversion must be made
Arduino UNO Systems
Electronics Design Laboratory 9ECEN 2270
ATmega328P 8-bit uC
ATmega8u2 USB To Serial
Power System
• The Arduino is built around an 8-bit microcontroller• Communication with the ATmega328P is achieved through a USB interface• Since the ATmega328P does not ‘talk’ USB, a serial to USB conversion must be made
Digital Inputs/Outputs: GPIO
Electronics Design Laboratory 10ECEN 2270
ATm
ega3
28P
8-bi
t uCGPIO: General Purpose Inputs/Outputs. These are digital
outputs, so only voltages of 0V or 5V may be generated. Pins marked with (~) may be used to output PWM signals. Maximum current of 40mA per output. Pins default to Inputs
100MΩ
5V
GPOIX5V
{0,1}
5V
RA
RB
{0,1}
{0V,5V}0V-5V
20kΩ
PULLUP
INPUT/OUTPUT
5V
Internal ATmega328P Hardware
digitalRead(GPIOX)
digitalWrite(GPIOX,{HIGH,LOW})
pinMode(GPIOX,mode)
Program Code
Digital Inputs/Outputs: GPIO
Electronics Design Laboratory 11ECEN 2270
ATm
ega3
28P
8-bi
t uCGPIO: General Purpose Inputs/Outputs. These are digital
outputs, so only voltages of 0V or 5V may be generated. Pins marked with (~) may be used to output PWM signals. Maximum current of 40mA per output. Pins default to Inputs
As Digital Outputs…
5V0V
digitalRead(GPIOX) returns 1digitalRead(GPIOX) returns 0
digitalRead(GPIOX) returns 1VGPIOX
t
5V0V
VGPIOX
tdigitalWrite(GPIOX,HIGH)
digitalWrite(GPIOX,LOW)digitalWrite(GPIOX,HIGH)
As Digital Inputs…
As PWM Outputs… analogWrite(GPIOX,duty_cycle)
=
PWM
on
Tt255Integerduty_cycle
t
5V0V
VGPIOXTPWM
(duty_cycle/255)*TPWM
Analog Inputs
Electronics Design Laboratory 12ECEN 2270
ATm
ega3
28P
8-bi
t uC Analog Inputs: 6 Analog inputs are available. These pins
convert an analog voltage in the range 0V-AREF to an integer in the range 0-1023. The value read by the ATmega328 can be calculated as:
Analog reference AREF defaults to 5V. It takes approximately 100 µs to preform an analog read. Analog inputs A0-A5 can be configured to act exactly like the GPIO pins if needed.
( )
= 1023IntegerVALX
AREFVA
AX 0V-5V
EXTERNAL 5V
ADCIN
REFAREFOUT 0-1023
Internal ATmega328P Hardware Program Code
analogRead(AX)
analogReference(mode)
Analog Inputs
Electronics Design Laboratory 13ECEN 2270
ATm
ega3
28P
8-bi
t uC Analog Inputs: 6 Analog inputs are available. These pins
convert an analog voltage in the range 0V-AREF to an integer in the range 0-1023. The value read by the ATmega328 can be calculated as:
Analog reference AREF defaults to 5V. It takes approximately 100 µs to preform an analog read. Analog inputs A0-A5 can be configured to act exactly like the GPIO pins if needed.
( )
= 1023IntegerVALX
AREFVA
5V
0VSample 1 Sample 2 Sample 3
Sample 1: 2.6VSample 2: 2.7VSample 3: 1.3V
analogRead(AX) returns
VAX
=
=
1023
0.56.210236.2IntegerVV
AREFV
analogRead(AX) returns 552 analogRead(AX) returns 266
531
As Analog Inputs…
Robot Control with the Arduino
Electronics Design Laboratory 14ECEN 2270
GPIO: General Purpose Inputs/Outputs. These are digital outputs, so only voltages of 0V or 5V may be generated. Pins marked with (~) may be used to output PWM signals. Maximum current of 40mA per output. Pins default to Inputs
Analog Inputs: 6 Analog inputs are available. These pins convert an analog voltage in the range 0V-AREF to an integer in the range 0-1023
( )
= 1023IntegerVALX
AREFVA
USB input used for programming and
debugging
Power system connected as seen previously
• Outputs to robot: Stop/Go controls (2 per wheel) Speed reference (1 or 2 total)• Voltages: 5VDC for speed sensing circuits and 10VDC for Motors/Encoders• Inputs from robot: On/Off and Encoder Pulses
ATm
ega3
28P
8-bi
t uC
Robot Control with the Arduino
Electronics Design Laboratory 15ECEN 2270
ATm
ega3
28P
8-bi
t uC
10VGND5V
ENCODER_L( 2)ENCODER_R( 3)
ON/OFF( 6)CW_L( 7)CC_L( 8)
REF_L( 9)
REF_R(10)CW_R(11)CC_R(12)
LED(13)
Input from bench supply or battery
Output used to supply +5V circuitry (Labs 2 and 3)
• Outputs to robot: Stop/Go controls (2 per wheel) Speed reference (1 or 2 total)• Voltages: 5VDC for speed sensing circuits and 10VDC for Motors/Encoders• Inputs from robot: On/Off and Encoder Pulses
Programming• Plug-in USB cable• First-time use only:
install USB driver (if need be)
• Start Arduinodevelopment environment
• Open an example: File Examples1.Basics Blink
• Click Upload• http://arduino.cc
Extensive on-line reference, examples, blog, forum, …
Electronics Design Laboratory 16ECEN 2270
“Blink” example
Electronics Design Laboratory 17ECEN 2270
/*BlinkTurns on an LED on for one second, then off for one second, repeatedly.
*/
void setup() {// initialize the digital pin as an output.// Pin 13 has an LED connected on most Arduino boards:pinMode(13, OUTPUT);
}
void loop() {digitalWrite(13, HIGH); // set the LED ondelay(1000); // wait for a seconddigitalWrite(13, LOW); // set the LED offdelay(1000); // wait for a second
}
Commands in setup are executed only once, at the beginning
Commands in loop are executed repeatedly
Speed Control Setup for One Wheel
Electronics Design Laboratory 18ECEN 2270
Pin 7
Pin 8Pin 9
Pin 6 ON/OFF switch
Power supplies:+10V from bench power supply (or from battery)+5V from Arduino board
+5 V
1K
1K
1K
ON
OFF
o1
o2
Speed control code example
Electronics Design Laboratory 19ECEN 2270
/*ECEN2270 motor speed control example (left wheel only)*/
// define pinsconst int pinON = 6; // connect pin 6 to ON/OFF switch, active HIGHconst int pinCW_Left = 7; // connect pin 7 to clock-wise PMOS gateconst int pinCC_Left = 8; // connect pin 8 to counter-clock-wise PMOS gateconst int pinSpeed_Left = 9; // connect pin 9 to speed reference
// setup pins and initial valuesvoid setup() {
pinMode(pinON,INPUT);pinMode(pinCW_Left,OUTPUT);pinMode(pinCC_Left,OUTPUT);pinMode(pinSpeed_Left,OUTPUT);pinMode(13,OUTPUT); // on-board LEDdigitalWrite(pinCW_Left,LOW); // stop clockwisedigitalWrite(pinCC_Left,LOW); // stop counter-clockwiseanalogWrite(pinSpeed_Left,100); // set speed reference, duty-cycle = 100/255
}
void loop() {digitalWrite(13,LOW); // turn LED offdo {} while (digitalRead(pinON) == LOW); // wait for ON switchdigitalWrite(13,HIGH); // turn LED ondelay(1000); // wait 1 seconddigitalWrite(pinCW_Left,HIGH); // go clockwisedelay(3000); // for 3 secondsdigitalWrite(pinCW_Left,LOW); // stop
}
Lab 4 Part A• Clean up Lab 3 circuit, make sure you have a speed sensor
and two feedback controllers for both wheels• Play with the Arduino. Test the Blink program, and run any
other test programs that look interesting• At the end of part A, you should be able to use the Arduino
for speed control of both wheels• Make sure you know which direction the wheels will spin!
Electronics Design Laboratory 20ECEN 2830