Shop OBEX P1 Docs P2 Docs Learn Events
Ping)))Dar But Using SRF04 Problems/Issues — Parallax Forums

Ping)))Dar But Using SRF04 Problems/Issues

RockoRocko Posts: 8
edited 2013-09-10 17:31 in BASIC Stamp
My end game is to have my BoeBot use the PingDar_GoToClosestObject.bs2 program successfully.
Let me first say that I am using a generic SFR04 instead of a Parallax Ping Sensor.

The SFR04 is connected to the top of a servo.
I have the SRF04 in Mode 2 whereby a 10K ohm resistor is placed between the Trigger and the Echo pins.


Using PingServoCenter.bs2, I was able to establish:
ServoCenter: 750
LimitLeft: 289
LimitRight: 1189


When I put these values into Ping)))Dar.bs2 (see below) and run the program, the SRF04/servo swings hard right and stops and the only thing that appears on the radar (Debug) screen is the X and a single blinking * off to the right.


If I swap the LimitLeft and LimitRight values, the SRF04/servo sweeps back and forth between limits as it should and the radar screen displays & updates the half circle of ***’s as it should with the exception that when an obstacle is put into the field, it’s ***’s appears on the opposite side of the X then where they should. (???)


(Note: I have the SRF Trigger connected to P1. The Echo is not connected with the exception of the 10K to the Trigger)


Someone at my robotics club meeting suggested turning the SRF04 upside down. I tried that and it didn’t have any effect.
I also tried mounting the SRF04 on the opposite side of the horn but that had no effect.


Any ideas as to what I can change in the Ping)))Dar.bs2 program to get the obstacle ***’s to appear on the correct side of the X ?
Thanks for any suggestions !

Ping)))Dar.bs2 Here's the entire code
' -----[ Title ]--------------------------------------------------------------
' Smart Sensors and Applications - Ping)))Dar.bs2
' Display radar style object distance measurements in the Debug Terminal.
' as the Boe-Bot sweeps the Ping))) Mounting Bracket servo from right
' (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 directs Ping)))
Ping           PIN     15                    ' Ping))) sensor signal pin

' -----[ Constants ]----------------------------------------------------------

LimitLeft      CON     1250                  ' Bracket 90-degrees LimitLeft
LimitRight     CON     250                   ' Bracket 90-degrees LimitRight
PingDirToAngle CON     8454                  ' Servo pulse -> angle with **

CmConstant     CON     2260                  ' Echo time -> cm with **
SinCosTo256    CON     517                   ' For */ -127..127 -> -256..256
Increment      CON     15                    ' Servo PULSOUT increment value
Negative       CON     1                     ' Negative sign
Positive       CON     0                     ' Positive sign

' -----[ Variables ]----------------------------------------------------------

pingDir        VAR     Word                  ' Pulse duration -> direction
time           VAR     Word                  ' Ping))) echo time
distance       VAR     Time                  ' Object distance
x              VAR     Word                  ' x Debug cursor coordinate
y              VAR     Word                  ' y Debug cursor coordinate
angle          VAR     Byte                  ' Angle from LimitRight in brads
counter        VAR     angle                 ' Loop counter
sweepInc       VAR     Nib                   ' Sweep increment
sweepDir       VAR     Bit                   ' Increment/decrement pingDir
xSign          VAR     Bit                   ' x variable sign
ySign          VAR     xSign                 ' Stores sign of y variable
nsign          VAR     Bit                   ' Numerator sign
dsign          VAR     Bit                   ' Denominator sign

' -----[ Initialization ]-----------------------------------------------------

pingDir = LimitRight                         ' Start servo at 0-degrees

FOR counter = 1 TO 40                        ' Initialize servo position
  PULSOUT PingServo, pingDir
  PAUSE 20
NEXT

sweepInc = Increment                         ' Set the sweep increment

' -----[ Main Routine ]-------------------------------------------------------

DO

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

  GOSUB Sweep_Increment                      ' Move servo by sweepInc

  ' 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 Debug Terminal

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

  x = x * 2                                  ' 2 spaces for every 1 CR

  DEBUG CRSRXY, 50 + x, 25 - y, "*"          ' Display (x, y) coordinate

LOOP

' -----[ 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

' -----[ Subroutine - Polar_To_Cartesian ]------------------------------------

' Calculates x and y (Cartesian coordinates) given distance and angle
' (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 Cartesian continued
  x = distance */ x
  IF xSign = negative THEN x = -x            ' Correct sign with sign bit

  ' Calculate y coordinate.
  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

