Python Multiprocessing Stops after a while - python

So basically we're using raspberry pi for a project and a part of it includes the usage of ultrasonic sensors. We have three, and we've been able to get readings from all of them (I actually came from multi-threading but decided to move to multiprocessing). Here is my code:
#Libraries
from multiprocessing import Process, Lock
import RPi.GPIO as GPIO
import time
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#set GPIO Pins
GPIO_TRIGGER1 = 23
GPIO_ECHO1 = 24
GPIO_TRIGGER2 = 27
GPIO_ECHO2 = 22
GPIO_TRIGGER3 = 25
GPIO_ECHO3 = 17
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER1, GPIO.OUT)
GPIO.setup(GPIO_ECHO1, GPIO.IN)
GPIO.setup(GPIO_TRIGGER2, GPIO.OUT)
GPIO.setup(GPIO_ECHO2, GPIO.IN)
GPIO.setup(GPIO_TRIGGER3, GPIO.OUT)
GPIO.setup(GPIO_ECHO3, GPIO.IN)
def sense_distance(lock, processName):
#lock.acquire()
gpio_echo_var = ''
gpio_trigger_var = ''
if processName == "Sensor-1":
gpio_echo_var = GPIO_ECHO1
gpio_trigger_var = GPIO_TRIGGER1
elif processName == "Sensor-2":
gpio_echo_var = GPIO_ECHO2
gpio_trigger_var = GPIO_TRIGGER2
elif processName == "Sensor-3":
gpio_echo_var = GPIO_ECHO3
gpio_trigger_var = GPIO_TRIGGER3
print "%s process created." % (processName)
try:
while True:
# set Trigger to HIGH
GPIO.output(gpio_trigger_var, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(gpio_trigger_var, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(gpio_echo_var) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(gpio_echo_var) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
if distance <= 10:
print "%s has read less than 10 cm." % (processName)
else:
pass
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
#lock.release()
if __name__ == '__main__':
lock = Lock()
Process(target=sense_distance, args=(lock, "Sensor-1")).start()
Process(target=sense_distance, args=(lock, "Sensor-2")).start()
Process(target=sense_distance, args=(lock, "Sensor-3")).start()
It successfully reads the input and prints out the text when input goes smaller than 10 cm. However, after some time, they stop. I have run out of ideas and have searched all over only to come up short. Any sort of help will be appreciated.

Related

Python Threading: Using 2 Light Dependant Resistors (LDR)

I am using a simple code from PiMyLife tutorial to read from a Light Dependant Resistor using a capacitor. This works great..
I then added a 2nd LDR and capacitor, added it to pin 31 and then duplicated the code (the rc_time function and the fuction call) to add an additional Light Dependant Resistor but for some reason the second function call never happens.
#!/usr/local/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
#define the pin that goes to the circuit
pin_ldr_one = 29
pin_ldr_two = 31
def rc_time_one (pin_ldr_one):
count_one = 0
#Output on the pin for
GPIO.setup(pin_ldr_one, GPIO.OUT)
GPIO.output(pin_ldr_one, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_one, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_one) == GPIO.LOW):
count_one += 1
return count_one
def rc_time_two (pin_ldr_two):
count_two = 0
#Output on the pin for
GPIO.setup(pin_ldr_two, GPIO.OUT)
GPIO.output(pin_ldr_two, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_two, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_two) == GPIO.LOW):
count_two += 1
return count_two
#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
print("LDR 1: ",rc_time_one(pin_ldr_one))
print("LDR 2: ",rc_time_two(pin_ldr_two))
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
So I thought I would try threading instead thinking that the 2 function calls were having issues.
So this is the threading code I came up with however this too fails to run the second (in this cask 2nd thread). Any ideas where I am going wrong. Thanks for all your help
#!/usr/local/bin/python
import RPi.GPIO as GPIO
import time
from time import sleep, perf_counter
from threading import Thread
GPIO.setmode(GPIO.BOARD)
#define the pin that goes to the circuit
pin_ldr_one = 29
pin_ldr_two = 31
def rc_time_one (pin_ldr_one = 29):
count_one = 0
#Output on the pin for
GPIO.setup(pin_ldr_one, GPIO.OUT)
GPIO.output(pin_ldr_one, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_one, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_one) == GPIO.LOW):
count_one += 1
print("LDR 1: ",count_one)
return count_one
def rc_time_two (pin_ldr_two = 31):
count_two = 0
#Output on the pin for
GPIO.setup(pin_ldr_two, GPIO.OUT)
GPIO.output(pin_ldr_two, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_two, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_two) == GPIO.LOW):
count_two += 1
print("LDR 2: ",count_two)
return count_two
#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
# create two new threads
t1 = Thread(target=rc_time_one)
t2 = Thread(target=rc_time_two)
# start the threads
t1.start()
t2.start()
t1.join()
t2.join()
#print("LDR 1: ",rc_time_one(pin_ldr_one))
#print("LDR 2: ",rc_time_two(pin_ldr_two))
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Peter

I want to know if I any mistakes?

I have a problem with the Jsn-sr04t water proof ultrasonic sensor, knowing that the connection in the raspberry pi zero W1 is well made becouse it is activated but the readings of this are totally wrong, the pins were entered correctly but this one seems to have a data entry error. I tried several programs in python that were used on this same sensor model
PD: I'm aware that this works, because I tested it on an arduino mega and the sensor worked correctly
import RPi.GPIO as GPIO
import os
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_TRIGGER = 10
GPIO_ECHO = 8
TRIGGER_TIME = 0.00001
MAX_TIME = 0.004 # max time waiting for response in case something is missed
GPIO.setup(GPIO_TRIGGER, GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Echo
GPIO.output(GPIO_TRIGGER, False)
def measure():
# Pulse the trigger/echo line to initiate a measurement
GPIO.output(GPIO_TRIGGER, True)
time.sleep(TRIGGER_TIME)
GPIO.output(GPIO_TRIGGER, False)
# ensure start time is set in case of very quick return
start = time.time()
timeout = start + MAX_TIME
while GPIO.input(GPIO_ECHO) == 0 and start <= timeout:
start = time.time()
if(start > timeout):
return -1
stop = time.time()
timeout = stop + MAX_TIME
# Wait for end of echo response
while GPIO.input(GPIO_ECHO) == 1 and stop <= timeout:
stop = time.time()
if(stop <= timeout):
elapsed = stop-start
distance = float(elapsed * 34300)/2.0
else:
return -1
return distance
if __name__ == '__main__':
try:
while True:
distance = measure()
if(distance > -1):
print("Measured Distance = %.1f cm" % distance)
else:
print("#")
time.sleep(0.5)
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
constantly OUTPUT: "#"
Seems like an issue with the GPIO settings or a timing issue as the same setup works with an Arduino.
Try with higher trigger on time (maybe 20us)
After elapsed calculation, print the elapsed time on the screen and check the actual value
If it still doesn't work, then you can also checkout the alternate output options like serial UART, PWM, etc. You can get more details here -
https://tutorials.probots.co.in/communicating-with-a-waterproof-ultrasonic-sensor-aj-sr04m-jsn-sr04t/

PI controller threading & strange issue

I have a attached a rotary encoder to a DC motor shaft in hopes of creating a python script on a PI4 in which one could set a desired angle, motor will move CW at a 10 - duty cycle, and motor will stop and hold position on desired angle that once set angle is read back into code from rotary encoder (# 800 pulses per revolution - reading out 0.45 deg increments).
The PI controller will ideally then hold the set angle (set in code but later GUI) and will not move regardless of outside force on the shaft.
Lastly i am using if statments after my PI control signal 'PI' into a duty cycle ouput thus controlling the speed of the motor as it reaches the set point (either slowing down or speeding up tp get to the set point) with error PI polarity governing directional output....if this could be done better i'd appreciate any suggestions
I am only getting 1 error i cant figure out and cant find a solution online...but i feel i am close to getting this code right. Any criticism is welcome. The error is regarding the "PI" output of the Pi equation:
"Value of type "int" is not indexable"
from RPi import GPIO
import time
import threading
#Encoder GPIO Pins
clk = 15 # GRN/YLW Z+
dt = 11 # BLU/GRN Z-
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Motor GPIO Pins
PWMPin = 18 # PWM Pin connected to ENA.
Motor1 = 16 # Connected to Input 1.
Motor2 = 18 # Connected to Input 2.
GPIO.setup(PWMPin, GPIO.OUT) # We have set our pin mode to output
GPIO.setup(Motor1, GPIO.OUT)
GPIO.setup(Motor2, GPIO.OUT)
GPIO.output(Motor1, GPIO.LOW)# When program start then all Pins will be LOW.
GPIO.output(Motor2, GPIO.LOW)
def MotorClockwise():
GPIO.output(Motor1, GPIO.LOW) # Motor will move in clockwise direction.
GPIO.output(Motor2, GPIO.HIGH)
def MotorAntiClockwise():
GPIO.output(Motor1, GPIO.HIGH) # Motor will move in anti-clockwise direction.
GPIO.output(Motor2, GPIO.LOW)
#Sim Values
duty_c = 10 #duty Cycle
PwmValue = GPIO.PWM(PWMPin, 10000) # We have set our PWM frequency to 5000.
PwmValue.start(duty_c) # That's the maximum value 100 %.
error = 0
Set_point = 1.8
encoder_counter = 0
encoder_angle = 0
Kp = 2
Ki = 1
PI = 0
#Sim Parameters
Ts = .1 #sampling time
Tstop = 200 #end simulation time
N = int(Tstop/Ts)#simulation length
def ReadEncoder():
global encoder_counter
global encoder_angle
clkLastState = GPIO.input(clk)
while True:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
encoder_angle = float(encoder_counter/800)*360
if clkState != clkLastState:
if dtState != clkState:
encoder_counter += 1
else:
encoder_counter -= 1
#print("encoder_counter", encoder_counter )
print("encoder_angle", encoder_angle )
time.sleep(1)
clkState = clkLastState
# some delay needed in order to prevent too much CPU load,
# however this will limit the max rotation speed detected
time.sleep(.001)
def PI_function():
global PI
global duty_c
global error
global Kp
global Ki
global encoder_angle
for k in range(N+1):
error = int(Set_point) - encoder_angle
print (f"{error} Error Detected")
PI = PI[k-1] + Kp*(error[k] - error[k-1]) + (Kp/Ki)*error[k] #PI equation
print (f"{PI} PI value")
if (PI > 0):
MotorClockwise()
else:
MotorAntiClockwise()
if ((PI > duty_c) & (duty_c <100)):
duty_c += 1
if ((PI < duty_c) & (duty_c > 10)):
duty_c -= 1
return()
def main_function():
if (N != 0):
encoder_thread = threading.Thread( target=ReadEncoder)
encoder_thread.start()
encoder_thread.join()
PI_thread = threading.Thread( target=PI_function)
PI_thread.start()
PI_thread.join()
PwmValue.ChangeDutyCycle(duty_c)
else:
PwmValue.ChangeDutyCycle(0)
print(f"Motor Stopped & Holding at {encoder_angle} Degrees")

Python RecursionError: maximum recursion depth exceeded

I have a python program I wrote that moves my solar panel with the sun throughout the day. It works fine, but about twice a week I get RecursionError: maximum recursion depth exceeded
I'm pretty new to python and I've looked up Recursion Errors, it says when a function calls itself too many times, I don't see where that is happening in this program.
from datetime import date
from datetime import datetime, timezone, timedelta
import astral
from astral import Astral
import time
import pytz
# import RPi.GPIO as GPIO
# Global Variables
ast = Astral()
city_Name = 'Cleveland'
local_City = ast[city_Name]
# sun_Position = local_City.sun(local=True)
reset_Solar = True
# Retrieves and returns current time
def get_Current_Time():
eastern = pytz.timezone('America/New_York')
curr_Time = datetime.now(eastern)
return curr_Time
def main_Function():
global local_City
sun_Position = local_City.sun(local=True)
current_Time = get_Current_Time()
solar_Sunrise = sun_Position.get('sunrise')
solar_Noon = sun_Position.get('noon')
solar_Sunset = sun_Position.get('sunset')
calc_Sunrise_Noon = solar_Noon - solar_Sunrise
total_Seconds = calc_Sunrise_Noon.seconds
calc_Hours, remainder = divmod(total_Seconds, 3600)
calc_Minutes, calc_Seconds = divmod(remainder, 60)
time_To_Adjust = total_Seconds / 24
print (current_Time)
print (solar_Sunrise)
if current_Time >= solar_Sunrise and current_Time < solar_Sunset:
solar_Adjust_Active(time_To_Adjust)
elif reset_Solar == True:
reset_Solar_Panel()
else:
solar_Adjust_Deactive()
def solar_Adjust_Active(time_To_Adjust):
while time_To_Adjust > 0:
current_Time = get_Current_Time()
time_To_Adjust = time_To_Adjust - 1
time.sleep(1)
print (current_Time)
print (time_To_Adjust)
daylight_Adjustment()
def solar_Adjust_Deactive():
global local_City
curr_Time = get_Current_Time()
calc_Tomorrow = curr_Time.date() + timedelta(days=1)
sun_Position_Tomorrow = local_City.sun(local=True, date = calc_Tomorrow)
solar_Sunrise_Tomorrow = sun_Position_Tomorrow.get('sunrise')
time_Till_Sunrise = solar_Sunrise_Tomorrow - curr_Time
sunrise_Total_Seconds = time_Till_Sunrise.seconds
calc_Sunrise_Hours, remainder = divmod(sunrise_Total_Seconds, 3600)
calc_Sunrise_Minutes, calc_Sunrise_Seconds = divmod(remainder, 60)
while sunrise_Total_Seconds > 0:
sunrise_Total_Seconds = sunrise_Total_Seconds - 1
time.sleep(1)
# print ('Seconds till Sunrise', sunrise_Total_Seconds)
print (solar_Sunrise_Tomorrow)
main_Function()
def daylight_Adjustment():
global reset_Solar
# Adustment to Solar Panel
#GPIO.setmode(GPIO.BCM)
# init pin numbers
#pin_Open = [6]
# set mode default state is 'low'
#GPIO.setup(pin_Open, GPIO.OUT)
# Activate Open Relay to High (High turns Relay on)
#GPIO.output(pin_Open, GPIO.HIGH) # Activate Open relay
# Start Timer for duration actuator will be activated
timer = 0
while timer < 1:
time.sleep(1)
timer = timer + 1
print ('Panal adjusted')
# set Open relay back to low (Turns Relay off)
#GPIO.output(pin_Open, GPIO.LOW)
# Reset GPIO settings
#GPIO.cleanup()
reset_Solar = True
main_Function()
def reset_Solar_Panel():
global reset_Solar
print ('Setting panel back to original position')
# Adustment to Solar Panel
# GPIO.setmode(GPIO.BCM)
# init pin numbers
# pin_Open = [XX]
# set mode default state is 'low'
# GPIO.setup(pin_Open, GPIO.OUT)
# Activate Open Relay to High (High turns Relay on)
# GPIO.output(pin_Open, GPIO.HIGH) # Activate Open relay
# Start Timer for duration actuator will be activated
timer = 0
while timer <= 48:
time.sleep(1)
timer = timer + 1
# set Open relay back to low (Turns Relay off)
# GPIO.output(pin_Open, GPIO.LOW)
# Reset GPIO settings
# GPIO.cleanup()
reset_Solar = False
main_Function()
main_Function()
You have (at least one) a loop in your code:
def main_Function():
-> solar_Adjust_Active(time_To_Adjust)
-> daylight_Adjustment()
-> main_Function() -> .... # lets run that for a couple of days ...
Normally you have a main loop (f.e. while True:) and call/return from functions into your main loop without recursing:
Pseudocode:
def resetToStart():
move panels to sunrise position
return
def movePanel():
get_currnt_optimal_position_for_datetime
move_to_that_position
return
resetToStart() # start at a well known position
while True:
if NightTime: # sun is down
resetToStart()
sleep till daytime
elif DayTime: # sun is up
movePanel()
sleep some till adjustment needed

Ultrasonic Sensors with Raspberry pi 2b+ creates compilation error

I have set up 3 HC-SR04 ultrasonic sensors to operate continuously with raspberry pi. Two of the sensors work perfectly while one is being very inconsistent. I have replaced the wires, sensor, and breadboard multiple times, as well as changing the trig and echo pins associated with the sensor. I have debugged the code and have isolated the problem sensor in the code below:
import RPi.GPIO as GPIO
import time
TRIG1 = 13
ECHO1 = 15
##TRIG2 = 22
##ECHO2 = 18
##
##TRIG3 = 37
##ECHO3 = 40
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(TRIG1, GPIO.OUT)
GPIO.output(TRIG1, 0)
##GPIO.setup(TRIG2, GPIO.OUT)
##GPIO.output(TRIG2, 0)
##
##GPIO.setup(TRIG3, GPIO.OUT)
##GPIO.output(TRIG3, 0)
GPIO.setup(ECHO1, GPIO.IN)
##GPIO.setup(ECHO2, GPIO.IN)
##GPIO.setup(ECHO3, GPIO.IN)
while True:
time.sleep(0.1)
GPIO.output(TRIG1, 1)
time.sleep(0.00001)
GPIO.output(TRIG1, 0)
print("anything")
while GPIO.input(ECHO1) == 0:
print("Works")
time.sleep(1)
start1 = time.time()
while GPIO.input(ECHO1) ==1:
stop1 = time.time()
print("sensor 1:")
print (stop1-start1) * 17000
## time.sleep(0.1)
##
## GPIO.output(TRIG2, 1)
## time.sleep(0.00001)
## GPIO.output(TRIG2, 0)
##
## while GPIO.input(ECHO2) == 0:
## start2 = time.time()
##
## while GPIO.input(ECHO2) == 1:
## stop2 = time.time()
## print("sensor 2:")
## print (stop2-start2) * 17000
##
## time.sleep(0.1)
##
## GPIO.output(TRIG3, 1)
## time.sleep(0.00001)
## GPIO.output(TRIG3, 0)
##
## while GPIO.input(ECHO3) == 0:
## start3 = time.time()
##
## while GPIO.input(ECHO3) == 1:
## stop3 = time.time()
## print("sensor 3:")
## print (stop3-start3) * 17000
GPIO.cleanup()
The line that reads "print("anything")" allows the code to compile, but take no distance reading and will continue to print "Works" indicating that it is running continuously in the first while loop. Unexpectedly when the "print("anything")" line is removed, the error:
print (stop1-start1) * 17000
NameError: name 'start1' is not defined
prints to the terminal. The code that is commented out above, which is identical to the code for the functioning sensors works without error when isolated from the code that is currently uncommented. Any thoughts or suggestions would be greatly appreciated.
Thanks.
I think you have a timing issue, that causes your code to run differently depending on "small" timing changes.
Your code fails due to not having initialized start1.
When you have a delay in your code (print "anything") is a significant delay, the while loop will run and initialize start1
When you do not have the delay,
GPIO.input(ECHO1)
will not equal zero, the while loop will not run and you get the error.
I think you should initialie your variables and you should check if there are timing requirements you are not aware of.
Also please note running python on a raspberry pi, will not be a real time system, so please be aware of real time constraints and be careful of making code that is timing sensitive.
I think you have declaration issue. At first you need to declare then declare global. I changed line number 6,7,40 and 44. you see
import RPi.GPIO as GPIO
import time
TRIG1 = 13
ECHO1 = 15
start1=0.00
stop1=0.00
##TRIG2 = 22
##ECHO2 = 18
##
##TRIG3 = 37
##ECHO3 = 40
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(TRIG1, GPIO.OUT)
GPIO.output(TRIG1, 0)
##GPIO.setup(TRIG2, GPIO.OUT)
##GPIO.output(TRIG2, 0)
##
##GPIO.setup(TRIG3, GPIO.OUT)
##GPIO.output(TRIG3, 0)
GPIO.setup(ECHO1, GPIO.IN)
##GPIO.setup(ECHO2, GPIO.IN)
##GPIO.setup(ECHO3, GPIO.IN)
while True:
time.sleep(0.1)
GPIO.output(TRIG1, 1)
time.sleep(0.00001)
GPIO.output(TRIG1, 0)
print("anything")
while GPIO.input(ECHO1) == 0:
print("Works")
time.sleep(1)
global start1
start1 = time.time()
while GPIO.input(ECHO1) == 1:
global stop1
stop1 = time.time()
print("sensor 1:")
print (stop1 - start1) * 17000
## time.sleep(0.1)
##
## GPIO.output(TRIG2, 1)
## time.sleep(0.00001)
## GPIO.output(TRIG2, 0)
##
## while GPIO.input(ECHO2) == 0:
## start2 = time.time()
##
## while GPIO.input(ECHO2) == 1:
## stop2 = time.time()
## print("sensor 2:")
## print (stop2-start2) * 17000
##
## time.sleep(0.1)
##
## GPIO.output(TRIG3, 1)
## time.sleep(0.00001)
## GPIO.output(TRIG3, 0)
##
## while GPIO.input(ECHO3) == 0:
## start3 = time.time()
##
## while GPIO.input(ECHO3) == 1:
## stop3 = time.time()
## print("sensor 3:")
## print (stop3-start3) * 17000
GPIO.cleanup()

Categories