Gaizka
Posts: 5
Joined: Tue Sep 05, 2017 11:29 am

Read encoder signals and view them on a screen

Tue Sep 05, 2017 1:43 pm

Hi to everyone, i'm at very beginning stage with raspberry. As report in the topic title, I would like to read the encoder signals, in particular from an incremental encoder 1000 lap pulses (https://www.elcis.com/elcis/ENGLISH/cat ... /E0059Z.pdf), and visualize the increment and decrement on a screen.

I begin from this script:

Code: Select all

import RPi.GPIO as GPIO
import time

RoAPin = 12    # CLK Pin
RoBPin = 26    # DT Pin


globalCounter = 0

flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0

def setup():
GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
GPIO.setup(RoAPin, GPIO.IN)    # input mode
GPIO.setup(RoBPin, GPIO.IN)
def rotaryDeal():
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter
Last_RoB_Status = GPIO.input(RoBPin)
while(not GPIO.input(RoAPin)):
Current_RoB_Status = GPIO.input(RoBPin)
flag = 1
if flag == 1:
flag = 0
if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
globalCounter = globalCounter + 1
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter - 1



def loop():
global globalCounter
tmp = 0 # Rotary Temperary

while True:
rotaryDeal()
if tmp != globalCounter:
print 'globalCounter = %d' % globalCounter
tmp = globalCounter
                        #f = open('file.txt', 'wb')
                        
                        #f.write('GlobalCounter= %d'%globalCounter)
                        #f.close()

def destroy():
GPIO.cleanup()             # Release resource

if __name__ == '__main__':     # Program start from here
setup()
         try: 
     loop()

           destroy() 

and I write this one:

Code: Select all

import RPi.GPIO as GPIO
import time
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.config import Config
from kivy.core.window import Window
from kivy.clock import Clock
#Clock.max_iteration = 20
from kivy.uix.widget import Widget

RoAPin = 12  # CLK Pin
RoBPin = 26   # DT Pin

globalCounter = 0

flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0

def setup(dt):
GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
GPIO.setup(RoAPin, GPIO.IN)    # input mode
GPIO.setup(RoBPin, GPIO.IN)
Clock.schedule_once(setup, 0)

def funct(dt):
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter

Last_RoB_Status = GPIO.input(RoBPin)

for i in range (61):
           
  
  if (not GPIO.input(RoAPin)) :
                 Current_RoB_Status = GPIO.input(RoBPin)
                 flag = 1
                 
                 
                 
           if flag == 1:
        
   flag = 0
   if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
        globalCounter = globalCounter + 1
   if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
        globalCounter = globalCounter - 1
                      
          
   Clock.schedule_once(funct,0) 

       

                        
Class FlickerGUI(GridLayout):

    
    
   
    def __init__(self, **kwargs):
        super(FlickerGUI, self).__init__(**kwargs)
        self.cols = 1
        self.add_widget(Label(text='Saturation Level'))
        self.sat_label = Label(text=str(globalCounter))
        self.add_widget(self.sat_label)
        Clock.schedule_once(setup,0)
        Clock.schedule_once(funct,0)     
        Clock.schedule_once(self.update_labels,0) 


    def update_labels(self, *args):
        self.sat_label.text = str(globalCounter)
        Clock.schedule_once(self.update_labels,0) 

  
class MyApp(App):
    
    def build(self):
        return FlickerGUI() 
 
              
        

    
  
if __name__ == '__main__':
        
MyApp().run()

   
GPIO.cleanup()
I'm able to read encoder but not in the right way, according to the speed of rotation I read a different increase or decrease, and by performing a full round I do not read 1000 impulses. Someone could help me to wirte the script in a good way, maybe is possicle work on the for cycle or the clock, or everithing is wrong?

Thank you in advance

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read encoder signals and view them on a screen

Tue Sep 05, 2017 2:13 pm

You need to clarify what you want to achieve.

In the meantime have a look at piscope.

http://abyz.me.uk/rpi/pigpio/piscope.html
Last edited by joan on Thu Oct 19, 2017 8:31 pm, edited 1 time in total.

Gaizka
Posts: 5
Joined: Tue Sep 05, 2017 11:29 am

Re: Read encoder signals and view them on a screen

Tue Sep 05, 2017 2:54 pm

Thanks Joan,
I try to explain in the better way i can, simply I want read the position and visualize it on the screen by an app . Like that:

Image

0 at beginning, posititive number position if I turned the encoder to the right and negative if I turned left. But for now I'm sure that by the code I don't get the data (position) in the right way.


Once it work, We have to make a better way to visualize the data, maybe a coloureted button
I had try with the pigpio but i need to start the demo everytime that is an issues, futhermore for some reason by this code i'm unable to read the encoder position, after the time sleep the program exit without results.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Read encoder signals and view them on a screen

Tue Sep 05, 2017 6:49 pm

You may need to design the encoder switching as either a pull up or pull down circuit. I think your problem atm is that you have a floating GPIO pin.

Gaizka
Posts: 5
Joined: Tue Sep 05, 2017 11:29 am

Re: Read encoder signals and view them on a screen

Wed Sep 06, 2017 6:38 am

Thanks, but I'm not sure that is a setup problem, bacause if I use the first code I posted I'm able to read the encoder position without problem, but when I used my own code (the second I posted), seems that some data were not collected and visualized. Seems that the generated signals are faster than the capapility to collect the data of the "program and/or raspberry"

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read encoder signals and view them on a screen

Wed Sep 06, 2017 6:55 am

The pigpio examples generally run for a few minutes then stop. Partly so that the example code can show a clean way to shut down. If you don't want that behaviour you need to edit the code.

Gaizka
Posts: 5
Joined: Tue Sep 05, 2017 11:29 am

Re: Read encoder signals and view them on a screen

Thu Sep 07, 2017 6:37 am

ok, I will try to edit the code, but I want read the position by a GUI not on the terminal (any suggestion?) , and there is a way to start pigiod from the code and not writing sudo pigiod on the terminal?

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read encoder signals and view them on a screen

Thu Sep 07, 2017 7:01 am

To autostart at boot use

sudo systemctl enable pigpiod

I may have got enable and pigpiod the wrong way around and it may be pigpio rather than pigpiod.

A somewhat complicated kivy example at http://abyz.me.uk/rpi/pigpio/examples.h ... vy_GPIO_py

Return to “Python”