Advertisement
RichPyke

Autonomous Roaming V1.2.0

Jul 23rd, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Autonomous roaming
  2. # Using Ultra Sonic Ping Sensor on servo for avoidance
  3. # Version 1.2.0
  4. # Last Updated: 23-07-2013
  5. # Author: Rich Pyke
  6. # Language: EZ-Script 2013.07.22 or later
  7. # www.EZ-Robot.com
  8.  
  9.  
  10. # ------- Configuration Settings -------
  11. # This script assumes the following ports;
  12. # Trig Port = D0
  13. # Echo Port = D1
  14. # Sweep Servo = D2
  15.  
  16. # Adjust values below for movement control
  17. $maxdistance = 30 # Change for maximum distance from object before avoiding in units
  18. $boxedindistance = 20 # Change for maximum distance for boxed in detection
  19. $turnamount = 500 # Change for how long to turn for in ms
  20. $reverseamount = 500 # Change for how long to reverse for in ms (if applicable)
  21. $movementspeed = 255 # Change for movement speed
  22. $slowturn = 127 # Change for slow turn speed
  23.  
  24. # Adjust values below for sweep configuration
  25. $sweepmin = 10 # Change for min limit
  26. $sweepmax = 90 # Change for max limit
  27. #sweepcenter automatically calculated
  28. $sweepservodelay = 500 # Change for delay between sweep servo movements and readings
  29.  
  30.  
  31. # ------- Do not adjust these values -------
  32. # Calculate the centre position for the sweeping servo
  33. # Minimum position plus maximum position divided by 2 then round to 0 decimal places
  34. $sweepcenter = Round((($sweepmin+$sweepmax)/2),0)
  35.  
  36. # Set the previous position to the max position for initial movement
  37. $sweepprevious = $sweepmax # Do not change
  38.  
  39. # Set the current position to the centre position for initial movement
  40. $sweepcurrent = $sweepcenter # Do not change
  41.  
  42. # Clear flag for IsBoxedIn
  43. $isboxedin = 0 # Do not change
  44.  
  45. # Set last and penultimate moves.
  46. $penultimatemove = "none"
  47. $lastmove = "none"
  48.  
  49.  
  50.  
  51. # ------- The Script --------
  52.  
  53. # Set the start point for any loops
  54. :begin
  55.  
  56. # Set the sweep servo speed
  57. ServoSpeed(D2, 0)
  58.  
  59. # Center the sweep servo
  60. Servo(D2, $sweepcenter)
  61.  
  62. # Start moving forwards
  63. FORWARD()
  64.  
  65.  
  66. # Start the detection
  67.  
  68. # ------- Detection code -------
  69. # Set a label for loops and gotos
  70. :detect
  71.  
  72. # Get distance
  73. $currentdistance = GetPing(D0, D1)
  74.  
  75. # Check the current distance against the max allowed distance
  76. IF ($currentdistance <= $maxdistance)
  77.  
  78. # If the current distance is below the max distance start avoiding
  79. GOTO(avoid)
  80.  
  81. # Set label for avoid return to avoid return errors
  82. :avoidreturn
  83. ENDIF
  84.  
  85. # Run the sweeping servo code
  86. GOTO(sweep)
  87.  
  88. # Wait
  89. SLEEP ($sweepservodelay)
  90.  
  91. # Loop back to the start of detection
  92. GOTO(detect)
  93.  
  94.  
  95.  
  96. # ------- Avoidance code -------
  97. # Set a label for loops and gotos
  98. :avoid
  99.  
  100. # First check if boxed in
  101. Goto(boxedin)
  102.  
  103. # If the robot is boxed in
  104. IF ($isboxedin = 1)
  105.  
  106. # Run the escape code
  107. Goto(escape)
  108.  
  109. # Avoid return error after escape loop by setting a label for a goto
  110. :escapereturn
  111.  
  112. # Otherwise just stop reverse and turn
  113. ELSE
  114.  
  115. # Stop and sleep before movement
  116. Stop()
  117. Sleep(200)
  118.  
  119. # Reverse away
  120. Reverse($movementspeed,$reverseamount)
  121.  
  122. # Check the servo position
  123.  
  124. # Check if it's to the left
  125. IF ($sweepcurrent = $sweepmin)
  126.  
  127. # If the servo is in the lowst position (left) move right
  128. Goto(moveright)
  129.  
  130. # Stop and sleep before movement
  131. Stop()
  132. Sleep(200)
  133.  
  134. # Move forwards
  135. FORWARD()
  136.  
  137. # Else check if it's to the right
  138. ELSEIF ($sweepcurrent = $sweepmax)
  139.  
  140. # If the servo is in the highest position (right) move left
  141. Goto(moveleft)
  142.  
  143. # Stop and sleep before movement
  144. Stop()
  145. Sleep(200)
  146.  
  147. # Move forwards
  148. FORWARD()
  149.  
  150. # Else assume it's in the middle
  151. ELSE
  152. # If the servo is in the center position check which side is closest to the object and move the other way
  153.  
  154. # Move and check the left side
  155. Servo(D2,$sweepmin)
  156.  
  157. # Get distance
  158. $pingmin = GetPing(D0, D1)
  159.  
  160. # Wait
  161. Sleep(400)
  162.  
  163. # Move and check the right side
  164. Servo(D2,$sweepmax)
  165.  
  166. # Get distance
  167. $pingmax = GetPing(D0, D1)
  168.  
  169. # Wait
  170. Sleep(400)
  171.  
  172. # Move and check the center
  173. Servo(D2,$sweepcenter)
  174.  
  175. # Check distances for left and right movement
  176. IF ($pingmin > $pingmax)
  177.  
  178. # If left side is closer move right
  179. Goto(moveright)
  180.  
  181. # Stop and sleep before movement
  182. Stop()
  183. Sleep(200)
  184.  
  185. # Move forwards
  186. FORWARD()
  187.  
  188. # Otherwise right side is closer
  189. ELSE
  190. # So move left
  191. Goto(moveleft)
  192.  
  193. # Stop and sleep before movement
  194. Stop()
  195. Sleep(200)
  196.  
  197. # Move forwards
  198. FORWARD()
  199. ENDIF
  200. ENDIF
  201. ENDIF
  202.  
  203. # Return to the main code
  204. Goto(avoidreturn)
  205.  
  206.  
  207.  
  208. # ------- The sweep code -------
  209.  
  210. # Set a label for loops and gotos
  211. :sweep
  212.  
  213. # Move in the correct direction and store previous position
  214.  
  215. # Check what the current position is
  216. # Check if left
  217. IF ($sweepcurrent = $sweepmin)
  218.  
  219. # Save the current position as the previous
  220. $sweepprevious = $sweepcurrent
  221.  
  222. # Move to the next position
  223. Servo(D2, $sweepcenter)
  224.  
  225. # Save the current position
  226. $sweepcurrent = GetServo(D2)
  227.  
  228. # Else check if its center and where it was before
  229. # If it is center and was left before
  230. ELSEIF ($sweepcurrent = $sweepcenter and $sweepprevious = $sweepmin)
  231.  
  232. # Save the current position as the previous
  233. $sweepprevious = $sweepcurrent
  234.  
  235. # Move to the next position
  236. Servo(D2, $sweepmax)
  237.  
  238. # Save the current position
  239. $sweepcurrent = GetServo(D2)
  240.  
  241. # If it is center and was right before
  242. ELSEIF ($sweepcurrent = $sweepcenter and $sweepprevious = $sweepmax)
  243.  
  244. # Save the current position as the previous
  245. $sweepprevious = $sweepcurrent
  246.  
  247. # Move to the next position
  248. Servo(D2, $sweepmin)
  249.  
  250. # Save the current position
  251. $sweepcurrent = GetServo(D2)
  252.  
  253. # Else check if right
  254. ELSEIF ($sweepcurrent = $sweepmax)
  255.  
  256. # Save the current position as the previous
  257. $sweepprevious = $sweepcurrent
  258.  
  259. # Move to the next position
  260. Servo(D2, $sweepcenter)
  261.  
  262. # Save the current position
  263. $sweepcurrent = GetServo(D2)
  264.  
  265. ENDIF
  266.  
  267. # Return back to the main script
  268. Return()
  269.  
  270.  
  271.  
  272. # ------- The sweep center code -------
  273.  
  274. # Set a label for loops and gotos
  275. :sweepcenter
  276.  
  277. # Move the servo to the left position
  278. Servo(D2,$sweepmin)
  279.  
  280. # Get distance
  281. $pingmin = GetPing(D0, D1)
  282.  
  283. # Wait
  284. Sleep(200)
  285.  
  286. # Move the servo to the right
  287. Servo(D2,$sweepmax)
  288.  
  289. # Get distance
  290. $pingmax = GetPing(D0, D1)
  291.  
  292. # Wait
  293. Sleep(200)
  294.  
  295. # Move the servo back to the center
  296. Servo(D2,$sweepcenter)
  297.  
  298. # Check which side has the closest object
  299. # If the object to the left is further away than the object to the right
  300. IF ($pingmin > $pingmax)
  301.  
  302. # Move to the right
  303. RIGHT($movementspeed,$turnamount)
  304.  
  305. # Move forwards again
  306. # Add in a stop and sleep before movement
  307. Stop()
  308. Sleep(200)
  309. FORWARD()
  310.  
  311. # Else if the object to the right is further away than the object to the left
  312. ELSEIF ($pingmin < $pingmax)
  313.  
  314. # Move to the left
  315. LEFT($movementspeed,$turnamount)
  316.  
  317. # Move forwards again
  318. # Add in a stop and sleep before movement
  319. Stop()
  320. Sleep(200)
  321. FORWARD()
  322.  
  323. # Else they are both the same
  324. ELSE
  325.  
  326. # So move left - this can be customised
  327. LEFT($movementspeed,$turnamount)
  328.  
  329. # And move forwards again
  330. # Add in a stop and sleep before movement
  331. Stop()
  332. Sleep(200)
  333. FORWARD()
  334. ENDIF
  335.  
  336. # Return to the main code
  337. Return()
  338.  
  339.  
  340.  
  341. # ------- The boxed in code -------
  342.  
  343. # Set a label for loops and gotos
  344. :boxedin
  345.  
  346. # Get distance to the side
  347. # Move the servo to the left
  348. Servo(D2,$sweepmin)
  349.  
  350. # Get distance
  351. $side1scan = GetPing(D0, D1)
  352.  
  353. # Get distance to the other side
  354. # Move the servo to the right
  355. Servo(D2,$sweepmax)
  356.  
  357. # Get distance
  358. $side2scan = GetPing(D0, D1)
  359.  
  360. # Get distance to the front
  361. # Move the servo to the center
  362. Servo(D2,$sweepcenter)
  363.  
  364. # Get distance
  365. $centerscan = GetPing(D0, D1)
  366.  
  367. # Check if boxed in by compairing the results against a fixed boxed in distance
  368. IF ($side1scan < $boxedindistance and $side2scan < $boxedindistance and $centerscan < $boxedindistance)
  369.  
  370. # If any are true set the boxed in flag
  371. $isboxedin = 1
  372.  
  373. ENDIF
  374.  
  375. # Return to the main script
  376. Return()
  377.  
  378.  
  379.  
  380. # ------- The escape code -------
  381.  
  382. # Set a label for loops and gotos
  383. :escape
  384.  
  385. # Reset the boxed in flag
  386. $isboxedin = 0
  387.  
  388. # Center the sweep servo
  389. Servo(D2,$sweepcenter)
  390.  
  391. # Turn slowly
  392. Left($slowturn)
  393.  
  394. # Set up a loop
  395. :escapeloop
  396.  
  397. # Scan until clear
  398. # Get distance
  399. $escapescan = GetPing(D0, D1)
  400.  
  401. # If the scan result is below the boxed in distance loop
  402. IF ($escapescan < $BoxedInDistance)
  403.  
  404. # Go back to the start of the escape loop
  405. Goto(escapeloop)
  406.  
  407. ENDIF
  408.  
  409. # Once clear stop and sleep before movement
  410. Stop()
  411. Sleep(200)
  412.  
  413. # Move forwards
  414. FORWARD()
  415.  
  416. # Return to the main script
  417. Goto(escapereturn)
  418.  
  419.  
  420.  
  421. # ------- Move Right code -------
  422.  
  423. # Set a label for loops and gotos
  424. :moveright
  425.  
  426. # Check the last 2 moves to avoid left right left right loops
  427. IF ($lastmove = "left" and $penultimatemove = "right")
  428.  
  429. # If it has been right then left dont move right again but escape from a loop
  430. Goto(escape)
  431.  
  432. # Reset the last move
  433. $lastmove = "none"
  434.  
  435. # Else just move right
  436. ELSE
  437. RIGHT($movementspeed,$turnamount)
  438. Sleep(200)
  439.  
  440. # Save the penultimate move
  441. $penultimatemove = $lastmove
  442.  
  443. # Save the last move
  444. $lastmove = "right"
  445.  
  446. ENDIF
  447.  
  448. # Go back to the main script
  449. Return()
  450.  
  451.  
  452.  
  453. # ------- Move left code -------
  454.  
  455. # Set a label for loops and gotos
  456. :moveleft
  457.  
  458. # Check the last 2 moves to avoid left right left right loops
  459. IF ($lastmove = "right" and $penultimatemove = "left")
  460.  
  461. # If it has been left then right dont move left afain but escape from a loop
  462. Goto(escape)
  463.  
  464. # Reset the last move
  465. $lastmove = "none"
  466.  
  467. # Else just move left
  468. ELSE
  469. LEFT($movementspeed,$turnamount)
  470. Sleep(200)
  471.  
  472. # Save the penultimate move
  473. $penultimatemove = $lastmove
  474.  
  475. # Save the last move
  476. $lastmove= "left"
  477.  
  478. ENDIF
  479.  
  480. # Go back to the main script
  481. Return()
  482.  
  483. # ------- End of scripts -------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement