Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. from gopigo import *
  2. import time
  3. import pigpio
  4.  
  5. #This program scans a 180 degree map of its surroundings then turns towards its most distant measurement
  6.  
  7. #Methods to control the robot better
  8. #!/usr/bin/python
  9. '''
  10. This module contains convenience functions to simplify
  11. the coding of simple tasks.
  12. This really needs to be moved to a GoPiGo package
  13. e.g. from gopigo.control import *
  14. '''
  15.  
  16. en_debug=1
  17.  
  18. ## 360 rotation is ~64 encoder pulses or 5 deg/pulse
  19. ## DPR is the Deg:Pulse Ratio or the # of degrees per
  20. ## encoder pulse.
  21. DPR = 360.0/64
  22. WHEEL_RAD = 3.25 # Wheels are ~6.5 cm diameter.
  23. CHASS_WID = 11 # Chassis is ~13.5 cm wide.
  24.  
  25. def left_deg(deg=None):
  26. '''
  27. Turn chassis left by a specified number of degrees.
  28. DPR is the #deg/pulse (Deg:Pulse ratio)
  29. This function sets the encoder to the correct number
  30. of pulses and then invokes left().
  31. '''
  32. if deg is not None:
  33. pulse= int(deg/DPR)
  34. enc_tgt(0,1,pulse)
  35. left()
  36.  
  37. def right_deg(deg=None):
  38. '''
  39. Turn chassis right by a specified number of degrees.
  40. DPR is the #deg/pulse (Deg:Pulse ratio)
  41. This function sets the encoder to the correct number
  42. of pulses and then invokes right().
  43. '''
  44. if deg is not None:
  45. pulse= int(deg/DPR)
  46. enc_tgt(0,1,pulse)
  47. right()
  48.  
  49. def fwd_cm(dist=None):
  50. '''
  51. Move chassis fwd by a specified number of cm.
  52. This function sets the encoder to the correct number
  53. of pulses and then invokes fwd().
  54. '''
  55. if dist is not None:
  56. pulse = int(cm2pulse(dist))
  57. enc_tgt(1,1,pulse)
  58. fwd()
  59.  
  60.  
  61. def bwd_cm(dist=None):
  62. '''
  63. Move chassis bwd by a specified number of cm.
  64. This function sets the encoder to the correct number
  65. of pulses and then invokes bwd().
  66. '''
  67. if dist is not None:
  68. pulse = int(cm2pulse(dist))
  69. enc_tgt(1,1,pulse)
  70. bwd()
  71.  
  72. def cm2pulse(dist):
  73. '''
  74. Calculate the number of pulses to move the chassis dist cm.
  75. pulses = dist * [pulses/revolution]/[dist/revolution]
  76. '''
  77. wheel_circ = 2*math.pi*WHEEL_RAD # [cm/rev] cm traveled per revolution of wheel
  78. revs = dist/wheel_circ
  79. PPR = 18 # [p/rev] encoder Pulses Per wheel Revolution
  80. pulses = PPR*revs # [p] encoder pulses required to move dist cm.
  81. if en_debug:
  82. print 'WHEEL_RAD',WHEEL_RAD
  83. print 'revs',revs
  84. print 'pulses',pulses
  85. return pulses
  86. #def int sleep = .5
  87. #def int servo_angle = 45
  88. def scanArea():
  89. distance = [0, 0, 0, 0, 0]
  90. for i in range(0, 5):
  91. servo(i*36) #Moves the servo at the angles 0, 10 up to 180
  92. time.sleep(.2) #Gives time for the sensor to be in position before start measuring
  93. distance[i] = us_dist(15)
  94. time.sleep(.2) #So that the servo doesn't spas out
  95. return distance
  96.  
  97. def determineAngle(distance):
  98. m = max(distance) #finds max measured distance in the array
  99. angles = [i for i, j in enumerate(distance) if j == m]
  100. #might return two angles if the values are the same
  101. #returns indexes of largest values
  102. #indexes are used to determine the angles
  103. #enumerate creates a list [(index, value) etc]
  104. #the for loop loops through the list
  105. #the first i is the index of what it puts in the new list
  106. #the second i and the j get increased until the if statement is met
  107. #once it is met the index second i gets put in the first i
  108. #I don't fully understand it but this is a really smart way to do it
  109. return max(angles) #always picks the lowest value because it corresponds to the rightmost value
  110.  
  111.  
  112. set_speed(50)
  113.  
  114. while True:
  115. fwd()
  116. distance = scanArea()
  117. print(distance)
  118. angle = determineAngle(distance) #Angles: 0 to 8 is right, 9 is front, 10 to 18 is left
  119. print(angle)
  120. #Right 0 1 2 3 4 5 6 7 8 9* 10 11 12 13 14 15 16 17 18 Left
  121.  
  122. #this value times 10 repsents the angle to go to
  123.  
  124. rotation = 90-(angle*36) #Right will be a positive value, left will be negative
  125.  
  126. if angle < 2:
  127. #right_deg(rotation) #Rotate right by that angle
  128. right()
  129. time.sleep(1)
  130. fwd() #Go forward 10 cm
  131. elif angle > 2:
  132. #left_deg(-rotation) #Rotate left by that
  133. left()
  134. time.sleep(1)
  135. fwd()
  136. elif angle == 2:
  137. time.sleep(1)
  138. fwd()
  139.  
  140. #time.sleep(3) #Depends on the time it takes it to stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement