ping))) - a radar style

20
Parallax Forums > Public Forums > Stamps In Class > Ping)))Dar - a Radar Style Display Forum Quick Jump Stamps In Class [ << Previous Thread | Next Thread >> ] | Show Newest Post First ] Andy Lindsay (Parallax) Forum Moderator Date Joined Jul 2004 Total Posts : 1046 Posted 6/5/2006 4:32 PM (GMT -7) Ping)))Dar – A Radar Style Display (Updated!) This activity features a program that displays what the Boe-Bot detects in the Debug Terminal as it sweeps the Ping))) rangefinder back and forth. Figure 1 shows an example with a cup and box set in the Boe-Bot's 180° field of detection along with their signatures displayed in the Debug Terminal. Experimenting with this program will help you better understand what the Boe-Bot can and cannot detect with the Ping))) Ultrasonic Rangefinder and Mounting Bracket, which is important for writing programs that navigate with this object detection system. View Video Introduction – Ping)))Dar Figure 2 shows a Boe-Bot assembled with the Ping))) Ultrasonic Rangefinder and Mounting Bracket kits. The mounting bracket kit makes it possible for the Boe-Bot to swivel the Ping))) rangefinder and measure object distances across a 180° field in front of it. Figure 1: Seeing what the Boe-Bot Detects with Ping)))Dar.bs2 Figure 2: Boe-Bot with Ping))) Ultrasonic Rangefinder and Mounting Bracket Kit Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099 1 de 20 3/14/2009 12:12 PM

Upload: mc-rene-solis-r

Post on 10-Apr-2015

988 views

Category:

Documents


0 download

DESCRIPTION

Example of PING ultrasonic sensor

TRANSCRIPT

Page 1: Ping))) - A Radar Style

Parallax Forums > Public Forums > Stamps In Class > Ping)))Dar - a Radar Style Display Forum Quick Jump

Stamps In Class

[ << Previous Thread | Next Thread >> ] | Show Newest Post First ]

Andy Lindsay (Parallax)Forum Moderator

Date Joined Jul 2004Total Posts : 1046

Posted 6/5/2006 4:32 PM (GMT -7)

Ping)))Dar – A Radar Style Display (Updated!) This activity features a program that displays what the Boe-Bot detects in the Debug Terminal as itsweeps the Ping))) rangefinder back and forth. Figure 1 shows an example with a cup and box set in theBoe-Bot's 180° field of detection along with their signatures displayed in the Debug Terminal.Experimenting with this program will help you better understand what the Boe-Bot can and cannotdetect with the Ping))) Ultrasonic Rangefinder and Mounting Bracket, which is important for writingprograms that navigate with this object detection system. View Video Introduction – Ping)))Dar Figure 2 shows a Boe-Bot assembled with the Ping))) Ultrasonic Rangefinder and Mounting Bracketkits. The mounting bracket kit makes it possible for the Boe-Bot to swivel the Ping))) rangefinder andmeasure object distances across a 180° field in front of it. Figure 1: Seeing what the Boe-Bot Detects with Ping)))Dar.bs2

Figure 2: Boe-Bot with Ping))) Ultrasonic Rangefinder and Mounting Bracket Kit

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

1 de 20 3/14/2009 12:12 PM

Page 2: Ping))) - A Radar Style

Getting Started

This project contains advanced topics, not intended for Boe-Bot beginners. Before continuing, it ishighly recommended you complete the following tasks in order to gain a better understanding on howthis program operates:

√ 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

Parts Required

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

Polar to Cartesian Coordinate Conversion

Ping)))Dar.bs2 will scan a 180° field in front of the Boe-Bot by incrementally rotating the Ping)))Mounting Bracket and then taking a distance measurement. It will do this rapidly enough that themounting bracket servo will appear to just be rotating from right to left and back again as it updates theDebug Terminal display. For each measurement in a given sweep, the information Ping)))Dar.bs2 will have to work with is adistance measurement (d) and an angle measurement (θ) as shown in Figure 3. When coordinates aregiven in terms of distance and angle, they are called polar coordinates. These coordinates are typicallyexpressed in parentheses like this: (d θ). When telling those coordinates to someone, you wouldnormally say, "d at an angle of theta." In order to display these measurements graphically in the DebugTerminal, Ping)))Dar.bs2 will have to convert these polar coordinates to Cartesian (x, y) coordinates.That way, the DEBUG CRSRXY, x, y, "*" command can be used to graphically display the measurementby positioning the cursor and then placing an asterisk. Figure 3: Object's Polar and Cartesian Coordinates

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

2 de 20 3/14/2009 12:12 PM

Page 3: Ping))) - A Radar Style

Calculating the x and y axis components given polar coordinates is not difficult. The equations for x andy are shown below. The x-axis component involves multiplying the distance by the cosine of the angle,and the y component is the distance multiplied by the sine of the angle.

Since the BASIC Stamp is an integer math processor, the programming for making these conversions is alittle different from what you might expect with a PC programming language. Ping)))Dar.bs2'sPolar_To_Cartesian subroutine is explained in Advanced Topic − Inside the Polar_To_CartesianSubroutine.

Assembly and Electrical Connections

√ Follow the instructions in the Ping))) Mounting Bracket Documentation for making themechanical and electrical connections to the servo and Ping))) Ultrasonic rangefinder. When youare done, the servo will be connected to the Board of Education's servo port 14, and the Ping)))rangefinder will be connected to port 15.

Mounting Bracket Adjustments

