Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python general cleanup #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 4.Code/python/CarRun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -124,5 +124,5 @@ def brake(delaytime):
pass
pwm_ENA.stop()
pwm_ENB.stop()
GPIO.cleanup()
GPIO.cleanup()

2 changes: 1 addition & 1 deletion 4.Code/python/ColorLED.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@
GPIO.output(LED_B, GPIO.LOW)
time.sleep(1)
except:
print "except"
print("except")
GPIO.cleanup()
28 changes: 14 additions & 14 deletions 4.Code/python/KeyScanStart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -128,14 +128,14 @@ 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()
pwm_ENB.stop()
GPIO.cleanup()
GPIO.cleanup()
56 changes: 28 additions & 28 deletions 4.Code/python/ServoControlColorVersion1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,51 @@ 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
#delay 2s
time.sleep(2)

#The try/except statement is used to detect errors in the try block.
Expand All @@ -82,8 +82,8 @@ def servo_control_color():
init()
pwm_servo.ChangeDutyCycle(2.5 + 10 * 90/180)
while True:
servo_control_color()
servo_control_color()

except KeyboardInterrupt:
pass
pwm_servo.stop()
Expand Down
12 changes: 6 additions & 6 deletions 4.Code/python/ServoControlColorVersion2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -90,7 +90,7 @@ def servo_control_color():
init()
while True:
servo_control_color()

except KeyboardInterrupt:
pass
GPIO.cleanup()
2 changes: 1 addition & 1 deletion 4.Code/python/advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions 4.Code/python/avoid_ultrasonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import RPi.GPIO as GPIO
import time

#Definition of pin
#Definition of pin
IN1 = 20
IN2 = 21
IN3 = 19
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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()
Expand Down
Loading