Advertisement
6677

Start Here Robot

Jun 29th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. #!usr/bin/env python
  2. import RPIO as GPIO
  3. from GPIO import PWM
  4. import time
  5.  
  6. #Motor Pin Declarations
  7. AIA = 25
  8. AIB = 24
  9. BIA = 23
  10. BIB = 18
  11.  
  12.  
  13. #Servo Pin Declarations
  14. "servoPin = 17"
  15. "servoMinPulse = 800" #Minimum Pulse width the servo can safely take
  16. "servoMaxPulse = 1900"    #Maximum pulse width the servo can safely take
  17.  
  18. "servoCentre = servoMinPulse + ((servoMaxPulse -" "servoMinPulse) / 2)"
  19.  
  20. #Ultrasound
  21. trigger = 8
  22. echo = 7
  23. dangerLevel = 30    #Distance in centimetres at which to avoid an object
  24. exitLevel = 10  #Distance in centimetres at which the program should exit
  25.  
  26. #GPIO Initialisations
  27. GPIO.setup(AIA, GPIO.OUT, initial = inactiveState)
  28. GPIO.setup(AIB, GPIO.OUT, initial = inactiveState)
  29. GPIO.setup(BIA, GPIO.OUT, initial = inactiveState)
  30. GPIO.setup(BIB, GPIO.OUT, initial = inactiveState)
  31.  
  32. "servo = PWM.Servo()"
  33. "servo.output(servoPin, servoCentre)"
  34.  
  35. GPIO.setup(trigger, GPIO.OUT, initial = GPIO.LOW)
  36. GPIO.setup(echo, GPIO.IN)
  37.  
  38. time.sleep(0.5) #Gives all I/O devices a chance to settle down
  39. #mainly the ultrasound and servo
  40.  
  41. running = True
  42. distance = dangerLevel * 2  #Just a semi random init value
  43.  
  44. while running == True:
  45.     GPIO.output(AIA, 0)
  46.     GPIO.output(AIB, 1)
  47.     GPIO.output(BIA, 0)
  48.     GPIO.output(BIB, 1)
  49.     while distance > dangerLevel:
  50.         GPIO.output(trigger, True)
  51.         time.sleep(0.00001)
  52.         GPIO.output(trigger, False)
  53.         start = time.time()
  54.         while GPIO.input(echo) == 0:
  55.             start = time.time()
  56.         while GPIO.input(echo) == 1:
  57.             stop = time.time()
  58.         elapsed = stop - start
  59.         distance = elapsed * 34000
  60.         distance = distance / 2
  61.         print distance
  62.     print "Danger Level Hit"
  63.     #Stop
  64.     GPIO.output(AIA, True)
  65.     GPIO.output(AIB, True)
  66.     GPIO.output(BIA, True)
  67.     GPIO.output(BIB, True)
  68.     if distance > exitLevel:
  69.         print "Obstacle, Avoid"
  70.         GPIO.output(AIA, 1)
  71.         GPIO.output(AIB, 0)
  72.         GPIO.output(BIA, 0)
  73.         GPIO.output(BIB, 1)
  74.         while distance <= dangerLevel:
  75.             GPIO.output(trigger, True)
  76.             time.sleep(0.00001)
  77.             GPIO.output(trigger, False)
  78.             start = time.time()
  79.             while GPIO.input(echo) == 0:
  80.                 start = time.time()
  81.             while GPIO.input(echo) == 1:
  82.                 stop = time.time()
  83.             elapsed = stop - start
  84.             distance = elapsed * 17000 #Why the hell I was multiplying by 34000 and dividing by 2 before....
  85.             print distance
  86.     else:
  87.         print "Ending"
  88.         running = False
  89.  
  90. #Cleanup before exit
  91. GPIO.cleanup()
  92. print "Done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement