Sep-14-2021, 04:16 PM
I am working on python code for a rotary encoder. By following others scripts I was able to get a working program but it skipped a lot of counts, by trial and error I was able to put together some code that counted almost all counts. You may notice my code below looks like 9th grade basic from the mid 1980's, that's when I learned to code so when helping assume I know very little. ( I have put together a robot car running python with my son since so I know a little something)
I Ultimately need two encoders running at the same time so I was going to make a custom function to calculate movement for each part of the program to call. In my mind (but not in reality apparently) breaking a working program into multiple parts should just work as nothing is really being changed. However it stopped working. I then made the entire program a function and had the main program call the function and again it ran. I have moved lines from the function to the main program running it each time to find the line that was causing a problem leaving as much in the function as possible.
principle of rotary encoder https://en.wikipedia.org/wiki/Rotary_enc...ncoder.gif
My principle
I am waiting for A_State to rise (1)
Then determining the direction of movement by looking at B_State (1) or (0)
waiting for A_State to drop (0)
then determining weather it dropped on the same side and removing the movement
Or the other side and continuing with the movement.
here is the program the runs correctly.
it runs without errors but the output is incorrect
with a small movement on the encoder.
It outputs numbers either increasing or decreasing even if the encoder is not changing.
clearly my logic is not correct, sorry this is hard to reproduce without an encoder, I am hopping there is a simple logic step I am missing.
Extra information
I am on a raspberry pi 3B+ (I think) running MU 1.0.2
Sorry there is a little extra slop in the code as I have been testing different things trying to get it to work.
As always thank you for taking the time to read and assist me in my learning experience.
Gary
I Ultimately need two encoders running at the same time so I was going to make a custom function to calculate movement for each part of the program to call. In my mind (but not in reality apparently) breaking a working program into multiple parts should just work as nothing is really being changed. However it stopped working. I then made the entire program a function and had the main program call the function and again it ran. I have moved lines from the function to the main program running it each time to find the line that was causing a problem leaving as much in the function as possible.
principle of rotary encoder https://en.wikipedia.org/wiki/Rotary_enc...ncoder.gif
My principle
I am waiting for A_State to rise (1)
Then determining the direction of movement by looking at B_State (1) or (0)
waiting for A_State to drop (0)
then determining weather it dropped on the same side and removing the movement
Or the other side and continuing with the movement.
here is the program the runs correctly.
from RPi import GPIO from time import sleep import csv from time import sleep, strftime, time A = 17 B = 18 GPIO.setmode(GPIO.BCM) GPIO.setup(A, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(B, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) counter = 0 AState = GPIO.input(A) while True: while AState == 0: AState = GPIO.input(A) BState = GPIO.input(B) if BState == 1: counter += .5 if BState == 0: counter -= .5 while AState == 1: AState = GPIO.input(A) BState = GPIO.input(B) if BState == 1: counter -= .5 if BState == 0: counter += .5 print (int(counter)) GPIO.cleanup()The similar code calling a function that does not run correctly
it runs without errors but the output is incorrect
from RPi import GPIO from time import sleep import csv from time import sleep, strftime, time A = 17 B = 18 GPIO.setmode(GPIO.BCM) GPIO.setup(A, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(B, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) counter = 0 AState = GPIO.input(A) def encoder(A_S, newcounter, A ,B): B_S = GPIO.input(B) if B_S == 1: newcounter += .5 if B_S == 0: newcounter -= .5 while A_S == 1: A_S = GPIO.input(A) B_S = GPIO.input(B) if B_S == 1: newcounter -= .5 if B_S == 0: newcounter += .5 return newcounter while True: while AState == 0: AState = GPIO.input(A) Ncounter = encoder(AState, counter, A, B) counter = Ncounter print (counter) GPIO.cleanup()The output of the second code starts paused, appears to be waiting for AState to change from 0
with a small movement on the encoder.
It outputs numbers either increasing or decreasing even if the encoder is not changing.
clearly my logic is not correct, sorry this is hard to reproduce without an encoder, I am hopping there is a simple logic step I am missing.
Extra information
I am on a raspberry pi 3B+ (I think) running MU 1.0.2
Sorry there is a little extra slop in the code as I have been testing different things trying to get it to work.
As always thank you for taking the time to read and assist me in my learning experience.
Gary