Advertisement
Guest User

BrickPi car

a guest
Oct 2nd, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Jaikrishna
  2. # Initial Date: June 25, 2013
  3. # Last Updated: June 25, 2013
  4. # http://www.dexterindustries.com/
  5. # This code is for testing the BrickPi with Lego Motors & Lego Ultrasonic Sensor
  6.  
  7. # Make the connections with Left Motor at Port A, Right Motor at Port D and Sensor at Port 4
  8. # Setup battery power source for the RPi & BrickPi and boot.
  9. # To control the program, connection must be made though SSH though PuTTY or similar software
  10. # Open up PuTTY and enter UserName:pi Password:raspberry (Default values)
  11. # Navigate to the directory containing this code and enter 'python Car.py'
  12. # The user needs to enter one of the following keys:
  13. # 8 - Forward
  14. # 4 - Left
  15. # 6 - Right
  16. # 2 - Reverse
  17. # 5 - Stop
  18. # Also, The motors automatically stop when any nearby object is detected using the Ultrasonic Sensor
  19.  
  20.  
  21. from BrickPi import *   #import BrickPi.py file to use BrickPi operations
  22. import threading
  23.  
  24. BrickPiSetup()  # setup the serial port for communication
  25.  
  26. BrickPi.MotorEnable[PORT_A] = 1 #Enable the Motor A
  27. BrickPi.MotorEnable[PORT_D] = 1 #Enable the Motor D
  28.  
  29. BrickPi.SensorType[PORT_4] = TYPE_SENSOR_ULTRASONIC_CONT    #Setting the type of sensor at PORT4
  30.  
  31. BrickPiSetupSensors()   #Send the properties of sensors to BrickPi
  32.  
  33. running = True
  34.  
  35. class myThread (threading.Thread):      #This thread is used for keeping the motor running while the main thread waits for user input
  36.     def __init__(self, threadID, name, counter):
  37.         threading.Thread.__init__(self)
  38.         self.threadID = threadID
  39.         self.name = name
  40.         self.counter = counter
  41.     def run(self):
  42.         while running:
  43.             if BrickPi.Sensor[PORT_4] < 20 :        #Lesser value means more close
  44.                 print "Car Stopped due to very close object"
  45.                 BrickPi.MotorSpeed[PORT_A] = 0      # Set Speed=0 which means stop
  46.                 BrickPi.MotorSpeed[PORT_D] = 0
  47.             BrickPiUpdateValues()       # Ask BrickPi to update values for sensors/motors
  48.             time.sleep(.2)              # sleep for 200 ms
  49.  
  50. thread1 = myThread(1, "Thread-1", 1)        #Setup and start the thread
  51. thread1.setDaemon(True)
  52. thread1.start()  
  53.  
  54. while True:
  55.     try:
  56.         c = raw_input("Enter direction: ")
  57.         if c == '8' :
  58.             print "Running Forward"
  59.             if BrickPi.MotorSpeed[PORT_A] + BrickPi.MotorSpeed[PORT_D] == 400 :
  60.                 BrickPi.MotorSpeed[PORT_A] = 0  
  61.                 BrickPi.MotorSpeed[PORT_D] = 0
  62.                 time.sleep(.5)
  63.             BrickPi.MotorSpeed[PORT_A] = -200  #Set the speed of MotorA (-255 to 255)
  64.             BrickPi.MotorSpeed[PORT_D] = -200  #Set the speed of MotorD (-255 to 255)
  65.         elif c == '2' :
  66.             print "Running Reverse"
  67.             if BrickPi.MotorSpeed[PORT_A] + BrickPi.MotorSpeed[PORT_D] == -400 :
  68.                 BrickPi.MotorSpeed[PORT_A] = 0  
  69.                 BrickPi.MotorSpeed[PORT_D] = 0
  70.                 time.sleep(.5)
  71.             BrickPi.MotorSpeed[PORT_A] = 200  #Set the speed of MotorA (-255 to 255)
  72.             BrickPi.MotorSpeed[PORT_D] = 200  #Set the speed of MotorD (-255 to 255)
  73.         elif c == '6' :
  74.             print "Turning Right"
  75.             BrickPi.MotorSpeed[PORT_A] = -200  #Set the speed of MotorA (-255 to 255)
  76.             BrickPi.MotorSpeed[PORT_D] = 0  #Set the speed of MotorD (-255 to 255)
  77.         elif c == '4' :
  78.             print "Turning Left"
  79.             BrickPi.MotorSpeed[PORT_A] = 0  #Set the speed of MotorA (-255 to 255)
  80.             BrickPi.MotorSpeed[PORT_D] = -200  #Set the speed of MotorD (-255 to 255)
  81.         elif c == '5' :
  82.             print "Stopped"
  83.             BrickPi.MotorSpeed[PORT_A] = 0  #Stop the motor
  84.             BrickPi.MotorSpeed[PORT_D] = 0
  85.     except KeyboardInterrupt:           #Triggered by pressing Ctrl+C
  86.         running = False             #Stop theread1
  87.         print "Bye"
  88.         break                   #Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement