+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Boe-Bot + Ping))) Rangefinder - Scan for and Go to Closest Object

  1. #1

    Default Boe-Bot + Ping))) Rangefinder - Scan for and Go to Closest Object

    Scan For and Go To Closest Object
    ·
    In Ping)))Dar – A Radar Style Display, we used the debug terminal to get an idea of what objects look like when the Boe-Bot scans them with the Ping))) Ultrasonic Rangefinder and Mounting Bracket kits. In this activity, we'll use the same setup along with a program that makes the Boe-Bot scan a field of objects and go to the closest one. Figure 1 shows the Boe-Bot's “scan and go to” sequence as it runs GotoClosestObject.bs2. Here's what it does:
    ·
    ······· (The Boe-Bot pauses for 2 seconds after the RESET button on the Board of Education is pressed and released.
    ······· The Boe-Bot directs the Ping))) Ultrasonic Rangefinder to the right (0°).
    ······· It scans from right to left (180°) while taking successive distance measurements and saving the angle and distance of the closest measurement it found.
    ······· The Boe-Bot then points the Ping))) rangefinder in the direction of its closest measurement.
    ······· The Ping))) is then swept to the right in smaller increments until the object's right edge is detected.
    ······· The Ping))) is swept to the left (in the same small increments) until the object's left edge is detected.
    ······· The Boe-Bot calculates the middle of the object and turns in place to face it.
    ······· The Boe-Bot goes to the object.


    View Video Introduction (YouTube)

    Download Source Code – Scan For and Go To Closest Object
    ·
    Figure 1 -Finding the Angle that Points Toward the Middle of the Object



    Getting Started

    This project contains advanced topics, not intended for Boe-Bot beginners.· Before continuing, it is highly recommended you complete the following tasks in order to gain a better understanding on how this application works:
    ·
    ······· Complete all activities in What’s a Microcontroller
    ······· Complete at minimum Chapters 1-4 in Robotics with the Boe-Bot
    ······· Complete at minimum Chapters 1-3 in Smart Sensors and Applications
    ······· Download the product documentation and assembly instructions for the PING))) Bracket Kit
    ······· Complete the Ping)))Dar – A Radar Style Display forum post

    Parts Required

    (1) Fully assembled and tested Boe-Bot® Robot
    (1) Ping))) Ultrasonic Rangefinder
    (1) Ping))) Mounting Bracket Kit

    Finding, Facing, and Going to the Closest Object



    ·
    Finding the closest object is pretty simple. Start by initializing a word variable, named minDistin GotoClosestObject.bs2, to 65536. Then, repeatedly take distance measurements with the Ping))) rangefinder while sweeping the mounting bracket servo from right to left. Each time the current distance measurement is less than minDist, copy the current distance measurement to minDist, and also copy pingDir, which is the mounting bracket servo control pulse duration to a variable named markDir. When the sweep is done, minDistwill store the closest distance, but more importantly, markDir will store the pulse duration that made the mounting bracket servo point the rangefinder at the closest object. As we saw in the previous activity, once the pulse duration to the mounting bracket servo is known, the angle the mounting bracket servo has pointed the rangefinder to can be calculated.

    Single_Sweep:
    ·
    · DO UNTIL pingDir >= LimitLeft
    ··· GOSUB Sweep_Increment
    ··· GOSUB Get_Ping_Cm
    ··· IF distance < minDist THEN
    ····· minDist = distance
    ····· markDir = pingDir
    ··· ENDIF
    ··LOOP
    ·
    · RETURN
    ·
    A quick sweep with fairly large servo increments may not be enough for a heading toward the middle of the closest object. Next, GotoClosestObject.bs2 points the rangefinder in the direction where it took its closest measurement. Then, it scans to the·right until the measured distance is more than 10 cm beyond than the closest distance measurement shown in Figure 3 (a). After that, it scans to the left until the measured distance is 10 cm beyond the closest distance shown in Figure 3 (b). The program marks both these servo pulse durations, and calculates the average of the two. This gives the Boe-Bot's BASIC Stamp a pulse duration to make the mounting bracket servo point the rangefinder to the object about half way between the two edges as shown in Figure 3 (c).
    ·
    Figure 3 - Finding the Angle that Points Toward the Middle of the Object

    Although the Ping))) Mounting Bracket servo is now pointing the Ping))) Ultrasonic Rangefinder directly at the object, it's not the right angle to help the Boe-Bot turn to face the object. Figure 4 shows why. Notice that θ1ping-object) is much less than θ2Boe-Bot-object). So, before facing the object, the program has to figure out what θ2 is given θ1 and the object's distance (d), which is (d··θ1) in polar coordinates.

    Figure 4 -PULSOUT Duration Argument vs. Rotation Angle

    Ping)))Dar.bs2 demonstrated how to make polar to Cartesian coordinate conversions using x = d cos(&#952;) and y = d sin(&#952;). Assuming the Boe-Bot's continuous rotation servos are mounted so that their output shafts are toward the back of the Boe-Bot, the offset between the front of the Ping))) rangefinder and the Boe-Bot's axis is about 7 cm. If they are mounted with their output shafts toward the front of the Boe-Bot, the offset is more like 5 cm. Assuming 7 cm, after the program calculates the x and y centimeter distances, all it has to do is add 7 cm to the y distance, and those are the x and y distances for θ2. Then, θ2Boe-Bot-Object) can be calculated using the inverse tangent of the (y + 7 cm) ÷ x distances.


    Once the program has figured out θ2, all it has to do to is face the object and make the continuous rotation servos cause the Boe-Bot to turn in place for that number of binary radians. One of the calibration programs in this activity involves figuring out the continuous rotation servo PULSOUT Duration arguments that will make the Boe-Bot turn 128 brads (180°) with 128 pulses. The constants that are determined will then be used in GotoClosestObject.bs2 to give the Boe-Bot turn-in-place control in terms of brads.

    Software Continuous Rotation Servo Calibration

    In Ping)))Dar, you adjusted the Ping))) Mounting Bracket servo's hardware to make sure it points straight forward when it receives PULSOUT 14, 750 signals. You also adjusted LimitLeft and LimitRight CON directives to make sure the mounting bracket servo scanned the Ping))) rangefinder across a 180° field in front of the Boe-Bot. There are two more sets of CON directives that have to be updated, and they will be used in PULSOUT commands that control the continuous rotation servos that turn the Boe-Bot's wheels. These CON directives are FwdLeftFast, FwdRightFast, RotateLeft, and RotateRight. FwdLeftFast and FwdRightFast are used in PULSOUT commands to make the Boe-Bot drive forward at top speed. They have to be adjusted so that they make the Boe-Bot roll straight forward (instead of a curved path that's mostly forward) to make sure the Boe-Bot can roll straight to the object. RotateLeft and RotateRight have to be adjusted so that they make the Boe-Bot turn 180° (128 brads) when 128 pulses are delivered. This adjustment will make it possible for GotoClosestObject.bs2 to turn the Boe-Bot to the correct angle so that it faces the closest object before driving up to it.




    CON Directives for Adjusting Boe-Bot Maneuvers

    Below are the two PULSOUT commands along with the PAUSE command to send signals to the Boe-Bot's continuous rotation servos that make them turn full speed forward. The number of times a loop repeats these signals dictates how long the servos will turn full speed forward. The speed at which each servo turns is controlled by the PULSOUT commands' Durationarguments, which are 850 and 650 in the example. Since one servo typically turns a little faster than the other at full speed, the 850 or 650 values have to be adjusted to equalize the speeds the wheels turn.

    PULSOUT 13, 850

    PULSOUT 12, 650
    PAUSE 20
    The CON directives below make these values adjustable, which is especially useful when they are used more than one place in the program. All you have to do is change a CON directive, and any PULSOUT command that uses the name for Duration argument will automatically be updated. For example, if you change the value of the FwdLeftFastCON directive from 850 to 820, the command PULSOUT BotServoLeft, FwdFastLeft will be changed from PULSOUT 13, 850 to PULSOUT 13, 820. The net effect will be that the left wheel will rotate slower when the program runs.

    BotServoLeft ·PIN 13 ··················· ' Boe-Bot servo pins

    BotServoRight PIN 12
    .
    .
    .
    Center ·······CON 750 ·················· ' Center servo pulse width
    FwdLeftFast ··CON 850 ·················· ' Fast settings - straight ahead
    FwdRightFast ·CON 650
    RotateRight ··CON 758 ·················· ' Boe-Bot rotate right pulse
    RotateLeft ···CON 729 ·················· ' Boe-Bot rotate left pulse
    .
    .
    .
    · PULSOUT BotServoLeft, RotateRight
    · PULSOUT BotServoRight, RotateRight
    · PAUSE 20
    .
    .
    .
    · PULSOUT BotServoLeft, FwdLeftFast
    · PULSOUT BotServoRight, FwdRightFast
    · PAUSE 20
    .
    .
    .

    Example Program − CalibrateStraightForward.bs2

    In this calibration task, you have to observe the Boe-Bot's forward travel and determine if it is tending to curve to either the left or right. Then, adjust the FwdLeftFast or FwdRightFast CON directives to correct any tendency toward traveling in a curve. To make the correction, adjust the CON directive for the faster wheel to make its speed equal to that of the slower wheel.


    ·
    ······· Run CalibrateStraightForward.bs2 and watch the Boe-Bot as it travels forward. Does it curve to the left, or right, or is its travel almost perfectly straight?
    ······· If the Boe-Bot already goes in a straight line (almost no curving whatsoever), skip to the next section: Example Program - Calibrate180DegreeTurns.bs2.
    ······· If the Boe-Bot curves to the left, the right wheel is going too fast. Try running the program with FwdRightFast set to 660. Still curving left? Try 670 to make it slower still. Still curving left? Try 680. Keep bringing the FwdRightFast value closer to 750 and running and testing the program until it either makes the Boe-Bot travel in a straight line, or it starts curving to the right instead. If the Boe-Bot starts to curve to the right, try a value between the current and previous FwdRightFast settings. Fine tune the value until it makes the Boe-Bot go in the straightest path possible.
    ······· If the Boe-Bot curves to the right, the left wheel is going too fast. Try running the program with FwdLeftFast set to 840. Still curving right? Try 830 to make it slower still. Still curving right? Try 820. Keep bringing the FwdLeftFast value closer to 750 and running and testing the program until it either makes the Boe-Bot travel in a straight line, or it starts curving to the left instead. If the Boe-Bot starts to curve to the left, try a value between the current and previous FwdLeftFast settings. Fine tune the value until it makes the Boe-Bot go in the straightest path possible.
    ······· Save your modified program and make a note of your FwdLeftFast and FwdRightFast CON directives so that you can update them in the upcoming navigation program, GotoClosestObject.bs2.

    Code:
    ' Smart Sensors and Applications - CalibrateStraightForward.bs2
     
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    BoeBotServoL PIN 13
    BoeBotServoR PIN 12
     
    ' Adjust these constants until the Boe-Bot goes full speed forward
    ' in a straight line.
     
    FwdLeftFast CON 850
    FwdRightFast CON 650
     
    counter VAR Byte
     
    FOR counter = 0 TO 250
      PULSOUT BoeBotServoL, FwdLeftFast
      PULSOUT BoeBotServoR, FwdRightFast
      PAUSE 20
    NEXT
     
    END

    Example Program − Calibrate180DegreeTurns.bs2

    This calibration task involves adjusting CON directives for PULSOUT command Durationarguments that will make the Boe-Bot rotate 128 brads (180°) when 128 pulses are sent to its continuous rotation drive servos. These constants will likely be different for left and right rotation, so Calibrate180DegreeTurns.bs2 has separate RotateRight and RotateLeft CON directives. By adjusting the values of the RotateRight and RotateLeft CON directives, you will adjust how far the Boe-Bot rotates left and right when it receives 128 pulses. The goal again is to adjust the constants until the program makes the Boe-Bot execute a 180° right turn that's as close to perfect as you can get it, followed by an equally perfect 180° left turn.



    ······· Open and run Calibrate180DegreeTurns.bs2 and watch how far the Boe-Bot turns to the left and then to the right. The calibration goal is for it to turn 180° to the right, then 180° to the left.
    ······· If the Boe-Bot turns more than 180° to the right, reduce the value of the RotateRight CON directive, and re-run the program. If it turns less than 180° to the right, increase the RotateRight CON directive. Repeat until you have tuned it to turn 180° to the right.
    ······· For tuning left rotation, if the Boe-Bot turns too far, increase the RotateLeft CON directive, or if it doesn't turn far enough, decrease the RotateLeft CON directive.
    ······· Keep adjusting the RotateRight and RotateLeft CON directives until your Boe-Bot executes perfect (or at least really good) 180° left and right turns.
    ······· Save your modified program, and be prepared to use those values when you update the constants in GoToClosestObject.bs2.

    Code:
    ' Smart Sensors and Applications - Calibrate180DegreeTurns.bs2
     
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    BotServoLeft  PIN 13                                                 ' Left drive servo
    BotServoRight PIN 12                                                 ' Right drive servo
     
    RotateRight   CON 765                                               ' Boe-Bot rotate right pulse
    RotateLeft    CON 735                                               ' Boe-Bot rotate left pulse
     
    counter VAR Word
     
    FOR counter = 1 TO 128
      PULSOUT BotServoLeft, RotateRight
      PULSOUT BotServoRight, RotateRight
      PAUSE 20
    NEXT
     
    FOR counter = 127 TO 0
      PULSOUT BotServoLeft, RotateLeft
      PULSOUT BotServoRight, RotateLeft
      PAUSE 20
    NEXT
     
    END

    Going to the Closest Object

    Once GotoClosestObject.bs2's CON directives that control the Boe-Bot's servos are updated, it should perform a respectable demo for objects 1 meter away or less. It also has code that deals with the anomalies you experimented with in the Ping)))Dar such as when two objects are so close together that they look like one object in the Ping)))Dar Debug Terminal display. In addition, GoToClosestObject.bs2 has code that can figure out if the closest object is slightly outside its field of detection to the left or right, and it can adjust the Boe-Bot's position and re-scan the object before going straight to it.

    Example Program − GoToClosestObject.bs2

    As you went through this and Ping)))Dar, you used TestPingDirection.bs2, CalibrateStraightForward.bs2, and Calibrate180DegreeTurns.bs2 to determine values for the calibration constants listed below. GoToClosestObject.bs2 relies on these eight constants to scan a field of objects, determine which one is closest, and accurately turn to face and go to the object. The calibration results for these CON directives have to be updated GotoClosestObject.bs2 before you run it.
    ········· LimitLeft
    ········· LimitRight
    ········· PingDirToAngle
    ········· ForwardLeftFast
    ········· ForwardRightFast
    ········· RotateRight
    ········· RotateLeft
    ·
    ······· IMPORTANT: Make sure to update GoToClosestObject.bs2's CONdirectives for the eight names listed above with the values you determined as you went through this and the previous activity.
    ······· Measure the distance from the front of the Ping))) rangefinder to the center of the Boe-Bot's turning axis. It should either be 7 cm or around 5 cm. If it's 5 cm, update the PingAxleOffsetCONdirective accordingly.
    ······· Save and run your modified version of GoToClosestObject.bs2.
    ······· Start by testing it with a single, cylindrical object 1 ft (30 cm) away from the Boe-Bot at various angles, such as 60°, 90°, and 120°.
    ······· In each case, the Boe-Bot should be able to scan its surroundings, find the object, and go straight to it. If it doesn't succeed, revisit the various calibration instructions in this and the previous activity. Then, use the values you collected, and update the CONdirectives in GoToClosestObject.bs2 and try again.
    ······· When the Boe-Bot can reliably go to one object that's 1 ft (30 cm) away, try a variety of other angles and distances. How far out can the Boe-Bot detect and go to an object?
    ······· Try 1 object 1 ft (30 cm) away from the Boe-Bot at 120° and the other one 2 ft (60 cm) away from the Boe-Bot at 60°. The Boe-Bot should go straight to the one at 120°. Swap the distances and try again; the Boe-Bot should go to the one at 60°.
    ······· Try two objects 1 ft (30 cm) apart and 1.5 ft (45 cm) from the front of the Boe-Bot. The Boe-Bot will initially think it sees a single object and attempt to go between the two cylinders. As it gets closer, it should detect the gap between them and choose the one it's closest to.
    ······· Try one object one ft (30 cm) away at 0° and the other object 2 ft (60 cm) away at 180 degrees. The Boe-Bot should discover that the right edge of the object at 0° is outside its field of detection. So it should rotate and re-scan the object, and then go directly to it.

    Code:
    ' -----[ Title ]--------------------------------------------------------------
    ' Smart Sensors and Applications - GoToClosestObject.bs2
    ' Sweep Ping))) Ultrasonic Rangefinder across 180-degrees and find the closest
    ' object.  Then calculate and execute the turn required to face the object.
    ' Travel forward until the object is less than or equal to 5 cm from the front
    ' of the rangefinder.
     
    ' IMPORTANT: This program has several constants that have to be tuned before
    '            it will work right.  Follow the instructions in Smart Sensors
    '            and Applications, Chapter 8, Activity #4 and #5 before you run
    '            this program!
     
    ' {$STAMP BS2}                               ' Target device = BASIC Stamp 2
    ' {$PBASIC 2.5}                              ' Language = PBASIC 2.5
     
    ' -----[ I/O Definitions ]----------------------------------------------------
     
    ' Ping))) Ultrasonc Rangefinder and Mounting Bracket
    PingServo      PIN     14                    ' Servo that directs Ping))) snsr
    Ping           PIN     15                    ' Ping))) sensor signal pin
     
    ' Boe-Bot servo pins (Left and right are from driver's seat perspective.)
    BotServoLeft   PIN     13                    ' Left drive servo
    BotServoRight  PIN     12                    ' Right drive servo
     
    ' -----[ Constants ]----------------------------------------------------------
     
    ' Ping))) mounting bracket constants
    LimitLeft      CON     1250 ' Bracket 90-degrees Left
    LimitRight     CON     250                  ' Bracket 90-degrees Right
    Center         CON     750        ' Center/0-degree pulse duration
    Increment      CON     15         ' Increment for pulse sweeping
    MinSweep       CON     40         ' Pulses -> 90-degree right
     
    ' Boe-Bot continuous rotation servo control constants
    FwdLeftFast    CON     850                  ' Fast settings - straight ahead
    FwdRightFast   CON     650
    RotateRight    CON     765                  ' Boe-Bot rotate right pulse
    RotateLeft     CON     735                  ' Boe-Bot rotate left pulse
    BtwnPulses     CON     20                    ' ms between servo pulses
     
    ' Ping))) Ultrasonic Rangefinder constants
    CmConstant     CON     2260       ' Echo time -> cm with **
    SinCosTo256    CON             517       ' For */ -127..127 -> -256..256
    Ms20           CON             330       ' 20 ms worth of cm
     
    ' MSB sign (twos complement)
    Negative       CON     1          ' Negative sign (bit-15)
    Positive                 CON     0       ' Positive sign (bit-15)
     
    ' Ping and turning axis geometry
    PingDirToAngle CON     8454       ' Servo pulse to angle ** con
    PingAxleOffset CON     7          ' 7 cm between ping))) and axis
     
    ' -----[ Variables ]----------------------------------------------------------
     
    time           VAR     Word ' Ping))) echo time
    pingDir        VAR     Word ' Pulse duration -> direction
    x              VAR     Word ' x coordinate
    y              VAR     Word ' y coordinate
    distance       VAR     Time ' Object centimeter distance
    markDir        VAR     y          ' Pulse points to closest object
    rightMark      VAR     x          ' Pulse to object's right side
    leftMark       VAR     pingDir    ' Pulse to object's left side
    pulses         VAR     x          ' +/- brads to turn toward object
    PrevDist       VAR     Byte ' Previous distance measurement
    angle          VAR     Byte ' Servo angle from right in brads
    counter        VAR     angle      ' Loop counter
    minDist        VAR     angle      ' Minimum distance measurement
    sweepInc       VAR     Nib        ' Increment for servo sweep
    sweepDir       VAR     Bit        ' Increment/decrement pingDir
    xSign          VAR     Bit        ' Stores sign of x variable
    ySign          VAR     xSign      ' Stores sign of x variable
    pSign          VAR     Bit        ' Sign of pulses variable
    edgesFound     VAR     Bit        ' Navigation flag
     
    ' -----[ Initialization ]-----------------------------------------------------
     
    PAUSE 2000                                             ' Delay program start by 2 s.
     
    ' -----[ Main Routine ]-------------------------------------------------------
     
    GOSUB Get_Ping_Cm                            ' First distance measurement
     
    DO UNTIL distance <= 5                       ' Repeat until distance <= 5 cm
     
      edgesFound = 0                             ' Clear edges found flag
     
      DO UNTIL edgesFound = 1                    ' Repeat until edges found = 1
        GOSUB Face_Closest_Object                ' Find & face closest object
      LOOP
     
      GOSUB Get_Ping_Cm                          ' Get current distance
     
      DO UNTIL distance <= 5                     ' Drive toward object
        prevDist = distance MAX 255              ' Current distance -> previous
        GOSUB Get_Ping_Cm                        ' Get new distance
        PULSOUT BotServoLeft, FwdLeftFast        ' Boe-Bot forward
        PULSOUT BotServoRight, FwdRightFast
        PAUSE BtwnPulses - (distance / Ms20)     ' 20 ms pause between pulses
        IF distance >= prevDist + 5 THEN EXIT    ' Exit if distance increasing
      LOOP
     
    LOOP                                         ' Main routine's outermost loop
     
    END
     
    ' -----[ Subroutines - BoeBot_Turn_Brads ]-----------------------------------
     
    ' Boe-Bot turns a certain number of binary radians to face an object.
     
    BoeBot_Turn_Brads:
     
      IF pSign = Positive THEN
        FOR counter = 0 TO ABS(pulses)
          PULSOUT BotServoLeft, RotateRight
          PULSOUT BotServoRight, RotateRight
          PAUSE BtwnPulses - (distance / Ms20)
        NEXT
      ELSE
        FOR counter = 0 TO ABS(pulses)
          PULSOUT BotServoLeft, RotateLeft
          PULSOUT BotServoRight, RotateLeft
          PAUSE BtwnPulses - (distance / Ms20)
        NEXT
      ENDIF
     
      RETURN
     
    ' -----[ Subroutines - Face_Closest_Object ]----------------------------------
     
    ' Scan for closest object using a Ping))) rangefinder mounted on a standard
    ' servo.  Locate the middle fo the object, and turn Boe-Bot to face it.
     
    Face_Closest_Object:
     
      ' Initialize sweep increment.
      sweepInc = Increment
     
      ' Start Servo rotated to the far right.
      pingDir = LimitRight
      GOSUB Point_At_PingDir
     
      ' Make minDist large and sweepDir positive (0).  Single_Sweep sweeps
      ' left -> right while measuring object distances and stores the direction
      ' of the closest object in markDir.
      minDist = 65535
      sweepDir = Positive
      GOSUB Single_Sweep
     
      ' Point the servo in the direction of the closest distance measurement.
      pingDir = markDir
      GOSUB Point_At_PingDir
     
      ' Scan to find object's right side.
      GOSUB Find_Right_Side
      IF edgesFound = 0 THEN RETURN
     
      ' Point the servo in the direction of the closest distance measurement.
      pingDir = markDir
      GOSUB Point_At_PingDir
     
      ' Scan to find object's right side.
      GOSUB Find_Left_Side
      IF edgesFound = 0 THEN RETURN
     
      ' Average the angles to the object's left and right sides.  That's the
      ' middle of the object.  Point rangefinder in that direction.
      pingDir = leftMark + rightMark / 2
      GOSUB Point_At_PingDir
     
      ' At this point, the Ping))) should be pointing directly at the closest
      ' object.
     
      ' Calculate the angle to the object's angle in brads, and turn the
      ' Boe-Bot to face that angle.
      GOSUB Turn_Angle_Adjust
      GOSUB BoeBot_Turn_Brads
     
      ' Face Ping))) rangefinder straight ahead.
      pingDir = Center
      GOSUB Point_At_PingDir
     
      RETURN
     
    ' -----[ Subroutines - Find_Left_Side ]---------------------------------------
     
    ' Scan left until the measured distance is 10 cm beyond the closest distance,
    ' which is assumed to mean the object's side has been found.
     
    ' If the object's side has been found within the 180-degree field of vision,
    ' set edgesFound = 1 and store the pulse duration (pingDir) at which the
    ' object was found in the leftMark variable.
     
    ' If the side was not found by the 180-degree point in the scan, rotate the
    ' Boe-Bot until the edge is found, and then set edgesFound to 0 signifying
    ' that the scan will have to be repeated because the Boe-Bot rotated
    ' to find the side, which would otherwise cause the markDir variable to
    ' store an incorrect value.
     
    Find_Left_Side:
     
      sweepDir  = Positive
      distance = minDist
      sweepInc = 1
     
      DO UNTIL distance > minDist + 10
        GOSUB Sweep_Increment
        GOSUB Get_Ping_Cm
        IF pingDir >= LimitLeft - 10 THEN
          pingDir = LimitLeft - 50
          GOSUB Point_At_PingDir
          DO UNTIL distance > minDist + 10
            PULSOUT BotServoLeft, RotateLeft
            PULSOUT BotServoRight, RotateLeft
            GOSUB Get_Ping_Cm
            PAUSE 20
          LOOP
          edgesFound = 0
          RETURN
        ENDIF
      LOOP
      leftMark = pingDir
      edgesFound = 1
      RETURN
     
    ' -----[ Subroutines - Find_Right_Side ]--------------------------------------
     
    ' Mirror image of Find_Left_Side.
     
    Find_Right_Side:
     
      sweepDir  = Negative
      distance = minDist
      sweepInc = 1
     
      DO UNTIL distance > minDist + 10
        GOSUB Sweep_Increment
        GOSUB Get_Ping_Cm
        IF pingDir <= LimitRight + 10 THEN
          pingDir = LimitRight + 50
          GOSUB Point_At_PingDir
          DO UNTIL distance > minDist + 10
            PULSOUT BotServoLeft, RotateRight
            PULSOUT BotServoRight, RotateRight
            GOSUB Get_Ping_Cm
            PAUSE 20
          LOOP
          edgesFound = 0
          RETURN
        ENDIF
      LOOP
      rightMark = pingDir
      edgesFound = 1
      RETURN
     
    ' -----[ Subroutine - Get_Ping_Cm ]-------------------------------------------
     
    ' Gets Ping))) rangefinder measurement and converts time to centimeters.
    ' Distance may be declared as time to save variable space.
     
    Get_Ping_Cm:
     
      PULSOUT Ping, 5
      PULSIN Ping, 1, time
      distance = time ** CmConstant
     
      RETURN
     
    ' -----[ Subroutines - Point_At_PingDir ]-------------------------------------
     
    ' Points servo mounted Ping))) rangefinder at an angle determined by the
    ' value of the pingDir variable.
     
    Point_At_PingDir:
     
      FOR counter = 0 TO MinSweep
        PULSOUT PingServo, pingDir
        PAUSE BtwnPulses
      NEXT
     
      RETURN
     
    ' -----[ Subroutine - Polar_To_Cartesian ]------------------------------------
     
    ' Calculates x and y (Cartesian coordinates) given distance and angle
    ' (polar coordinates).
     
    Polar_To_Cartesian:
     
      ' Calculate left/right component.
      x = COS angle                              ' Polar to Cartesian
      xSign = x.BIT15                            ' Store sign bit
      x = ABS(x) */ SinCOsTo256                  ' Polar to Cartesian continued
      x = distance */ x
      IF xSign = negative THEN x = -x            ' Correct sign with sign bit
     
      ' Calculate straight ahead component.
      y = SIN angle                              ' Polar to Cartesian
      ySign = y.BIT15                            ' Store sign bit
      y = ABS(y) */ SinCOsTo256                  ' Polar to Cartesian continued
      y = distance */ y
      IF ySign = negative THEN y = -y            ' Correct sign with sign bit
     
      RETURN
     
    ' -----[ Subroutines - Single_Sweep ]-----------------------------------------
     
    ' Do one sweep, and find the closest distance measurement and the
    ' pulse value that points the servo in that direction.
     
    Single_Sweep:
     
      DO UNTIL pingDir >= LimitLeft
        GOSUB Sweep_Increment
        GOSUB Get_Ping_Cm
        IF distance < minDist THEN
          minDist = distance
          markDir = pingDir
        ENDIF
      LOOP
     
      RETURN
     
    ' -----[ Subroutine - Sweep_Increment ]---------------------------------------
     
    ' Increment/decrement the position of the servo that directs the Ping)))
    ' rangefinder. When pingDir goes outside either LimitRight or LimitLeft,
    ' the sweep direction toggles.
     
    Sweep_Increment:
     
      ' Change sweepDir for adding/subtracting increment if at rotation limit.
      IF pingDir <= LimitRight THEN
        sweepDir = Positive
      ELSEIF pingDir >= LimitLeft THEN
        sweepDir = Negative
      ENDIF
     
      ' Add/subtract increment to/from pingDir.
      IF sweepDir = negative THEN
        pingDir = pingDir - sweepInc
      ELSEIF sweepDir = Positive THEN
        pingDir = pingDir + sweepInc
      ENDIF
     
      ' Send positioning pulse to Ping))) Mounting Bracket servo.
      PULSOUT PingServo, pingDir
     
      RETURN
     
    ' -----[ Subroutines - Turn_Angle_Adjust ]------------------------------------
     
    ' Adjusts required turn angle based on 7 mm offset of Ping))) rangefinder from
    ' Boe-Bot's turning axis.
     
    Turn_Angle_Adjust:
     
      ' Get the object's distance at its center.
      GOSUB Get_Ping_Cm
     
      ' Position servo & calculate angle from far-right in brads.
      angle = pingDir - 250 ** PingDirToAngle
     
      GOSUB Polar_To_Cartesian
     
      ' Add distance between Ping))) and center of Boe-Bot axis.
      y = y + PingAxleOffset
     
      ' Recalculate the turning angle with respect to Boe-Bot's turning axis.
      angle = x ATN y
      pulses = 64 - angle
      pSign = pulses.BIT15
     
      RETURN
    

    __________________________________________________ ___________________________
    ··
    (c) 2009·by Parallax Inc - all rights reserved.··


    Post Edited By Moderator (Jessica Uelmen (Parallax)) : 8/25/2010 6:07:28 PM GMT
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	Figure1.JPG‎
Views:	7874
Size:	48.1 KB
ID:	59393   Click image for larger version

Name:	Figure2.JPG‎
Views:	3499
Size:	12.9 KB
ID:	59394   Click image for larger version

Name:	Figure4.JPG‎
Views:	7881
Size:	19.9 KB
ID:	59395   Click image for larger version

Name:	Figure5.JPG‎
Views:	3333
Size:	25.5 KB
ID:	59396   Click image for larger version

Name:	Figure3.JPG‎
Views:	7910
Size:	22.7 KB
ID:	59399  

    Click image for larger version

Name:	p590119-1.gif‎
Views:	4829
Size:	94.4 KB
ID:	73653   Click image for larger version

Name:	p590119-2.gif‎
Views:	4568
Size:	57.9 KB
ID:	73654   Click image for larger version

Name:	p590119-3.gif‎
Views:	4766
Size:	103.4 KB
ID:	73655   Click image for larger version

Name:	p590119-4.gif‎
Views:	4539
Size:	13.3 KB
ID:	73656  
    Last edited by Jessica Uelmen; 01-28-2011 at 03:49 PM. Reason: Forum Migration

  2. #2

    Default

    Post Edited By Moderator (Jessica Uelmen (Parallax)) : 4/13/2009 5:22:03 PM GMT
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	Equation1.JPG‎
Views:	7665
Size:	4.7 KB
ID:	59400  
    Attached Files Attached Files
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  3. #3

    Default

    Andy,

    Ran this code myself this afternoon. Very impressive! The Pings))) look like eyes and the Boe-Bot looks smarter than ever in this application. It's very "alive" (though not as much as that Linda Blair head that John was making pop out of the 50-gallon drum last week. . .that thing made me run out of the machine shop).

    The simplicity is really nice and it the results are very consistent.

    Ken Gracey
    Parallax, Inc.
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  4. #4

    Default

    Thanks Ken!

    I'm glad to hear about the repeatability.· There's a lot more calibration here than there is in most of the other Boe-Bot apps.

    I discovered a bug yesterday that you may want to keep in mind if you are doing any demos in the near future. For good performance with the current program/hardware, the maximum distance threshold appears to be 50 cm.

    If the closest object is further out than that, the Boe-Bot does not correctly swivel the Ping))) rangefinder to locate the object's left and right sides. So keep the closest object within a 50 cm radius, and it should still work really well. I'll post updated code as soon as I fix the bug.

    Andy

    Post Edited (Andy Lindsay (Parallax)) : 6/7/2006 11:04:09 PM GMT
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  5. #5

    Default

    i have actally been watiting for andy`s new book.btw when will it be realsed
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  6. #6

    Default

    Hey Andy,

    Am I supposed to know all of the answers because I work at Parallax?

    First I'll set the stage. Today I did a 40-minute presentation (six times) for my son's elementary school. I brought the most amazing set of robots from Parallax (lawnmowers, fish robots, hexcrawler, 10 Boe-Bots, a half-dozen Scribblers) and the best robot videos out there (Mars - Spirit, SumoBots, the "RipSaw", the running gas-powered dog thing, etc) and the kids were going nuts over the whole gig. I even threw some rock music over the videos which caused them to jump up and down like monkeys when they saw the Delta rocket launch.·

    But that's·just the background·to my question.

    Trying to make the "GoToClosestObject" Ping))) demo even more interesting, I thought I'd use a couple of stuffed animals (a big cow and a small purple thing called "Baloo") instead of wooden blocks. Strangely, the ultrasonic sensor had a fair amount of trouble with them. Sometimes it seemed to not see them at all, and other times it would track them and loose them. The small purple one with less soft areas seemed to work better.

    I assume that small furry critters absorb the ultrasonic waves, is that the case?

    Ken Gracey
    Parallax, Inc.

    Post Edited (Ken Gracey (Parallax)) : 6/9/2006 3:42:51 AM GMT
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  7. #7

    Default

    Ken -

    At the base level ultrasonics are nothing more or less than sound. Sound is reflected by "hard" objects, and absorbed by "soft" objects, regardless of the frequency of the sound.

    If you have a room which is too noisy, or has echoes, due to its size or configuration, then carpets, drapes and furniture will all help to ameliorate the problem. So too, "softening" the "hard" ceiling with acoustical tile. All are "soft" objects (comparatively speaking) and all will absorb sound.

    I'd love to have some Aero-Gel to play with. I'd bet ultrasonics would pass right through it, as though it weren't even there. Aero-Gel is about 99% air! Speaking of SOFT! Any LLNL or NASA folks out there want to give it a shot, and report back?

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->

    Post Edited (Bruce Bates) : 6/9/2006 7:48:56 AM GMT
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  8. #8

    Default

    Ken,

    It'll be interesting to see what the stuffed animals' signatures are with Ping)))Dar - a Radar Style Display, especially compared to a soda can or water bottle. Try taking a screen capture of the Ping)))Dar display with a stuffed animal along with a photo, then replace it with a water bottle and take another screen capture and photo.

    The demo is designed for use with cylinders, not blocks. I put one of those i-boxes about that on page 342. I also put a !-box about how important calibration is on page 347. If you have any suggestions about how I can more effectively convey those two important points to the reader, I'll certainly do my best to make it so.

    Andy
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  9. #9

    Default

    Three more notes:

    1) So far, I haven't found background noise to be a problem, and I've done this demo in several really noisy environments. Of course, there are a lot of different flavors of noice, and maybe the grade school environment has one that interferes while trade shows do not.

    2) If the stuffed animals were placed more than 50 cm from the front of the Boe-Bot, you may have seen that bug I mentioned earlier. Of course, if it worked with the blocks at the same distance in the same environment, that's probably not it. I'm still working on that bug.

    3) Aaach! I just remembered, carpet can be a problem, and I didn't include a warning in the PDF. Yes, in many cases, carpet can look like an array of objects within the 180-degree Ping))) rangefinder's field of detection.· Sorry about that...

    Post Edited (Andy Lindsay (Parallax)) : 6/9/2006 6:00:47 PM GMT
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  10. #10

    Default

    Andy,

    Brilliant series of projects based around sensors! I can't wait for the new SIC book to come out. The Ping))) Rangefinder has helped me to finally get to grips with ultrasonic sensors and I am waiting for my ordered bracket set to arrive so I can try out the newer programs. I echo the comment that others have made - the Ping))) really looks like eyes and, swivelling around on the bracket, it makes the BOE-Bot 'come alive' as·the robot·resembles a small, curious animal. As 'superworms' posted,·I would be interested to know how far off the book's publication is; will there be a complete kit?

    I've also ordered the Sensor Collection as these projects really show some practical uses for Basic Stamps and sensors·in control and monitoring·systems.

    Keep up the excellent work and posting the chapters for review.

    John

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---------------------------

    Manxstamp,
    Isle of Man, British Isles
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  11. #11

    Default

    I was working on a project based on "go to closest object" and since Brads give me headaches sometimes, I made up a PDF protractor (sp?) that shows brads and degrees (I used this for calibrating some servos to x servo pulses = 90 degrees = 64 brads). I didn't label every 5th degree, but otherwise it should be Postscript accurate, and looks pretty decent on an injket printer.

    Thanks again, Mr. Lindsay, for the elegant and incredibly explanatory code projects.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
    Attached Files Attached Files
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  12. #12

    Default

    Andy -- I adapted some of the concepts in "Closest Object" so that one of my 'bots can calculate theta, x pos, y pos in cartesian space based on wheel encoder odometry (works great, and I would never have remembered all my trig w/o your great article).

    One thing that I took into account in my code, that I do not find in "Closest Object" is checking for x/y values greater than (abs) -127/+127 before using ATN (to derive angle from a target x/y point).

    In my own code, I normalize x/y values before doing ATN; "Closest Object" does not. Wouldn't you get bogus values if the closest target found has a distance of greater than 127 cm? I realize on a table top this highly unlikely, and out-of-bounds values don't matter until target heading calculations are performed in your code... but still. Or am I missing something? In my code·I shift x/y right with rounding until both are ABS <= 127, so I get the best resolution -- my inputs are signed Word x/y values in cm).


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  13. #13

    Default

    does anyone know how i can convert the GO TO CLOSEST OBJECT code so that the boe bot can avoid the closest object when it comes in range of something like 10 cm or 15 cm, and then it will scan the object and the go in the opposite angle of the ping servo so that instead of going left where the closest object is it will go right where an object is further away? Im going to keep playing with the code but let me know if there is some way to make it work.
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  14. #14

    Default

    The third to last line of the code is where the number of pulses the Boe-Bot uses to turn is defined.

    pulses = 64 - angle

    The angle variable should store a value ranging from 0 to 128. If you swap the 64 and the angle, it will turn the same distance in the opposite direction. That should work really well for 30 degrees or more off center. However, if the object is only one brad off center, that could be a problem since the Boe-Bot would turn one brad in the opposite direction. An IF...THEN statement increasing the pulses value if it's close to 64 would fix that problem.

    In the Main Routine, you might need to get rid of the UNTIL distance <= 5, and move GoSUB Get_Ping_Cm just below the DO (that used to have UNTIL next to it). This will cause the Boe-Bot to repeat the check the surroundings and navigate loop periodically.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  15. #15

    Default

    I am trying to modify the original program, the one that just acts as a sort of radar.· Ultimatley what i am trying to do is make it so that the second scan is compared to·the the first scan to detect large amounts of motion.· I need to tie this in so i can point an array of sensors in the direction of the motion.· I had thought about having it detect it in sort of quadrants, but i am open to ideas, because I am really stuck.· Any suggestions are great!· Code is even better : )
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  16. #16

    Default

    Dear Andy,

    I thank you for the Excellent code.




    Post Edited (MovieMaker) : 11/22/2008 4:51:28 AM GMT
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  17. #17

    Default

    I just tried the program and it worked perfect. It is neat!@

    Andy, Mike and ALL of the Parallax Gang, Thanks for making this a nice Holiday Season for me by your EXCELLENT Support!




    Post Edited (MovieMaker) : 12/25/2008 4:42:08 PM GMT
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  18. #18

    Default

    Hi everybody, new here. Just got the Boe-Bot for Christmas, and I've been having a blast with it. Just yesterday I got the PING+servo/bracket deal for it, and have been trying to understand it better. However, while everything else has been documented greatly, it seems that the servo with PING has not - I was under the impression that it was all to be combined and explained in the Smart Sensors and Applications book, however only the PING sensor itself is explained there. No mention of the servo with PING, etc. While following the source code attached ina couple of threads is fine, I was just wondering if there will be something published that describes it in more detail?

    Thanks, and keep up the good work, the series of manuals/books is really what makes this a great learning experience!

    Polbit
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  19. #19

    Default

    Take a look in "What's a microcontroller" on the free downloads page for more information. I beliive that there's a full chapter on the subject.

    In short though, all you need to do (with a regular BS2) is send a PULSOUT on the correct pin with a value between 500 and 1000. Going outside these values can damage your servo. 750 is approximately the center position (for a standard servo), while 1000 and 500 are left and right (maybe not in that order). The second thing you need to do is to refresh this every 20 ms or so.
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

  20. #20

    Default

    Hi·all!

    Just like Ping)))Dar, we've updated this project to include a brief video introduction/demonstration for your viewing pleasure.· You can check it out on YouTube here.

    Happy Developing!

    Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax, Inc.
    Last edited by ForumTools; 09-30-2010 at 12:45 PM. Reason: Forum Migration

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts