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

Read encoder signal and read it on screen

Tue Sep 05, 2017 12:06 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).
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, someone could help me to wirte the script in a good way, maybe is possicle work on the for cycle, or everithing is wrong?

Thank you in advance

Return to “Beginners”