You are on page 1of 45

Motors, Control

Agenda for today

This talk:


PWM, Encoders, Servos, etc.


Control Strategies Using the ORC API

Open Lab

rolling Remote control Integrate a sensor Work towards Friday checkpoint

Reminder: Fridays Checkpoint

Robot should be put together


peg-board fine Dont need a target-collecting mechanism

Robot should be controllable by remote, using color coded cards


Wiki for details

Have a plan/strategy for contest


mechanism? Waypoint demonstration

Motors- Physics

Motor windings have a fixed resistance (ignoring heating) Voltage differential on motor leads causes current to flow. (Ohms law) Magnetic field proportional to current Torque proportional to magnetic field strength (and thus current) Torque causes rotor to begin rotating.

way? It tries to reduce the magnetic field strength.

Motors- Physics (continued)

(Magnetic) Rotor is now turning


Law applies. A voltage is generated!

Which way? To reduce the magnetic field strength => to reduce the current => cancelling out the externally-applied voltage

Motor Voltage = Applied voltage Back EMF


is the voltage we used when determining how much current flows

Motors- Physics (continued)

Scenario: motor just turned on

Back Back Back

EMF = 0, current is Vin/R EMF = 0, current is Vin/R

Scenario: motor rotor is stalled Scenario: motor rotating unimpeded

EMF = - Vin, current is 0 (With friction, current is non-zero but small.)

Scenario: applied voltage changes polarity from +Vin to -Vin


EMF = -Vin (inertia!), current is 2Vin/R

Motors as Inductors
Another complication: motors windings are inductors Inductors oppose changes in current


store energy When current would otherwise change, inductors become voltage sources such that the current remains constant


Motors as Inductors (continued)

V=LdI/dt Scenario: you unplug a running motor


=0 dI/dt = infinity This makes V really big Makes a big spark so that dt isnt quite 0

Scenario: you rapidily change the external voltage


= very large (finite switching times) V very big.

Motor Physics- Take home lessons

Dont unplug a running motor Dont rapidly change the voltage on a motor; slowly increase/decrease it to avoid a current spike

accelerating, accelerate in a couple steps: 25% for 30 ms, 50% for 30ms, 75% for 30ms, then 100%. This goes for changing direction and decelerating too! Your wheels are less likely to slip too!

Powering Motors

CPU cant control directly


pins usually can source/sink < 10mA pins are 1s and 0s

Our motors can use 200mA or more!


There is no -1, so no backwards There is no 0.5, so no medium speed

We want these things! How do we do it?

Motors: H-bridges


H-bridge allows bidirectional control A & D on: forward C & B on: backwards A & B on or C & D on: horrible! Our H-bridges have direction + enable What would B & D on do?

Motors: PWM
How do we run at fractional power? Consider rapidly toggling H-bridge on & off

85% power? 40% power?

40% power?

Motors: PWM continued

Two degrees of freedom: period and duty cycle


Two degrees of freedom: period and duty cycle (percentage on)

PWM- Why does this work?

Why does a 50% duty cycle look like half the voltage? It doesnt not always, at least:


period=1 day, duty cycle=0.5.

Motor will run full blast for 12 hours, then stop for 12 hours

PWM- how to chose period?

Rotor/Magnets have a mechanical time constant


of time it takes for rotor to achieve 63.2% of its final velocity after a step input.

Motor windings (inductor) and resistance have electrical time constant


of time it takes for current to achieve 63.2% of its final value after a step input with rotor locked in position.

Motor drivers take time to switch. If period is too short, drivers will spend the whole time switching and not conducting => low efficiency.

PWM- how to choose period?

Mechanical time constant ~ 10 msec Electrical time constant: ~ 1 msec PWM must be faster than mech time constant, or motor will vibrate Faster than electrical time constant ensures PWM waveform is effectively an analog voltage

do we care?

Avoid frequencies < 20KHz due to human hearing! (magnetostriction) Avoid frequencies > driver switching frequency (~100KHz?)

