Guest User

woah1

a guest
Nov 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #R0B0T C0D3!
  2. #
  3. from adafruit_crickit import crickit
  4. import time
  5. import serial
  6. import asyncio
  7. crickit.init_neopixel(8)
  8.  
  9.  
  10.  
  11. ## --- Serial Stuff -- ##
  12. ser = serial.Serial('/dev/ttyUSB0')
  13. inputdata = [0,0] # data which is currently being parsed. we have this because it is not yet a full set of readings which will cause the program to cake its trousers.
  14. outputdata = [0,0] # last set of completed readings
  15. counter = 0
  16. buffer = "" # temporary input buffer
  17.  
  18. ## -- Motor stuff -- ##
  19. motorleftspeed = 0 # Variable to keep track of how fast each motor is going
  20. motorrightspeed = 0
  21.  
  22. leftmotor = crickit.dc_motor_1 #Just tidying up the names of the motors here
  23. rightmotor = crickit.dc_motor_2
  24.  
  25. def runmotors(lspeed,rspeed): #function which we use to control the speed of the left and right motors
  26. if (lspeed != motorleftspeed):
  27. leftmotor.throttle = lspeed
  28. motorlspeed = lspeed
  29. if (rspeed != motorrightspeed):
  30. rightmotor.throttle = rspeed
  31. motorrightspeed = rspeed
  32.  
  33. if lspeed and rspeed == 0: sopped = True
  34. else: stopped = False
  35.  
  36. #stuff that happens when we quit
  37. def quitclean():
  38. crickit.neopixel.fill((0, 0 , 0))
  39. ser.close()
  40. exit()
  41.  
  42. ## -- Turn around bits --
  43.  
  44. stopped = False # how we know if the robot is moving or not
  45. laststoptime = time.time() # Reference timestamp for turnaround function
  46.  
  47. def turnaround():
  48. turnaroundtime = time.time() + 10000 # what time we should stop turning around (ms)
  49. while time.time() <= turnaroundtime:
  50. runmotors(-.7,.7) # slide to the left, now take it back now y'all ....
  51.  
  52.  
  53.  
  54. ## -- Main While Loop -- ##
  55. try:
  56. while True: # at some point you need to make this asyncronous so it can do this in the background while it's thinking aboyut other stuff.
  57.  
  58. tmpbuf = ser.read()
  59. buffer += tmpbuf.decode('utf-8')
  60.  
  61.  
  62. if ":" in buffer: #if we find a : in our buffer we know that it is the end of that reading
  63.  
  64. # print(buffer)
  65. currentdata = buffer #set the current data we're working on to what we have in the buffer
  66. currentdata = currentdata.replace(":", "") #remove the : in the buffer so we can convert it into an integer
  67. currentdata = int(currentdata) #make it in to an integer
  68. inputdata[counter] = currentdata #add what we have in the buffer into our "inputdata" array
  69. counter += 1
  70.  
  71. buffer = "" # clear the buffer
  72.  
  73.  
  74. elif ";" in buffer: # if we find a ; in the buffer, we know it is the end of the road!
  75.  
  76. currentdata = buffer #set the current data we're working on to what we have in the buffer
  77. currentdata = currentdata.replace(";", "") #remove the : in the buffer so we can convert it into an integer
  78. currentdata = int(currentdata) #make it in to an integer
  79. inputdata[counter] = currentdata #add what we have in the buffer into our "inputdata" array
  80.  
  81. outputdata = inputdata # we can now set the "outputdata" array to be the same as the "inputdata" as we know it is a complete set
  82. buffer = "" # clear the buffer so we are ready to start again!
  83. counter = 0
  84.  
  85. # Begin Motor Function Control
  86. # Distance is measured in MM
  87.  
  88. if outputdata[0] > 200:
  89. runmotor(.5,.5) # set both motors to .5 speed
  90. crickit.neopixel.fill((0, 255, 0))
  91. print("CHOOCH FAST")
  92. else:
  93. if outputdata[0] < 100:
  94. runmotor(0,0)
  95. crickit.neopixel.fill((255, 0, 0))
  96. print("NO CHOOCH")
  97.  
  98. ## Turnaround logic starts here
  99. now = time.time()
  100. waittime = 5000 # How long we wait before giving up and turning around in ms
  101. if (!sopped): laststoptime = now # if this is the first time we've recieved command yet we can let the program now we are stopped now
  102.  
  103. if (now - laststoptime) > waittime:
  104. turnaround() ## if we have waited longer than the amount of patients the robot has we should start turning around
  105.  
  106. else:
  107. runmotor(.3,.3)
  108. crickit.neopixel.fill((50, 50, 50))
  109. print("CHOOCH SLOW")
  110.  
  111. except KeyboardInterrupt:
  112. quitclean()
  113.  
  114. # crickit.dc_motor_1.throttle = 0.5
  115. # crickit.dc_motor_2.throttle = 0.5
  116. # time.sleep(3)
Advertisement
Add Comment
Please, Sign In to add comment