Skip to content

How to get consistent results? #23

@kramer65

Description

@kramer65

I'm trying to read out a rotary encoder using a Raspberry Pi, but I'm having trouble getting good results.

I first tried the code in the rotary_interrupt_test.py file, but as soon as the motor/encoder moves, the program dies without any error. It just ends as if it's finished. No output whatsoever.

So then I tried the code in the rotary_worker_test.py file, but whichever way I turn it, it only gives the following output (I removed the switch print):

rotate -1
rotate 1
rotate -1
rotate 1
rotate -1
etc.

So then I tried the code in the rotary_test.py file and adjusted it to the code below so that I can test if it always gets positive deltas when I expect only positive ones.

#!/usr/bin/env python
import math
import gaugette.gpio
import gaugette.rotary_encoder
import gaugette

A_PIN = 7
B_PIN = 9

gpio = gaugette.gpio.GPIO()
encoder = gaugette.rotary_encoder.RotaryEncoder(gpio, A_PIN, B_PIN)

last_state = None
last_delta = 0
last_sequence = encoder.rotation_sequence()
last_heading = 0

# for coarser granularity reading
steps_per_cycle = 4  # arbitrary, usually equal to steps per detent

while True:
    state = encoder.rotation_state()

    if state != last_state:
        last_state = state
        last_heading += 1

        a_state = state & 0x01
        b_state = (state & 0x02) >> 1

        # compute sequence number:
        # This is the same as the value returned by encoder.rotation_sequence()
        sequence = (a_state ^ b_state) | b_state << 1

        # compute delta:
        # This is the same as the value returned by encoder.get_delta()
        delta = (sequence - last_sequence) % 4
        if delta == 3:
            delta = -1
        elif delta == 2:
            # this is an attempt to make sense out of a missed step:
            # assume that we have moved two steps in the same direction that we were previously moving.
            delta = int(math.copysign(delta, last_delta))

        last_delta = delta
        last_sequence = sequence

        if delta < 0:
            print 'got a negative delta when I expect only positive ones:', delta

When I turn the motor manually I get the results that I expect (positive deltas), but when I turn the motor on at full speed I'm sometimes getting negative deltas. That sometimes is also not very consistent. Sometimes nothing for a minute, and then I get hundreds of wrong results within seconds:

got a negative delta when I expect only positive ones: -1
got a negative delta when I expect only positive ones: -1
got a negative delta when I expect only positive ones: -2
got a negative delta when I expect only positive ones: -2
got a negative delta when I expect only positive ones: -1
got a negative delta when I expect only positive ones: -1
got a negative delta when I expect only positive ones: -2
got a negative delta when I expect only positive ones: -1
got a negative delta when I expect only positive ones: -1
got a negative delta when I expect only positive ones: -1
got a negative delta when I expect only positive ones: -2

So at this point I don't really know what to do. Of the three examples;

  • one simply dies
  • one gives a 1 and a -1 interchangeably whichever way I turn the motor
  • and one gives inconsistent wrong results

So at this point I really have no idea how to proceed.

Does anybody have any tips on what I can do to get good results from my encoder. Any help would be really welcome! :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions