RevPi DIO Encoder - forward counts twice as fast as moving backwards.

Topics about the Software of Revolution Pi
Post Reply
daniel_123
Posts: 3
Joined: 30 Jan 2025, 01:11

RevPi DIO Encoder - forward counts twice as fast as moving backwards.

Post by daniel_123 »

I am investigating the use of the RevPi DIO encoder functionality to capture position information from a quadrature encoder. During my investigations I have found that when moving forward the encoder counts at twice the rate as when moving backwards (for the same frequency signal). Is this behaviour expected? Any ideas on how I can change this?

To test encoder functionality I am using a signal generator to simulate encoder wave forms, varying the phase of these signals to simulate moving forwards and backwards.

Code to capture output data
I have also confirmed the same behaviour using piTest.

Code: Select all

import time

# Testing an input output loop, with the output 1 wire connected to input 1
# PiCtory configuration needs to be set appropriately
# with OutputValue_1 and InputValue_1 set to 0-5V
import revpimodio2

# Initialize the RevPi communication
rpi = revpimodio2.RevPiModIO(autorefresh=True)
print(rpi.cycletime)

rpi.io["Counter_3"].reset()
rpi.io["Counter_4"].reset()

rpi.io["Counter_3"].signed = True
rpi.io["Counter_4"].signed = True

time.sleep(1)

start_time = time.time()

previous_count = 0
previous_time = start_time
while True:


    time_now = time.time()
    counter1 = rpi.io["Counter_3"].value
    counter2 = rpi.io["Counter_4"].value

    print("Count3: " + str(counter1) + " Count4: " + str(counter2) +" delta: " + str(counter1 - previous_count) + " time_delta: " + str(time_now-previous_time))

    previous_count = counter1
    previous_time = time_now
    time.sleep(1)  # Sleep for 1 second
Simulated encoder signals moving backwards
encoder backwards waveform.jpg

Code: Select all

Count3: 35534 Count4: 0 delta: -10 time_delta: 1.000213384628296
Count3: 35524 Count4: 0 delta: -10 time_delta: 1.0002143383026123
Count3: 35513 Count4: 0 delta: -11 time_delta: 1.0002446174621582
Count3: 35503 Count4: 0 delta: -10 time_delta: 1.0002529621124268
Count3: 35492 Count4: 0 delta: -11 time_delta: 1.0002257823944092
Count3: 35482 Count4: 0 delta: -10 time_delta: 1.000220537185669
Simulated encoder signals moving forwards
encoder forward waveform.jpg

Code: Select all

Count3: 35045 Count4: 0 delta: 20 time_delta: 1.0002079010009766
Count3: 35065 Count4: 0 delta: 20 time_delta: 1.0002102851867676
Count3: 35085 Count4: 0 delta: 20 time_delta: 1.0002093315124512
Count3: 35105 Count4: 0 delta: 20 time_delta: 1.000211238861084
Count3: 35125 Count4: 0 delta: 20 time_delta: 1.0002107620239258
Count3: 35145 Count4: 0 delta: 20 time_delta: 1.0002062320709229
Note that output 1 on the signal generator is plugged into DIO I4 and output 2 is plugged into I3

Device: RevPi Connect 4

Firmware version
:
pi@RevPi106155:~/projects $ piTest -f
firmware is up to date: 1.5 >= 1.5

Pictory Configuration
pictory config 2.png
User avatar
dirk
Posts: 2271
Joined: 15 Dec 2016, 13:19

Re: RevPi DIO Encoder - forward counts twice as fast as moving backwards.

Post by dirk »

Hello daniel_123, Foremost, thanks for all the information that I am still collecting, as a first idea so far - can you try to solve the problem by activating debouncing? Here is the spot in the video tutorial:
https://youtu.be/cBxmCPLKnlk?list=PLwce ... hNZu&t=329
daniel_123
Posts: 3
Joined: 30 Jan 2025, 01:11

Re: RevPi DIO Encoder - forward counts twice as fast as moving backwards.

Post by daniel_123 »

Hi Dirk,

Thanks for your reply.

I have tried with all different debouncing settings (no debouce, 25us, 750us and 3ms) the behaviour is still the same.

Let me know if you have any other ideas.

Cheers
User avatar
dirk
Posts: 2271
Joined: 15 Dec 2016, 13:19

Re: RevPi DIO Encoder - forward counts twice as fast as moving backwards.

Post by dirk »

Hello daniel_123, can you check the wiring? It may be a ground wire that is causing the whole timig to go out of sync. Do you have the possibility to look at the signals with an oscilloscope?

Edit: I can try to create a test setup. However, as a function generator is then required as an external signal source I think it will take a few days.
User avatar
dirk
Posts: 2271
Joined: 15 Dec 2016, 13:19

Re: RevPi DIO Encoder - forward counts twice as fast as moving backwards.

Post by dirk »

Hello daniel_123 my friend, so i have performed a test setup without a function generator.

I used this signal diagram as a template
image-20250224-140716.png
I used a DIO and wired it like this

Output Channel 1 -> Input Channel 1
Output Channel 2 -> Input Channel 2

I have set PiCtory like this

InputDebounce: 3ms
InputMode_1: Encoder
InputMode_2: Encoder
Output 1: A
Output 2: B

Code: Select all

import revpimodio2
import time
import sys

rpi     = revpimodio2.RevPiModIO(autorefresh=True)
encoder = [(0,0),(1,0),(1,1),(0,1)]
state   = 0
i       = 0
iMax    = 50
iSleep  = 0.1

rpi.io.Counter_1.reset()
time.sleep(1)
print("Counter_1: " + str(rpi.io.Counter_1.value))

print("Counting up")
for i in range(0,iMax):
        state = i % len(encoder)
        rpi.io.A.value=encoder[state][0]
        rpi.io.B.value=encoder[state][1]
        print("Counter_1: " + str(rpi.io.Counter_1.value))
        time.sleep(iSleep)

print("Counter_1: " + str(rpi.io.Counter_1.value))

print("Counting down")
while i>0:
        state = i % len(encoder)
        rpi.io.A.value=encoder[state][0]
        rpi.io.B.value=encoder[state][1]
        print("Counter_1: " + str(rpi.io.Counter_1.value))
        time.sleep(iSleep)
        i=i-1

print("Counter_1: " + str(rpi.io.Counter_1.value))
If I start like this

Code: Select all

python3 EncoderTest.py > /tmp/EncoderTest.txt
This file comes out
EncoderTest.zip
(330 Bytes) Downloaded 640 times
I could not detect any error

My device configuration:

Code: Select all

pi@RevPi94369:~$ piTest -d
Found 2 devices:

Address: 0 module type: 138 (0x8a) RevPi Connect 5 V1.0
Module is present
     input offset: 113 length: 6
    output offset: 119 length: 7

Address: 31 module type: 96 (0x60) RevPi DIO V1.5
Module is present
     input offset: 0 length: 70
    output offset: 70 length: 18
User avatar
dirk
Posts: 2271
Joined: 15 Dec 2016, 13:19

Re: RevPi DIO Encoder - forward counts twice as fast as moving backwards.

Post by dirk »

Hello daniel_123, there may be an electrical problem. Can you visualize the signals with an oscilloscope?
Post Reply