Sterling
Sterling
DIIDevHeads IoT Integration Server
Created by Sterling on 6/27/2024 in #middleware-and-os
I need to monitor hall sensors for position encoding with up to 350 changes per second.
Alright guys, I need to monitor hall sensors for position encoding with up to 350 changes per second. My current Python program watches GPIO pins but uses too much CPU. Here's my polling loop:
REFRESH_RATE = .0005

while True:
new_p1_state = GPIO.input(hall_p1)
new_p2_state = GPIO.input(hall_p2)

if new_p1_state != p1_state or new_p2_state != p2_state:
if p1_state == GPIO.HIGH:
if new_p2_state == GPIO.HIGH:
position -= 1
else:
position += 1
else:
if new_p2_state == GPIO.LOW:
position -= 1
else:
position += 1

p1_state = new_p1_state
p2_state = new_p2_state

time.sleep(REFRESH_RATE)
REFRESH_RATE = .0005

while True:
new_p1_state = GPIO.input(hall_p1)
new_p2_state = GPIO.input(hall_p2)

if new_p1_state != p1_state or new_p2_state != p2_state:
if p1_state == GPIO.HIGH:
if new_p2_state == GPIO.HIGH:
position -= 1
else:
position += 1
else:
if new_p2_state == GPIO.LOW:
position -= 1
else:
position += 1

p1_state = new_p1_state
p2_state = new_p2_state

time.sleep(REFRESH_RATE)
How can I make this more efficient? Should I use a different language or tool? If so, which one? @Middleware & OS
4 replies