Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print Mystery
#1
Smile 
This is my first thread, so please excuse my newbie errors and mistakes Blush Blush
The first print works just fine, but the second print causes the code to hang up and lock up.

 Tell controler to excute the task
	GPIO.output(Run_Pin, GPIO.HIGH) 

#Debug Statement
	print(f" Run Pin >> { GPIO.input(Run_Pin) }")
	print(f" Setting Busy_Pin True { GPIO.input(Busy_Pin)}").
So the question is why is it crashing?

Here is the complete function code

def Run_Task() -> None:
	
# First start the LED flashing thread
#	Stop_LED_Flashing = False
#	Ready_LED_Flashing = Thread(Set_Ready_LED_Flashing)
#	Ready_LED_Flashing.daemon = True
#	Ready_LED_Flashing.start()

# Debug Statement
	print("Run_Task Telling controller to excute task")

# Tell controler to excute the task
	GPIO.output(Run_Pin, GPIO.HIGH) 

#Debug Statement
	print(f" Run Pin >> { GPIO.input(Run_Pin) }")
	print(f" Setting Busy_Pin True { GPIO.input(Busy_Pin)}").

# Wait until stepper controler has started doing task
	while(GPIO,input(Busy_Pin) == False):

#Debug Statement
		print("Waiting Busy Pin >> false")		
		sleep(1) #Wait 1 mS to allow logic controler time to respond 
		
# Now wait until stepper controler has finish the task

#Debug Statment
	print(" Waiting for Busy_Pin to return to false", GPIO,input(Busy_Pin) )	
	while(GPIO,input(Busy_Pin) == True):
#Debug Statement
		print("Waiting Busy Pin >> True")	
		sleep(1)
		
# Control has finshed so return Run pin to false to telling the controller the PI has 	
	GPIO.output(Run_Pin, False)	
	GPIO.output(Abort_Pin, False)
	Stop_LED_Flashing = True

# Debug Statement
	print("Run Task completed task Retuning")
	return
Thanks for sharing your wisdom with me Big Grin Big Grin
Thank you for sharing your wisdom
Reply
#2
What does this mean?
Quote: causes the code to hang up and lock up
Describe what you are observing. Something like:
"My program prints the Run pin message and nothing else happens. The program keeps running until I kill it, but nothing else gets printed.

The posted code will not run. There are multiple syntax errors that prevent it from compiling. On line 38 you have a trailing "."
    print(f" Setting Busy_Pin True { GPIO.input(Busy_Pin)}").
Lines 49 and 50 have a comma separating GPIO and input.
    print(" Waiting for Busy_Pin to return to false", GPIO, input(Busy_Pin))
    while (GPIO, input(Busy_Pin) == True):
In addition to syntax errors, there are logic errors. Do you really want to print a message every millisecond?
    while (GPIO, input(Busy_Pin) == False):
        # Debug Statement
        print("Waiting Busy Pin >> false")
        sleep(1)  # Wait 1 mS to allow logic controler time to respond
False should only ever be used in assignment (is_done = False), never in comparison (is_done == False). GPIO.input() retruns an int, 0 or 1 for an input pin. Is False == 0? False might be defined as the int 0, but did you check? If you want to loop while GPIO.input(Busy_Pin) is low, use == 0, not == False. Or you could use the guaranteed convention that 0 evaluates to False when used in logic. You could write you loop like this:
    while not GPIO.input(Busy_Pin):
        sleep(1)  # Wait 1 mS to allow logic controler time to respond
Functions don't need a return statement:
# Debug Statement
    print("Run Task completed task Retuning")
    return
Use a "return" statement when you want to return a value, or if you want to exit the function early. A return statement at the end of a function is no different than no return statement at all.

Python variable and function naming conventions are described in a document called PEP8. You can look at it here:
https://peps.python.org/pep-0008/

Following the style guidelines will make it easier for others to read your code. It will also help you read code written by others because you will be familiar with the conventions. Some style conventions you did not follow:

Indenting for comments should be the same the code. This is hard for me to read.
# Wait until stepper controler has started doing task
    while(GPIO,input(Busy_Pin) == False):
 
#Debug Statement
        print("Waiting Busy Pin >> false")        
        sleep(1) #Wait 1 mS to allow logic controler time to respond 