Keep in mind that Ping)))Dar.bs2 will measure distances and angles. Recall from What's aMicrocontroller that a PULSOUT command's Duration argument sends a message to a standard servotelling it what rotational position to hold. In other words, the PULSOUT command's Duration argument tellsthe Ping))) mounting bracket's standard servo what angle to turn to. Ping)))Dar.bs2 will need to convertthe PULSOUT command's Duration argument to a binary radian measurement before the program can dopolar to Cartesian coordinate conversion. Since the object's position is dependent on good distance andangle measurements, it will be important mechanically adjust and calibrate your Ping))) MountingBracket system so that the program's PULSOUT commands can sweep it from 0° (to the right) to 180° (tothe left). The first adjustment step toward good Ping))) Mounting Bracket servo angular control ismaking sure that the Ping))) rangefinder is mounted so that the servo points the Ping))) rangefinderstraight ahead when the servo receives 1.5 ms center pulses.

√ Run PingServoCenter.bs2.√ Make sure the Board of Education's 3-position power switch is set to position-2.√ Check to see whether the mounting bracket servo points the Ping))) rangefinder straightahead on the Boe-Bot.√ If it does, then continue to the Software Calibration for 180° Sweep section. Otherwise,

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

3 de 20 3/14/2009 12:12 PM

Page 4: Ping))) - A Radar Style

keep following the checklist instructions here.√ Make sure CenterPing.bs2 is running for the remaining checklist instructions in this section.√ Start by removing the screw that connects the servo horn to the output shaft shown inFigure 4 (a).√ Pull upward on the Ping))) rangefinder. The horn, which is attached to the Ping)))rangefinder with screws should slide up and off the servo's output shaft, also shown in Figure 4(a).√ Orient the Ping))) rangefinder so that it is pointing straight ahead as shown in Figure 4 (b).√ Slide the servo horn back onto the output shaft.

Perfect alignment may not be possible, so choose the closest to straight ahead.

If the teeth on the output shaft do not line up with the servo horn when it is pointing exactly straight ahead, there will be two ways that theservo horn lines up: with the Ping))) pointing slightly to the left, or slightly to the right. Choose position that is as closest to straight ahead. Youwill then be able to fine tune straight-ahead by adjusting the screws that attach the servo to the Boe-Bot chassis.

√ Screw the screw back in that holds the servo horn to the Ping))) mounting bracket.

Figure 4: Mounting the Ping))) Rangefinder so that It Points Straight Ahead

√ To adjust for any slight error caused by the output shaft to servo horn gear teeth alignment,start by loosening the screws that attach the servo to the Boe-Bot chassis.√ Now, you will have some wiggle room to rotate the servo's housing slightly to make up forany offset resulting from the alignment of the servo horn and output shaft's teeth.√ Make sure to re-tighten all the screws when you are done.

Example Program – PingServoCenter.bs2

' Smart Sensors and Applications - PingServoCenter.bs2' Send 1.5 ms "center" pulses to the servo the Ping))) rangefinderis' attached to.

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

4 de 20 3/14/2009 12:12 PM

Page 5: Ping))) - A Radar Style

' {$STAMP BS2}' {$PBASIC 2.5} PingServo PIN 14 ' Servo directs Ping))) Center CON 750 ' Center/0-degree pulse durationBtwnPulses CON 20 ' ms between servo pulses DO ' Main loop PULSOUT PingServo, Center ' Center signal -> Ping))) servo PAUSE BtwnPulses ' 20 ms delay between pulsesLOOP ' Repeat main loop

Software Calibration for 180° Sweep

The Boe-Bot needs to be able to point the Ping))) rangefinder from 0° (to the right) to 180° (to the left)as shown in Figure 5. The Ping))) mounting bracket's servo should direct the ping to 0° when it receives0.5 ms pulses and to 180° when it receives 2.5 ms pulses. Since the PULSOUT command times pulsedurations in terms of 2 μs units, the command PULSOUT 14, 250 will cause the servo to rotate the Ping)))rangefinder to 0°, and PULSOUT 14, 1250 will direct it to 180°. Figure 5: Angle and Ping))) Rangefinder Direction

The TestPingDirection.bs2 program declares LimitRight and LimitLeft as constants equal to 250 and1250. The program also declares PingServo to be PIN 14 and Center to be 750. So the commandPULSOUT PingServo, LimitRight should direct the rangefinder 0° to the right, and PULSOUTPingServo, LimitLeft direct the rangefinder 180°. Also, PULSOUT PingServo, Center should point therangefinder straight ahead. If the LimitLeft and LimitRight constants do not make the servo point the Ping))) rangefinder to 0° and180°, the program's CON directive values should be adjusted. For LimitRight, values smaller than 250will cause the servo to rotate further in the clockwise direction (to the right) while values larger than 250will cause it to rotate to a position closer to center. Likewise, values greater than 1250 will cause theservo to rotate further in the counterclockwise direction (to the left) while values smaller that 1250 willcause it to rotate to a position closer to center.

√ Run TestPingDirection.bs2 and make sure the servo points the Ping))) rangefinder to 0°,then to 180°, then to 90° (straight ahead).√ If the servo doesn't point to 0°, adjust the LimitRight constant accordingly. Values lessthan 250 will result in more clockwise rotation and values greater than 250 will result in lessclockwise rotation.

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

5 de 20 3/14/2009 12:12 PM

Page 6: Ping))) - A Radar Style

√ If the servo doesn't point to 180° to the left, adjust the LimitLeft constant accordingly.Values greater than 1250 will result in more counterclockwise rotation, and values less than 1250will result in less counterclockwise rotation.√ Update these CON directives, and make notes of the values you used. These same CONdirectives will have to be updated in this activity's Ping)))Dar.bs2 program and also in the nextactivity's GotoClosestObject.bs2 program.

Example Program – TestPingDirection.bs2

' Smart Sensors and Applications - TestPingDirection.bs2' Point the Ping))) 0-degrees (to the right), then 180-degrees (tothe right),' then straight ahead. ' {$STAMP BS2}' {$PBASIC 2.5} PingServo PIN 14 ' Servo directsPing))) LimitLeft CON 1250 ' Ping servo90-degrees leftLimitRight CON 250 ' Ping servo90-degrees right Center CON 750 ' Center/0-degreepulse durationBtwnPulses CON 20 ' ms between servopulsescounter VAR Byte ' Loop index DEBUG "Look to 0-degrees (right).", CRFOR counter = 1 TO 100 ' 100 pulses 0-degrees(right) PULSOUT 14, LimitRight PAUSE 20NEXT DEBUG "Look to 180-degrees (left).", CRFOR counter = 1 TO 100 ' 100 pulses 180-degrees(left) PULSOUT 14, LimitLeft PAUSE 20NEXT DEBUG "Look to 90-degrees (straight ahead).", CRFOR counter = 1 TO 100 ' 100 pulses 90-degrees(ahead) PULSOUT 14, Center PAUSE 20NEXT END

Testing Ping)))Dar

One of the most important things about Ping)))Dar.bs2 is that it will help give you a better idea of whatthe Boe-Bot "sees" as it sweeps the Ping))) rangefinder from left to right. Make sure to test a variety ofobjects at a variety of distances. Keep in mind also that the program is imposing a 100 cm distance limiton measurements to make the display more convenient. Before running the program, the LimitRight and LimitLeft CON directives should be updated so that theservo that directs the Ping))) rangefinder to sweep from 0° to 180° using the values you determined withTestPingDirection.bs2. Ping)))Dar converts the PULSOUT command's Duration arguments into binary radian servo rotation angle

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

6 de 20 3/14/2009 12:12 PM

Page 7: Ping))) - A Radar Style

measurements with the help of a constant and the ** operator. Let's assume that LimitRight is 250 andLimitLeft is 1250. The main routine in Ping)))Dar.bs2 has code that sweeps the PULSOUT command'sDuration argument from 250 to 1250 in increments of 15. As shown in Figure 6, that's about 2.7° foreach increment of 15, which is pretty close to 2 brads (2.8125°). Figure 6: Rotation Angle vs. PULSOUT Duration

Mapping the PULSOUT command's duration argument to the number of brads the servo has rotatedinvolves the ** operator. In the next example program, the command that does the job is angle =pingDir - LimitRight ** PingDirToAngle. The pingDir variable is used in the command PULSOUTPingServo, PingDir to point the Ping))) rangefinder to 0 brads (0°), then 2 brads, then 4 brads and soon all the way to 128 brads (180°). As it does this, the pingDir variable starts at LimitRight (250), then265, then 280, and so on up through LimitLeft (1250). Since the expression angle = pingDir - LimitRight ** PingDirToAngle subtracts LimitRight frompingDir before the ** operation, the value ** scales will range from 0 through 1000 instead of 250 to 1250. ThePingDirToAngle constant has to be the right number of 65535ths to scale this range of 0 to 1000 into 0 through 128brads. This is another job for the ** scale constant equation introduced in Chapter 3, Activity #3 in Smart Sensors andApplications

Here is an example of calculating PingDirToAngle constant when LimitRight is 250 and LimitLeft is1250. Make sure to follow along because you will need to recalculate this constant based on theLimitLeft and LimitRight constants you determined for your mounting bracket system withTestPingDirection.bs2. The number of output scale elements is 0 through 128, which is 129 elementssince the range includes zero. Next, LimitLeft − LimitRight = 1250 − 250 = 1000. Again, we aretalking about a range that's inclusive of 0, so there are really 1001 elements in the input scale.

Substituting 129 and 1001 into the ** scale constant equation yields:

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

7 de 20 3/14/2009 12:12 PM

Page 8: Ping))) - A Radar Style

Why is 1 subtracted from the denominator?

The */ operators and ** operator always round down to the next integer. Subtracting 1 from thedenominator, corrects these rounding errors and, on the average, makes value **

ScaleConstant sort the input scale elements to their most correct output scale elements. Tolearn more about how this works, see Chapter 3, Activity #3 in Smart Sensors and Applications.

Example Program – Ping)))Dar.bs2

Some of the checklist instructions below involve updating CON directives that you determined withTestPingDirection.bs2. While they are not necessarily crucial to this program, they will be crucial forGotoClosestObject.bs2, the Boe-Bot Ping))) Mounting Bracket navigation program in the next activity.

√ Open Ping)))Dar.bs2 with your BASIC Stamp Editor.√ Update the LimitRight and LimitLeft CON directives in the program to tune the 0° right and180° left pulse widths.√ If you changed either LimitLeft or LimitRight, calculate the value needed for thePingDirToAngle CON directive with the PingDirToAngle equation just discussed. Make sure toupdate this value in your program too.√ Make notes of all these CON directives because you will need to substitute them into the nextactivity's GotoClosestObject.bs2 example program.√ Run the program and maximize your Debug Terminal so that it takes up the entire computerscreen.√ Observe the Debug Terminal as you try placing a variety of objects at various distancesfrom the Boe-Bot at various locations in its 180° field of detection.√ Make sure to try the suggestions in the section after the example program entitled YourTurn - Understanding what the Boe-Bot Does and Does not "See". It brings to light some of thissetup's capabilities and drawbacks, and they turn out to be important for writing navigationprograms.

' -----[ Title]--------------------------------------------------------------' Smart Sensors and Applications - Ping)))Dar.bs2' Display radar style object distance measurements in the DebugTerminal.' as the Boe-Bot sweeps the Ping))) Mounting Bracket servo fromright' (0-degrees) to left (180-degrees). ' {$STAMP BS2} ' Target device =BASIC Stamp 2' {$PBASIC 2.5} ' Language =PBASIC 2.5 ' -----[ I/O Definitions]---------------------------------------------------- PingServo PIN 14 ' Servo that directsPing)))Ping PIN 15 ' Ping))) sensorsignal pin

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

8 de 20 3/14/2009 12:12 PM

Page 9: Ping))) - A Radar Style

' -----[ Constants]---------------------------------------------------------- LimitLeft CON 1250 ' Bracket 90-degreesLimitLeftLimitRight CON 250 ' Bracket 90-degreesLimitRightPingDirToAngle CON 8454 ' Servo pulse -> anglewith ** CmConstant CON 2260 ' Echo time -> cm with**SinCosTo256 CON 517 ' For */ -127..127 ->-256..256Increment CON 15 ' Servo PULSOUTincrement valueNegative CON 1 ' Negative signPositive CON 0 ' Positive sign ' -----[ Variables]---------------------------------------------------------- pingDir VAR Word ' Pulse duration ->directiontime VAR Word ' Ping))) echo timedistance VAR Time ' Object distancex VAR Word ' x Debug cursorcoordinatey VAR Word ' y Debug cursorcoordinateangle VAR Byte ' Angle fromLimitRight in bradscounter VAR angle ' Loop countersweepInc VAR Nib ' Sweep incrementsweepDir VAR Bit ' Increment/decrementpingDirxSign VAR Bit ' x variable signySign VAR xSign ' Stores sign of yvariablensign VAR Bit ' Numerator signdsign VAR Bit ' Denominator sign ' -----[ Initialization]----------------------------------------------------- pingDir = LimitRight ' Start servo at0-degrees FOR counter = 1 TO 40 ' Initialize servoposition PULSOUT PingServo, pingDir PAUSE 20NEXT sweepInc = Increment ' Set the sweepincrement ' -----[ Main Routine]------------------------------------------------------- DO IF pingDir <= LimitRight THEN ' Refresh display iffar right DEBUG CLS, CRSRXY, 50, 25, "X" ENDIF

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

9 de 20 3/14/2009 12:12 PM

Page 10: Ping))) - A Radar Style

GOSUB Sweep_Increment ' Move servo bysweepInc ' Calculate angle from far right in brads. angle = pingDir - LimitRight ** PingDirToAngle GOSUB Get_Ping_Cm ' Get cm measurement distance = distance MAX 100 / 4 ' Scale for DebugTerminal GOSUB Polar_To_Cartesian ' distnace @ angle ->(x, y) x = x * 2 ' 2 spaces forevery 1 CR DEBUG CRSRXY, 50 + x, 25 - y, "*" ' Display (x, y)coordinate LOOP ' -----[ Subroutine - Get_Ping_Cm]------------------------------------------- ' Gets Ping))) rangefinder measurement and converts time tocentimeters.' Distance may be declared as time to save variable space. Get_Ping_Cm: PULSOUT Ping, 5 PULSIN Ping, 1, time distance = time ** CmConstant RETURN ' -----[ Subroutine - Polar_To_Cartesian]------------------------------------ ' Calculates x and y (Cartesian coordinates) given distance andangle' (polar coordinates). Polar_To_Cartesian: ' Calculate x coordinate. x = COS angle ' Polar to Cartesian xSign = x.BIT15 ' Store sign bit x = ABS(x) */ SinCOsTo256 ' Polar to Cartesiancontinued x = distance */ x IF xSign = negative THEN x = -x ' Correct sign withsign bit ' Calculate y coordinate. y = SIN angle ' Polar to Cartesian ySign = y.BIT15 ' Store sign bit y = ABS(y) */ SinCOsTo256 ' Polar to Cartesiancontinued y = distance */ y IF ySign = negative THEN y = -y ' Correct sign withsign bit RETURN

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

10 de 20 3/14/2009 12:12 PM

Page 11: Ping))) - A Radar Style

' -----[ Subroutine - Sweep_Increment]--------------------------------------- ' Increment/decrement the position of the servo that directs thePing)))' rangefinder. When pingDir goes outside either LimitRight orLimitLeft,' the sweep direction toggles. Sweep_Increment: ' Change sweepDir for adding/subtracting increment if atrotation 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

Your Turn - Understanding what the Boe-Bot does and does not "See"

To understand what your Boe-Bot does and does not "see" with the Ping))) rangefinder, a fewexperiments are in order. Here are some questions that can be answered with Ping)))Dar.bs2 and variousobjects placed in the Boe-Bot's field of detection.

√ If you place one object behind another object, can it see the object in back?√ How far do you have to rotate a flat object before it is no longer visible to Ping)))?√ Start with one or two cylindrical objects about 3 ft (91 cm) apart and 2 ft (61 cm) from thefront of the Boe-Bot.

For best results, use tall cylindrical objects, such as soda cans, water bottles, etc. Sheets ofpaper can also be conveniently rolled into 8.5 inch (21.5 cm) tall by 2 to 3 inch (5 to 7.5 cm)diameter cylinders with a couple pieces of tape.

√ As you move the objects closer to each other, how close can they be to each other beforethey appear to be one object in the Debug Terminal?√ If you keep the objects the same distance from each other but move them closer to the frontof the Boe-Bot, is the gap between them detected again at some point?√ To what extent does setting the Increment constant is set to a smaller value help theBoe-Bot detect the gap between objects?

How Ping)))Dar.bs2 Works

The initialization routine applies forty pulses to make sure the servo turns and starts 0° (far-right). In thisprogram, the FOR...NEXT loop is only used once. In GoToClosestObject.bs2 in the next activity, the loopis used frequently to point the Ping))) rangefinder in various directions, so in that program, the loop isplaced in a subroutine called Point_At_PingDir. One other initialization detail is setting thesweepInc variable equal to the Increment constant (15). This value is used by the Sweep_Increment

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

11 de 20 3/14/2009 12:12 PM

Page 12: Ping))) - A Radar Style

subroutine to rotate the Ping))) Mounting Bracket servo slightly between each distance measurement.

pingDir = LimitRight ' Start servo at 0-degrees FOR counter = 1 TO 40 ' Initialize servo position PULSOUT PingServo, pingDir PAUSE 20NEXT sweepInc = Increment ' Set the sweep increment

The main routine's DO...LOOP refreshes the Debug Terminal display after each right-left sweep. As themain routine's DO...LOOP repeats, the first thing the program does is clear the Debug Terminal and placethe "X" character at 50 spaces over and 25 carriage returns down. This "X" indicates the Boe-Bot'sposition.

IF pingDir <= LimitRight THEN ' Refresh display if far right DEBUG CLS, CRSRXY, 50, 25, "X"ENDIF

Next, the program calls the Sweep_Increment subroutine, which adjusts the Ping))) Mounting Bracket'sservo slightly each time it gets called. Calling this subroutine repeatedly each time through the mainroutine's DO...LOOP results in the back and forth sweeping motion. This subroutine uses the pingDirvariable to direct the servo.

GOSUB Sweep_Increment ' Move servo by sweepInc The Sweep_Increment subroutine modified the value of pingDir, and then used it in a PULSOUTcommand's Duration argument to point the Ping))) Mounting Bracket servo in a new direction. Thatmeans we can use the value of pingDir to determine the angle to which the Ping))) rangefinder ispointing in terms of binary radians. Figure 7 shows a few examples of how the PULSOUT command's Duration argument relates to the Ping)))Mounting Bracket servo's rotational position. For example, when pingDir is LimitRight, (250), theangle is 0°, which is 0 binary radians (brads). When pingDir is 500, the angle is about 45°, or 32 brads.Likewise, pingDir can be 750 or1000, and the respective servo angles will be 90° (64 brads) and 135°(96 brads). By the time pingDir gets to LimitLeft (1250), the angle will be 180° = 128 brads. Figure 7: PULSOUT Duration Argument vs. Rotation Angle

Here is the command that converts pingDir, which ranges from LimitRight to LimitLeft (250 to 1250)to angle, which ranges from 0 to 128 brads. Note that this is the same kind of equation we used inChapter 3, Activity #3 and #4.

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

12 de 20 3/14/2009 12:12 PM

Page 13: Ping))) - A Radar Style

' Calculate angle from far right in brads.angle = pingDir - LimitRight ** PingDirToAngle

Without parentheses, PBASIC executes operators from left to right. So the command angle = pingDir- LimitRight ** PingDirToAngle first subtracts LimitRight from pingDir. This resulting value, whichcould be anywhere from 0 through 1000, has the ** PingDirToAngle operation performed on it. SincePingDirToAgle is 8454 in the example program, it means a value from 0 through 1000 gets multiplied by8454 / 65536. If pingDir stores 1000, the result is 128.9978, and the ** operator rounds it down to 128,which is the correct number of brads assuming the servo is in fact pointing left (180° from far right). Likewise, (750 - 250) × 8454 / 65536 results is 64.4989, which rounds down to 64 brads, which is 90°,and the servo should be pointing straight ahead. Next, the distance of the object at the angle the Ping))) Mounting Bracket servo is pointing to getsmeasured and scaled to a centimeter measurement. This centimeter measurement is further scaled downby 1/4 of its actual value to make it fit more conveniently in a Debug Terminal maximized in a typical1024 by 768 pixel monitor.

GOSUB Get_Ping_Cm ' Get cm measurementdistance = distance MAX 100 / 4 ' Scale for Debug Terminal

Now, we know the object's distance and angle relative to the Ping))) rangefinder on the front of theBoe-Bot. The problem is that these values are polar coordinates (distance angle), but the program needsto express them in terms of Cartesian coordinates (x, y) for Debug Terminal display. The Cartesiancoordinates, are necessary for displaying the asterisks that denote the object's position in the DebugTerminal with the DEBUG command's CRSRXY formatter. So, the program calls the Polar_To_Cartesiansubroutine. Given distance and angle, the subroutine calculates the corresponding x and y coordinates.

GOSUB Polar_To_Cartesian ' distance @ angle -> (x, y)

Before repeating the main routine's DO...LOOP, the last step is to plot an asterisk at the (x, y) coordinate.x will be a value that ranges from about -50 to 50. So, the x-coordinate in DEBUG CRSRXY is 50 + x. They-coordinate will range from 0 to 25. However, CRSRXY will plot the value in terms of carriage returnsdown from the top line of the Debug Terminal. What we really want is carriage returns upward from the"X" that was plotted at 50 spaces over and 25 carriage returns down. That's why the CRSRXY formatter'sy-coordinate is 25 - y. As y gets larger, the asterisk is plotted closer to the X at (50, 25). As y getssmaller, the asterisk is plotted closer to the top of the Debug Terminal.

' Display asterisk at x,y coordinate.DEBUG CRSRXY, 50 + x, 25 - y, "*"

Advanced Topic − Inside the Polar_To_Cartesian Subroutine

Please see additional content posted below.

Post Edited By Moderator (Jessica Uelmen (Parallax)) : 3/13/2009 3:35:25 PM GMT

Image Attachment :

BoeBotSees.JPG 17KB(image/pjpeg)

This image has been viewed201 time(s).

Image Attachment :

FullyAssembled.JPG 31KB(image/pjpeg)

This image has been viewed200 time(s).

Image Attachment :

PolarToCartesian.JPG 13KB(image/pjpeg)

This image has been viewed200 time(s).

Image Attachment :

PingMounted.JPG 40KB(image/pjpeg)

This image has been viewed201 time(s).

Image Attachment :

Direction.JPG 48KB(image/pjpeg)

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

13 de 20 3/14/2009 12:12 PM

Page 14: Ping))) - A Radar Style

This image has been viewed201 time(s).

Back to TopRinksCustoms

Tickling critical mass...

Date Joined Nov 2005Total Posts : 403

Posted 6/19/2006 12:49 PM (GMT -7)

Friggin COOL!!! Interlinked with an infared detection system, and maybe a camera system (poke around thepropeller forums), this qould make for one heck of a robotic guidance system. Possibly better than the ones i seein prototype on TV used to control real cars.

Back to Top

George SuttonRegistered Member

Date Joined Jul 2004Total Posts : 12

Posted 9/19/2006 9:37 AM (GMT -7)

Andy,

I am unable on multiple attenpts to download the attached file(s) in the ZIP attachment. Are the code and PDFstill available? Thanks.

Back to Top

Andy Lindsay (Parallax)Forum Moderator

Date Joined Jul 2004Total Posts : 1046

Posted 9/19/2006 6:40 PM (GMT -7)

Advanced Topic − Inside the Polar_To_Cartesian Subroutine Given a distance at an angle (d θ), you can calculate the x and y coordinates (x, y) with thehelp of a calculator and these two formulas:

Calculating x and y with the BASIC Stamp involves the SIN, COS, and */ operators.

√ Look up and review the */, SIN and COS operators in either the BASIC Stamp Manualor the BASIC Stamp Editor's Help feature

Given an angle in brads, PBASIC's COS operator returns 127 to 0 to -127 to 0 as the brad anglegoes from 0 to 64 to 128 to 192. Calculating the cosine from 0° to 90° to 180° to 270° with acalculator will yield results that range from 1 to 0 to -1 and back to 0 again. The SIN operatorbehaves similarly, returning values that range from 0 to 127 to 0 to -127 as the angle in brads goesfrom 0 to 64 to 128 to 192. Again, if you calculate the sine of angles from 0° to 90° to 180° to 270°with a calculator, you will see that the actual sine values range from 0 to 1 to 0 to -1. The problemhere is that we want to multiply distance results for SIN and COS values that range from -1 to 1, not-127 to 127. One way around this is creative use of the */ operator. This operator multiples a value by a numberof 256ths. By using the */ operator twice, we can first scale a value in the range of -127 through127 up to a range from -256 through 256. Then, we can use */ again to multiply that result by thedistance. The second time, the */ operator will be multiplying the distance variable by sine andcosine values that are in terms of a number of 256ths. The result will be very close to what youwould get from a calculator, multiplying the distance sine and cosine value that range from -1through 1.

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

14 de 20 3/14/2009 12:12 PM

Page 15: Ping))) - A Radar Style

To scale a value that will fall in the range of -127 through 127 to its equivalent in the range from-256 to 256, we'll use the */ scale constant equation introduced in Chapter 3, Activity #5 in SmartSensors and Applications.

Since the input range is -127 through 127, that's 255 possible values. The output range is going tobe -256 to 256, which is 513 possible values.

To calculate a constant we'll call SinCosTo256, we'll substitute 255 and 513 into the */ scale constant equation:

Now that we know SinCosTo256 has to be 517, here is the routine for polar to Cartesian coordinate conversion.Notice that the sign of both the cosine and sine calculations are stored in the xSign and ySign bits, and the rest ofthe calculations are done with the absolute values of x and y. That's because /, */, //, and ** were designed to beused with positive integers only. So, the absolute value of each measurement is taken first. Then the rest of theoperations are done on the positive integer values, and the sign of the x and y results are restored at the end byIF...THEN statements.

' Calculate x coordinate.x = COS angle ' Polar to CartesianxSign = x.BIT15 ' Store sign bitx = ABS(x) */ SinCOsTo256 ' Polar to Cartesian continuedx = distance */ xIF xSign = negative THEN x = -x ' Correct sign with sign bit ' Calculate y coordinate.y = SIN angle ' Polar to CartesianySign = y.BIT15 ' Store sign bity = ABS(y) */ SinCOsTo256 ' Polar to Cartesian continuedy = distance */ yIF ySign = negative THEN y = -y ' Correct sign with sign bit

Post Edited By Moderator (Jessica Uelmen (Parallax)) : 3/13/2009 3:31:41 PM GMT

Image Attachment :

DegreesVsPULSOUT.JPG 16KB (image/pjpeg)

This image has been viewed196 time(s).

Image Attachment :

PULSOUTDurationVsRotationAngle.JPG 21KB (image/pjpeg)

This image has been viewed 199 time(s).

Image Attachment :

sincos.JPG 2KB(image/pjpeg)

This image has beenviewed 200 time(s).

Image Attachment :

(1)scaleconstant.JPG 7KB (image/pjpeg)This image has beenviewed 10 time(s).

Image Attachment :angle.JPG 1KB (image/pjpeg)

Back to Top

Aristides Alvarez (Parallax)Forum Moderator

Date Joined Jul 2004Total Posts : 315

Posted 9/20/2006 7:28 AM (GMT -7)

George,

You should be able to download the zipped file from the link in the first post.

If just clicking on the link doesn't work, please try the following:1- right click (contextual) on the link2- select "Save Target as..."3- In the "Save As" window specify where you want the file to be saved in your computer.4- Once you have the file in your computer you can just UnZip it and see the content.

Regards,

Post Edited By Moderator (Jessica Uelmen (Parallax)) : 3/13/2009 3:33:14 PM GMT

Image Attachment :

(2)outputinputscale.JPG 7KB (image/pjpeg)

This image has been viewed197 time(s).

Image Attachment :

(4)scaleconstant.JPG 7KB(image/pjpeg)

This image has been viewed182 time(s).

Image Attachment :

(5)outputinputscale.JPG 4KB (image/pjpeg)

This image has been viewed178 time(s).

Image Attachment :

(6)SinCosTo256.JPG 6KB(image/pjpeg)

This image has been viewed182 time(s).

Image Attachment :

(3)PingDirToAngle.JPG 12KB (image/pjpeg)

This image has been viewed10 time(s).

Back to Top

johnsroboticsSpining Roboticist

Date Joined Aug 2004Total Posts : 26

