Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rising edge of detection
#1
Hello, i'm new in the forum and i' begin in Python. I want to detect rising edge of incremental encoder and mesure the time between tow rising edge.
I do a code, but this code display the actual time. But i need the second time when i have another rising edge. I don't know how to do
# coding: utf-_
import RPi.GPIO as GPIO
import time
 
temps = time.time()
def callback_up(channel) :
    temps = time.time()
    print temps)
 
GPIO.setmode(GPIO.BCM)
PIR = 23
GPIO.setup(PIR, GPIO.IN)
try:
    GPIO.add_event_detect(PIR, GPIO.RISING, callback=callback_up)
    while 1:
        time.sleep(10)
exept KeyboardInterrupt:
   print("fin")
thank you
Reply
#2
You can use system clock, see:
https://raspberrypi.stackexchange.com/qu...ect-timing

but using on board timer is best: discussion here: https://www.raspberrypi.org/forums/viewt...p?p=273886

In either case, you will trigger the timer toggled on rising edge of GPIO pin.
Reply
#3
Thank you to your answer.
I don't anderstand how to use this système to count a time between two rising edge.
Do you think it's possible to do a countdown :
I make a time, the countdown begin and i have a counter who count the number of rising edge. When the countdown is finisch, we can deduct a period and the counter goes back to 0. I don't knox how to do a counter, but i think it's possible?
Sorry to my english but i'm french
Reply
#4
I have done this type of thing many times in the past, and that's my problem, it's sort of faded as exactly what has to be done.
I think there is plenty of documentation on how to use a timer with pi, google 'raspberry pi timer' brings up quite a bit.
I think it's a lot easier now then when I was doing similar work (but on TI msp430 or Microchip PIC).
I recall setting up registers on the timer, prior to starting, then first rising edge on GPIO triggers the timer to start, then next GPIO signal triggers timer to stop, the register of the timer can then be read to get the time.
Sorry so vague, take a look at the docs, also another moderator with more recent knowledge may pick up here.
Reply
#5
thanks four the answers,

i have another question, how to do to have only two number after the comma on a variable?
Reply
#6
Quote:i have another question, how to do to have only two number after the comma on a variable?
If I understand correctly:
# As tuple
myvariable = (1, 2)
myothervariable = ('Tom', 'Dick')

# as list:
myvariable = [1, 2]
myothervariable = ['Tom', 'Dick']
Reply
#7
Hello,
thank you, it works. I do a code to print the speed of the encoder bu it does'nt works, the max that he prints is 4, i think it's a probleme like the speed of python to detect rising edge. Can you help me because i need the speed but i dont know how to do now. My first idea is mesuring the time between two riing edge and after calculate the speed but it doesn't works. Do you have other idea of code that could works?
# coding: utf-_
import RPi.GPIO as GPIO
import time

lasttime=0
 
def callback_up(channel):
    global lasttime
    if lasttime==0:
        lasttime=time.time()
    else:
        now = time.time()
        gap=now-lasttime
        vitesse=(1/(gap*256))*257
        print(vitesse)
        lasttime=now
    
 
GPIO.setmode(GPIO.BCM)
PIR = 23
GPIO.setup(PIR, GPIO.IN)
try:
    GPIO.add_event_detect(PIR, GPIO.RISING, callback=callback_up)
    while 1:
        time.sleep(10)
exept KeyboardInterrupt:
   print("fin")
Reply
#8
Hello, i do an other code who works, i just have a problem whith the lcd. When he displays a big number an after i have an other smaller number, the lcd keep figures that exess. So i want that the first linechange all the number. Can you help me?
# coding: utf-8
import RPi.GPIO as GPIO
import lcddriver
import time
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
 
lcd = lcddriver.lcd()
start_time = time.time()
 
def effacer_ligne():
    lcd.lcd_display_string("                ",1)
 
GPIO.add_event_detect(23, GPIO.RISING)
def delta_angle():
    counter = 0
    base_temps=time.time()
    delta_temps=0
    while delta_temps<1 :
        if GPIO.event_detected(23):
            counter +=1
        delta_temps=(time.time())-(base_temps)
    return(counter)
while 1:
    start_time=time.time()#definition base des temps
    vitesse = delta_angle()/((time.time())-(start_time))#delta angle / delta temps
    vitesse2 = ((vitesse/128)*258)
    vitesse3 = "%.0f"% vitesse2
    lcd.lcd_display_string("     "+(vitesse3),1)
    lcd.lcd_display_string("     cm/min",2)
    #time.sleep(1)
    #effacer_ligne()
thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Networkx: Visualize an edge weight with a bubble/circle uvw 0 1,945 Sep-01-2021, 06:26 AM
Last Post: uvw
Photo Setting Marker Edge Color In Plotting JoeDainton123 0 2,659 Oct-26-2020, 10:48 PM
Last Post: JoeDainton123
  How do I apply a Gaussian blur to a particular edge of geometry in Matplotlib? hbolandi 0 1,950 Feb-02-2020, 06:08 PM
Last Post: hbolandi
  Randomise network while maintaining topology of nodes using edge-swap approach Tom1988 3 4,062 May-25-2017, 10:59 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020