From 95408c053ca611ca3b617e8c3a5b9ebe25a7b853 Mon Sep 17 00:00:00 2001 From: Benjamin Porter Date: Sun, 12 Apr 2020 20:41:13 -0600 Subject: [PATCH 1/3] Fix mixed tabs and spaces Most of the code is indented with 4 spaces but there are a few lines that use tabs. This is super risky in python since it is whitespace sensitive. This commit removes the tabs and replaces with spaces --- 4.Code/python/KeyScanStart.py | 22 +++++----- 4.Code/python/ServoControlColorVersion1.py | 50 +++++++++++----------- 4.Code/python/bluetooth_control.py | 2 +- 4.Code/python/infrared_follow.py | 6 +-- 4.Code/python/light_follow.py | 8 ++-- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/4.Code/python/KeyScanStart.py b/4.Code/python/KeyScanStart.py index 807c721..8c7cb2c 100644 --- a/4.Code/python/KeyScanStart.py +++ b/4.Code/python/KeyScanStart.py @@ -114,11 +114,11 @@ def key_scan(): pass while not GPIO.input(key): time.sleep(0.01) - if not GPIO.input(key): - time.sleep(0.01) - while not GPIO.input(key): - pass -#delay 2s + if not GPIO.input(key): + time.sleep(0.01) + while not GPIO.input(key): + pass +#delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -128,12 +128,12 @@ def key_scan(): key_scan() while True: run(1) - back(1) - left(2) - right(2) - spin_left(3) - spin_right(3) - brake(1) + back(1) + left(2) + right(2) + spin_left(3) + spin_right(3) + brake(1) except KeyboardInterrupt: pass pwm_ENA.stop() diff --git a/4.Code/python/ServoControlColorVersion1.py b/4.Code/python/ServoControlColorVersion1.py index 8c657f0..c52e570 100644 --- a/4.Code/python/ServoControlColorVersion1.py +++ b/4.Code/python/ServoControlColorVersion1.py @@ -29,49 +29,49 @@ def init(): pwm_servo = GPIO.PWM(ServoPin, 50) pwm_servo.start(0) -def corlor_light(pos): +def color_light(pos): if pos > 150: GPIO.output(LED_R, GPIO.HIGH) - GPIO.output(LED_G, GPIO.LOW) - GPIO.output(LED_B, GPIO.LOW) + GPIO.output(LED_G, GPIO.LOW) + GPIO.output(LED_B, GPIO.LOW) elif pos > 125: - GPIO.output(LED_R, GPIO.LOW) - GPIO.output(LED_G, GPIO.HIGH) - GPIO.output(LED_B, GPIO.LOW) + GPIO.output(LED_R, GPIO.LOW) + GPIO.output(LED_G, GPIO.HIGH) + GPIO.output(LED_B, GPIO.LOW) elif pos >100: GPIO.output(LED_R, GPIO.LOW) - GPIO.output(LED_G, GPIO.LOW) - GPIO.output(LED_B, GPIO.HIGH) + GPIO.output(LED_G, GPIO.LOW) + GPIO.output(LED_B, GPIO.HIGH) elif pos > 75: - GPIO.output(LED_R, GPIO.HIGH) - GPIO.output(LED_G, GPIO.HIGH) - GPIO.output(LED_B, GPIO.LOW) + GPIO.output(LED_R, GPIO.HIGH) + GPIO.output(LED_G, GPIO.HIGH) + GPIO.output(LED_B, GPIO.LOW) elif pos > 50: - GPIO.output(LED_R, GPIO.LOW) - GPIO.output(LED_G, GPIO.HIGH) - GPIO.output(LED_B, GPIO.HIGH) + GPIO.output(LED_R, GPIO.LOW) + GPIO.output(LED_G, GPIO.HIGH) + GPIO.output(LED_B, GPIO.HIGH) elif pos > 25: GPIO.output(LED_R, GPIO.HIGH) - GPIO.output(LED_G, GPIO.LOW) - GPIO.output(LED_B, GPIO.HIGH) + GPIO.output(LED_G, GPIO.LOW) + GPIO.output(LED_B, GPIO.HIGH) elif pos > 0: GPIO.output(LED_R, GPIO.HIGH) - GPIO.output(LED_G, GPIO.HIGH) - GPIO.output(LED_B, GPIO.HIGH) + GPIO.output(LED_G, GPIO.HIGH) + GPIO.output(LED_B, GPIO.HIGH) else : GPIO.output(LED_R, GPIO.LOW) - GPIO.output(LED_G, GPIO.LOW) - GPIO.output(LED_B, GPIO.LOW) + GPIO.output(LED_G, GPIO.LOW) + GPIO.output(LED_B, GPIO.LOW) def servo_control_color(): for pos in range(181): pwm_servo.ChangeDutyCycle(2.5 + 10 * pos/180) - corlor_light(pos) - time.sleep(0.009) + color_light(pos) + time.sleep(0.009) for pos in reversed(range(181)): pwm_servo.ChangeDutyCycle(2.5 + 10 * pos/180) - corlor_light(pos) - time.sleep(0.009) + color_light(pos) + time.sleep(0.009) #delay 2s time.sleep(2) @@ -82,7 +82,7 @@ def servo_control_color(): init() pwm_servo.ChangeDutyCycle(2.5 + 10 * 90/180) while True: - servo_control_color() + servo_control_color() except KeyboardInterrupt: pass diff --git a/4.Code/python/bluetooth_control.py b/4.Code/python/bluetooth_control.py index ea27aec..4279d04 100644 --- a/4.Code/python/bluetooth_control.py +++ b/4.Code/python/bluetooth_control.py @@ -1,4 +1,4 @@ -#-*- coding:UTF-8 -*- +#-*- coding:UTF-8 -*- import RPi.GPIO as GPIO import time import string diff --git a/4.Code/python/infrared_follow.py b/4.Code/python/infrared_follow.py index 27a9070..a41486a 100644 --- a/4.Code/python/infrared_follow.py +++ b/4.Code/python/infrared_follow.py @@ -113,8 +113,8 @@ def key_scan(): time.sleep(0.01) if not GPIO.input(key): time.sleep(0.01) - while not GPIO.input(key): - pass + while not GPIO.input(key): + pass #delay 2s time.sleep(2) @@ -137,7 +137,7 @@ def key_scan(): time.sleep(0.002) elif RightSensorValue == False and LeftSensorValue == True: spin_right() - time.sleep(0.002) + time.sleep(0.002) elif RightSensorValue == True and LeftSensorValue == True : brake() diff --git a/4.Code/python/light_follow.py b/4.Code/python/light_follow.py index a09a067..fcc4afe 100644 --- a/4.Code/python/light_follow.py +++ b/4.Code/python/light_follow.py @@ -1,4 +1,4 @@ -#-*- coding:UTF-8 -*- +#-*- coding:UTF-8 -*- import RPi.GPIO as GPIO import time @@ -113,8 +113,8 @@ def key_scan(): time.sleep(0.01) if not GPIO.input(key): time.sleep(0.01) - while not GPIO.input(key): - pass + while not GPIO.input(key): + pass #delay 2s time.sleep(2) @@ -135,7 +135,7 @@ def key_scan(): time.sleep(0.002) elif LdrSersorRightValue == True and LdrSersorLeftValue == False: spin_right() - time.sleep(0.002) + time.sleep(0.002) elif LdrSersorRightValue == False and LdrSersorLeftValue == False : brake() From d06cda299ba3b023f8a237c046d6dec6b87d5c7f Mon Sep 17 00:00:00 2001 From: Benjamin Porter Date: Sun, 12 Apr 2020 20:42:52 -0600 Subject: [PATCH 2/3] Add parens to print so ColorLED.py works on python 3 --- 4.Code/python/ColorLED.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/4.Code/python/ColorLED.py b/4.Code/python/ColorLED.py index 7c05f95..537bea5 100644 --- a/4.Code/python/ColorLED.py +++ b/4.Code/python/ColorLED.py @@ -48,5 +48,5 @@ GPIO.output(LED_B, GPIO.LOW) time.sleep(1) except: - print "except" + print("except") GPIO.cleanup() From f359cabaaa3545aafc6b94aaf7b6fc2884004b6e Mon Sep 17 00:00:00 2001 From: Benjamin Porter Date: Sun, 12 Apr 2020 20:43:32 -0600 Subject: [PATCH 3/3] Remove trailing whitespace This makes vim stop highlighting the line endings in red and stop git from yelling whenever a file is committed. --- 4.Code/python/CarRun.py | 6 +- 4.Code/python/KeyScanStart.py | 6 +- 4.Code/python/ServoControlColorVersion1.py | 8 +- 4.Code/python/ServoControlColorVersion2.py | 12 +-- 4.Code/python/advance.py | 2 +- 4.Code/python/avoid_ultrasonic.py | 32 +++--- 4.Code/python/bluetooth_control.py | 118 ++++++++++----------- 4.Code/python/infrared_avoid.py | 22 ++-- 4.Code/python/infrared_follow.py | 24 ++--- 4.Code/python/light_follow.py | 22 ++-- 4.Code/python/servo_ultrasonic_avoid.py | 50 ++++----- 4.Code/python/tracking.py | 38 +++---- 12 files changed, 170 insertions(+), 170 deletions(-) diff --git a/4.Code/python/CarRun.py b/4.Code/python/CarRun.py index 4571de4..1794776 100644 --- a/4.Code/python/CarRun.py +++ b/4.Code/python/CarRun.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of motor pin +#Definition of motor pin IN1 = 20 IN2 = 21 IN3 = 19 @@ -103,7 +103,7 @@ def brake(delaytime): pwm_ENB.ChangeDutyCycle(50) time.sleep(delaytime) -#Delay 2s +#Delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -124,5 +124,5 @@ def brake(delaytime): pass pwm_ENA.stop() pwm_ENB.stop() -GPIO.cleanup() +GPIO.cleanup() diff --git a/4.Code/python/KeyScanStart.py b/4.Code/python/KeyScanStart.py index 8c7cb2c..d12e2e2 100644 --- a/4.Code/python/KeyScanStart.py +++ b/4.Code/python/KeyScanStart.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of motor pin +#Definition of motor pin IN1 = 20 IN2 = 21 IN3 = 19 @@ -37,7 +37,7 @@ def motor_init(): pwm_ENA.start(0) pwm_ENB.start(0) GPIO.setup(key,GPIO.IN) - + #advance def run(delaytime): GPIO.output(IN1, GPIO.HIGH) @@ -138,4 +138,4 @@ def key_scan(): pass pwm_ENA.stop() pwm_ENB.stop() -GPIO.cleanup() +GPIO.cleanup() diff --git a/4.Code/python/ServoControlColorVersion1.py b/4.Code/python/ServoControlColorVersion1.py index c52e570..92740d1 100644 --- a/4.Code/python/ServoControlColorVersion1.py +++ b/4.Code/python/ServoControlColorVersion1.py @@ -62,18 +62,18 @@ def color_light(pos): GPIO.output(LED_R, GPIO.LOW) GPIO.output(LED_G, GPIO.LOW) GPIO.output(LED_B, GPIO.LOW) - + def servo_control_color(): for pos in range(181): pwm_servo.ChangeDutyCycle(2.5 + 10 * pos/180) color_light(pos) - time.sleep(0.009) + time.sleep(0.009) for pos in reversed(range(181)): pwm_servo.ChangeDutyCycle(2.5 + 10 * pos/180) color_light(pos) time.sleep(0.009) -#delay 2s +#delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -83,7 +83,7 @@ def servo_control_color(): pwm_servo.ChangeDutyCycle(2.5 + 10 * 90/180) while True: servo_control_color() - + except KeyboardInterrupt: pass pwm_servo.stop() diff --git a/4.Code/python/ServoControlColorVersion2.py b/4.Code/python/ServoControlColorVersion2.py index 8c35be7..ee345a7 100644 --- a/4.Code/python/ServoControlColorVersion2.py +++ b/4.Code/python/ServoControlColorVersion2.py @@ -25,7 +25,7 @@ def init(): GPIO.setup(LED_B, GPIO.OUT) GPIO.setup(ServoPin, GPIO.OUT) -#Define a pulse function to generate the PWM value in the analog mode. +#Define a pulse function to generate the PWM value in the analog mode. #The base pulse is 20ms, and the high level of the pulse is controlled at 0-180 degrees in 0.5-2.5ms. def servo_pulse(myangle): pulsewidth = (myangle * 11) + 500 @@ -34,7 +34,7 @@ def servo_pulse(myangle): GPIO.output(ServoPin, GPIO.LOW) time.sleep(20.0/1000-pulsewidth/1000000.0) - + # The servo turns from 0-180, then turns from 180 to 0. #At the same time, the 180 degree angle is divided into 7 sections to represent 7 different colors. def corlor_light(pos): @@ -70,18 +70,18 @@ def corlor_light(pos): GPIO.output(LED_R, GPIO.LOW) GPIO.output(LED_G, GPIO.LOW) GPIO.output(LED_B, GPIO.LOW) - + def servo_control_color(): for pos in range(181): servo_pulse(pos) corlor_light(pos) - time.sleep(0.009) + time.sleep(0.009) for pos in reversed(range(181)): servo_pulse(pos) corlor_light(pos) time.sleep(0.009) -#delay 2s +#delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -90,7 +90,7 @@ def servo_control_color(): init() while True: servo_control_color() - + except KeyboardInterrupt: pass GPIO.cleanup() diff --git a/4.Code/python/advance.py b/4.Code/python/advance.py index 8a757e1..085e80a 100644 --- a/4.Code/python/advance.py +++ b/4.Code/python/advance.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of motor pin +#Definition of motor pin IN1 = 20 IN2 = 21 IN3 = 19 diff --git a/4.Code/python/avoid_ultrasonic.py b/4.Code/python/avoid_ultrasonic.py index 79347ae..fc6826d 100644 --- a/4.Code/python/avoid_ultrasonic.py +++ b/4.Code/python/avoid_ultrasonic.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of pin +#Definition of pin IN1 = 20 IN2 = 21 IN3 = 19 @@ -43,7 +43,7 @@ def init(): pwm_ENB = GPIO.PWM(ENB, 2000) pwm_ENA.start(0) pwm_ENB.start(0) - + #Advance def run(leftspeed, rightspeed): GPIO.output(IN1, GPIO.HIGH) @@ -61,7 +61,7 @@ def back(leftspeed, rightspeed): GPIO.output(IN4, GPIO.HIGH) pwm_ENA.ChangeDutyCycle(leftspeed) pwm_ENB.ChangeDutyCycle(rightspeed) - + #turn left def left(leftspeed, rightspeed): GPIO.output(IN1, GPIO.LOW) @@ -79,7 +79,7 @@ def right(leftspeed, rightspeed): GPIO.output(IN4, GPIO.LOW) pwm_ENA.ChangeDutyCycle(leftspeed) pwm_ENB.ChangeDutyCycle(rightspeed) - + #turn left in place def spin_left(leftspeed, rightspeed): GPIO.output(IN1, GPIO.LOW) @@ -115,7 +115,7 @@ def key_scan(): time.sleep(0.01) while not GPIO.input(key): pass - + #Ultrasonic function def Distance_test(): GPIO.output(TrigPin,GPIO.HIGH) @@ -130,7 +130,7 @@ def Distance_test(): print "distance is %d " % (((t2 - t1)* 340 / 2) * 100) time.sleep(0.01) return ((t2 - t1)* 340 / 2) * 100 - + time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -141,31 +141,31 @@ def Distance_test(): while True: distance = Distance_test() if distance > 50: - run(100, 100) + run(100, 100) elif 30 <= distance <= 50: - run(60, 60) + run(60, 60) elif distance < 30: spin_right(85, 85) - time.sleep(0.35) + time.sleep(0.35) brake() time.sleep(0.001) - distance = Distance_test() + distance = Distance_test() if distance >= 30: - run(60, 60) + run(60, 60) elif distance < 30: spin_left(85, 85) - time.sleep(0.6) + time.sleep(0.6) brake() time.sleep(0.001) - distance = Distance_test() + distance = Distance_test() if distance >= 30: - run(60, 60) + run(60, 60) elif distance < 30: - spin_left(85, 85) + spin_left(85, 85) time.sleep(0.3) brake() time.sleep(0.001) - + except KeyboardInterrupt: pass pwm_ENA.stop() diff --git a/4.Code/python/bluetooth_control.py b/4.Code/python/bluetooth_control.py index 4279d04..c99849f 100644 --- a/4.Code/python/bluetooth_control.py +++ b/4.Code/python/bluetooth_control.py @@ -4,14 +4,14 @@ import string import serial -#Definition of key value -run_car = '1' -back_car = '2' -left_car = '3' -right_car = '4' -stop_car = '0' - -#Definition of status value +#Definition of key value +run_car = '1' +back_car = '2' +left_car = '3' +right_car = '4' +stop_car = '0' + +#Definition of status value enSTOP = 0 enRUN =1 enBACK = 2 @@ -39,7 +39,7 @@ #Definition of RGB module pins LED_R = 22 LED_G = 27 -LED_B = 24 +LED_B = 24 #Definition of servo pin ServoPin = 23 @@ -65,8 +65,8 @@ LdrSensorLeft = 7 LdrSensorRight = 6 -global timecount -global count +global timecount +global count red = 0 green = 0 @@ -74,10 +74,10 @@ NewLineReceived = 0 InputString = '' InputStringcache = '' -g_CarState = 0 -CarSpeedControl = 50 +g_CarState = 0 +CarSpeedControl = 50 g_num = 0 -g_packnum = 0 +g_packnum = 0 ReturnTemp = '' infrared_track_value = '' infrared_avoid_value = '' @@ -137,9 +137,9 @@ def init(): pwm_rled.start(0) pwm_gled.start(0) pwm_bled.start(0) - - + + #Advance def run(): GPIO.output(IN1, GPIO.HIGH) @@ -157,7 +157,7 @@ def back(): GPIO.output(IN4, GPIO.HIGH) pwm_ENA.ChangeDutyCycle(CarSpeedControl) pwm_ENB.ChangeDutyCycle(CarSpeedControl) - + #trun left def left(): GPIO.output(IN1, GPIO.LOW) @@ -175,7 +175,7 @@ def right(): GPIO.output(IN4, GPIO.LOW) pwm_ENA.ChangeDutyCycle(CarSpeedControl) pwm_ENB.ChangeDutyCycle(CarSpeedControl) - + #turn left in place def spin_left(): GPIO.output(IN1, GPIO.LOW) @@ -211,7 +211,7 @@ def key_scan(): time.sleep(0.01) while not GPIO.input(key): pass - + #Ultrasonic function def Distance_test(): GPIO.output(TrigPin,GPIO.HIGH) @@ -226,7 +226,7 @@ def Distance_test(): print "distance is %d " % (((t2 - t1)* 340 / 2) * 100) time.sleep(0.01) return ((t2 - t1)* 340 / 2) * 100 - + #The servo rotates to the specified angle def servo_appointed_detection(pos): for i in range(18): @@ -246,7 +246,7 @@ def tracking_test(): infrared_track_value_list[2] = str(1^ TrackSensorRightValue1) infrared_track_value_list[3] = str(1^ TrackSensorRightValue2) infrared_track_value = ''.join(infrared_track_value_list) - + #infrared avoid test def infrared_avoid_test(): @@ -258,28 +258,28 @@ def infrared_avoid_test(): infrared_avoid_value_list[0] = str(1 ^ LeftSensorValue) infrared_avoid_value_list[1] = str(1 ^ RightSensorValue) infrared_avoid_value = ''.join(infrared_avoid_value_list) - + # follow light test def follow_light_test(): global LDR_value LdrSersorLeftValue = GPIO.input(LdrSensorLeft) - LdrSersorRightValue = GPIO.input(LdrSensorRight) + LdrSersorRightValue = GPIO.input(LdrSensorRight) LDR_value_list = ['0','0'] LDR_value_list[0] = str(LdrSersorLeftValue) - LDR_value_list[1] = str(LdrSersorRightValue) + LDR_value_list[1] = str(LdrSersorRightValue) LDR_value = ''.join(LDR_value_list) - + #whistle def whistle(): GPIO.output(buzzer, GPIO.LOW) time.sleep(0.1) GPIO.output(buzzer, GPIO.HIGH) - time.sleep(0.001) - + time.sleep(0.001) + #Color_LED light the specified color def color_led_pwm(iRed,iGreen, iBlue): - print iRed + print iRed print iGreen print iBlue v_red = (100*iRed)/255 @@ -292,7 +292,7 @@ def color_led_pwm(iRed,iGreen, iBlue): pwm_gled.ChangeDutyCycle(v_green) pwm_bled.ChangeDutyCycle(v_blue) time.sleep(0.02) - + #The serial port data is parsed and the corresponding action is specified def serial_data_parse(): global NewLineReceived @@ -304,7 +304,7 @@ def serial_data_parse(): #Analyze the control instructions of the servo sent by the host computer and execute corresponding operation #For exmaple:$4WD,PTZ180# servo turn 180° if (InputString.find("$4WD,PTZ", 0, len(InputString)) != -1): - i = InputString.find("PTZ", 0, len(InputString)) + i = InputString.find("PTZ", 0, len(InputString)) ii = InputString.find("#", 0, len(InputString)) if ii > i: string = InputString[i+3:ii] @@ -314,23 +314,23 @@ def serial_data_parse(): InputString.zfill(len(InputString)) print "in" print InputString - + #Analyze the control instructions sent by the host computer and execute corresponding operation #For exmaple:$4WD,CLR255,CLG0,CLB0# color_red if (InputString.find("CLR", 0, len(InputString)) != -1): - i = InputString.find("CLR", 0, len(InputString)) + i = InputString.find("CLR", 0, len(InputString)) ii = InputString.find(",CLG", 0, len(InputString)) if ii > i: string = InputString[i+3:ii] m_kp = int(string) red = m_kp - i = InputString.find("CLG", 0, len(InputString)) + i = InputString.find("CLG", 0, len(InputString)) ii = InputString.find(",CLB", 0, len(InputString)) if ii > i: string = InputString[i+3:ii] m_kp = int(string) green = m_kp - i = InputString.find("CLB", 0, len(InputString)) + i = InputString.find("CLB", 0, len(InputString)) ii = InputString.find("#", 0, len(InputString)) if ii > i: string = InputString[i+3:ii] @@ -339,10 +339,10 @@ def serial_data_parse(): print "red :%d " % red print green print blue - color_led_pwm(red, green, blue) + color_led_pwm(red, green, blue) NewLineReceived = 0 InputString.zfill(len(InputString)) - + #Analyze the control instructions sent by the host computer and execute corresponding operation #For exmaple:$1,0,0,0,0,0,0,0,0,0# advance if (InputString.find("$4WD", 0, len(InputString)) == -1) and (InputString.find("#", 0, len(InputString)) != -1): @@ -374,12 +374,12 @@ def serial_data_parse(): if InputString[13] == '3': color_led_pwm(0, 255, 0) if InputString[13] == '4': - color_led_pwm(0, 0, 255) - + color_led_pwm(0, 0, 255) + if InputString[15] == '1': GPIO.output(OutfirePin,not GPIO.input(OutfirePin) ) time.sleep(1) - + if InputString[17] == '1': servo_appointed_detection(90) print "carstate:%d" % g_CarState @@ -391,7 +391,7 @@ def serial_data_parse(): g_CarState = enRUN print "run car" elif InputString[1] == back_car: - g_CarState = enBACK + g_CarState = enBACK elif InputString[1] == left_car: g_CarState = enLEFT elif InputString[1] == right_car: @@ -399,10 +399,10 @@ def serial_data_parse(): elif InputString[1] == stop_car: g_CarState = enSTOP else: - g_CarState = enSTOP + g_CarState = enSTOP NewLineReceived = 0 - InputString.zfill(len(InputString)) - + InputString.zfill(len(InputString)) + #The collected sensor data is transmitted by the serial port to the host computer for display def serial_data_postback(): @@ -425,7 +425,7 @@ def serial_data_postback(): ReturnTemp += "#" print ReturnTemp ser.write(ReturnTemp) - + def serialEvent(): global InputString global InputStringcache @@ -435,7 +435,7 @@ def serialEvent(): while True: size = ser.inWaiting() if size == 0: - + break else: while size != 0: @@ -451,12 +451,12 @@ def serialEvent(): InputStringcache = '' StartBit = 0 size = 0 - print InputString - + print InputString + try: ser = serial.Serial("/dev/ttyAMA0", 9600, timeout = 0.001) print "serial.isOpen() = ",ser.isOpen() - ser.write("serial is on!") + ser.write("serial is on!") timecount = 2000 count = 100 init() @@ -468,9 +468,9 @@ def serialEvent(): serial_data_parse() NewLineReceived = 0 - #print "nice to meet you" + #print "nice to meet you" if g_CarState == enSTOP: - brake() + brake() elif g_CarState == enRUN: run() print "running" @@ -486,9 +486,9 @@ def serialEvent(): spin_right() else: brake() - + # print "hello woek" - + timecount -= 1 #print time if timecount == 0: @@ -498,7 +498,7 @@ def serialEvent(): serial_data_postback() timecount = 2000 count = 100 - + except KeyboardInterrupt: pass ser.close() @@ -509,9 +509,9 @@ def serialEvent(): pwm_bled.stop() pwm_servo.stop() GPIO.cleanup() - - - - - - + + + + + + diff --git a/4.Code/python/infrared_avoid.py b/4.Code/python/infrared_avoid.py index 70dad5d..325b1d9 100644 --- a/4.Code/python/infrared_avoid.py +++ b/4.Code/python/infrared_avoid.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of motor pin +#Definition of motor pin IN1 = 20 IN2 = 21 IN3 = 19 @@ -43,7 +43,7 @@ def init(): pwm_ENB = GPIO.PWM(ENB, 2000) pwm_ENA.start(0) pwm_ENB.start(0) - + #advance def run(): GPIO.output(IN1, GPIO.HIGH) @@ -61,7 +61,7 @@ def back(): GPIO.output(IN4, GPIO.HIGH) pwm_ENA.ChangeDutyCycle(50) pwm_ENB.ChangeDutyCycle(50) - + #turn left def left(): GPIO.output(IN1, GPIO.LOW) @@ -79,7 +79,7 @@ def right(): GPIO.output(IN4, GPIO.LOW) pwm_ENA.ChangeDutyCycle(50) pwm_ENB.ChangeDutyCycle(0) - + #turn left in place def spin_left(): GPIO.output(IN1, GPIO.LOW) @@ -114,7 +114,7 @@ def key_scan(): time.sleep(0.01) while not GPIO.input(key): pass - + time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -129,17 +129,17 @@ def key_scan(): RightSensorValue = GPIO.input(AvoidSensorRight); if LeftSensorValue == True and RightSensorValue == True : - run() + run() elif LeftSensorValue == True and RightSensorValue == False : - spin_left() + spin_left() time.sleep(0.002) elif RightSensorValue == True and LeftSensorValue == False: - spin_right() - time.sleep(0.002) + spin_right() + time.sleep(0.002) elif RightSensorValue == False and LeftSensorValue == False : - spin_right() + spin_right() time.sleep(0.002) - + except KeyboardInterrupt: pass pwm_ENA.stop() diff --git a/4.Code/python/infrared_follow.py b/4.Code/python/infrared_follow.py index a41486a..3916a71 100644 --- a/4.Code/python/infrared_follow.py +++ b/4.Code/python/infrared_follow.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of motor pin +#Definition of motor pin IN1 = 20 IN2 = 21 IN3 = 19 @@ -13,7 +13,7 @@ #Definition of key key = 8 -#Definition of infrared follow Sensor pin +#Definition of infrared follow Sensor pin FollowSensorLeft = 12 FollowSensorRight = 17 @@ -43,7 +43,7 @@ def init(): pwm_ENB = GPIO.PWM(ENB, 2000) pwm_ENA.start(0) pwm_ENB.start(0) - + #advance def run(): GPIO.output(IN1, GPIO.HIGH) @@ -61,7 +61,7 @@ def back(): GPIO.output(IN4, GPIO.HIGH) pwm_ENA.ChangeDutyCycle(50) pwm_ENB.ChangeDutyCycle(50) - + #trun left def left(): GPIO.output(IN1, GPIO.LOW) @@ -79,7 +79,7 @@ def right(): GPIO.output(IN4, GPIO.LOW) pwm_ENA.ChangeDutyCycle(50) pwm_ENB.ChangeDutyCycle(0) - + #trun left in place def spin_left(): GPIO.output(IN1, GPIO.LOW) @@ -116,7 +116,7 @@ def key_scan(): while not GPIO.input(key): pass -#delay 2s +#delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -131,16 +131,16 @@ def key_scan(): RightSensorValue = GPIO.input(FollowSensorRight); if LeftSensorValue == False and RightSensorValue == False : - run() + run() elif LeftSensorValue == False and RightSensorValue == True : - spin_left() - time.sleep(0.002) + spin_left() + time.sleep(0.002) elif RightSensorValue == False and LeftSensorValue == True: - spin_right() + spin_right() time.sleep(0.002) elif RightSensorValue == True and LeftSensorValue == True : - brake() - + brake() + except KeyboardInterrupt: pass pwm_ENA.stop() diff --git a/4.Code/python/light_follow.py b/4.Code/python/light_follow.py index fcc4afe..328bf57 100644 --- a/4.Code/python/light_follow.py +++ b/4.Code/python/light_follow.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of motor pin +#Definition of motor pin IN1 = 20 IN2 = 21 IN3 = 19 @@ -10,7 +10,7 @@ ENA = 16 ENB = 13 -#Definition of button +#Definition of button key = 8 #Definition of photoresistor pin @@ -43,7 +43,7 @@ def init(): pwm_ENB = GPIO.PWM(ENB, 2000) pwm_ENA.start(0) pwm_ENB.start(0) - + #advance def run(): GPIO.output(IN1, GPIO.HIGH) @@ -61,7 +61,7 @@ def back(): GPIO.output(IN4, GPIO.HIGH) pwm_ENA.ChangeDutyCycle(50) pwm_ENB.ChangeDutyCycle(50) - + #turn left def left(): GPIO.output(IN1, GPIO.LOW) @@ -79,7 +79,7 @@ def right(): GPIO.output(IN4, GPIO.LOW) pwm_ENA.ChangeDutyCycle(50) pwm_ENB.ChangeDutyCycle(0) - + #turn left in place def spin_left(): GPIO.output(IN1, GPIO.LOW) @@ -116,7 +116,7 @@ def key_scan(): while not GPIO.input(key): pass -#delay 2s +#delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -129,16 +129,16 @@ def key_scan(): LdrSersorRightValue = GPIO.input(LdrSensorRight); if LdrSersorLeftValue == True and LdrSersorRightValue == True : - run() + run() elif LdrSersorLeftValue == True and LdrSersorRightValue == False : - spin_left() + spin_left() time.sleep(0.002) elif LdrSersorRightValue == True and LdrSersorLeftValue == False: - spin_right() + spin_right() time.sleep(0.002) elif LdrSersorRightValue == False and LdrSersorLeftValue == False : - brake() - + brake() + except KeyboardInterrupt: pass pwm_ENA.stop() diff --git a/4.Code/python/servo_ultrasonic_avoid.py b/4.Code/python/servo_ultrasonic_avoid.py index 554e1e9..da5363f 100644 --- a/4.Code/python/servo_ultrasonic_avoid.py +++ b/4.Code/python/servo_ultrasonic_avoid.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -#Definition of motor pins +#Definition of motor pins IN1 = 20 IN2 = 21 IN3 = 19 @@ -66,7 +66,7 @@ def init(): pwm_servo = GPIO.PWM(ServoPin, 50) pwm_servo.start(0) - + #advance def run(leftspeed, rightspeed): GPIO.output(IN1, GPIO.HIGH) @@ -84,7 +84,7 @@ def back(leftspeed, rightspeed): GPIO.output(IN4, GPIO.HIGH) pwm_ENA.ChangeDutyCycle(leftspeed) pwm_ENB.ChangeDutyCycle(rightspeed) - + #turn left def left(leftspeed, rightspeed): GPIO.output(IN1, GPIO.LOW) @@ -94,7 +94,7 @@ def left(leftspeed, rightspeed): pwm_ENA.ChangeDutyCycle(leftspeed) pwm_ENB.ChangeDutyCycle(rightspeed) -#trun right +#trun right def right(leftspeed, rightspeed): GPIO.output(IN1, GPIO.HIGH) GPIO.output(IN2, GPIO.LOW) @@ -102,7 +102,7 @@ def right(leftspeed, rightspeed): GPIO.output(IN4, GPIO.LOW) pwm_ENA.ChangeDutyCycle(leftspeed) pwm_ENB.ChangeDutyCycle(rightspeed) - + #turn left in place def spin_left(leftspeed, rightspeed): GPIO.output(IN1, GPIO.LOW) @@ -138,7 +138,7 @@ def key_scan(): time.sleep(0.01) while not GPIO.input(key): pass - + #Ultrasonic function def Distance_test(): GPIO.output(TrigPin,GPIO.HIGH) @@ -153,12 +153,12 @@ def Distance_test(): print "distance is %d " % (((t2 - t1)* 340 / 2) * 100) time.sleep(0.01) return ((t2 - t1)* 340 / 2) * 100 - + #The servo rotates to the specified angle def servo_appointed_detection(pos): for i in range(18): - pwm_servo.ChangeDutyCycle(2.5 + 10 * pos/180) - + pwm_servo.ChangeDutyCycle(2.5 + 10 * pos/180) + def servo_color_carstate(): #red GPIO.output(LED_R, GPIO.HIGH) @@ -167,11 +167,11 @@ def servo_color_carstate(): back(20, 20) time.sleep(0.08) brake() - + servo_appointed_detection(0) time.sleep(0.8) rightdistance = Distance_test() - + servo_appointed_detection(180) time.sleep(0.8) leftdistance = Distance_test() @@ -179,7 +179,7 @@ def servo_color_carstate(): servo_appointed_detection(90) time.sleep(0.8) frontdistance = Distance_test() - + if leftdistance < 30 and rightdistance < 30 and frontdistance < 30: #Magenta GPIO.output(LED_R, GPIO.HIGH) @@ -201,8 +201,8 @@ def servo_color_carstate(): GPIO.output(LED_B, GPIO.HIGH) spin_right(35, 35) time.sleep(0.28) - -#delay 2s + +#delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -219,15 +219,15 @@ def servo_color_carstate(): RightSensorValue = GPIO.input(AvoidSensorRight) if LeftSensorValue == True and RightSensorValue == True : - run(50, 50) + run(50, 50) elif LeftSensorValue == True and RightSensorValue == False : - spin_left(35, 35) + spin_left(35, 35) time.sleep(0.002) elif RightSensorValue == True and LeftSensorValue == False: - spin_right(35, 35) - time.sleep(0.002) + spin_right(35, 35) + time.sleep(0.002) elif RightSensorValue == False and LeftSensorValue == False : - spin_right(35, 35) + spin_right(35, 35) time.sleep(0.002) run(100, 100) GPIO.output(LED_R, GPIO.LOW) @@ -240,20 +240,20 @@ def servo_color_carstate(): RightSensorValue = GPIO.input(AvoidSensorRight) if LeftSensorValue == True and RightSensorValue == True : - run(50, 50) + run(50, 50) elif LeftSensorValue == True and RightSensorValue == False : - spin_left(35, 35) + spin_left(35, 35) time.sleep(0.002) elif RightSensorValue == True and LeftSensorValue == False: - spin_right(35, 35) - time.sleep(0.002) + spin_right(35, 35) + time.sleep(0.002) elif RightSensorValue == False and LeftSensorValue == False : - spin_right(35, 35) + spin_right(35, 35) time.sleep(0.002) run(50, 50) elif distance < 30: servo_color_carstate() - + except KeyboardInterrupt: pass pwm_ENA.stop() diff --git a/4.Code/python/tracking.py b/4.Code/python/tracking.py index 37b2e39..541fc09 100644 --- a/4.Code/python/tracking.py +++ b/4.Code/python/tracking.py @@ -48,7 +48,7 @@ def init(): pwm_ENB = GPIO.PWM(ENB, 2000) pwm_ENA.start(0) pwm_ENB.start(0) - + #advance def run(leftspeed, rightspeed): GPIO.output(IN1, GPIO.HIGH) @@ -66,8 +66,8 @@ def back(leftspeed, rightspeed): GPIO.output(IN4, GPIO.HIGH) pwm_ENA.ChangeDutyCycle(leftspeed) pwm_ENB.ChangeDutyCycle(rightspeed) - -#turn left + +#turn left def left(leftspeed, rightspeed): GPIO.output(IN1, GPIO.LOW) GPIO.output(IN2, GPIO.LOW) @@ -84,7 +84,7 @@ def right(leftspeed, rightspeed): GPIO.output(IN4, GPIO.LOW) pwm_ENA.ChangeDutyCycle(leftspeed) pwm_ENB.ChangeDutyCycle(rightspeed) - + #turn left in place def spin_left(leftspeed, rightspeed): GPIO.output(IN1, GPIO.LOW) @@ -121,7 +121,7 @@ def key_scan(): while not GPIO.input(key): pass -#delay 2s +#delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. @@ -146,44 +146,44 @@ def key_scan(): if (TrackSensorLeftValue1 == False or TrackSensorLeftValue2 == False) and TrackSensorRightValue2 == False: spin_right(35, 35) time.sleep(0.08) - + #4 tracking pins level status - # 0 X 0 0 - # 0 X 0 1 - # 0 X 1 0 - #Turn right in place,speed is 50,delay 80ms - #Handle left acute angle and left right angle + # 0 X 0 0 + # 0 X 0 1 + # 0 X 1 0 + #Turn right in place,speed is 50,delay 80ms + #Handle left acute angle and left right angle elif TrackSensorLeftValue1 == False and (TrackSensorRightValue1 == False or TrackSensorRightValue2 == False): spin_left(35, 35) time.sleep(0.08) - + # 0 X X X #Left_sensor1 detected black line elif TrackSensorLeftValue1 == False: spin_left(35, 35) - + # X X X 0 #Right_sensor2 detected black line elif TrackSensorRightValue2 == False: spin_right(35, 35) - + #4 tracking pins level status # X 0 1 X elif TrackSensorLeftValue2 == False and TrackSensorRightValue1 == True: left(0,40) - + #4 tracking pins level status - # X 1 0 X + # X 1 0 X elif TrackSensorLeftValue2 == True and TrackSensorRightValue1 == False: right(40, 0) - + #4 tracking pins level status # X 0 0 X elif TrackSensorLeftValue2 == False and TrackSensorRightValue1 == False: run(45, 45) - + #When the level of 4 pins are 1 1 1 1 , the car keeps the previous running state. - + except KeyboardInterrupt: pass pwm_ENA.stop()