From 0fe6f542246dd150b4793df18253ed6d4a443264 Mon Sep 17 00:00:00 2001 From: Raspberry Pi Date: Mon, 2 Dec 2019 20:27:29 -0600 Subject: Update test files to do GPIO cleanup on completion (ex: encoders don't work if GPIO.cleanup is not performed). Add system destructor and add GPIO.cleanup to that too. Everything appears to be working still. --- System/initialize_system.py | 2 ++ System/system.py | 9 ++++++++- System/test_Encoder.py | 13 ++++++++++--- System/test_Motor.py | 28 +++++++++++++++++----------- 4 files changed, 37 insertions(+), 15 deletions(-) (limited to 'System') diff --git a/System/initialize_system.py b/System/initialize_system.py index 4eb287a..6df880f 100755 --- a/System/initialize_system.py +++ b/System/initialize_system.py @@ -1,7 +1,9 @@ # This file should be run on system startup. It will initialize the linear position to the center so that all tests originate from a proper position. # The center is found by using the hardware limit switches from system import System +import RPi.GPIO as GPIO # Main program sys = System() sys.initialize() +GPIO.cleanup() diff --git a/System/system.py b/System/system.py index 8dd9f38..7b2ffcf 100755 --- a/System/system.py +++ b/System/system.py @@ -20,7 +20,7 @@ encoder_data_pin = 3 ### Angular encoder pins encoder_angular_cs_pin = 4 ### Linear encoder pins -encoder_linear_cs_pin = 14 +encoder_linear_cs_pin = 14 ### Limit switch pins (configured to PULLUP) limit_negative_pin = 19 limit_positive_pin = 26 @@ -89,6 +89,13 @@ class System: self.encoder_thread.start() # END __init__() + # Destructor + # Brake the motor and call GPIO.cleanup as a last-chance of doing so + def __del__(self): + self.motor.brake() + GPIO.cleanup() + # END __del__() + def initialize(self): # Temporarily disable the limit switch interrupts: we do not want the program to exit if the switch is triggered GPIO.remove_event_detect(limit_negative_pin) diff --git a/System/test_Encoder.py b/System/test_Encoder.py index b667cc1..d0b7ef8 100755 --- a/System/test_Encoder.py +++ b/System/test_Encoder.py @@ -1,5 +1,6 @@ from encoder import Encoder import time +import RPi.GPIO as GPIO # Decide which pins to hook up to on the Pi before running clk_pin = 2 @@ -9,6 +10,12 @@ data_pin = 3 e = Encoder(clk_pin, cs_pin, data_pin) e.set_zero() -while(1): - print(e.read_position('Degrees')) - time.sleep(0.001) \ No newline at end of file +try: + while(1): + print(e.read_position('Degrees')) + time.sleep(0.001) +except: + print("Program killed by Ctrl-C") +finally: + # Perform GPIO cleanup. Things may get weird otherwise... + GPIO.cleanup() diff --git a/System/test_Motor.py b/System/test_Motor.py index 80cff1e..f48b6ee 100755 --- a/System/test_Motor.py +++ b/System/test_Motor.py @@ -1,5 +1,6 @@ from motor import Motor import time +import RPi.GPIO as GPIO # Decide which pins to hook up to on the Pi before running speed_pin = 17 @@ -10,14 +11,19 @@ m = Motor(speed_pin, forward_pin, reverse_pin) dir = 'ascending' speed = 0.0 -while 1: - if speed >= 15.0: - dir = 'descending' - elif speed <= -15.0: - dir = 'ascending' - if dir == 'ascending': - speed = speed + 2.0 - else: - speed = speed - 2.0 - m.move(speed) - time.sleep(0.1) \ No newline at end of file +try: + while 1: + if speed >= 15.0: + dir = 'descending' + elif speed <= -15.0: + dir = 'ascending' + if dir == 'ascending': + speed = speed + 2.0 + else: + speed = speed - 2.0 + m.move(speed) + time.sleep(0.1) +except: + print("Program killed by Ctrl-C") +finally: + GPIO.cleanup() \ No newline at end of file -- cgit v1.2.3