class GPIO(): def __init__(self): pass def __enter__(self): self.GP = GPIOProcessor() self.forward_pin = self.GP.getPin25() self.forward_pin.out() self.right_high = self.GP.getPin26() self.right_high.out() self.right_low = self.GP.getPin27() self.right_low.out() self.left_high = self.GP.getPin29() self.left_high.out() self.left_low = self.GP.getPin30() self.left_low.out() return self def __exit__(self, exc_type, exc_val, exc_tb): self.GP.cleanup() def brake(self): self.forward_pin.low() def unbrake(self): self.forward_pin.high() def turn_left(self): self.left_low.low() self.left_high.high() def turn_right(self): self.right_low.low() self.right_high.high()
class DC: def __init__(self): """ DC maps DragonBoard 410c GPIO pins for Debian to the L293N H-Bridge I/C, ready to receive control messages from Manager.py. Green wire : Pin 23 -> OpAmp Node 1 -> L298N h-bridge Input 2 White wire : Pin 24 -> OpAmp Node 4 -> L298N h-bridge Enable A Yellow wire : Pin 26 -> OpAmp Node 3 -> L298N h-bridge Input 1 Set Input 2 to Low, Input 1 to High for clockwise spin. """ self.gp = GPIOProcessor() # h-bridge self.green = self.gp.getPin23() self.white = self.gp.getPin24() self.yellow = self.gp.getPin26() self.green.out() self.white.out() self.yellow.out() # clockwise self.green.low() # Input 2 self.yellow.high() # Input 1 self.isPropSpinning = False def start_motor(self): """ Set Enable A to High to start motor. :rtype: Boolean :return: True if motor started, False if failed. """ try: self.white.low() self.white.high() self.isPropSpinning = True return True except KeyboardInterrupt(): print "Keyboard interrupt received. Cleaning up ..." self.gp.cleanup() return False def stop_motor(self): """ Set Enable A to Low to stop motor. :rtype: Boolean :return: True if motor stopped, False if failed. """ try: self.white.low() self.isPropSpinning = False return True except KeyboardInterrupt(): print "Keyboard interrupt received. Cleaning up ..." self.gp.cleanup()
if resetting: if steps_made == 0: resetting = False continue elif steps_made < 0: direction = 1 else: direction = -1 else: if reset.getValue(): resetting = True continue if not (right.getValue() or left.getValue()): continue elif right.getValue(): direction = 1 elif left.getValue(): direction = -1 pattern = sequence[i] for j, p in enumerate(pins): if pattern[j]: p.high() steps_made += direction i = (i + direction) % len(sequence) finally: GP.cleanup()
while True: print 'Enter degrees:' x = input() if x < 0: FR = 1 x = abs(x) else: FR = 0 x = int(x/SA) # Run Stepper Motor sequencen for i in range(0,x): A1.setValue(SS[FR][i%4][0]) time.sleep(T) B1.setValue(SS[FR][i%4][1]) time.sleep(T) A2.setValue(SS[FR][i%4][2]) time.sleep(T) B2.setValue(SS[FR][i%4][3]) time.sleep(T) print 'again? [y/n]' r = raw_input() if r == 'n': break finally: GP.cleanup()
time.sleep(2) else: print "Starting motor ..." prop.start_motor() counter += 1 # ======================== # Individual component tests ... # ====================== # print "Testing Shallow Water Sensor now ..." # counter = 0 # while counter <= 10: # sws.check_depth() # print "Depth :", sws.get_depth() # counter += 1 # gp.cleanup() # ======================= # if prop.start_motor(): # print "Testing motor by running for 5 seconds ..." # time.sleep(5) # prop.stop_motor() # gp.cleanup() # else: # print "Motor NOT running! Test failed." # ======================== except KeyboardInterrupt(): print "Keyboard interrupt received. Cleaning up ..." gp.cleanup()
class StepperMotor: # Stepper motor switching sequence seq = [[1,0,0,0], [1,1,0,0], [0,1,0,0], [0,1,1,0], [0,0,1,0], [0,0,1,1], [0,0,0,1], [1,0,0,1]] def __init__(self, pin1=24, pin2=28, pin3=25, pin4=33): """Initialize the interface passing the four GPIO pins to connect to. Be sure to subtract 902 from the pin out diagram. For example, GPIO pin 29 corresponds to pin out 926. To use this pin pass 926-902 or 24 """ self.GP = GPIOProcessor() self.pin1 = self.GP.getPin(pin1) self.pin2 = self.GP.getPin(pin2) self.pin3 = self.GP.getPin(pin3) self.pin4 = self.GP.getPin(pin4) self.pinl = [self.pin1, self.pin2, self.pin3, self.pin4] for k in range(4): self.pinl[k].out() self.speed = 100.0 @property def speed(self): return self.__speed @speed.setter def speed(self,speed): if speed < 0: self.__speed = 0.0 elif speed > 100: self.__speed = 100.0 else: self.__speed = float(speed) def cleanup(self): """cleanup must be the last call to the class so that all GPIO pins are properly released. """ self.GP.cleanup() def move(self, degrees=360): """Move the stepper motor shaft dgrees. If degrees is negative the shaft will spin in reverse. """ pinl = list(self.pinl) if degrees < 0: pinl.reverse() degrees = abs(degrees) steps_per_deg =4076.0/360.0 steps = int(steps_per_deg * degrees) delay = 0.03 / self.speed for k in range(steps): seq_inx = k % 8 # print("seq_inx= {0:2d}".format(seq_inx)) for j in range(4): # print("\t Pin= {0:d}".format(j)) # print("\t Value= {0:d}".format(StepperMotor.seq[seq_inx][j])) pinl[j].setValue(StepperMotor.seq[seq_inx][j]) time.sleep(delay)
class UltrasonicHCSR04: """ Sensor Class maps DragonBoard 410c GPIO pins for Debian to the HC-SR04 ultra-sonic sensor. Approximate Speed of Sound through Air at 20 degrees centigrade 343 metres per second. For "Shallow Water Sensor" Approximate Speed of Sound through Water at 15 degrees centigrade 1464 metres per second. """ def __init__(self): self.speed = 343 # metres per second (m/s) through Air # self.speed = 1464 # metres per second (m/s) through Water self.depth = 0 """ Trig (blue wire) : Pin 33 -> OpAmp Node 2 -> Ultra-sonic sensor Trig Echo (red wire) : Ultra-sonic sensor Echo -> Pin 30 """ self.gp = GPIOProcessor() self.trig = self.gp.getPin33() self.echo = self.gp.getPin34() self.trig.out() self.echo.input() def get_depth(self): return self.depth def set_depth(self, new_depth): self.depth = new_depth def check_depth(self): """ Activate the sensor to send sound ping (Trig) and receive the echo (Echo) underwater, measuring the time interval and calculating and storing the value in depth. """ try: print "Activating Shallow Water Sensor ..." self.trig.low() # time.sleep(0.000002) # at least 2 micro-seconds time.sleep(0.5) # At least 2 micro-seconds. 0.5 tested and working. self.trig.high() time.sleep(0.0001) # At least 5 micro-seconds. 100 tested and working. self.trig.low() print "Pulse sent." # defining variables pulse_start = time.time() pulse_end = time.time() # Wait for pulse to be sent, then # save start time. # Note : Adding counter to break while loop due to weird glitch. counter = 10000 while self.echo.getValue() == 0 and counter > 0: pulse_start = time.time() counter -= 1 # print "counter :", counter if self.echo.getValue() == 1: print "Received echo." while self.echo.getValue() == 1: pulse_end = time.time() if counter == 0: print "No echo received for long period. Return to Manager and start again." self.set_depth(0) return False # Calculate total pulse duration # print "pulse_end time :", pulse_end # print "pulse_start time : ", pulse_start pulse_duration = pulse_end - pulse_start # print "pulse_duration time :", pulse_duration # Use pulse duration to calculate distance # Remember that the pulse has to go there and come back distance = round((pulse_duration * self.speed) / 2, 2) # print "distance :", distance self.set_depth(distance) return True except KeyboardInterrupt(): print "Keyboard interrupt received. Cleaning up ..." self.gp.cleanup() return False
class StepperMotor: # Stepper motor switching sequence seq = [[1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 1]] def __init__(self, pin1=24, pin2=28, pin3=25, pin4=33): """Initialize the interface passing the four GPIO pins to connect to. Be sure to subtract 902 from the pin out diagram. For example, GPIO pin 29 corresponds to pin out 926. To use this pin pass 926-902 or 24 """ self.GP = GPIOProcessor() self.pin1 = self.GP.getPin(pin1) self.pin2 = self.GP.getPin(pin2) self.pin3 = self.GP.getPin(pin3) self.pin4 = self.GP.getPin(pin4) self.pinl = [self.pin1, self.pin2, self.pin3, self.pin4] for k in range(4): self.pinl[k].out() self.speed = 100.0 @property def speed(self): return self.__speed @speed.setter def speed(self, speed): if speed < 0: self.__speed = 0.0 elif speed > 100: self.__speed = 100.0 else: self.__speed = float(speed) def cleanup(self): """cleanup must be the last call to the class so that all GPIO pins are properly released. """ self.GP.cleanup() def move(self, degrees=360): """Move the stepper motor shaft dgrees. If degrees is negative the shaft will spin in reverse. """ pinl = list(self.pinl) if degrees < 0: pinl.reverse() degrees = abs(degrees) steps_per_deg = 4076.0 / 360.0 steps = int(steps_per_deg * degrees) delay = 0.03 / self.speed for k in range(steps): seq_inx = k % 8 # print("seq_inx= {0:2d}".format(seq_inx)) for j in range(4): # print("\t Pin= {0:d}".format(j)) # print("\t Value= {0:d}".format(StepperMotor.seq[seq_inx][j])) pinl[j].setValue(StepperMotor.seq[seq_inx][j]) time.sleep(delay)