Shop OBEX P1 Docs P2 Docs Learn Events
Smartpin Quadrature encoder example — Parallax Forums

Smartpin Quadrature encoder example


Here's a demo of the smartpin quadrature encoder mode.
Totalizer value is displayed on the 16 leds of the P123-A9 board.

'Quadrature encoder demo - P123-A9 on P0/P1

dat		org

		or	dirb,##$ffff	'enable leds
		pinsetm	mode,#0		'setup smartpin
		pinsetx	#0,#0		'set x parameter
		or	dira,#1		'enable smartpin
.loop		pingetz	adra,#0		'get totalizer value
		mov	outb,adra	'send to leds
		jmp	#.loop

mode		long	%1_10_10000_0001_0000_00_0_0000000000000

'
{
MMMMM = 10000 = A/B-input quadrature encoder counter

X[31:0] establishes a measurement period in clock cycles.
If 0 is used, the count operation will not be periodic, but continuous, like a totalizer.

Quadrature steps are counted through the measurement period, then the result is placed in Z, IN is raised, and the process repeats.

D/# = %1_DD_MMMMM_BBBB_AAAA_LL_T_PPPPPPPPPPPPP


BBBB = x001 = relative +1 pin’s read state
AAAA = x000 = this pin’s read state (default)
DD = 10 = output enable off

}

Multiple encoders handled in a single cog will be a breeze. :)

«13

