import Jetson.GPIO as GPIO import time print("Start of Python Script") GPIO.setmode(GPIO.BOARD) GPIO.setup(31, GPIO.OUT) # set pin to high GPIO.output(31, GPIO.HIGH) # let pin be high for 1 sec time.sleep(1) # set pin to low GPIO.output(31, GPIO.LOW) print("Python Script run successfully")
def st(): global output_pin1 global output_pin2 global output_pin3 global output_pin4 tm = 0 global r_index signal=[['R','R','R'],['R','R','R'],['R','R','R'],['R','R','R']] remaining={2:0,3:0,4:0} while (1): time.sleep(1) tm=tm+1 return_value=emergency(tm) if return_value==True: c5=0 while(1): time.sleep(1) #tm=tm+1 c5=c5+1 if c5==10: break ''' road1=[15,15,30,75,312,400,500,500,500,500] road2=[1,1,5,25,5,15,5,5,5,5] road3=[2,1,7,17,65,30,100,100,100,5] road4=[13,13,16,20,15,45,5,5,5,5] ''' road1=[15 ,15 ,30 ,45 ,12 ,4 ,5 ,5 ,5 ,5] road2=[1 ,25 ,5 ,25 ,5 ,15 ,5 ,45 ,5 ,5] road3=[2 ,1 ,7 ,60 ,65 ,30 ,1 ,1 ,80 ,5] road4=[18 ,13 ,16 ,20 ,75 ,45 ,5 ,5 ,5 ,50] ''' road1=[70 ,70 ,70 ,70 ,70 ,5 ,5 ,5 ,5 ,5 ,5] road2=[30 ,30 ,30 ,30 ,30 ,30 ,30 ,30 ,30 ,5 ,5] road3=[10 ,10 ,10 ,10 ,10 ,10 ,10 ,10 ,10 ,10 ,10] road4=[20 ,20 ,20 ,20 ,20 ,20 ,20 ,20 ,20 ,20 ,5] ''' #if 1: try: road_count={1:road1[r_index],2:road2[r_index],3:road3[r_index],4:road4[r_index]} except: break sort_road = sorted(road_count.items(), key=operator.itemgetter(1),reverse=True) #print sort_road t = 240 * (float(sort_road[0][1]))/sum(road_count.values()) #total_time-sum(remaining.values())<=(len(remaining)*20): if tm >= t: print "star" st1() break else: try: del remaining[(sort_road[0][0])] except: r=1 signal=[['R','R','R'],['R','R','R'],['R','R','R'],['R','R','R']] signal[(sort_road[0][0])-1]=['G','G','G'] print signal GPIO.output(output_pin1,GPIO.LOW) GPIO.output(output_pin2,GPIO.LOW) GPIO.output(output_pin3,GPIO.LOW) GPIO.output(output_pin4,GPIO.LOW) if (sort_road[0][0])==1: output_pin1=signal_pins[3] else: output_pin1=signal_pins[1] if (sort_road[0][0])==2: output_pin2=signal_pins[6] else: output_pin2=signal_pins[4] if (sort_road[0][0])==3: output_pin3=signal_pins[9] else: output_pin3=signal_pins[7] if (sort_road[0][0])==4: output_pin4=signal_pins[12] else: output_pin4=signal_pins[10] GPIO.setup(output_pin1, GPIO.OUT) GPIO.setup(output_pin2, GPIO.OUT) GPIO.setup(output_pin3, GPIO.OUT) GPIO.setup(output_pin4, GPIO.OUT) GPIO.setup(output_pin1, GPIO.OUT) GPIO.setup(output_pin2, GPIO.OUT) GPIO.setup(output_pin3, GPIO.OUT) GPIO.setup(output_pin4, GPIO.OUT) GPIO.output(output_pin1,GPIO.HIGH) GPIO.output(output_pin2,GPIO.HIGH) GPIO.output(output_pin3,GPIO.HIGH) GPIO.output(output_pin4,GPIO.HIGH) for i in remaining: remaining[i]=remaining[i]+1 if len(remaining)==0: remaining={1:0,2:0,3:0,4:0} r_index=r_index+1 if tm%20==0: t = (240 - tm) * (float(sort_road[0][1]))/sum(road_count.values()) r_index=r_index+1 #print sum(remaining.values()) print remaining
def white_display(): GPIO.output(COLORS[0], GPIO.HIGH) GPIO.output(COLORS[1], GPIO.LOW)
def open_door(self): GPIO.output(self.bolt_pin, GPIO.HIGH) logging.info("door opening") sleep(0.3) GPIO.output(self.bolt_pin, GPIO.LOW)
def close_door(self): GPIO.output(self.bolt_pin, GPIO.LOW) logging.info("door closing")
def set_mode_reverse(): global mode, modePins modeVal = (0, 1, 0) GPIO.output(modePins, modeVal) mode = "Reverse" brake(10)
def startComplete(self): GPIO.output(7, GPIO.LOW) self.metricCalculation() self.setAutonomousMode('Complete')
def runWaveTrigger(self): GPIO.output(WAVE_TRIG, False) sleep(0.000002) GPIO.output(WAVE_TRIG, True) sleep(0.00001) GPIO.output(WAVE_TRIG, False)
def Disable(self): GPIO.output(self.En_Pin, GPIO.HIGH) print("Disable!")
def setDC(self, status): if status == SHIFT_FORWARD: GPIO.output(DC_ENA, GPIO.HIGH) GPIO.output(DC_IN1, GPIO.HIGH) GPIO.output(DC_IN2, GPIO.LOW) elif status == SHIFT_BACKWARD: GPIO.output(DC_ENA, GPIO.HIGH) GPIO.output(DC_IN1, GPIO.LOW) GPIO.output(DC_IN2, GPIO.HIGH) elif status == SHIFT_STOP: GPIO.output(DC_ENA, GPIO.LOW) GPIO.output(DC_IN1, GPIO.LOW) GPIO.output(DC_IN2, GPIO.LOW)
def setWaveTrig(self, status): GPIO.output(WAVE_TRIG, status)
def backLightONOFF(self, status): GPIO.output(BACKLIGHT, status)
def breakLightONOFF(self, status): GPIO.output(BREAKLIGHT, status)
def headLightONOFF(self, status): GPIO.output(HEADLIGHT, status)
import Jetson.GPIO as GPIO import time as time LED_Pin = 13 GPIO.setmode(GPIO.BOARD) GPIO.setup(LED_Pin, GPIO.OUT) for i in range(2): GPIO.output(LED_Pin, GPIO.HIGH) print "on" time.sleep(1) GPIO.output(LED_Pin, GPIO.LOW) print "off" time.sleep(1) GPIO.cleanup()
def OneStep(self): GPIO.output(self.Step_Pin, GPIO.HIGH) time.sleep(1 / 10000000) GPIO.output(self.Step_Pin, GPIO.LOW)
def set_mode_forward(): global mode, modePins modeVal = (0, 0, 1) GPIO.output(modePins, modeVal) mode = "Forward" brake(10)
def Enable(self): GPIO.output(self.En_Pin, GPIO.LOW) print("Enable!")
def set_mode_off(): global mode, modePins modeVal = (1, 1, 1) GPIO.output(modePins, modeVal) mode = "Off"
try: data = client.recv(1024) if not data: break message = data.decode('utf-8') print("Received: ", message) parts = message.split() if len(parts) == 0 or not useHardware: continue command = parts[0] args = parts[1:] if command == "set": if len(args) == 1: color = args[0] if color == "BLEU": GPIO.output(yellow_pin, GPIO.LOW) GPIO.output(blue_pin, GPIO.HIGH) elif color == "JAUNE": GPIO.output(yellow_pin, GPIO.HIGH) GPIO.output(blue_pin, GPIO.LOW) else: GPIO.output((yellow_pin, blue_pin), GPIO.LOW) else: GPIO.output((yellow_pin, blue_pin), GPIO.LOW) elif command == "score": if len(args) == 1: nb = int(args[0]) nb4 = nb % 10 nb //= 10 nb3 = nb % 10
initial=LED_OUPUT_Status) # initial 為針腳設定預設值,設定輸出針腳同時給予低電位 GPIO.setup(LED_OUTPUT2_Num, GPIO.OUT, initial=LED_OUPUT2_Status) GPIO.setup(LED_INPUT_Num, GPIO.IN) # 設定輸入針腳 print("LED_OUPUT_Status=" + str(LED_OUPUT_Status)) print("LED_OUPUT2_Status=" + str(LED_OUPUT2_Status)) # setup()語法:GPIO.setup({channel | chan_list}, {GPIO.OUT | GPIO.IN}, [initial= {GPIO.HIGH | GPIO.LOW}]) while True: if (GPIO.input(LED_INPUT_Num) ): # input(),當針腳已被設定高低電位時,可以用此函式取得該針腳的當下狀態為何。若偵測輸入為高電位 time.sleep(1) LED_OUPUT_Status = not LED_OUPUT_Status # ~:正、負、倒數。輸出電位由低變高 LED_OUPUT2_Status = not LED_OUPUT2_Status GPIO.output( LED_OUTPUT_Num, LED_OUPUT_Status) # GPIO.output(channel,GPIO.HIGH),輸出腳位變高電位 GPIO.output(LED_OUTPUT2_Num, LED_OUPUT2_Status) print("LED_OUPUT_Status=" + str(LED_OUPUT_Status)) print("LED_OUPUT2_Status=" + str(LED_OUPUT2_Status)) # if( GPIO.input(LED_INPUT_Num) ): # LED_OUPUT2_Status = ~LED_OUPUT2_Status # GPIO.output(LED_OUTPUT2_Num, LED_OUPUT2_Status) # time.sleep(0.001) #LED_OUTPUT2_Num = 19 # 輸出腳位 #LED_OUPUT2_Status = GPIO.HIGH #GPIO.setup(LED_OUTPUT2_Num, GPIO.OUT, initial = LED_OUPUT2_Status) #LED_OUPUT2_Status = ~LED_OUPUT2_Status
def tLeft(dts=1, verbose=False): if verbose: print('Drive the motor Left') # Motor A: GPIO.output(IN2L, GPIO.LOW) # Set AIN1 GPIO.output(IN1L, GPIO.HIGH) # Set AIN2 # Motor B: GPIO.output(IN1R, GPIO.LOW) # Set BIN1 GPIO.output(IN2R, GPIO.HIGH) # Set BIN2 # Set the motor speed # Motor A: GPIO.output(PWML, GPIO.HIGH) # Set PWMA # Motor B: GPIO.output(PWMR, GPIO.HIGH) # Set PWMB # Wait 5 seconds time.sleep(dts) stop()
def keep_door_opening(self): GPIO.output(self.bolt_pin, GPIO.HIGH) logging.info("door opening")
def stop(verbose=False): if verbose: print('stopping...') # Reset all the GPIO pins by setting them to LOW GPIO.output(IN1L, GPIO.LOW) # Set AIN1 GPIO.output(IN2L, GPIO.LOW) # Set AIN2 GPIO.output(PWML, GPIO.LOW) # Set PWMA GPIO.output(IN2R, GPIO.LOW) # Set BIN1 GPIO.output(IN1R, GPIO.LOW) # Set BIN2 GPIO.output(PWMR, GPIO.LOW) # Set PWMB
if tm >= t: print "star" st() break else: try: del remaining[(sort_road[0][0])] except: r=1 signal=[['R','R','R'],['R','R','R'],['R','R','R'],['R','R','R']] signal[(sort_road[0][0])-1]=['G','G','G'] print signal GPIO.output(output_pin1,GPIO.LOW) GPIO.output(output_pin2,GPIO.LOW) GPIO.output(output_pin3,GPIO.LOW) GPIO.output(output_pin4,GPIO.LOW) if (sort_road[0][0])==1: output_pin1=signal_pins[3] else: output_pin1=signal_pins[1] if (sort_road[0][0])==2:
from smbus import SMBus import Jetson.GPIO as GPIO from time import sleep addr = 0x8 # bus address bus = SMBus(0) # indicates /dev/ic2-0 controlPin = 11 def init_rpi(): GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(controlPin, GPIO.OUT, initial = GPIO.LOW) init_rpi() while True: try: num = [69,127,254] GPIO.output(controlPin,GPIO.LOW) bus.write_block_data(addr,6,num) GPIO.output(controlPin, GPIO.HIGH) sleep(0.01) except Exception as e: print(e) pass
def violet_display(): GPIO.output(COLORS[0], GPIO.LOW) GPIO.output(COLORS[1], GPIO.HIGH)
# you need to guess this number if __name__ == '__main__': while True: #顏色設定 color = (0, 0, 0) #攝影機設定 cap = cv2.VideoCapture(r"/dev/video2") a = 0 _, frame = cap.read() #读取一帧视频 try: if GPIO.input(40) == 1: raise OBSTACLE_DETECTION GPIO.output(straight, GPIO.HIGH) GPIO.output(left, GPIO.HIGH) GPIO.output(right, GPIO.HIGH) print('go.....') #人臉偵測的路徑 cascade_path = r'haarcascade_frontalface_alt2.xml' image = exposure.adjust_gamma(frame, 0.5) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #使用人脸识别分类器,读入分类器 cascade = cv2.CascadeClassifier(cascade_path) #利用分类器识别出哪个区域为人脸 faceRects = cascade.detectMultiScale(image, scaleFactor=1.2,
def green_display(): GPIO.output(COLORS[0], GPIO.HIGH) GPIO.output(COLORS[1], GPIO.HIGH)
def setLED(stat): GPIO.output(33, stat)