MINOS 04 Software for Stepper Motors Pete Harrison
Mar 26, 2015
MINOS 04
Software for Stepper Motors
Pete Harrison
http://micromouse.cannock.ac.uk/ Pete Harrison 2
Why Steppers
• Easy to get going
• Simple Hardware
• Simple Software
• Open Loop
• Easy mechanics
http://micromouse.cannock.ac.uk/ Pete Harrison 3
Why Not Steppers
• Poor Power to Weight ratio
• High Current Drain
• Open Loop
• Tricky to drive at speed
http://micromouse.cannock.ac.uk/ Pete Harrison 4
Stepper Characteristics
• Open loop digital control
• One pulse gives one step
• Fixed step size
• Resonances
http://micromouse.cannock.ac.uk/ Pete Harrison 5
Constant speed
• Constant speed implies constant drive frequency
• Jitter can cause mis-stepping
• A lost step is the last step
• Poor torque at speed
• Some speeds will suffer from resonances
http://micromouse.cannock.ac.uk/ Pete Harrison 6
Acceleration
• Accelerate quickly through resonances
• Don’t start too slowly
• Changes only happen at each step
• That is – a fixed distance not a fixed time so cant just add a time interval
• Acceleration has to be adjusted at each step
http://micromouse.cannock.ac.uk/ Pete Harrison 7
Hardware Requirements
• Digital controls– Step (one each)– Direction (one each)– Enable (shared)
• Accurate timing source for a pulse generator
• 2 ms-1 probably implies 2500Hz each
http://micromouse.cannock.ac.uk/ Pete Harrison 8
Software Requirements
• Each motor needs independent pulse train.
• Frequency sets speed
• Pulse length not critical
• Frequency changes on the fly to accelerate and decelerate
http://micromouse.cannock.ac.uk/ Pete Harrison 9
Timer Options
• Software Loops
• Dual timers – separate interrupts
• Single timer – single interrupt
• Single timer – Output compare/PCA
• Slave Processor
http://micromouse.cannock.ac.uk/ Pete Harrison 10
Software timing
• Simple to design and execute
• Step on demand
• Tricky to coordinate actions
• Low speeds
• Poor performance
http://micromouse.cannock.ac.uk/ Pete Harrison 11
Single Timer
• Frequency division/synthesis• Set to a high rate – say 5kHz• On each interrupt add constant to accumulator• On overflow, perform action• ALL motor code must run in the same time slot• e.g. 16 bit accumulator, constant = 3932 =>
f=5000*3932/65536 = 300Hz• Convenient overflow in assembler• There will be jitter
http://micromouse.cannock.ac.uk/ Pete Harrison 12
Dual Timers
• The easy way if you have them
• Two 16 bit timers needed
• One timer interrupt per motor
• Independent unless the timers are simultaneous
• Check interrupt priorities – they need to be high
http://micromouse.cannock.ac.uk/ Pete Harrison 13
One Timer with Output Compare
• Fairly common– 8051 derivatives (PCA)– AVR (OCRx)– PIC (Timer 1 CCPx)
• Single 16 bit timer with independent interrupts at user set rates
• Low overhead
http://micromouse.cannock.ac.uk/ Pete Harrison 14
Trapezoidal Profile
http://micromouse.cannock.ac.uk/ Pete Harrison 15
Calculating Acceleration
• Steppers need distance instead:
2
2
1ats
a
st
2
• Normally work with time as independent variable:
http://micromouse.cannock.ac.uk/ Pete Harrison 16
Calculating Acceleration
• For each step we need the interval to the next step
• Either– Calculate on the fly (square root)
• Or– Pre-calculate a lookup table
http://micromouse.cannock.ac.uk/ Pete Harrison 17
Lookup Table
• Use Excel or a program and load into mouse – can live in ROM/FLASH
• Several tables can live in memory
• Calculate whenever we need different speed/acceleration – needs to be in RAM
• May need 1024 16 bit values
http://micromouse.cannock.ac.uk/ Pete Harrison 18
Typical Table
• Step Elapsed per Step Frequency Velocity (m/s)0 01 30.834 30.834 32 0.0152 43.606 12.772 78 0.0373 53.406 9.800 102 0.0484 61.668 8.262 121 0.0565 68.947 7.279 137 0.0646 75.527 6.581 152 0.0717 81.579 6.051 165 0.0778 87.211 5.633 178 0.0839 92.501 5.290 189 0.088
10 97.505 5.004 200 0.093
Time(ms)
http://micromouse.cannock.ac.uk/ Pete Harrison 19
Using the Table
• Acceleration is just working through the table, picking out values
• Maximum speed is a number that tells us how far into the table to go
• Each entry is one step so speed index is also the number of steps to come to a halt
http://micromouse.cannock.ac.uk/ Pete Harrison 20
Typical Acceleration
Velocity Profile
0.00
0.10
0.20
0.30
0.40
0.50
0.60
0 100 200 300 400 500 600Time (milliseconds)
Vel
oci
ty (
m/s
)
http://micromouse.cannock.ac.uk/ Pete Harrison 21
Sample Code// motor interrupt
interrupt [TIM1_COMPA] void timer1_compa_isr(void){
UINT temp;
if (!steppersEnabled) return; // global bit variable
temp = OCR1A; // remember the counter value
STEP_LEFT=0; // get the pulse done early
delay_us(5); // we only need a short pulse
STEP_LEFT=1;
remaining--; // one more step done
if (remaining <= 0)
arrived = 1; // global flag
if (currentSpeed < remaining) // accelerate if we cancurrentSpeed++;
else // be sure we are able to decelerate
currentSpeed--;
if (currentSpeed > maxSpeed) // not too fast
currentSpeed = maxSpeed;
if (currentSpeed < 0) // or off the table
currentSpeed = 0;
OCR1A = temp + acc_table[currentSpeed];
}
MINOS 04
Software for Stepper Motors
Pete Harrison