' -----[ 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

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-09-08 17:51
    It sounds like you have a reversed servo. This should be easy to fix. If you post a link to the code or attach it to a post, I bet someone will be willing to fix it for you.

    (I'm nice enough I might fix it but not nice enough to search for the code myself.)
  • ercoerco Posts: 20,256
    edited 2013-09-08 19:48
    http://forums.parallax.com/showthread.php/86110-Ping)))Dar-a-Radar-Style-Display?p=590115#post590115

    Go Duane Go!

    Yes, it may be a case of servo reversal since this is copied from that code:

    LimitLeft CON 1250 ' Ping servo 90-degrees left
    LimitRight CON 250 ' Ping servo 90-degrees right
    Center CON 750 ' Center/0-degree pulse duration
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-09-08 20:51
    erco wrote: »
    Go Duane Go!

    Why thanks buddy ol' pal.

    Okay, this is the lazy way to do it.

    Add the following constant:
    LimitLeft      CON     1250                  ' Bracket 90-degrees LimitLeft
    LimitRight    CON     250                   ' Bracket 90-degrees LimitRight
    [COLOR=#ff0000]Limits         CON     LimitLeft + limitRight[/COLOR]
    

    And add the following variable:
    pingDir        VAR     Word                  ' Pulse duration -> direction
    [COLOR=#ff0000]pingRev        VAR     Word                  ' Pulse duration -> direction for reversed servo[/COLOR]
    

    Now there are two of these statements in the code:
      PULSOUT PingServo, pingDir
    

    Change them to this:
    [COLOR=#ff0000]pingRev = Limits - pingDir[/COLOR]
    [COLOR=#ff0000]  PULSOUT PingServo, pingRev[/COLOR]
    

    The above is a really sloppy way of making the changes but I would need to wire up a servo and ping to a BS2 to make sure the code still worked correctly if I did it the proper way.

    The proper way involves changing the equations so the shorter servo pulse is on the left with the longer pulse on the right as the code increments through the angles. I don't use the BS2 enough to trust myself to make these changes without testing them.

    If my sloppy version doesn't work, let me know and I'll dig out a servo and Ping to make sure the next revision works correctly.

    Since I started with an erco quote, I'd also like to end with one.
    erco wrote: »
    Don't worry, everyone hates erco.
  • RockoRocko Posts: 8
    edited 2013-09-10 15:45
    Thanks for your reply!
    The Ping)))Dar program resides in a zipped folder at Andy Lindsay's (Parallax) thread, Ping)))Dar - A Radar Style Display
    For some reason, I was unable to attach the link, so I copied/paste the entire code and added it at the end of my thread.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-09-10 16:08
    Rocko wrote: »
    For some reason, I was unable to attach the link, so I copied/paste the entire code and added it at the end of my thread.

    When you post code without code tags, you lose all indentation which makes it harder to read. Had you used code tags I might have copied it from the post.

    Not a big deal since no one had told you have to use code tags. Next time you won't have that excuse. :smile:

    Here's link to Phil's tutorial on using code tags.

    attachment.php?attachmentid=78421&d=1297987572

    BTW, that last quote from erco was to point out how irritating it can be since he's right about stuff so often. There are many examples of erco setting me straight in the ways of robot but post #23 of this thread shows my failed attempt at keeping treads on a robot. Erco told me how to fix it (the solution was very counter intuitive). Erco gave more advice in post #53 which (once again) greatly improved the performance of the robot.

    I suppose I can live with erco being right all the time since it ends making my robots work so much better than they would have if I hadn't had his help.

    So, did you try the changes I suggested? Did they do the trick?
  • RockoRocko Posts: 8
    edited 2013-09-10 16:21
    Many THANKS, Duane; I just edited the code and it's working correctly !

    Hopefully, these editions will help the BoeBot perform less hinky with the PingDar_GoToClosestObject.bs2 program running.

    Thanks again,
    rocko
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-09-10 17:31
    I'm glad it's working.
    Rocko wrote: »
    Hopefully, these editions will help the BoeBot perform less hinky with the PingDar_GoToClosestObject.bs2 program running.

    I think it should help. I haven't seen that particular program. I didn't see on the page erco linked to (and once again I'm too lazy to search for it myself).

    I wrote a little tutorial on how to create links in forum posts. Here's a link to it.

    I think questions on the forum are more likely to answered when accompanied by a link to the code or device the question is about (not that you asked another question in your last post).

    Near the bottom of post #3 in my index there's a link to a couple tutorials on how to add pictures to a post. I think it's fun to see pictures and videos of others robots. When you get your Boe-Bot working like you want, I hope you make a video of it in action.

    Oh, by the way, welcome to the forum.
Sign In or Register to comment.