Posted 9/20/2006 1:07 PM (GMT -7)

I have an odd problem downloading anything from the parallax website in .zip format. I'm using winXP pro sp2.The file always downloads with no extension, but if I add ".zip" to the end of the filename, windows automaticallyrecognizes it as a .zip archive...maybe that would help?

Back to Top

Kevin WoodRegistered Member

Date Joined Aug 2004Total Posts : 768

Posted 9/20/2006 1:38 PM (GMT -7)

Andy,

Can you give a timeframe for the release of the text/course?

Thanks.

Back to Top

George SuttonRegistered Member

Posted 9/20/2006 1:42 PM (GMT -7)

Aristides and johnsrobotics,

Thanks for the ideas. I tried everything that you have recommended, to no avail. It shows a "valid" ZIP file, witha 1.11MB file size. But when opened, it shows "0 files and 0 bytes". Latest version of WinZip, never had thisproblem with all the thousands of zip downloads I have accessed. But thanks for the ideas. And Andy hasprovided me with a solution. Thanks to Andy as well!

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

15 de 20 3/14/2009 12:12 PM

Page 16: Ping))) - A Radar Style

Date Joined Jul 2004Total Posts : 12

Back to Top

Aristides Alvarez (Parallax)Forum Moderator

Date Joined Jul 2004Total Posts : 315

Posted 9/20/2006 1:58 PM (GMT -7)

Hello Kevin, Thanks for your interest. Andy is totally done with this book. Now it’s in the editorial pipeline. Since the release ofthe Propeller™ microcontroller we’re been assigning higher priority to Propeller related books, as Propeller Manualand Hydra manual. Our editor is getting close to finishing a huge job right now and next in the list is Smart Sensors and Applications.We are trying to finish this Stamps in Class book as soon as possible. As soon as we have a firm deadline we’re going to announce it in this forum (meanwhile you can keep accessingthe pieces that form this book through forum posts as this one and the ones in this threadhttp://forums.parallax.com/forums/default.aspx?f=6&m=98784). Regards,

Aristides AlvarezEducation and Technical Support ManagerParallax, Inc.

Back to Top

bulkheadRegistered Member

Date Joined Sep 2005Total Posts : 406

Posted 9/20/2006 7:27 PM (GMT -7)

This is intersting, I've tried this out and it didn't work. Based on my experimenting, a cup about 1.5feet in front ofthe BOE would be detected in about 90 degrees of the sensor's 180 degree sweep. This is consistent with thedata given in the instruction manual which I believe says the width of the detection beam is from 20 to 40degrees (40 degrees being closer objects).

From the picture, this certainly looks to be the case (close range), so how does one get so many data points?Even if it had a 20 angle beam, objects would "appear" much wider and the accuracy would be much less. That'swhy the laser rangers with .3 degree or so detection beam cost over $1000. Can anyone explain this?

Back to Top

Andy Lindsay (Parallax)Forum Moderator

Date Joined Jul 2004Total Posts : 1046

Posted 9/20/2006 8:55 PM (GMT -7)

bulkhead, regarding:

bulkhead said...This is intersting, I've tried this out and it didn't work. Based on my experimenting, ...

Did you follow the instructions in the PDF and run the source code? If your picture matches mine, but the screencapture is way different, post it and let's take a closer look. The center of the cup was about 1.5 ft at 135degrees, and it was conical, 4.25 inches tall, with a 2.5 inch diameter base and a 3.5 inch diameter opening. Thecenter of the box was about 2.3 ft at 45 degrees, and the box was about 4 inches wide by 6 inches tall by 3inches deep.

bulkhead said...From the picture, this certainly looks to be the case (close range), so how does one get so manydata points?

The PDF and source code explain the details. You can download them by right clicking the Ping)))Dar.zip file atthe beginning of this thread and selecting Save target as... After downloading and unzipping the file, you willhave a PDF with instructions to follow and source code to modify per the PDF and then run and test.

Post Edited (Andy Lindsay (Parallax)) : 9/21/2006 4:21:59 AM GMT

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

16 de 20 3/14/2009 12:12 PM

Page 17: Ping))) - A Radar Style

Back to Top

AImanRegistered Member

Date Joined Jun 2006Total Posts : 411

Posted 1/9/2007 1:19 PM (GMT -7)

Andy,

What happens with radar style sonar if the robot is in a sort of enclosure like a closet? Can it detect whatsbehind? I would imaging with a full rotation servo and extra wire it could work...

Back to Top

Andy Lindsay (Parallax)Forum Moderator

Date Joined Jul 2004Total Posts : 1046

Posted 1/9/2007 2:06 PM (GMT -7)

It's certainly doable, though extra hardware would be required. With a continuous rotation servo, some type offeedback mechanism would probably be necessary to know which direction the Ping))) Ultrasonic Distance Sensoris pointing. The first thing that comes to mind is an encoder.

A "quick and easy" approach would be to stack two standard servos on top of each other. The bottom servo has alittle over 180 degrees in rotation for +/- 90 degrees. The one on top has an additional 180-degrees for +/- 180degrees. For example, to look behind you to 170 degrees to the left, turn the bottom servo 90 degrees to the left,and the top servo 80 degrees to the left. For that matter, you could simply direct both of them to turn 85-degreesleft, and potentially save a variable.

Andy LindsayEducation DepartmentParallax, Inc.

Back to Top

computer guyRegistered Member

Date Joined Jan 2007Total Posts : 821

Posted 6/10/2007 11:57 PM (GMT -7)

It would be cool to get a 3D version of this going on the propeller.I am not volunteering by the way.

Check out my robot using the propeller robot.tmcp.com.auIf you offer cheap PCB fabrication, perl programming or any other helpful services please email me [email protected]

Back to Top

Bean (Hitt Consulting) Posted 6/11/2007 4:41 AM (GMT -7)

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