Motor performance measurement

How do we find out how a motor is performing?

Current Sensing Back EMF (tricky!) Tachometer Encoders

Current Sensing
Tells you how much torque the motor is producing. Not directly related to angular velocity


on terrain!

Back EMF

Use Back EMF to measure speed


must be zero

Must disconnect power and wait! (a couple electrical time constants) ORC board can measure, but requires great care and special cable.

Add an additional little motor to the output shaft with very low inertia This motor generates a back EMF This voltage proportional to rotational velocity.

Simple Encoders

Attach a disk to the motor shaft and attach a break-beam sensor across the teeth.


Output of Encoder

Or, use a reflectivity sensor and a disk with black & white colored wedges. Are we going forwards or backwards?

Quadrature Phase Encoders

Use TWO single encoders, 90 degrees out of phase. Close-up of teeth





Forward and backward are now distinguishable!

Servo Motors

Servo motors seek and hold a specific angle automatically! Have integrated gearhead & control electronics Use PWM as interface!


About 180 degrees of rotation, to within ~1 degree Pretty good torque, but take it easy on them!

Power dissipation can be bad Plastic gears are wimpy Intermittent duty only

Servo Motors, continued

PWM is not used in the same way as normal motors. Servo uses PWM to build an analog voltage that is compared to a potentiometer on the output (feedback). Just send a PWM with a duty cycle proportional to the desired angle! Pulse widths that are too short/long cause servo to shake violently!

very good for them. Consumes a lot of power

Other Actuators

Other actuators you might consider:

Solenoids Muscle Wire Cheap simple DC motors (5 or 12V) Electromagnetic <something> An actuator + a spring is a different actuator Use mechanical advantages when you can: levers, pulleys, ramps, winches Servos have a fair amount of torque for a motor their size, but can easily be overworked! Design so that actuators expend power only intermittently!

Dont forget 8.01

Robot Control Strategies

How do you make your robot do what you want? Common design: build behaviors, then coordinate behaviors from a planner

Control Logic


Go Straight

Follow Wall

Close Claw

Control Strategies

Fixed Plan (Scripted)

Preprogram a number of operations or phases which will run in a fixed sequence

Cant cope well with the unexpected!

Finite State Machine

Preprogram a number of contingencies

E.g., If Im looking for a target and I hit a wall, turn right.

Presents some ability to cope with the unusual But still cant cope with things you havent considered

AI Planner

Consider all possible plans (sequence of actions)

Give each one a score Pick the best one

Might be able to figure a way out of a bind that you hadnt considered. Complicated, sometimes slow. Assigning scores to agree with how you would decide can be really hard.

Control Strategies


the above

Example: Fixed plan + Finite State Machines


the first minute, run the find a target FSM For the second minute, run the search for and go to a scoring area Release the target For the third minute, try to go home

Philosophy of Control

Know Everything

My current position is (2.6,3.7), Im facing exactly east. Theres a target behind me at (1.5,3.6) and a wall between us. The following series of precise moves will get me there. Robot maintains a lot of state (information about the world)

Very easy to be wrong about some of it

Very powerful. Very hard. Gee, George. That sure is a pretty target over there. Mmmm, pretty red color. I should go there. (later) Ah, I have a red thing. I feel happy. But Id feel happier if I saw some yellow right about now so Ill spin! Not much state to maintain.

Know Nothing

Hard to be wrong! Hard to make good decisions.

Philosophy of Control (continued)

You want to be somewhere in the middle. For example:


as much state as you can, but understand the margin of errors in your state and take this into account. Expect to be wrong. Maintain only a little state. Do lots of wandering, but try to remember where youve explored so you maximize the ground youve covered. Gather information about particular places. When you get to one, know how to recognize it. Then figure out what the next few things you should try to do are. As you wander, youll get lost; adapt and simplify your plan based on what goes wrong.

Building Behaviors

How would you build a right-wall follower? Finite State Machine

1. 2. 3.

If Im getting farther away from the wall, turn a unit to the right If Im too close to the wall, turn a unit to the left Go forward a bit, then back to step 1.

Easy to write, easy to understand Not terribly flexible

What if were getting farther away from the wall (a wheel is slipping, for example) faster than we are turning to the right?

Building Behaviors- Wall Follower

Use a feedback-based control system


Compute error=distance from wall desired distance from wall

Error<0: too close Error>0: too far away

Compute some function of error, and use this to determine how sharply (and in what direction) to turn.


mathematics available to help design Robust in theory, a bit tricky to perfect in practice
Potential for instability => amusement for audience

often has derivative and integral components (PID control)

PID control

If youre really far away from the wall, turn sharply. If youre only slightly veering, turn just a bit.

Thats Proportional

If youre rapidly getting farther away from the wall, start turning harder.

Thats Derivative

If, despite your efforts, you keep getting farther away for a long time, start turning harder.

Thats Integral

Turnrate=Cpe + Ci e + Cdde/dt Discrete-time calculus This is actually what a servo motor does to hold a particular angle

Using the ORC API

Im almost done talking Where is the ORC library?

Look on the cs00s NFS server

Patches come out periodically. Each version is time stamped. E.g., orc010803.tgz. Copy it somewhere useful Untar it (tar xzvf orc010803.tgz) Edit/copy the helloworld example to get started quickly

What do I do with it?

API available from the Wiki

Largely self explanatory Its worth sitting down and reading through

ORC API Documentation

Getting started

must call orc_initialize() before anything else. You must link with liborc.a after your .o files
E.g.: g++ o myprogram main.o liborc.a Use g++, not gcc

ORC Communication

Bandwidth very limited


a couple hundred transactions per second Starvation! Consider caching results

Latency very high


transaction can take >10ms Lost opportunity for computation Consider doing communication asynchronously or heavy computation in separate thread.

LCD commands

orc_lcdHome orc_lcdPrint, orc_lcdGoto


LCD screen is only 16x2 characters!

Analog Command

orc_analog orc_analogSetResolution orc_analogSetMaxPort orc_currentSense

Analog values are [0,65535] corresponding to [0,5] volts.

Digital Commands


orc_buttonsWait orc_digitalRead orc_digitalWrite orc_digitalSetDirection

ORC supports digital out, but this is dangerous


you plug in sensor that outputs a value into a digital out port, you can damage your ORC. Ask a TA if you need this, and always be careful!

Motor Commands

orc_motorPWM orc_motorDir

Motors are indexed [1,4] different than other ports which are [0,x]! Use symbolic directions: MOTOR_FORWARD,

may not correspond to forward/backward for your robot Motors have different strengths forward and backward

Servo Commands

orc_servoPWM orc_servoSetProfile orc_servoSeek hitec300profile

Servos should not be seeked out of range for extended periods of time (>2 seconds?) Servos support profiles, allowing you to specify an angle, rather than a pulse width.

a convenience function; just calls servoPWM.

Ultrasound Commands

orc_ultrasoundPing orc_ultrasoundRead

Can only use one ultrasound at a time Must wait for the sonar to echo before reading!

Batteries: Care and Feeding

12V Lead Acid Battery, 5 Ah Battery recharges whenever ORC is plugged into AC & battery is connected Recharging battery can use lots of current

deeply discharge battery. Its bad for it anyway! Disconnect geode if regulator gets hot
That frees up 0.7A for battery charging!

regulator still too hot, disconnect battery. Recharge with a bench supply.

Soldering Iron Care

Immediately tin (cover with solder) new tips. When soldering, if tip isnt shiny, it needs to be cleaned
Sponge Re-tin

with solder, or retinning goop

Keep solder on tip when storing to prevent oxidation of tip Never, ever, sharpen the tip!


has special alloy on outer coating. Dont remove

Im done talking!
Now get to work! Not sure where to start?


to your advisor!