|
- #!usr/bin/python
- import RPi.GPIO as GPIO
- import time
- import sys
- import getopt
- import string
- from distance.py import measure_average
- GPIO.setmode(GPIO.BCM)
- L1 = 5
- R1 = 6
- L2 = 23
- R2 = 24
- distance = 0
- GPIO.setup(L1,GPIO.OUT)
- GPIO.setup(R1,GPIO.OUT)
- GPIO.setup(L2,GPIO.OUT)
- GPIO.setup(R2,GPIO.OUT)
- def forward():
- GPIO.output(L1,GPIO.LOW)
- GPIO.output(R1,GPIO.HIGH)
- GPIO.output(L2,GPIO.HIGH)
- GPIO.output(R2,GPIO.LOW)
-
- def backward():
- GPIO.output(L1,GPIO.HIGH)
- GPIO.output(R1,GPIO.LOW)
- GPIO.output(L2,GPIO.LOW)
- GPIO.output(R2,GPIO.HIGH)
-
- def turn_right():
- GPIO.output(L1,GPIO.LOW)
- GPIO.output(R1,GPIO.LOW)
- GPIO.output(L2,GPIO.HIGH)
- GPIO.output(R2,GPIO.LOW)
-
- def turn_left():
- GPIO.output(L1,GPIO.LOW)
- GPIO.output(R1,GPIO.HIGH)
- GPIO.output(L2,GPIO.LOW)
- GPIO.output(R2,GPIO.LOW)
-
- def stop():
- GPIO.output(L1,GPIO.LOW)
- GPIO.output(R1,GPIO.LOW)
- GPIO.output(L2,GPIO.LOW)
- GPIO.output(R2,GPIO.LOW)
- def main(argv):
- try:
- opts, args = getopt.getopt(argv[1:], "a:t:")
- except getopt.GetoptError, err:
- print str(err)
- sys.exit(2)
- for op,value in opts:
- if op == "-a":
- if value == "f":
- print "Foward..."
- stop()
- forward()
- if value == "b":
- print "Backward..."
- stop()
- backward()
- if value == "l":
- print "Turn left..."
- stop()
- turn_left()
- if value == "r":
- print "Turn right..."
- stop()
- turn_right()
- if value == "s":
- print "Stop..."
- stop()
- if op == "-t":
- time.sleep(string.atoi(value))
-
- def autoMove():
- while True:
- distance = measure_average()
- if distance > 10:
- print "Distance : %.1fcm" % distance
- time.sleep(1)
- forward()
- stop()
- turn_left()
- if __name__ == '__main__':
- print "Hi,I'm coprobot, ready to go"
- autoMove()
- # main(sys.argv)
- # forward()
- # time.sleep(2220)
- # print "Turn right..."
- # turn_right()
- # time.sleep(200)
- # print "Turn left..."
- # turn_left()
- # time.sleep(200)
- # print "Forward..."
- # backward()
- # time.sleep(100)
- print "Byebye!"
- stop()
- GPIO.cleanup()
复制代码 中的一段代码- ef main(argv):
- try:
- opts, args = getopt.getopt(argv[1:], "a:t:")
- except getopt.GetoptError, err:
- print str(err)
- sys.exit(2)
- for op,value in opts:
- if op == "-a":
- if value == "f":
- print "Foward..."
- stop()
- forward()
- if value == "b":
- print "Backward..."
- stop()
- backward()
- if value == "l":
- print "Turn left..."
- stop()
- turn_left()
- if value == "r":
- print "Turn right..."
- stop()
- turn_right()
- if value == "s":
- print "Stop..."
- stop()
- if op == "-t":
- time.sleep(string.atoi(value))
-
- def autoMove():
- while True:
- distance = measure_average()
- if distance > 10:
- print "Distance : %.1fcm" % distance
- time.sleep(1)
- forward()
- stop()
- turn_left()
- if __name__ == '__main__':
- print "Hi,I'm coprobot, ready to go"
- autoMove()
- # main(sys.argv)
- # forward()
- # time.sleep(2220)
- # print "Turn right..."
- # turn_right()
- # time.sleep(200)
- # print "Turn left..."
- # turn_left()
- # time.sleep(200)
- # print "Forward..."
- # backward()
- # time.sleep(100)
- print "Byebye!"
- stop()
- GPIO.cleanup()
复制代码 是调用前面先后左转吗,这个py文件在树莓派里面打开以后,还要怎么操作才能让车走动,要打开网页吗? |
|