You can use the distance measurement method to make some pretty nifty navigation routines. This example will follow your hand (without running into it), and it can even follow another robot.
Following Propeller Boe-Bot Code
This code makes the Propeller Boe-Bot follow an object in front of it without running into it.
- Load “5 Following PropBOE-Bot.spin” into your board.
- Make sure the PWR switch is in position-2.
- Please your hand in front of it. You should be able to lead it places as well as push it away from you in different directions.
''5 Following PropBOE-Bot.spin ''Use IR LED and IR receiver to detect object presence/distance. OBJ system : "Propeller Board of Education" ' System configuration freq : "PropBOE Square Wave" ' Square wave signal generator ir : "PropBOE IR Detect" ' IR object detection drive : "PropBOE-Bot Servo Drive" ' Propeller Boe-Bot servo control time : "Timing" ' Delay and wait convenience methods VAR byte distanceL, distanceR ' Variables to store results long speedL, speedR ' Signed variables for speed calcs PUB Go ' Startup method system.Clock(80_000_000) ' System clock -> 80 MHz freq.Out(4, 1000, 3000) ' P4 sends 1 s, 3 kHz tone to speaker repeat ' Main loop repeats indefinitely distanceL := ir.Distance(13, 12) ' Check for left object distanceR := ir.Distance(0, 1) ' Check for right object speedL := distanceL * 20 - 100 ' Calculate left wheel speed speedR := distanceR * 20 - 100 ' Calculate right wheel speed drive.Wheels(speedL, speedR) ' Set wheel speeds time.Pause(20) ' Wait 20 ms & before repeating loop
How the Code Example Works
The ir.Distance method returns a value from 0 to 10. The program is set up so that 0 is way too close, 10 is way too far, and 5 is just the right distance. Take a look at speedL = distanceL * 20 – 100. If distanceL is 0, speedL = 0 * 20 – 100 = -100. Object is too close, so speedL is full speed backwards. Now what if distanceL is 5? speedL = 5 * 20 – 100 = 0. So, when the object is the right distance, the wheel stays still. If distanceL is 10, speedL = 10 * 20 – 100 = 100, which is full speed forward.
Try This
Try a few more speed calculations to figure out what effect various distances have have on wheel speed and direction.
- Repeat the speedL := distance * 20 -100 calculation for distanceL = 4. What will the wheel do?
- Do the calculation again with distanceL = 6. What would the wheel do this time?
Your Turn
Modify the code so that it uses the average of both sensors. You should be able to push it straight backwards and straight forwards then. Hints: Declare a new variable named average, and then use average := (distanceL + distanceR) / 2. Then, use average in place if distanceL and distanceR in the speed calculations.
- Modify your program to use the average of the two sensors for distance.
- Test it, you should be able to push your Propeller Boe-Bot backwards and forward depending on whether you put your hand close to or far away.
You can also create a deadband range like this:
if average < 7 and average > 3 average := 5
If you add this code at the right point in your program, it’ll prevent the robot from twitching when your hand is at the 5 distance.