#!/usr/bin/env python # coding: Latin-1 #import statements for required libaarys #Code Written By J Hider import threading import PicoBorgRev import time import sys #set up the piborg boards the PBR = PicoBorgRev.PicoBorgRev() PBR.busNumber = 0 PBR.i2cAddress = 0xA PBR.Init() PBR.ResetEpo() PBR2 = PicoBorgRev.PicoBorgRev() PBR2.busNumber = 0 PBR2.i2cAddress = 0x14 PBR2.Init() PBR2.ResetEpo() #PBR3 = PicoBorgRev.PicoBorgRev() #PBR3.i2cAddress = 12 #PBR3.Init() #PBR3.ResetEpo() #Global BVariables for ride control sleepTime = 10 cakeWalkRunning = False ferrisWheelRunning = False merryGoRoundRunning = False swingBoatRunning = False mLock = threading.Lock() loopRun = True class cakeWalk(threading.Thread): 'Controls cake walk' def __init__(self,PBR): threading.Thread.__init__(self) self.PBR = PBR self.stepDelay = 5 self.sleepTime = 60 self.rampUpFwd = [ +0.27,+0.28,+0.29, +0.3, +0.31, +0.32, +0.33, +0.34, +0.35] self.rampDownFwd = [+0.35, +0.34, +0.33, +0.32, +0.31, +0.3, +0.29, +0.28, +0.27] def run(self): global cakeWalkRunning global loopRun if cakeWalkRunning == False and loopRun == True: mLock.acquire() cakeWalkRunning = True mLock.release() for step in self.rampUpFwd: self.PBR.SetMotor1(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == True: time.sleep(self.sleepTime) else: print(''); if cakeWalkRunning == True and loopRun == True: mLock.acquire() cakeWalkRunning = False mLock.release() for step in self.rampDownFwd: self.PBR.SetMotor1(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == False: self.PBR.SetMotor1(0) print ('cWalk Stopping') class ferrisWheel(threading.Thread): 'Controls ferris wheel' def __init__(self,PBR): threading.Thread.__init__(self) self.PBR = PBR self.stepDelay = 3 self.sleepTime = 180 self.rampUpFwd = [ +0.0, +0.1, +0.15, +0.2, +0.25, +0.3, +0.35] self.rampDownFwd = [+0.35, +0.3, +0.25, +0.2, +0.15, +0.1, +0.0] def run(self): global ferrisWheelRunning global loopRun if ferrisWheelRunning == False and loopRun == True: mLock.acquire() ferrisWheelRunning = True mLock.release() for step in self.rampUpFwd: self.PBR.SetMotor1(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == True: time.sleep(self.sleepTime) else: print(''); if ferrisWheelRunning == True and loopRun == True: mLock.acquire() ferrisWheelRunning = False mLock.release() for step in self.rampDownFwd: self.PBR.SetMotor1(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == False: self.PBR.SetMotor1(0) print('Ferris Stopping') class merryGoRound(threading.Thread): 'Controls the merrygoround ride' def __init__(self, PBR): threading.Thread.__init__(self) self.PBR = PBR self.stepDelay = 5 self.sleepTime = 60 self.rampUpFwd = [ +0.0, +0.49, +0.50, +0.51, +0.52] self.rampDownFwd = [ +0.52, +0.51, +0.50, +0.49, +0.0] def run(self): global merryGoRoundRunning global loopRun if merryGoRoundRunning == False and loopRun == True: mLock.acquire() merryGoRoundRunning = True mLock.release() for step in self.rampUpFwd: self.PBR.SetMotor2(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == True: time.sleep(self.sleepTime) else: print(''); if merryGoRoundRunning == True and loopRun == True: mLock.acquire() merryGoRoundRunning = False mLock.release() for step in self.rampDownFwd: self.PBR.SetMotor2(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == False: self.PBR.SetMotor2(0) print('Mgr Stopping'); class swingBoat(threading.Thread): 'Controls Swing Boat' def __init__(self, PBR): threading.Thread.__init__(self) self.PBR = PBR self.stepDelay = 5 self.sleepTime = 60 self.rampUpRvs = [ -0.0,-0.5, -0.6, -0.7, -0.8, -0.9, -1.0] self.rampDownRvs = [-1.0, -0.9, -0.8, -0.7, -0.6, -0.5,-0.0] def run(self): global swingBoatRunning global loopRun if swingBoatRunning == False and loopRun == True: mLock.acquire() swingBoatRunning = True mLock.release() for step in self.rampUpRvs: self.PBR.SetMotor2(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == True: time.sleep(self.sleepTime) else: print(''); if swingBoatRunning == True: mLock.acquire() swingBoatRunning = False mLock.release() for step in self.rampDownRvs: self.PBR.SetMotor2(step) print ('%+.2f' % (step)); time.sleep(self.stepDelay) if loopRun == False: break if loopRun == False: self.PBR.SetMotor2(0) print('Swing Stopping'); #main thread #if __name__ =='__main__': print ('Press CTRL C to Finish'); print ('loopRun %s' % (loopRun)); try: while loopRun: if cakeWalkRunning == False: cWK = cakeWalk(PBR) cWK.start() time.sleep(sleepTime) if ferrisWheelRunning == False: fWL = ferrisWheel(PBR2) fWL.start() time.sleep(sleepTime) if swingBoatRunning == False: sBR = swingBoat(PBR) sBR.start() time.sleep(sleepTime) if merryGoRoundRunning == False: mGR = merryGoRound(PBR2) mGR.start() time.sleep(sleepTime) except KeyboardInterrupt: #stop all motors print ('Keyboard Interupt caught'); loopRun = False print('stop motors'); PBR.MotorsOff() PBR2.MotorsOff() sys.exit()