17 de 20 3/14/2009 12:12 PM

Page 18: Ping))) - A Radar Style

Forum Moderator

Date Joined Jul 2004Total Posts : 5401

AIman said...Andy,

What happens with radar style sonar if the robot is in a sort of enclosure like a closet? Can itdetect whats behind? I would imaging with a full rotation servo and extra wire it could work...

Andy, Could you mount two pings back-to-back, and fire them independantly (so they don't confuse each other) ? Bean.

“The United States is a nation of laws - poorly written and randomly enforced.” - Frank Zappa- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -www.hittconsulting.com

Back to Top

Andy Lindsay (Parallax)Forum Moderator

Date Joined Jul 2004Total Posts : 1046

Posted 6/11/2007 7:56 AM (GMT -7)

computer guy,

That should be interesting. Pan and tilt servos are easy enough. The interesting part will be coming up with analgorithm that resolves successive measurements into a better representation of the object's shape.

Bean,

Either the Propeller or SX microcontrollers could deliver simultaneous pulses and track the echo times. Thequestion is, will one Ping))) sensor get confused by the echo from the other Ping))) sensor's echo if it gets therefirst. I'm assuming it won't be a problem since the sound is coming from the opposite direction and gettingblocked by the PCB, but we won't know until somebody tests it.

Andy P.S. Bean, I read your message again after clicking Submit and realized that I misunderstood the question (andthe point of the question). Yes, if one Ping))) sensor starts after the other has finished its measurement, therobot will have 360 degree ultrasonic object detection with a BASIC Stamp 2 at the helm.

Andy LindsayEducation DepartmentParallax, Inc.

Post Edited (Andy Lindsay (Parallax)) : 6/11/2007 3:06:07 PM GMT

Back to Top

Jessica Uelmen (Parallax)Forum Moderator

Date Joined Oct 2008Total Posts : 44

Posted 3/6/2009 5:17 PM (GMT -7)

Hey guys! We've updated the Ping)))Dar project to include a brief video introduction/demonstration for your viewingpleasure. You can check it out on YouTube here. Happy Developing! Jessica

Jessica UelmenEducation DepartmentParallax, Inc.

Post Edited (Jessica Uelmen (Parallax)) : 3/11/2009 7:15:27 PM GMT

Back to Top

WhitRegistered Member

Posted 3/6/2009 6:48 PM (GMT -7)

Nice work Jessica!

Whit+ "We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keepsleading us down new paths." - Walt Disney

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

18 de 20 3/14/2009 12:12 PM

Page 19: Ping))) - A Radar Style

Date Joined Mar 2007Total Posts : 857

Back to Top

Jessica Uelmen (Parallax)Forum Moderator

Date Joined Oct 2008Total Posts : 44

Posted 3/9/2009 11:50 AM (GMT -7)

Thanks, Whit! Always great to get feedback - and positive feedback is even better! :)

Glad you enjoyed it,

Jessica

Jessica UelmenEducation DepartmentParallax, Inc.

Back to Top

nkBotManRegistered Member

Date Joined Mar 2009Total Posts : 1

Posted Yesterday 8:08 PM (GMT -7)

I was on youtube the other day and found some BOE Bots roaming, using the ping dar for object avoidance. I waswondering if anyone would have some source code to point me in the right direction on how to go about doingthat.

Thanks in advance,

Nick

Back to Top

Mike GreenRegistered Member

Date Joined Oct 2004Total Posts : 10850

Posted Yesterday 9:01 PM (GMT -7)

Go to the Parallax webstore page for the PING))) sensor and you'll find links to Roaming with the PING))) samplecode

Back to Top

Bean (Hitt Consulting)Forum Moderator

Posted Today 4:51 AM (GMT -7)

Andy, In the very first post you have images of several equations. In each of them the value 66536 is used. I beleivethese should be 65536.

Bean.

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

19 de 20 3/14/2009 12:12 PM

Page 20: Ping))) - A Radar Style

Date Joined Jul 2004Total Posts : 5401

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -There is a fine line between arrogance and confidence. Make sure you don't cross it...

Back to Top

WhitRegistered Member

Date Joined Mar 2007Total Posts : 857

Posted Today 7:02 AM (GMT -7)

Nice catch Bean.

It is listed as 65535ths in the text (following fig. 6), 66536 in the equations (also following fig. 6 and the65535ths note) and then as 65536 further along in the text (following fig. 7).

Whit+ "We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keepsleading us down new paths." - Walt Disney

Back to Top

Jessica Uelmen (Parallax)Forum Moderator

Date Joined Oct 2008Total Posts : 44

Posted Today 8:43 AM (GMT -7)

Thanks guys! We've updated the post.

Jessuca

Jessica UelmenEducation DepartmentParallax, Inc.

Back to Top

[ << Previous Thread | Next Thread >> ]

Forum Information

Currently it is Friday, March 13, 2009 1:07 PM (GMT -7)There are a total of 327,999 posts in 49,000 threads.In the last 3 days there were 94 new threads and 721 reply posts. View Active Threads

Who's Online

This forum has 15453 registered members. Please welcome our newest member, firdaus.31 Guest(s), 18 Registered Member(s) are currently online. Detailsjb1311, Bob H, nutson, Bill Drummond, CJMJ, modemman, Jay Kickliter, Carl Hayes, Phil Pilgrim (PhiPi), rjo_, Fred Hawkins, LEDboy, kwinn,Mightor, Timmoore, SRLM, RobotWorkshop, Bill Chennault

Forum powered by dotNetBB v2.42EC SP2.02dotNetBB © 2000-2009

Need Support? | Forum Help Manual

Ping)))Dar - a Radar Style Display :: Parallax Forums http://forums.parallax.com/forums/default.aspx?f=6&m=129099

20 de 20 3/14/2009 12:12 PM