The comments obscure the indenting, making the code blocks more difficult to find. This is easier to read.
# Wait until stepper controler has started doing task
    while(GPIO.input(Busy_Pin) == 0):
 
        #Debug Statement
        print("Waiting Busy Pin >> false")        
        sleep(1) #Wait 1 mS to allow logic controller time to respond 
PEP8 suggests using snake case for function and variable names. Capital letters should be saved for constants or class names. In your code Run_Task should be run_task or runtask. Busy_Pin, which I assume is a constant (a variable whose value never changes), should be BUSY_PIN.

Have you looked at gpiozero? I like it more that RPi.GPIO.
Reply
#3
As for my post, yes, you are correct in your message. I let this run overnight without success.
My program prints the Run pin message and nothing else happens.
Thank you for finding my syntax errors Smile

That message every millisecond is a debug message to let me know that section of code is working. Once I debug the code I will remove all the Debug Statment messages. I wish there was a way to return home without a new line. I have tried "\r without success is there a way so this message does not scroll?

OK, thanks for teaching about the "not" in conditional code and "return". I will correct the rest of the code reflecting this info.

Thank you for the other web link. I will study it and make the suggested code changes.

I will also look into using the gpiozero commands. The will be a major rewrite of the code.

My project is a stepper motor controller. This Python program tells the stepper motor controller (Ardino itsy bitsy 32u4 3v) what is required, thus there are a lot of GPIO operations that tell the controller what to do. This function tells the controller all GPIO pins (14 ) are set and ready for the task, so tell the controller to run the task by setting the run pin to logic 1. Then the controller sets the busy pin high. This function then waits for the busy pin to return back to logic low, which the controller has finished the task. When the busy pin returns back to low, this function will set the run pin low.

I hope this explains what I am trying to do.
Thank you for sharing your wisdom
Reply
#4
I just got the same problem in a different function.

It prints the first two Debug Statement print code line, and then nothing happens.

Thus I am feeling it is not a code problem but possibly a bug.

def StepperMotor_Initialize() -> None:

	global app_height
	global IntensitySelect_Pins
	global Push_BUTTON_Pin
	global Stop_LED_Flashing
	


#Debug Point
	print('*************************')
	print("StepperMotor_Initializing")


# First start the LED flashing thread
#	Stop_LED_Flashing = False
#	Ready_LED_Flashing = Thread(Set_Ready_LED_Flashing)
#	Ready_LED_Flashing.daemon = True
#	Ready_LED_Flashing.start()

# Tell the controler to initialize

#Debug Statement 
	print(" Seting Intensity pins")

	GPIO.output(IntensitySelect_Pins, (0,0,0,0))
	GPIO.output(DIR_Pin, False)     # Tells controler what deirection to rotate (CW)
	
# The controller is ready to control the stepper motor

#Debug Statement
	print(" Calling Run Task")

	Run_Task()

	statusbar.config(text="Status: Please Make Selection.")
	
	Initializing_Label.grid_forget()

	toytype_Label.grid(column=0, row = 0, sticky=W)
	orientation_Label.grid(column=0, row = 1, sticky=W)	
	toytype_drop.grid(column = 1, row = 0, padx = 10, pady = 10, sticky='w')
	Orientation_drop.grid(column = 1, row = 1, padx = 10, pady = 10, sticky='w')
	SetAlignment_Button.grid(column = 0, row = 2, columnspan=2, padx=10, pady=10, sticky = "WE")	

	# Update window size
	app_height = 325
	app_width = SetAlignment_Button.winfo_width() + 25
	root.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')

#Debug Point
	print("StepperMotor_Initialized")
I looked at gpiozero but I need all the class names that can be imported from the included file. Where can I find this info?
I found that I can use the button import because there is a push button in the design.
Thank you for sharing your wisdom
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sorted function example mystery sabaidii2 4 3,366 Feb-10-2020, 09:37 AM
Last Post: DeaD_EyE
  Reference counting mystery beezergeezer 4 3,772 Jul-24-2018, 02:37 PM
Last Post: beezergeezer
  Need some help with a simple syntax mystery kverbeeck 3 3,562 Dec-02-2017, 08:46 PM
Last Post: Windspar
  Help with mystery data paulr 4 5,351 Sep-07-2017, 08:02 AM
Last Post: paulr

Forum Jump:

User Panel Messages

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