Advertisements

Archive for June 13th, 2011

Stepper Motor Oscillocope Synchronization: Arduino to the Rescue!

In order to get good scope pictures of the winding current in a stepper motor, the scope must sync to the step pulses. However, it must also sync to the groups of 32 step pulses that make up a single set of four full steps, because the winding current repeats for each of those groups. Triggering once per revolution and delaying for a fixed amount will get you where you need to be.

The sync wheel provides a once-per-revolution pulse, but there’s some jitter in the edge for all the usual reasons and you’d be better off with a sync based on the stepper driver’s step input. The general idea is to find the leading edge of the optical pulse, find the next step pulse, then produce output pulses based on the step signal. Assuming a regular step pulse stream (from a pulse generator, for example), the output will be both locked to the wheel rotation and to the step pulses.

Normally this calls for a tedious wiring session involving logic gates and counters, but an Arduino has all the requisite machinery built in. The trick is to generate the pulses using the ATmega’s hardware, rather than program instructions, thus eliminating the usual jitter caused by instruction execution time.

I set up Timer 1 in Mode 4 (CTC with OCR1A controlling the matches) to count step pulse inputs on its T1 external clock input pin and produce a once-per-revolution output pulse on the OC1A pin. Because the output changes on the rising edge of the input clock, its rising and falling edges will provide rock-solid stable scope synchronization.

The big picture goes a little something like this:

  • Tell the counter to set the output on match, load the duration of the output pulse
  • Wait for the once-per-revolution signal, then enable the external clock input
  • Wait for the comparison to happen and reset the match flag
  • Set a one-pulse delay and tell set the counter to clear the output on match
  • Wait for the compare, clear the flag, turn off the counter
  • Wait until the once-per-rev signal goes low
  • And then do it all over again

Which produces this:

Sync Wheel

Sync Wheel

Top trace = optical signal from interrupter, middle = 1/rev sync from Arduino OC1A pin, bottom = step pulses. The motor is turning 3.5 rev/s = 210 rev/min. The top half of the screen is at 2 ms/div, the bottom half at 200 μs/div.

You could synchronize the counter to the 1/rev input exactly once, then produce the output pulse just by counting stepper pulses. It’d also be nice to have a pulse that repeats for each group of 32 microsteps within each set of four full steps, perhaps settable to a particular microstep within the group. All that’s in the nature of fine tuning.

Of course, devoting an Arduino to this project would be absurd, but for a one-off effort it makes a lot of sense.

The Arduino source code:

// Stepper motor driver synchronization
// Ed Nisley KE4ZNU June 2011

//-- Pin definitions, all of which depend on internal hardware: do *not* change

#define PIN_REV	2					// INT0 = positive 1/rev pulse from optical switch
#define PIN_STEP 5					// T1 = positive 1/step pulse from stepper driver
#define PIN_TRIGGER 9				// OC1A = positive trigger pulse to scope

#define SYNC_OFFSET	15				// steps from 1/rev puse to start of first 4-full-step group

#define PIN_TRACE_A    10
#define PIN_TRACE_B    11
#define PIN_TRACE_C    12

#define PIN_LED		13

//---------------------
// Useful routines

//--- Input & output pins

void TogglePin(char bitpin) {
	digitalWrite(bitpin,!digitalRead(bitpin));    // toggle the bit based on previous output
}

//----------------
// Initializations

void setup() {

  pinMode(PIN_REV,INPUT);		// INT0 1/rev pulse from wheel

  pinMode(PIN_STEP,INPUT);		// T1 step pulse from stepper driver

  pinMode(PIN_LED,OUTPUT);
  digitalWrite(PIN_LED,LOW);

  pinMode(PIN_TRACE_A,OUTPUT);
  pinMode(PIN_TRACE_B,OUTPUT);
  pinMode(PIN_TRACE_C,OUTPUT);

//--- Prepare Timer1 to count external stepper drive pulses

  TCCR1B = B00001000;				// Timer1: Mode 4 = CTC, TOP = OCR1A, clock stopped

  pinMode(PIN_TRIGGER,OUTPUT);		// OC1A to scope trigger

}

//----------------
// The main event

void loop() {

//-- Wait for rising edge of 1/rev pulse from optical switch

  TCCR1A = B11000000;						// COM1A set on compare
  TCNT1 = 0;								// ensure we start from zero
  OCR1A = SYNC_OFFSET;						// set step counter

  while(!digitalRead(PIN_REV)) {			// stall until 1/rev input rises
	TogglePin(PIN_TRACE_A);
  }

//-- Got it, fire up the timer to count stepper driver pulses

  TCCR1B |= B00000111;						// enable clock from T1 pin, rising edge

  digitalWrite(PIN_LED,HIGH);				// show we got here
  digitalWrite(PIN_TRACE_A,LOW);

  while(!(TIFR1 & _BV(OCF1A))) {			// wait for compare
	digitalWrite(PIN_TRACE_B,digitalRead(PIN_STEP));
	continue;
  }
  TIFR1 |= _BV(OCF1A);						// clear match flag

//-- Scope sync pulse now active

  digitalWrite(PIN_LED,LOW);				// show we got here
  digitalWrite(PIN_TRACE_B,LOW);

//-- Wait for another step pulse to clear scope sync

  TCCR1A = B10000000;						// COM1A clear on compare
  OCR1A = 1;								// wait for another pulse

  while(!(TIFR1 & _BV(OCF1A))) {			// wait for compare
	digitalWrite(PIN_TRACE_B,digitalRead(PIN_STEP));
	continue;
  }
  TIFR1 |= _BV(OCF1A);						// clear match flag
  digitalWrite(PIN_TRACE_B,LOW);

//-- Shut down counter and wait for end of 1/rev pulse

  TCCR1B &= ~B00000111;						// turn off timer clock

  while(digitalRead(PIN_REV)) {				// stall until 1/rev pulse goes low again
	TogglePin(PIN_TRACE_C);
  }
  digitalWrite(PIN_TRACE_B,LOW);

}
Advertisements

11 Comments