Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4890

Python • A script written in Python doesn't behave consistently depending on how/where it is run

$
0
0
System: Pi-4 (8g) running Buster on a GoPiGo-3 robot.

Background:
I have a script that I (attempt) to run during startup, (as a cron job), that shakes and centers the pan-and-tilt that holds the robot's "head". This is done to indicate that the O/S has finished loading and the robot is ready for use, especially if I am running the robot remotely without a monitor.

Script used:

Code:

#!/usr/bin/python3.7import easygopigo3 as easyimport time# Instantiate easy GoPiGo librarieseasy_gpg = easy.EasyGoPiGo3()servo_1 = easy_gpg.init_servo('SERVO1')servo_2 = easy_gpg.init_servo('SERVO2')sleep_time = 0.50  # pause time in secondsprint("Running: Test Servos.\n")# Read the connected robot's serial numberserial_number = easy_gpg.get_id() # read and display the serial number# For testing only - "invalid" serial number# serial_number = "invalid"print("This robot's serial number is\n"+serial_number+"\n")# Servo constants for each robot# Each robot is identified by its unique serial number# Charlene:if serial_number == "A0F6E45F4E514B4B41202020FF152B11":# Charlene's servo constants    print("Robot is \"Charlene\"")    center_1 = 86  # Horizontal centering - smaller is further right.    center_2 = 76  # Vertical centering - smaller is further up.# Charlie:elif serial_number == "64B61037514E343732202020FF111A05":# Charlie's servo constants    print("Robot is \"Charlie\"")    center_1 = 86  # Horizontal centering - smaller is further right.    center_2 = 90  # Vertical centering - smaller is further up.else:# Unknown serial number    print("I don't know who robot", serial_number, "is,")    print("If we got this far, it's obviously a GoPiGo robot")    print("But I don't know what robot it is, so I'm using")    print("the default centering constants of 90/90.\n")# Default servo constants    print("Robot is \"Unknown\"")    print("Please record the robot's serial number, name,")    print("and derived centering constants.")    center_1 = 90    center_2 = 90# Start Test Servos# Define excursionsright = center_1 - 45left = center_1 + 45up = center_2 - 45down = center_2 + 45# Test servosprint("\nStarting test:")print("Using centering constants "+str(center_1)+"/"+str(center_2), "for this robot")print("\nCenter Both Servos")servo_1.rotate_servo(center_1)servo_2.rotate_servo(center_2)time.sleep(sleep_time)print("Test Servo 1 (horizontal motion)")servo_1.rotate_servo(right)time.sleep(sleep_time)servo_1.rotate_servo(left)time.sleep(sleep_time)servo_1.rotate_servo(center_1)time.sleep(sleep_time)print("Test Servo 2 (vertical motion)")servo_2.rotate_servo(up)time.sleep(sleep_time)servo_2.rotate_servo(down)time.sleep(sleep_time)print("Re-Center Both Servos")servo_1.rotate_servo(center_1)servo_2.rotate_servo(center_2)time.sleep(sleep_time)servo_1.disable_servo()servo_2.disable_servo()print("Complete: Test Servos - Exiting.")
Problem:
Depending on how the script is run, the script either runs successfully, or generates an "indentation" error.

====================================
Running on Thonny.
Successful run with the following output:

Code:

Python 3.7.3 (/usr/bin/python3.7)>>> %cd /home/pi/Project_Files/startup_scripts>>> %Run test_servos.shRunning: Test Servos.This robot's serial number isA0F6E45F4E514B4B41202020FF152B11Robot is "Charlene"Starting test:Using centering constants 86/76 for this robotCenter Both ServosTest Servo 1 (horizontal motion)Test Servo 2 (vertical motion)Re-Center Both ServosComplete: Test Servos - Exiting.>>> 
===================================
Running using Geany:
Successful run with the following output in a terminal window:

Code:

Running: Test Servos.This robot's serial number isA0F6E45F4E514B4B41202020FF152B11Robot is "Charlene"Starting test:Using centering constants 86/76 for this robotCenter Both ServosTest Servo 1 (horizontal motion)Test Servo 2 (vertical motion)Re-Center Both ServosComplete: Test Servos - Exiting.------------------(program exited with code: 0)Press return to continue
===============================
Run by double-clicking on the script in a file-manager window:
Program runs successfully though there is no output.

===============================
Run on startup as part of the system crontab:
Runs successfully.

================================
Run by opening a terminal window into the directory where the script lives and running it from there via the command line:
It fails with the following message:

Code:

pi@CharlineWiFi:~/Project_Files/startup_scripts $ ./test_servos.shTraceback (most recent call last):  File "./test_servos.sh", line 3, in <module>    import easygopigo3 as easy  File "/home/pi/Test_Libraries/easygopigo3.py", line 13, in <module>    import easysensors  File "/home/pi/Test_Libraries/easysensors.py", line 5, in <module>    import gopigo3  File "/home/pi/Test_Libraries/gopigo3.py", line 363    def load_robot_constants(self, config_file_path="/home/pi/Dexter/gpg3_config.json"):                                                                                       ^IndentationError: unindent does not match any outer indentation levelpi@CharlineWiFi:~/Project_Files/startup_scripts $ 
Questions:
1. Why does it fail when run directly through a terminal window and succeed when run any other way, including double-clicking?
2. How do I determine which instance of a library is being used, prior to it failing?
3. If that library fails in the way described here, why doesn't the entire robot fail since that library is a fundamental requirement for any robot functionality to work? (i.e. The entire robot depends on that library and the entire robot wouldn't work if it failed with that error every time it wasn't run within an IDE like Thonny or Geany.)

Statistics: Posted by jharris1993 — Fri Nov 15, 2024 7:24 pm



Viewing all articles
Browse latest Browse all 4890

Trending Articles