Monitoring the Position of a Stepper Motor

nfk

Senior Member
I need to keep track of the position of a stepper motor. I intend to do this by monitoring the step and direction lines that are controlling the motor (it's a normal step and direction stepper driver). Critically, I have to display the result on a serial LCD.

I have set up a system with an 08M running at 8MHz that uses an interrupt to check the status of the step and direction lines and at low frequencies this works fine. As soon as the step frequency gets higher it seems that while it is writing to the LCD there is enough time for a step to occur without it being recorded and it therefore the chip loses track of the stepper motor's exact position.

Is there a better way to do this or should I just use a faster chip (a 20X2 for example) and a higher baud rate?

(The steps occur up to 400 Hz. I do not want to use an optical encoder.)

Cheers,
Nigel
 

eclectic

Moderator
First thought.
If it's important, then spend an extra quid on a 20X2.

Second thought.

What's your circuit and program?
There might be other ways ??

e
 

nfk

Senior Member
Yes, I suspect the 20X2 is the way to go.

The circuit is as basic as it gets with the inputs of the 08M connected to the step and direction lines. The code is as follows:

Code:
setfreq m8
w0=32000
symbol minimum=w2
symbol maximum=w3
minimum=32000
maximum=32000
init:
	symbol flag=b2
	flag=0
	input 1
	high 2
	pause 2000
	setint %00000000,%00000010

main:

	serout 2,N4800_8,(254,"H",#w0,"   ",13)
	serout 2,N4800_8,(#minimum," ",#maximum)
	if w0>maximum then let maximum=w0
	endif
	if w0<minimum then let minimum=w0
	endif
	goto main
	
interrupt:
	if pin3=0 then let w0=w0+1
		else let w0=w0-1
		endif
	if flag=0 then let flag=1
		setint %00000010,%00000010
		else  flag=0
		setint %00000000,%00000010
	endif
	return
Don't get too hung up on the code as it was written very hastily. The main thing I want to know is whether or not the basic principle of using an interrupt in this way is the best approach.

(Oh, and by the way, in case you're wondering, the code is written in a way that simply tells me if steps are being lost - I make a note of the maximum and minimum values after one complete cycle of the stepper motor and then see if they change over time. Yes, very crude!)

Thanks,
Nigel

PS: The 08M starts to make 'mistakes' with a step frequency of over 18 Hz even when I alter the code to send just five bytes to the screen.
 
Last edited:

hippy

Ex-Staff (retired)
Interrupts are polled between commands so you need to make each command as short as possible and as fast as possible. Things like -

serout 2,N4800_8,(#minimum," ",#maximum)

will take a long time to execute, both decoding the variables to digits and sending each character.

Even if you speed up execution, serial comms may be a limiting factor; it will take over 10ms to send five characters at 4800 baud. Using BINTOASCII then sending each digit separately will help there.
 

westaust55

Moderator
interrupt:
if pin3=0 then let w0=w0+1
else let w0=w0-1
endif
additionally, how long are the signals on pin 3?
If of very short duration, at higher speeds, they signal may have gone before the test in the interrupt routine is done.

Could it be okay at slower speed but not fast enough at higher speeds?
 

nfk

Senior Member
Thanks all, yes, I think my approach isn't ideal. Perhaps the best way to do this is to use two chips instead of one.

On the first chip, which is doing the counting, I could place the position of the stepper, in binary, on the pins and then use a second to read that position and transmit it via serial.
 

MFB

Senior Member
The only way to reliably keep track of the output position is to put an encoder on the output shaft, either an analog pot or digital optical type. Merely keeping a count of input step pulses could lead to accumulative position errors.
 

Michael 2727

Senior Member
I agree with MFB,
The only reliable way is to use an analogue counter and read off that.
If your Picaxe is doing anything else (besides counting Pulses/States < >)
when a new pulse arrives you may miss it.
Unless you can guarantee the motor is stopped and the state will
not change while you output or whatever else you need to do.
 

hippy

Ex-Staff (retired)
A PICAXE or any digital system will work providing every step is detected and counter adjusted correctly. At 400Hz that's 2.5ms per step which could be possible to count with a fast PICAXE, and certainly possible using a pure logic gate solution.

If stepping is always in one direction it should be possible to use the on-chip counting peripherals of any PICAXE which has that, though that will require accessing SFR's directly.
 

MFB

Senior Member
Hippy, the problem is not keeping a count of the pulses sent to the stepping motor but that the motor cannot be guaranteed to respond proportionately under all load conditions. For example, a motor may miss an increment at start-up and such resultant errors can accumulate. One method to reduce this problem is to use a position calibration switch that is periodically activated by driving the motor to extreme travel, but even this will not catch steps that have been missed since the last re-calibration.

It does seem disappointing that this apparently 'digital motor' may still require output position sensors. When I first started to design automation systems that employed stepper motors I was amazed to find that whole books had been written about how to use these ‘simple’ devices. Including how best to accelerate the output shaft under different load conditions using software ramping techniques. For many applications this level of complexity is not necessary but if you do need to reliably positioning, then at least fit an output encoder.
 

hippy

Ex-Staff (retired)
@ MFB : Good points. I'd assumed acceleration up to speed and no skipping / losses. If one hasn't got that the only thing to do is measure 'where it points to' which won't be raleted to how many steps were applied.
 

BeanieBots

Moderator
Many commercial stepper driven robotic systems keep track of motor steps AND use an encoder feedback. Any discrepancy is used to determine a stall which either means maintenance is required or there has been a 'strike'.
 
Nigel,

With reference to your original posting and writing to the LCD. I had a similar problem with a DDS frequency synthesiser where the time taken to send an 8 digit number to the LCD made the control so slow as to be unusable. My solution was to use a second PICAXE to control the LCD and to send the data to it using the high speed serial port. It worked very well.

Richard
 
Top