From 2d11ea18ca05f31fcab727fabd68e0b602eacb29 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 2 Jan 2018 15:24:27 +0100 Subject: [PATCH] - added the hardwarelib to handle all the GPIO stuff --- .idea/inspectionProfiles/Project_Default.xml | 12 --- .../Spyder-Controls.odt | Bin lib/hardwarelib.py | 93 ++++++++++++++++++ lib/{servo_2.py => servolib.py} | 0 lib/thermo.py | 8 -- lib/thermolib.py | 8 ++ main.py | 6 +- main_new.py | 9 ++ 8 files changed, 113 insertions(+), 23 deletions(-) delete mode 100644 .idea/inspectionProfiles/Project_Default.xml rename Spyder-Controls.odt => Manuals etc/Spyder-Controls.odt (100%) create mode 100644 lib/hardwarelib.py rename lib/{servo_2.py => servolib.py} (100%) delete mode 100644 lib/thermo.py create mode 100644 lib/thermolib.py create mode 100644 main_new.py diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index abd6203..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - \ No newline at end of file diff --git a/Spyder-Controls.odt b/Manuals etc/Spyder-Controls.odt similarity index 100% rename from Spyder-Controls.odt rename to Manuals etc/Spyder-Controls.odt diff --git a/lib/hardwarelib.py b/lib/hardwarelib.py new file mode 100644 index 0000000..513b06f --- /dev/null +++ b/lib/hardwarelib.py @@ -0,0 +1,93 @@ +from subprocess import call, check_output +from lib import ultrasonic +import RPi.GPIO as GPIO + +class Navigator(): + """Forward Motor with relais, Steering with servo""" + + def __init__(self, mrelaispin): + self.mrelpin = mrelaispin + self.steer = None + GPIO.setup(self.mrelpin, GPIO.OUT) + self.stop() + + + def left(self): + if self.steer != 'left': + call(['python', './lib/servolib.py', 'left']) + self.steer = 'left' + + + def right(self): + if self.steer != 'right': + call(['python', './lib/servolib.py', 'right']) + self.steer = 'right' + + + def straight(self): + if self.steer: + call(['python', './lib/servolib.py']) + self.steer = None + + + def forward(self): + GPIO.output(self.mrelpin, False) + + + def stop(self): + GPIO.output(self.mrelpin, True) + + +class Light: + """Light switched with a relais""" + + def __init__(self, lightpin): + self.pin = lightpin + GPIO.setup(self.pin, GPIO.OUT) + GPIO.output(self.pin, True) + self.shine = False + + + def switch(self): + GPIO.output(self.pin, not self.shine) + self.shine = not self.shine + + + def switch_on(self): + GPIO.output(self.pin, False) + + + def switch_of(self): + GPIO.output(self.pin, True) + + +class Ultrasonic: + """A ultrasonic sensor""" + + def __init__(self, trigger, echo): + self.sensor =ultrasonic.Sensor() + self.sensor.init(trigger, echo) + + + def get_distance(self): + distance = self.sensor.echo() + return distance + + + def __del__(self): + self.sensor.clean() + + +class Temperature: + """A temperature sensor""" + + def get_Temperature(self): + outp = check_output(['python','-u','./lib/thermolib.py']).decode('ISO-8859-1') + temp, hum = outp.split('|') + return temp + + + def get_Humidity(self): + outp = check_output(['python','-u','./lib/thermolib.py']).decode('ISO-8859-1') + temp, hum = outp.split('|') + return hum \ No newline at end of file diff --git a/lib/servo_2.py b/lib/servolib.py similarity index 100% rename from lib/servo_2.py rename to lib/servolib.py diff --git a/lib/thermo.py b/lib/thermo.py deleted file mode 100644 index 7524db8..0000000 --- a/lib/thermo.py +++ /dev/null @@ -1,8 +0,0 @@ -import sys -import Adafruit_DHT - -while True: - - humidity, temperature = Adafruit_DHT.read_retry(11, 14) - - print 'Temp: {} C Humidity: {} %'.format(temperature, humidity) \ No newline at end of file diff --git a/lib/thermolib.py b/lib/thermolib.py new file mode 100644 index 0000000..1e85e3d --- /dev/null +++ b/lib/thermolib.py @@ -0,0 +1,8 @@ +import Adafruit_DHT + +def main(): + humidity, temperature = Adafruit_DHT.read_retry(11, 14) + print '{}|{}'.format(temperature, humidity) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/main.py b/main.py index 01814c8..dfa4fdc 100644 --- a/main.py +++ b/main.py @@ -72,17 +72,17 @@ def reset_dire(): def drive_left(): if not left: - call(['python', './lib/servo_2.py','left']) + call(['python', './lib/servolib.py','left']) return def drive_right(): if not right: - call(['python', './lib/servo_2.py','right']) + call(['python', './lib/servolib.py','right']) return def reset_turn(): if left or right: - call(['python','./lib/servo_2.py']) + call(['python','./lib/servolib.py']) return # ____________________________ # -- special functions diff --git a/main_new.py b/main_new.py new file mode 100644 index 0000000..006cd68 --- /dev/null +++ b/main_new.py @@ -0,0 +1,9 @@ +import pygame, time +from lib import ultrasonic + +def main(): + pygame.display.init() + pass + +if __name__ == '__main__': + main() \ No newline at end of file