Comments

  • jmgjmg Posts: 15,173
    edited 2016-02-12 01:24
    ozpropdev wrote: »
    Here's a demo of the smartpin quadrature encoder mode.

    Cool, Looks simple enough :)
    31 Encoders anyone ?

    Given the Selector-Reach feature, it should be possible to Generate a test case on 2 OUTPUT pin, and connect Quad to that, to check readback.
    Then, the dual-connect feature can extract period and Counts, with 2 reads. - all on just 2 pins.

    eg Test Period of ~127 SysCLKs and a slower read-difference loop of maybe 24384 SysClks should display 11000000 on Quad-Diff and 01111111 on Period capture.
  • cgraceycgracey Posts: 14,152
    On the A9 board, you could have that run off one of the pins which has a button, with another button next to it.
  • cgraceycgracey Posts: 14,152
    As jmg pointed out, you could take two adjacent A-B pins and use one smart pin as a totalizer for position and the other as a periodic counter for velocity.
  • jmgjmg Posts: 15,173
    cgracey wrote: »
    On the A9 board, you could have that run off one of the pins which has a button, with another button next to it.
    That's a little open ended / too simple ? - the self-test approach I mentioned above, should work ?

  • cgraceycgracey Posts: 14,152
    jmg wrote: »
    cgracey wrote: »
    On the A9 board, you could have that run off one of the pins which has a button, with another button next to it.
    That's a little open ended / too simple ? - the self-test approach I mentioned above, should work ?

    It would work, but it's fun to play with pushbuttons and manipulate the total. It's instructive.
  • I've got a real encoder hooked up at the moment and todays todo list first item is "Play with Smartpin's"
  • Ahem, none of these examples are "too simple"! We come here with a variety of depth and breadth of experiences and nobody should ever cast something off as "too simple". :)

  • jmgjmg Posts: 15,173
    mindrobots wrote: »
    Ahem, none of these examples are "too simple"! We come here with a variety of depth and breadth of experiences and nobody should ever cast something off as "too simple". :)

    Well, yes, but ozpropdev already had Quad counting working with an encoder, and I'm thinking about Smart Pin Test Coverage.

    To me, self test is simple, as it is highly portable and can duplicate on other systems.
    The test code does not change with buttons vs a real encoder.
  • jmgjmg Posts: 15,173
    cgracey wrote: »
    As jmg pointed out, you could take two adjacent A-B pins and use one smart pin as a totalizer for position and the other as a periodic counter for velocity.

    What is the theoretical upper Clock limit in Quad Mode ?
    (edges per second) ?
    What happens above that ?

  • cgraceycgracey Posts: 14,152
    edited 2016-02-12 02:50
    jmg wrote: »
    cgracey wrote: »
    As jmg pointed out, you could take two adjacent A-B pins and use one smart pin as a totalizer for position and the other as a periodic counter for velocity.

    What is the theoretical upper Clock limit in Quad Mode ?
    (edges per second) ?
    What happens above that ?

    It will track an edge per clock.

    On every clock, A and B and their prior states are considered, in order to inc or dec the counter.

    So, at 80MHz it could track up to 80M edges per second. You would probably want to not feed it more than 40M edges per second, though, to be safe. If you exceed the edge-per-clock limit, it would skip.
  • jmgjmg Posts: 15,173
    cgracey wrote: »
    It will track an edge per clock.

    On every clock, A and B and their prior states are considered, in order to inc or dec the counter.

    So, at 80MHz it could track up to 80M edges per second. You would probably want to not feed it more than 40M edges per second, though, to be safe. If you exceed the edge-per-clock limit, it would skip.

    To self test this at every clock, P2 could generate two /4 Period signals, (20MHz) shifted 90' & connect 2 Quad pins to that ?

    Which modes can do that Generate ? NCO, with different preload phase ?
  • cgraceycgracey Posts: 14,152
    NCOs set to $40000000 and then started two cycles apart by 'SETB DIRx,#n' then 'SETB DIRx,#n+1' would create a quadrature pattern.
  • cgraceycgracey Posts: 14,152
    I could make the NCO phase preloadable.
  • I can't help but think what implications these smart pins will have on SPIN2.
    The idea of Spin2 being able to tap into these marvellous features perhaps with a single macro like instruction.
    i.e QUADENC(20,21,V1) ' for quad encoder input on pins 20,21
    Then from there any reference to V1 simply is a PINGETZ V1,#20
    :)
  • jmgjmg Posts: 15,173
    cgracey wrote: »
    I could make the NCO phase preloadable.
    That would be useful for lower frequencies, where a preload and single shared start could launch many phases - Is there much logic cost ?

    - I think up to 32 NCOs could then be preloaded and launched this way ? - more with a small correction on two launch points.

  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-02-12 04:14
    Can the custom state machine mode also do quadrature encoders? If so, does that make quadrature mode redundant?
  • jmgjmg Posts: 15,173
    ozpropdev wrote: »
    I can't help but think what implications these smart pins will have on SPIN2.
    The idea of Spin2 being able to tap into these marvellous features perhaps with a single macro like instruction.
    i.e QUADENC(20,21,V1) ' for quad encoder input on pins 20,21
    Then from there any reference to V1 simply is a PINGETZ V1,#20
    :)
    plus you can get Quad-Period attached to the pins, with no extra pin cost....

  • cgraceycgracey Posts: 14,152
    Can the custom state machine mode also do quadrature encoders? If so, does that make quadrature mode redundant?

    Yes and yes. It saves loading X and Y with special 32-bit constants.
  • cgraceycgracey Posts: 14,152
    briskmover wrote: »
    ..not too bad for a first step approach...its a good dro...not really that great for initiating a servo motor or a stepper. i would call them knowledgable pins not smart pins.

    What would be good for initiating a servo motor or a stepper? How can we improve the design?
  • rjo__rjo__ Posts: 2,114
    edited 2016-02-13 17:42
    ozpropdev wrote: »
    I can't help but think what implications these smart pins will have on SPIN2.
    The idea of Spin2 being able to tap into these marvellous features perhaps with a single macro like instruction.
    i.e QUADENC(20,21,V1) ' for quad encoder input on pins 20,21
    Then from there any reference to V1 simply is a PINGETZ V1,#20
    :)

    Brian,

    I actually cnc'd my own encoders a while back... and it is a good thing, because it now
    allows me to remember what is going on and I can worry about Chip's wonderful manifestations.

    Andy's diagrams, your example and this discussion really helps. I just went through the documentation again and I still need to see an example as Chip mentions for using phase. I was assuming that we would watch with a waitedge ... but then that doesn't seem to make any sense:)

    Thanks again to jmg.

    Rich

  • jmgjmg Posts: 15,173
    edited 2016-02-13 19:21
    cgracey wrote: »
    What would be good for initiating a servo motor or a stepper? How can we improve the design?

    Servo is DC based, so the PWM would cover that.
    Q: Does the dithering feature discussed, have the ability to apply to a PWM LSB ? **
    - ie allow higher frequency PWM switching, with a lower-period average. Motor inertia smooths to the average.

    Stepper is along the lines of test-modes for Quad Encoder.
    Something like a NCO feeding a Quadrature Pair, with Up/Down (or left/right) control for direction.

    That might be a good example for State Modes (unless another mode can do this ?)
    Can State do NCO Clocked, 2 bit Gray Counter U/D ?

    This could also connect to a Quad-Counter, to feed-back actual position.

    Gets close to Stepper-Servo like operation in the pins :)

    Feed in Speed and Direction, and get back nett position.

    How many pin cells are needed here ? 2? ( or 3?)
    (Speed is known, so just one Quad Counter is needed)

    ** Addit: Seems like much of the Dither logic is already there in DAC modes, just need mapping to PWM.LSB rather than DAC.LSB ?

    Q: Should dither be Compare Based, or Rate Multiplier Based ?
  • not too bad for a first step approach...its a good dro...not really that great for initiating a servo

    Oh, I interpreted this to be a reference to no mention of the Index signal (aka: Marker Pulse) which is typically gated with the CHA and CHB to establish a zero point. The ability to compare this once/rev pulse with the encoder count is useful for checking count integrity, also.
    How can we improve the design?
    It seems that pretty much every dedicated quad-decode device on the market features digital noise filtering. The following is a quote from a MC document:
    15.3 PROGRAMMABLE DIGITAL NOISE FILTERS
    The QEI module uses digital noise filters to reject noise on the incoming index and quadrature
    phase signals. These filters reject low-level noise and large, short duration noise spikes that
    typically occur in motor systems.
    The filtered output signals can change only after an input level has the same value for three
    consecutive rising clock edges. The result is that short noise spikes between rising clock edges
    are ignored, and pulses shorter than two clock periods are rejected.
    The rate of the filter clocks determines the low passband of the filter. A slower filter clock results
    in a passband rejecting lower frequencies. The filter clock is the device FCY clock divided by a
    programmable divisor.
    Setting the Digital Filter Output Enable (QEOUT) bit in Digital Signal Control (DFLTxCON<7>)
    register enables the filter for QEAx, QEBx, and INDXx inputs. The Digital Filter Clock (QECK)
    bits (DFLTxCON<6:4>) specify the filter clock divisor used for the QEAx, QEBx and INDXx
    channels.
    Figure 15-4 is a simplified block diagram of the digital noise filter. Figure 15-5 shows the
    relationship between the incoming signal and the filtered output signal, where three consecutive
    clock pulses validate the input signal value.
  • jmgjmg Posts: 15,173
    briskmover wrote: »
    chip...if you immediatly compared the final count from the encoders to a 32 bit user defined variable you would have either a 1 or 0. this is now your direction for either a stepper or servo. it would be the programmers responsibilty to control the speed thru pwm or steppings

    That is quite a lot of logic you have added.
    The Pins are best doing bit-level stuff, especially those operations that need to be fast.
    Word-level slower operations, can be done in software.

    That said, Chip does say there are single bit FLAGS available in each mode. "to indicate when the smart pin has completed some function"

    With the Multiple-attach feature, you can allocate more than one Pin-Cell to the Quad Pins.
    Attach-2 is natural (no pin cost), and I think Attach-6 is the limit.(but that has a pin cost)

    Position and Period(velocity) are already suggested as one pair of attach.

    What you describe, may be possible with
    Position (absolute) and Position(relative) where the counter sign (MSB) is mapped to Flag.

    In operation user preloads the distance to travel, and the signal flag changes when the relative counter gets to zero.

    This could allow lower power or lower MHz operation, than fast Full Value polling of a number of Quad channels.

  • cgraceycgracey Posts: 14,152
    Mickster wrote: »
    not too bad for a first step approach...its a good dro...not really that great for initiating a servo

    Oh, I interpreted this to be a reference to no mention of the Index signal (aka: Marker Pulse) which is typically gated with the CHA and CHB to establish a zero point. The ability to compare this once/rev pulse with the encoder count is useful for checking count integrity, also.
    How can we improve the design?
    It seems that pretty much every dedicated quad-decode device on the market features digital noise filtering. The following is a quote from a MC document:
    15.3 PROGRAMMABLE DIGITAL NOISE FILTERS
    The QEI module uses digital noise filters to reject noise on the incoming index and quadrature
    phase signals. These filters reject low-level noise and large, short duration noise spikes that
    typically occur in motor systems.
    The filtered output signals can change only after an input level has the same value for three
    consecutive rising clock edges. The result is that short noise spikes between rising clock edges
    are ignored, and pulses shorter than two clock periods are rejected.
    The rate of the filter clocks determines the low passband of the filter. A slower filter clock results
    in a passband rejecting lower frequencies. The filter clock is the device FCY clock divided by a
    programmable divisor.
    Setting the Digital Filter Output Enable (QEOUT) bit in Digital Signal Control (DFLTxCON<7>)
    register enables the filter for QEAx, QEBx, and INDXx inputs. The Digital Filter Clock (QECK)
    bits (DFLTxCON<6:4>) specify the filter clock divisor used for the QEAx, QEBx and INDXx
    channels.
    Figure 15-4 is a simplified block diagram of the digital noise filter. Figure 15-5 shows the
    relationship between the incoming signal and the filtered output signal, where three consecutive
    clock pulses validate the input signal value.

    I read that PDF about the digital noise filtering and it was very useful. I've come to the conclusion that this would be best addressed by having a general filter in place for the A and B inputs on every smart pin. This would add minimal logic, but serve every mode. I just need to find a nice way to implement it via the PINSETx instructions. It's really good you brought this up.
  • I can see a killer multi-axis motion control chip in the making, here.
  • evanhevanh Posts: 15,915
    edited 2016-02-16 09:53
    I'd go so far as to say a fixed 3-bit filter could be useful for all input modes except 1:1 synchronous serial clocking. No, even that mode too - Just run the filter directly on the system clock so it's not slowed down by an external clock or reference divider.
  • jmgjmg Posts: 15,173
    edited 2016-02-16 19:00
    cgracey wrote: »
    I read that PDF about the digital noise filtering and it was very useful. I've come to the conclusion that this would be best addressed by having a general filter in place for the A and B inputs on every smart pin. This would add minimal logic, but serve every mode. I just need to find a nice way to implement it via the PINSETx instructions. It's really good you brought this up.

    Some optional filtering is nice to offer, as there are pluses and minuses.

    Quadrature counters already have natural filtering, you just need to ensure the narrowest fed pulse is still legal.
    I think that is there already ?

    On Capture or Count type signals, additional filtering can be beneficial, as no filtering at all can capture very narrow glitches, if they occur right on the clock edge,
    That is certainly best avoided.

    The down side is, each filter stage vote you add, extends the glitch width, but also lowers the maximum frequency.

    It is common for MCUs to run PLLs at least 2x the SysCLK, as that guarantees 50% duty cycles and so improves timing margins.

    I like the idea of feeding that 2x SysCLK into the Filter FF's so you still have the vote benefit, but can have less impact on maximum frequencies.

    I've also seen small Async prescalers provided, at the pins, which allows higher than SysCLK pin toggle, but can still feed a filter with Sub-SysCLK limits.

    This compromise works, because usually if you have high MHz signals that push SysCLK, you do not mind measuring over some multiple of cycles.

  • cgraceycgracey Posts: 14,152
    If we could get three bits free for this purpose:

    000 = no filtering
    001 = divide by 1 filtering
    010 = divide by 4 filtering
    011 = divide by 16 filtering
    100 = divide by 64 filtering
    101 = divide by 256 filtering
    110 = divide by 512 filtering
    111 = divide by 1024 filtering
  • ErNaErNa Posts: 1,752
    Imagine to have a quadrature sensor, that doesn't just give states, but is able to create an micro stepping count signal. At low speed, and very exactly positioned at the transition, the AB-signal would create a 50% forward/backward signal e.g. 00, 01, 00, 01, 00.... But when the physical position moves slightly away, the duration of the states changes. Being just before the middle between two transitions, the signal has a duty cycle close to 100%, when crossing it switches to nearly 0% to then increase again to 50% when the next step is reached. Such a sensor would allow to signal much higher resolution using only A/B wires. Positioning would be resolved and so speed estimation. As the communication line must transmit the bandwidth corresponding to the top speed of the moving sensor anyway, the PWM frequency and linked to that the resolution, is given by the highest speed. This feature to my best knowledge would be unique and a corresponding quadrature detector can easily be build on a kitchen table.
  • cgraceycgracey Posts: 14,152
    ErNa wrote: »
    Imagine to have a quadrature sensor, that doesn't just give states, but is able to create an micro stepping count signal. At low speed, and very exactly positioned at the transition, the AB-signal would create a 50% forward/backward signal e.g. 00, 01, 00, 01, 00.... But when the physical position moves slightly away, the duration of the states changes. Being just before the middle between two transitions, the signal has a duty cycle close to 100%, when crossing it switches to nearly 0% to then increase again to 50% when the next step is reached. Such a sensor would allow to signal much higher resolution using only A/B wires. Positioning would be resolved and so speed estimation. As the communication line must transmit the bandwidth corresponding to the top speed of the moving sensor anyway, the PWM frequency and linked to that the resolution, is given by the highest speed. This feature to my best knowledge would be unique and a corresponding quadrature detector can easily be build on a kitchen table.

    This would require some analog on the front end to resolve the sub-bit position, wouldn't it? Then, it would pwm, so to speak, the sub-bit position, so that the encoder could sum it up and gain another N bits of resolution. The normal count would start at bit N of the numerical output, and bits below N would be the fractional step. That would be pretty neat. The AB encoder would need to track time, not just state.
Sign In or Register to comment.