Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import serial
  4.  
  5. ser = serial.Serial("/dev/ttyAMA0", 115200) #ttysomething
  6. GPIO.setmode (GPIO.BCM)
  7.  
  8. ControlPin = [21,20,16,12] #pins for LiDAR stepper motor
  9.  
  10. cc_seq = [ [1,0,0,0], #sequence of magnets to spin motor
  11. [1,1,0,0],
  12. [0,1,0,0],
  13. [0,1,1,0],
  14. [0,0,1,0],
  15. [0,0,1,1],
  16. [0,0,0,1],
  17. [1,0,0,1] ]
  18.  
  19. c_seq = [ [1,0,0,1],
  20. [0,0,0,1],
  21. [0,0,1,1],
  22. [0,0,1,0],
  23. [0,1,1,0],
  24. [0,1,0,0],
  25. [1,1,0,0],
  26. [1,0,0,0], ]
  27.  
  28. def LiScan():
  29. for pin in ControlPin:
  30. GPIO.setup (pin, GPIO.OUT) #set pins to GPIO Out
  31. GPIO.output (pin, 0) #default output = 0
  32.  
  33. dataPoints = 0
  34. for i in range (256): #half revolution (512 steps for 360*)
  35. for halfstep in range (8): #each sequence of halfstep
  36. for pin in range (4): #each magnet
  37. GPIO.output (ControlPin[pin], cc_seq[halfstep][pin])
  38. #output to wanted sequence
  39. count = ser.in_waiting #how many bytes of data in through LiDAR
  40. time.sleep (0.00015) #time for armature to set (smaller is quicker, less reliable)
  41. if count > 8: #9 bytes in buffer is full
  42. recv = ser.read(9) #read bytes, put into recv
  43. ser.reset_input_buffer()
  44. #reset the buffer
  45.  
  46. if recv[0] == 0x59 and recv[1] == 0x59:
  47. #proper if bits 0 and 1 are 89
  48. distance = recv[2] + recv[3] * 256
  49. strength = recv[4] + recv[5] * 256
  50. print('(', distance, ',', strength, ')')
  51. #can skip the print if don't need
  52. dataPoints=dataPoints+1
  53. ser.reset_input_buffer()
  54. #reset the buffer
  55.  
  56. for i in range (256): #same as before, but reverse sequence
  57. for halfstep in range (8):
  58. for pin in range (4):
  59. GPIO.output (ControlPin[pin], c_seq[halfstep][pin])
  60. time.sleep(0.00015)
  61.  
  62. GPIO.cleanup() #reset GPIO pins to end
  63. #return distance
  64. print("Number of Data Points: ",dataPoints)
  65.  
  66. if __name__ == '__main__':
  67. try:
  68. if ser.is_open == False:
  69. ser.open()
  70. LiScan()
  71. except KeyboardInterrupt: # Ctrl+C
  72. if ser != None:
  73. ser.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement