Encoders, Motors, Power, Mini Project #1 10/24/2014.
-
Upload
jesus-carvin -
Category
Documents
-
view
223 -
download
0
Transcript of Encoders, Motors, Power, Mini Project #1 10/24/2014.
![Page 1: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/1.jpg)
Micromouse Lecture 1Encoders, Motors, Power, Mini Project #1
10/24/2014
![Page 2: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/2.jpg)
Power Regulation: A Problem We cannot use a simple voltage divider
to provide a consistent voltage source.
As the voltage from the battery decreases during discharge, the output voltage will decrease!
![Page 3: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/3.jpg)
Power Regulation: The Solution In order to provide a
consistent source of potential difference, we need to use voltage regulators.
We will provide you with a 5V regulator to connect to your LiPo battery pack for Mini Project #1.
![Page 4: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/4.jpg)
Power Regulation: Capacitors Use capacitors to smooth out noise in your
signal
![Page 5: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/5.jpg)
Motors
Brushed and Brushless
![Page 6: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/6.jpg)
Motors: Brushed
Brushed motors take a DC signal. Powers an inductor to rotate a magnet. Increase the voltage and/or current ->
Increase the rotation speed. Reverse the polarity of the input voltage ->
Reverse the rotation. Most digital microcontrollers do not have
an analog signal output. MCU’s output digital signals – either high or
low. So how do we control brushed motors?
![Page 7: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/7.jpg)
Pulse Width Modulation (PWM) Mimics an analog
voltage signal Square wave with
a certain frequency This can be used
to control the speed of a motor
![Page 8: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/8.jpg)
Pulse Width Modulation (PWM)Speed is controlled by rapidly turning the motor on and off
Turn the motor on for a greater fraction of the time to make it rotate faster
The percent of time the PWM signal is on is the duty cycle
0% duty cycle is same as off all the time; 100% duty is same as on all the time
![Page 9: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/9.jpg)
Motor Driver: A Problem
We cannot connect the MCU to the motor. MCUs don’t provide enough current to
power motors. Microcontrollers cannot invert the PWM
signal to rotate the motor in the other direction.
![Page 10: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/10.jpg)
Motor Driver: A Solution
Have the PWM control a H-bridge PWM controls transistors (think of them
as switches) that allows the battery to pour all its current to the motor
Easy to control direction
![Page 11: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/11.jpg)
Motor Driver: The H-bridge
Simplified diagram
Turn Left Turn Right
![Page 12: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/12.jpg)
Motor Driver: The H-bridge
void turnRight(){ digitalWrite(1A, LOW); digitalWrite(2A, HIGH); digitalWrite(3A, HIGH); digitalWrite(4A, LOW); analogWrite(motorPwrR, 100); analogWrite(motorPwrL, 100);}
void turnLeft(){ digitalWrite(1A, HIGH); //Right wheel digitalWrite(2A, LOW); //Right wheel digitalWrite(3A, LOW); //Left wheel digitalWrite(4A, HIGH); //Left wheel analogWrite(motorPwrR, 100); analogWrite(motorPwrL, 100);}
Actual implementation for two motors
![Page 13: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/13.jpg)
Motors: Brushless
Goal is the same as brushed motors: rotate something
Mechanics is different Multiple inductors attract
and repel the magnet Has more control than DC
motors Controlling brushless
motors are more complicated But fairly easy to do with
IC chips/software libraries
![Page 14: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/14.jpg)
Encoders
Helps you determine how far you have travelled in the maze.
Rotary Encoders attached to wheels Optical Magnetic with Hall Effect Sensor
![Page 15: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/15.jpg)
Rotary Encoder: Optical
Light reflects off alternating bright or dark areas. The detector determines when the light was shone on it. (Mini Project #1 uses this).
Another method: LED shines through a teeth in a disc to detector on other side.
![Page 16: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/16.jpg)
Rotary Encoder: Magnetic Attach magnets to a disc Use Hall effect sensors to detect the
changing magnetic field
![Page 17: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/17.jpg)
Now How Does the Code Work? Count the ticks to see how far the wheels
have turned
![Page 18: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/18.jpg)
How do we put an encoder to use?
Encoders are constantly outputting data
Your MCU needs to read the values all the time!
If your MCU is reading values 100% of the time it can’t do anything useful!
Otherwise you are losing data, or must litter your code with checks every few lines!
Not a good solution
![Page 19: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/19.jpg)
Interrupts to the rescue!
Interrupts allow you to process data and then go back to what you were doing
![Page 20: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/20.jpg)
Interrupts to the rescue!
Interrupts allow you to process data and then go back to what you were doing
“Yo imma let you finish, but this is some of the most important data of ALL TIME”
![Page 21: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/21.jpg)
Interrupts
An interrupt handler is a short function that runs when an external event happens
Rest of the program pauses, and continues after interrupt is done
From the perspective of each, the other doesn’t exist* *If your interrupt handler runs for too
long (and too often) it can choke your entire program!
![Page 22: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/22.jpg)
Using interrupts
Types of interrupts: RISING FALLING CHANGE LOW HIGH
Arduino boards: only two interrupt pins
Teensy: all pins!
![Page 23: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/23.jpg)
Sample Interrupt program
![Page 24: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/24.jpg)
Programming with interrupts
The volatile keyword: Tells the compiler “this value can change
at any time!” MCU will look up value in memory each
time and not an old value in a register Anything your interrupt handler modifies
should be volatile, or you may get bugs!
![Page 25: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/25.jpg)
Programming with interrupts Increment a counter and return (be fast)! Don’t want to use this counter directly If you accidentally overwrite it, you might
not be able to know how far you went! Using good coding style you can prevent
mistakes! But I’m too good to make such silly mistakes!▪ Nonsense, we are all human, mistakes happen!
![Page 26: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/26.jpg)
Programming with interrupts
The static keyword: Means “this variable/function can only
be used in this file only!” Return value of counter with a function! Now nobody from the outside can mess
with it directly!
![Page 27: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/27.jpg)
How to choose interrupt type
Depends on how fast your encoders are
On super high resolution encoders, it may be sufficient to track a single pin per encoder
On lower resolution encoders its better to track both pins changing
Tracking a pin change gives you even “more” resolution If a wheel is on the edge between ticks,
its possible to get “false positives”
![Page 28: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/28.jpg)
Mini Project #1: Encoders
Demonstrate basic understanding of motor control and encoders.
DUE DATE: 11/7/2014 SIGN UP EARLY Parts (abridged)
Teensy 3.1 Motor H-bridge Encoders
![Page 29: Encoders, Motors, Power, Mini Project #1 10/24/2014.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649c905503460f9494a77a/html5/thumbnails/29.jpg)
So What Do I Need to Do?
Keep your eyes peeled for an email for when the spec sheet for Mini Project #1 is up.
Do not hesitate joining a group you don’t know to complete the Mini Projects. Make friends!
Start early!