SHOW:
|
|
- or go back to the newest paste.
| 1 | - | # Ping Object Avoidance |
| 1 | + | # Autonomous roaming |
| 2 | # Using Ultra Sonic Ping Sensor on servo for avoidance | |
| 3 | - | # Version 1.1.2 |
| 3 | + | # Version 1.2.0 |
| 4 | # Last Updated: 23-07-2013 | |
| 5 | # Author: Rich Pyke | |
| 6 | - | # Configuration Settings |
| 6 | + | # Language: EZ-Script 2013.07.22 or later |
| 7 | # www.EZ-Robot.com | |
| 8 | - | # |
| 8 | + | |
| 9 | ||
| 10 | # ------- Configuration Settings ------- | |
| 11 | # This script assumes the following ports; | |
| 12 | # Trig Port = D0 | |
| 13 | # Echo Port = D1 | |
| 14 | - | $reverseturn = 1 # Reverse before turn? 0 = no, 1 = yes |
| 14 | + | |
| 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 | - | # Testing Options |
| 27 | + | #sweepcenter automatically calculated |
| 28 | - | $testmode = 0 # Change to 1 if testing without PING sensor |
| 28 | + | |
| 29 | ||
| 30 | ||
| 31 | # ------- Do not adjust these values ------- | |
| 32 | - | # Do not adjust these values |
| 32 | + | |
| 33 | # Minimum position plus maximum position divided by 2 then round to 0 decimal places | |
| 34 | - | # Minimum position plus maximum position divided by 2 |
| 34 | + | |
| 35 | - | # Round to 0 decimal places |
| 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 | - | # Set some flags for testing - to be removed in final release |
| 41 | + | |
| 42 | - | $SweepErrorFlag = 0 # Do not change |
| 42 | + | # Clear flag for IsBoxedIn |
| 43 | - | $BoxedInErrorFlag = 0 # Do not change |
| 43 | + | |
| 44 | - | $BoxedInRun = 0 # Do not change |
| 44 | + | |
| 45 | - | $SweepCenterRun = 0 # Do not change |
| 45 | + | |
| 46 | - | $EscapeRun = 0 # Do not change |
| 46 | + | |
| 47 | - | $ScriptErrorFlag = 0 # Do not change |
| 47 | + | |
| 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 | - | Goto(detect) # This line is redundant but left for educational purposes |
| 67 | + | |
| 68 | # ------- Detection code ------- | |
| 69 | # Set a label for loops and gotos | |
| 70 | :detect | |
| 71 | - | # Detection code |
| 71 | + | |
| 72 | # Get distance | |
| 73 | $currentdistance = GetPing(D0, D1) | |
| 74 | ||
| 75 | # Check the current distance against the max allowed distance | |
| 76 | - | # Use random numbers if in test mode - to be removed in final release |
| 76 | + | |
| 77 | - | IF ($testmode=0) |
| 77 | + | |
| 78 | - | $currentdistance = GetPing(D0, D1) |
| 78 | + | |
| 79 | GOTO(avoid) | |
| 80 | - | $currentdistance = GetRandom(0,255) |
| 80 | + | |
| 81 | # Set label for avoid return to avoid return errors | |
| 82 | :avoidreturn | |
| 83 | - | # Check the current distance against the max allowed distance |
| 83 | + | |
| 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 | - | # Avoidance code |
| 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 | - | # If the robot is boxed in run the escape code |
| 111 | + | |
| 112 | # Otherwise just stop reverse and turn | |
| 113 | ELSE | |
| 114 | ||
| 115 | # Stop and sleep before movement | |
| 116 | Stop() | |
| 117 | Sleep(200) | |
| 118 | - | # Change to else to avoid reversing and turning after an escape - dont forget the endif at the bottom |
| 118 | + | |
| 119 | # Reverse away | |
| 120 | Reverse($movementspeed,$reverseamount) | |
| 121 | - | # Check to see if to reverse before turning |
| 121 | + | |
| 122 | - | IF ($reverseturn = 1) |
| 122 | + | |
| 123 | - | # If the option of reverse before turning is set stop then reverse |
| 123 | + | |
| 124 | - | # Add in a stop and sleep before movement |
| 124 | + | |
| 125 | IF ($sweepcurrent = $sweepmin) | |
| 126 | ||
| 127 | - | Reverse($movementspeed,$reverseamount) |
| 127 | + | |
| 128 | Goto(moveright) | |
| 129 | ||
| 130 | # Stop and sleep before movement | |
| 131 | Stop() | |
| 132 | Sleep(200) | |
| 133 | ||
| 134 | # Move forwards | |
| 135 | FORWARD() | |
| 136 | - | # Continue moving forwards |
| 136 | + | |
| 137 | - | # Add in a stop and sleep before movement |
| 137 | + | |
| 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 | - | # Continue moving forwards |
| 146 | + | |
| 147 | - | # Add in a stop and sleep before movement |
| 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 | - | # Use random numbers if in test mode |
| 158 | + | $pingmin = GetPing(D0, D1) |
| 159 | - | IF ($testmode=0) |
| 159 | + | |
| 160 | - | $pingmin = GetPing(D0, D1) |
| 160 | + | |
| 161 | Sleep(400) | |
| 162 | - | $pingmin = GetRandom(0,255) |
| 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 | - | # Use random numbers if in test mode |
| 170 | + | |
| 171 | - | IF ($testmode=0) |
| 171 | + | |
| 172 | - | $pingmax = GetPing(D0, D1) |
| 172 | + | |
| 173 | Servo(D2,$sweepcenter) | |
| 174 | - | $pingmax = GetRandom(0,255) |
| 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 | - | # Add in a stop and sleep before movement |
| 184 | + | |
| 185 | # Move forwards | |
| 186 | FORWARD() | |
| 187 | ||
| 188 | # Otherwise right side is closer | |
| 189 | ELSE | |
| 190 | - | # Add in a stop and sleep before movement |
| 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 | - | # The sweep code |
| 203 | + | |
| 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 | - | # Else something has gone wrong |
| 260 | + | |
| 261 | ||
| 262 | - | # Set an error flag for debugging purposes - to be removed for final release |
| 262 | + | |
| 263 | - | $SweepErrorFlag = 1 |
| 263 | + | |
| 264 | ||
| 265 | ENDIF | |
| 266 | ||
| 267 | # Return back to the main script | |
| 268 | Return() | |
| 269 | ||
| 270 | ||
| 271 | - | # The sweep center code |
| 271 | + | |
| 272 | # ------- The sweep center code ------- | |
| 273 | ||
| 274 | # Set a label for loops and gotos | |
| 275 | :sweepcenter | |
| 276 | - | # Set a flag so we know it has run for debugging purposes |
| 276 | + | |
| 277 | - | $SweepCenterRun = 1 |
| 277 | + | |
| 278 | Servo(D2,$sweepmin) | |
| 279 | ||
| 280 | # Get distance | |
| 281 | $pingmin = GetPing(D0, D1) | |
| 282 | - | # Use random numbers if in test mode |
| 282 | + | |
| 283 | - | IF ($testmode=0) |
| 283 | + | |
| 284 | - | $pingmin = GetPing(D0, D1) |
| 284 | + | |
| 285 | ||
| 286 | - | $pingmin = GetRandom(0,255) |
| 286 | + | |
| 287 | Servo(D2,$sweepmax) | |
| 288 | ||
| 289 | # Get distance | |
| 290 | $pingmax = GetPing(D0, D1) | |
| 291 | ||
| 292 | # Wait | |
| 293 | Sleep(200) | |
| 294 | ||
| 295 | - | # Use random numbers if in test mode |
| 295 | + | |
| 296 | - | IF ($testmode=0) |
| 296 | + | |
| 297 | - | $pingmax = GetPing(D0, D1) |
| 297 | + | |
| 298 | # Check which side has the closest object | |
| 299 | - | $pingmax = GetRandom(0,255) |
| 299 | + | |
| 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 | - | # The boxed in code |
| 351 | + | |
| 352 | ||
| 353 | # Get distance to the other side | |
| 354 | # Move the servo to the right | |
| 355 | Servo(D2,$sweepmax) | |
| 356 | - | # Set a flag so we know it has run for debugging - to be removed for final release |
| 356 | + | |
| 357 | - | $BoxedInRun = 1 |
| 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 | - | # Use random numbers if in test mode - to be removed for final release |
| 363 | + | |
| 364 | - | IF ($testmode=0) |
| 364 | + | # Get distance |
| 365 | $centerscan = GetPing(D0, D1) | |
| 366 | ||
| 367 | - | $side1scan = GetRandom(0,255) |
| 367 | + | |
| 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 | - | # Use random numbers if in test mode |
| 374 | + | |
| 375 | - | IF ($testmode=0) |
| 375 | + | |
| 376 | - | $side2scan = GetPing(D0, D1) |
| 376 | + | |
| 377 | ||
| 378 | - | $side2scan = GetRandom(0,255) |
| 378 | + | |
| 379 | ||
| 380 | # ------- The escape code ------- | |
| 381 | ||
| 382 | # Set a label for loops and gotos | |
| 383 | :escape | |
| 384 | ||
| 385 | - | # Use random numbers if in test mode |
| 385 | + | |
| 386 | - | IF ($testmode=0) |
| 386 | + | |
| 387 | - | $centerscan = GetPing(D0, D1) |
| 387 | + | |
| 388 | # Center the sweep servo | |
| 389 | - | $centerscan = GetRandom(0,255) |
| 389 | + | |
| 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 | - | # The escape code |
| 405 | + | |
| 406 | ||
| 407 | ENDIF | |
| 408 | ||
| 409 | # Once clear stop and sleep before movement | |
| 410 | - | # Set a flag so we know it has run for debugging |
| 410 | + | |
| 411 | - | $EscapeRun = 1 |
| 411 | + | |
| 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 | - | # Use random numbers if in test mode |
| 426 | + | |
| 427 | - | IF ($testmode=0) |
| 427 | + | |
| 428 | - | $escapescan = GetPing(D0, D1) |
| 428 | + | |
| 429 | # If it has been right then left dont move right again but escape from a loop | |
| 430 | - | $escapescan = GetRandom(0,255) |
| 430 | + | |
| 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 | - | # Continue forwards |
| 441 | + | |
| 442 | - | # Add in a stop and sleep before movement |
| 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 | - | # Move Right code |
| 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 ------- |