arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

My program stuck after sometime

Wed Jul 17, 2019 11:57 am

My incoming data is in hex . After sometime of program start it is getting stuck and i am getting no output. My program stuck at mostly

if start==0:
GPIO.output(12,GPIO.LOW)
print("wait")
this place in code.

Code: Select all

import threading
import queue
import serial
import time 
import RPi.GPIO as GPIO
count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
#rbuff=[]
rbuff=list(range(100))
temp=0
temp1=0
start=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)

events_queue = queue.Queue()

start1 = time.time()
start2 = time.time()
def thread_read():
	global count_enable, flag,temp, count,start2
	while True:
		rbuff=ser.read()
		if count_enable == 1:
			count+=1
		if rbuff[0] == 0x02 and flag==0: # and zero_detect==1: 
			count_enable=1
			#ser.write(rbuff)
			#print (count)
			flag=1
		if rbuff[0] == 0x01 and flag==1  and count==1:
			#ser.write(rbuff)
			#print(count)
			flag=2
		if rbuff[0] == 0x04 and flag==2 and count==2:
			#ser.write(rbuff)
			#print(count)
			flag=3
		if rbuff[0] == 0x03 and flag==3 and count==3:
			#ser.write(rbuff)
			#print(count)
			flag=4

		if flag==4 and count==28:
			ser.write(rbuff)
			#print(count)
			count=0
			flag=0

			if rbuff[0]-2 <= temp <= rbuff[0]+2:
				GPIO.output(12,GPIO.LOW)
				#ser.write(rbuff)
				#start=0
				temp=rbuff[0]
				state = GPIO.input(12)
				print(state)


			else:
				GPIO.output(12,GPIO.HIGH)
				#start=0
				start=10+(time.time())
				#print(start)
				temp=rbuff[0]
				# ser.write(ord(buff))
			if start==0:
				GPIO.output(12,GPIO.LOW)
				print("wait")
			else:
				now=time.time()
				seconds=start-now
				if seconds>0:
					GPIO.output(12,GPIO.HIGH)
					print("on")
				else:
					GPIO.output(12,GPIO.LOW)
					start=0

		if count == 1 and rbuff[0] != 0x01:
			count_enable=0
			count=0
			flag=0





def thread_check():
	global start1
	while True:
		if time.time() - start1 >= 1:
			start1 = time.time()
			#print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read)
read_thread.start()

check_thread = threading.Thread(target=thread_check)
check_thread.start()

event=threading.Event()
try:
    event.wait()
except KeyboardInterrupt:
    pass





pcmanbob
Posts: 6681
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: My program stuck after sometime

Wed Jul 17, 2019 12:18 pm

Looking at your code this line is not correct

Code: Select all

if rbuff[0]-2 <= temp <= rbuff[0]+2:
I assume you want both to be true to execute the if.

Then I think it should look more like this

Code: Select all

if ( rbuff[0]-2 <= temp and temp <= rbuff[0]+2 ):
but the less than or more than might be the wrong way round , may be if you actually wrote what operators you want for each we could be sure.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: My program stuck after sometime

Wed Jul 17, 2019 12:46 pm

Hi pcmanbob,

Here i want both the condition to be true.So i need and operator. When i make change what you have suggested still it is not working smoothly as before. Now It takes time to respond to make led on. Are you sure that sometimes my program get stuck because of this condition not written in a way like you write?

pcmanbob
Posts: 6681
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: My program stuck after sometime

Wed Jul 17, 2019 1:23 pm

I did not say it was the answer but the line you had just did not seem right, if your old line works better for you then use it.

As we cant test your code as we don't have your hardware you are going to have to debug the code yourself if no one else can see any errors.

try adding lots of print statements to you code before the if statements that you think are causing the problem and watch the code execute by running it at the command line watching what the variables are doing might reveal what the problem is.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ghp
Posts: 1402
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: My program stuck after sometime

Wed Jul 17, 2019 6:29 pm

I simulated inputs from serial in a random way.

Code: Select all

class S:
    def __init__(self, name, speed, timeout=0):
        pass
    
    def read(self):
        return random.choice( [b'\x01', b'\x02', b'\x03', b'\x04', b'\x05', ])
    def write(self, data):
        print("data", data)
ser=S('/dev/ttyAMA0',921600,timeout=1)
This produces byte inputs. Added a print statement after the ser.read.

Code: Select all

def thread_read():
    global count_enable, flag,temp, count,start2
    while True:
        rbuff=ser.read()
        print("rbuff", rbuff, "count_enable", count_enable, "flag", flag, "count", count)
        if count_enable == 1:
            count+=1
        if rbuff[0] == 0x02 and flag==0: # and zero_detect==1: 
            count_enable=1
the output is

Code: Select all

staring program
rbuff b'\x01' count_enable 0 flag 0 count 0
rbuff b'\x02' count_enable 0 flag 0 count 0
rbuff b'\x01' count_enable 1 flag 1 count 0
rbuff b'\x05' count_enable 1 flag 2 count 1
rbuff b'\x02' count_enable 1 flag 2 count 2
rbuff b'\x04' count_enable 1 flag 2 count 3
rbuff b'\x05' count_enable 1 flag 2 count 4
rbuff b'\x01' count_enable 1 flag 2 count 5
rbuff b'\x03' count_enable 1 flag 2 count 6
rbuff b'\x04' count_enable 1 flag 2 count 7
rbuff b'\x05' count_enable 1 flag 2 count 8
rbuff b'\x04' count_enable 1 flag 2 count 9
rbuff b'\x03' count_enable 1 flag 2 count 10
rbuff b'\x02' count_enable 1 flag 2 count 11
rbuff b'\x01' count_enable 1 flag 2 count 12
rbuff b'\x05' count_enable 1 flag 2 count 13
rbuff b'\x02' count_enable 1 flag 2 count 14
rbuff b'\x02' count_enable 1 flag 2 count 15
rbuff b'\x05' count_enable 1 flag 2 count 16
rbuff b'\x04' count_enable 1 flag 2 count 17
rbuff b'\x02' count_enable 1 flag 2 count 18
rbuff b'\x02' count_enable 1 flag 2 count 19
rbuff b'\x05' count_enable 1 flag 2 count 20
rbuff b'\x02' count_enable 1 flag 2 count 21
rbuff b'\x03' count_enable 1 flag 2 count 22
rbuff b'\x02' count_enable 1 flag 2 count 23
rbuff b'\x05' count_enable 1 flag 2 count 24
rbuff b'\x03' count_enable 1 flag 2 count 25
rbuff b'\x02' count_enable 1 flag 2 count 26
rbuff b'\x04' count_enable 1 flag 2 count 27
rbuff b'\x02' count_enable 1 flag 2 count 28
rbuff b'\x04' count_enable 1 flag 2 count 29
rbuff b'\x01' count_enable 1 flag 2 count 30
and so on, counter increases and increases, the other values count_enable and flag stay same.
It is not obvious what this code should achieve. Could you describe in short sentences what you expect it should do ?

User avatar
Paeryn
Posts: 2636
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: My program stuck after sometime

Wed Jul 17, 2019 7:31 pm

pcmanbob wrote:
Wed Jul 17, 2019 12:18 pm
Looking at your code this line is not correct

Code: Select all

if rbuff[0]-2 <= temp <= rbuff[0]+2:
I assume you want both to be true to execute the if.
That line is fine in of itself. In Python, unlike a lot of other languages, a test like a <= b <= c works like it does in maths, it tests both that a is less than or equal to b and that b is less than or equal to c.
She who travels light — forgot something.

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: My program stuck after sometime

Thu Jul 18, 2019 10:35 am

Still same problem is coming for the given change. And why there is error coming while i have written "or " instead of "and".
Error:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "iwrserial3.py", line 75, in thread_read
if start==0:
UnboundLocalError: local variable 'start' referenced before assignment

User avatar
B.Goode
Posts: 8271
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: My program stuck after sometime

Thu Jul 18, 2019 10:48 am

local variable 'start' referenced before assignment



Nothing to do with and/or

You have commented out the assignments to the start variable in your function definition.

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: My program stuck after sometime

Thu Jul 18, 2019 11:07 am

Hi B.Goode,

I forgot to post my modified code.My code is below and for that i am facing above issue. And what about stuck problem?And still i am getting same error while replacing "and" with "or".

Code: Select all

import threading
import queue
import serial
import time 
import RPi.GPIO as GPIO
count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
#rbuff=[]
rbuff=list(range(100))
temp=0
temp1=0
start=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)

events_queue = queue.Queue()

start1 = time.time()
start2 = time.time()

def thread_read():
	global count_enable, flag,temp, count,start2
	while True:
		rbuff=ser.read()
		if count_enable == 1:
			count+=1
		if rbuff[0] == 0x02 and flag==0: # and zero_detect==1: 
			count_enable=1
			#ser.write(rbuff)
			#print (count)b
			flag=1
		if rbuff[0] == 0x01 and flag==1  and count==1:
			#ser.write(rbuff)
			#print(count)
			flag=2
		if rbuff[0] == 0x04 and flag==2 and count==2:
			#ser.write(rbuff)
			#print(count)
			flag=3
		if rbuff[0] == 0x03 and flag==3 and count==3:
			#ser.write(rbuff)
			#print(count)
			flag=4

		if flag==4 and count==28:
			ser.write(rbuff)
			#print(count)
			count=0
			flag=0

			if (rbuff[0]-2 <= temp and temp<= rbuff[0]+2):
				GPIO.output(12,GPIO.LOW)
				#ser.write(rbuff)
				#start=0
				temp=rbuff[0]
				state = GPIO.input(12)
				print(state)


			else:
				GPIO.output(12,GPIO.HIGH)
				#start=0
				start=10+(time.time())
				#print(start)
				temp=rbuff[0]
				# ser.write(ord(buff))

			if start==0:
				GPIO.output(12,GPIO.LOW)
				print("wait")
			else:
				now=time.time()
				seconds=start-now
				if seconds>0:
					GPIO.output(12,GPIO.HIGH)
					print("on")
				else:
					GPIO.output(12,GPIO.LOW)
					start=0

		if count == 1 and rbuff[0] != 0x01:
			count_enable=0
			count=0
			flag=0





def thread_check():
	global start1
	while True:
		if time.time() - start1 >= 1:
			start1 = time.time()
			#print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read)
read_thread.start()

check_thread = threading.Thread(target=thread_check)
check_thread.start()

event=threading.Event()
try:
    event.wait()
except KeyboardInterrupt:
    pass





User avatar
B.Goode
Posts: 8271
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: My program stuck after sometime

Thu Jul 18, 2019 11:16 am

Not happy about wasting time commenting on outdated code.

The answer remains the same. There is no assignment to start in the else branch of your if statement. So start might be undefined when its value is tested later.

If it is important, give it a default value at the outset.

pcmanbob
Posts: 6681
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: My program stuck after sometime

Thu Jul 18, 2019 12:30 pm

B.Goode wrote:
Thu Jul 18, 2019 11:16 am
Not happy about wasting time commenting on outdated code.

The answer remains the same. There is no assignment to start in the else branch of your if statement. So start might be undefined when its value is tested later.

If it is important, give it a default value at the outset.
Start is defined right at the beginning of the code line 15, but as you are using start in a function you need to add it to the global line at the start of the function, or just add start=0 as the first line in your function.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: My program stuck after sometime

Thu Jul 25, 2019 4:33 am

Hi

Start is working now by define it as global but sill code stuck at the same place and code is not respond to any movement detected by my sensor as it should be.

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: My program stuck after sometime

Thu Jul 25, 2019 5:14 am

What do you use this program for? What is it supposed to achieve?
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: My program stuck after sometime

Thu Jul 25, 2019 5:43 am

Hi Michiel ,

I am using this program for object detection. If next object detection is greater or less then 2 than previous object led must on for 10sec. This code is working fine for sometime but after that it is not making led on even though given object condition is satisfied means it only executes the conditions at which led goes off(print("wait") & GPIO.output(12,GPIO.LOW)).

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: My program stuck after sometime

Fri Aug 02, 2019 5:56 am

Hi pcmanbob,

You have suggested me to take "and " operator instead of writing it tother. Actually i need only one of the conditions (use "or" operator) to be true at a time instead of both to be true at the same time. But when i am taking or operator code is not responding of the object variation.

Could you guide me for this?

pcmanbob wrote:
Wed Jul 17, 2019 12:18 pm
Looking at your code this line is not correct

Code: Select all

if rbuff[0]-2 <= temp <= rbuff[0]+2:
I assume you want both to be true to execute the if.

Then I think it should look more like this

Code: Select all

if ( rbuff[0]-2 <= temp and temp <= rbuff[0]+2 ):
but the less than or more than might be the wrong way round maybe if you actually wrote what operators you want for each we could be sure.

pcmanbob
Posts: 6681
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: My program stuck after sometime

Fri Aug 02, 2019 11:49 am

Then just replace the and with or , its as simple as that

there are plenty of python help pages on line I suggest you look some of them up.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ghp
Posts: 1402
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: My program stuck after sometime

Fri Aug 02, 2019 4:45 pm

Code: Select all

print("""
I proposed to use
 if rbuff[0]-2 <= temp <= rbuff[0]+2:
 
which is a "interval comparison", see 5.9. Comparisons in https://docs.python.org/2/reference/expressions.html
 
instead of (the user is active with same code and different names here):
https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=245261&p=1497335&hilit=count%3D%3D28#p1497335
 
 if temp == chr(ord(rbuff)+1) or temp == chr(ord(rbuff)-1) or temp == chr(ord(rbuff)+2) or temp == chr(ord(rbuff)-2) or temp == chr(ord(rbuff)):
 
Now there is a proposal to use 
  rbuff[0]-2 <= temp and temp<= rbuff[0]+2 instead. Which produces same results.
  
Some test code:
"""
)         
        
x = 3
print("x=", x, " 2 < x < 5       :", 2 < x < 5)
print("x=", x, " 2 < x and x < 5 :", 2 < x and x < 5)

x = 0
print("x=", x, " 2 < x < 5       :", 2 < x < 5)
print("x=", x, " 2 < x and x < 5 :", 2 < x and x < 5)

x = 3
print("x=", x, " 2 < x < 5       :", 2 < x < 5)
print("x=", x, " 2 < x or  x < 5 :", 2 < x or  x < 5)

x = 0
print("x=", x, " 2 < x < 5       :", 2 < x < 5)
print("x=", x, " 2 < x or  x < 5 :", 2 < x or  x < 5)

IMHO the user's program needs some redesign. Using multiple variables count, flag, start in various combinations to control the program makes it difficult to track down what the intended usage is.

arpitbt
Posts: 12
Joined: Wed Jul 10, 2019 8:57 am

Re: My program stuck after sometime

Mon Aug 05, 2019 5:06 am

Hi ghp,

Here i have used flag because these bytes(02 01 04 03) comes in serial and if code detects these then and then only go for a main object which is count=28count so i can catch particular bit which is a main object that i want to process. Start is used here to check whether timer value is 10 or less than 10.If value becomes 10 it makes start 0 so that when object detected again + or - 2 it again start counter so that led again on for 10 seconds.

Here each variable is important.If you have some different logic in mind kindly suggest me.

Return to “Python”