Shop OBEX P1 Docs P2 Docs Learn Events
Pulsin Timeout and HC SR04 problems — Parallax Forums

Pulsin Timeout and HC SR04 problems

GrimGrim Posts: 12
edited 2014-02-22 01:44 in BASIC Stamp
Hi,
I've searched around and I can't find a solution to my problem. I'm using 3 HC SR04 sensors on a Boe-bot to create a maze solving robot. In my code I continuously request the distance from each of the sensors as the maze is very tight and I use the feedback to adjust the motor speeds; i.e. my main code goes -> get distances -> pulse out motor speeds.
In the maze it works brilliantly but I have problems outside the maze when the senors report no echo pulse and cause the PULSIN function to time out which then causes the motors to stop and start. At the moment when this happens I then use some more code to ignore the sensors for a few iterations of the program but I'm not happy with this!

I've tried writing my own PULSIN function that would time out much quicker (I'm only interested in really short distances with these sensors so i can make the timeout really short) but I cant get it to work. I'm now thinking about using something like a 555 timer to send a 'fake' echo pulse if none occurs but i'm not sure if this would work.

I was wondering what other people do to avoid this problem?

Comments

  • Henry005Henry005 Posts: 4
    edited 2014-02-08 06:36
    PULSIN stores a zero to the variable if it times out. I'm not sure exactly how your code works but I assume the PULSIN feedback is received then scaled somehow to a motor speed output. So a zero PULSIN would translate to a zero motor speed and "stop" the motor?

    You could write in a line of code to just maintain the current motor speeds if whatever variable you are using in PULSIN is equal to zero.

    Maybe post your code if I was misreading what your intent was here. Hope this helps
  • GrimGrim Posts: 12
    edited 2014-02-08 07:01
    Sorry if I was slightly ambiguous, I'm not the only one doing this and so I wish not to share all the details!
    What I already do is detect the PULSIN and set the distance to a long distance.
    I believe that the problem is that the timeout takes 0.135 seconds and the servos need a pulse every 20ms or they stop moving. My code executes in less than 20ms unless this timeout occurs, causing the motors to stop and start.
  • Mike GreenMike Green Posts: 23,101
    edited 2014-02-08 13:45
    You're in a tough spot. PULSIN does a good job of timing pulses unless the pulses take a long time to come in as with ultrasonic sensors with long distances. Other techniques can work but are hampered by the lack of an accurate timing source in Stamp code. The servo motors, by themselves, have a tight timing constraint (about every 20ms). You can get around the servo timing constraint by using some kind of external servo controller like a ServoPal or Propeller Servo Controller. You can get around the ultrasonic sensor timing limitations by using some kind of external timing peripheral controller. Depending on what time you have available and what constraints you have on your project, you might be better off using a Propeller Activity Board on your BoeBot chassis.
  • ercoerco Posts: 20,256
    edited 2014-02-08 20:25
    One workaround would be to only check one sensor between sending servo pulses instead of all three, and round-robin your sensors. Something like:

    do
    pulsout servo1
    pulsout servo2
    check sensor 1
    pulsout servo1
    pulsout servo2
    check sensor 2
    pulsout servo1
    pulsout servo2
    check sensor 3
    loop

    That will still give you more than enough sensor data.
  • GrimGrim Posts: 12
    edited 2014-02-13 09:27
    Thanks for the help, I've tried the above but still run into problems. I think what I'm going to do is use a 555 in a monostable configuration to trigger off the ultrasound trigger and then timeout and send a short pulse back on the ultrasound echo pin but I'm not sure quite how to accomplish this yet.
  • ercoerco Posts: 20,256
    edited 2014-02-13 09:53
    I feel your pain, Grim. Those ultrasonic sensors are cheap and I have a lot of them, but I much prefer using the Sharp family of IR distance sensors. Simple to use as digital sensors directly as a proximity detector, or with an ADC to get a numeric value. No timeout to worry about.

    Given your situation, I think Mike's ServoPal suggestion is the best solution to offload servo management and let the BS2 focus on monitoring the ultrasonic sensors, timeout & all.
  • GrimGrim Posts: 12
    edited 2014-02-19 13:09
    Thanks, I think fo now that's how I'll have to do it.
    Just a thought has anyone used something like an attiny85 to interface with the boe-bot via I2C with the tiny doing the distance measurements and transmitting them to the Stamp?
  • ercoerco Posts: 20,256
    edited 2014-02-19 13:35
    Grim wrote: »
    Just a thought has anyone used something like an attiny85 to interface with the boe-bot via I2C with the tiny doing the distance measurements and transmitting them to the Stamp?

    That would prolly work fine. I2C or serial comms could take place during the 20 ms pauses between Stamp-generated servo pulses.

    IIRC, the ServoPal is PIC-based. So whether you use s ServoPal or attiny, you're adding a second processor.

    I still enjoy getting the most out of the 20-year old BS2, even if it's not terribly practical: http://forums.parallax.com/showthread.php/137671-BS2-Drives-16-Servos!?highlight=stamp+drives+servos
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-02-19 14:40
    An RC circuit might help with this.

    [SIZE=1]    Stamp pin  -------/\/\/\----o------/\/\/\------- Vss
                             10KΩ   |          R
                                  =====  C
                                    |
                                    |
                                pulse source, low-high-low [/SIZE]
    
    Rising edge of pulse couples to Stamp pin and starts the pulsin count. It times out when the echo returns, or the RC time constant expires, or 0.131 second, whichever comes first.
    Can set RC = 0.01µF * 2MΩ to set a time constant of 20ms.

    I'm not sure that would be compatible with the SR04.
  • ercoerco Posts: 20,256
    edited 2014-02-19 16:48
    THAT sir, is the genius of Tracy Allen. A simple, beautiful analog solution that begs to be tried.

    Things aren't looking so grim, Grim!
  • GrimGrim Posts: 12
    edited 2014-02-19 17:10
    erco wrote: »
    IIRC, the ServoPal is PIC-based. So whether you use s ServoPal or attiny, you're adding a second processor.
    Is it an improvement from an electronics point of view to use the attiny on the sensors so that the commands to the servos have less possiblity of being delayed to the point of hitting the wall ( for example if for some odd reason all three sensors time out in a row?)
  • GrimGrim Posts: 12
    edited 2014-02-19 17:14
    An RC circuit might help with this.

    [SIZE=1]    Stamp pin  -------/\/\/\----o------/\/\/\------- Vss
                             10KΩ   |          R
                                  =====  C
                                    |
                                    |
                                pulse source, low-high-low [/SIZE]
    
    I'm not quite sure I understand the circuit as I thought the 'Stamp pin' would have to be the echo line of the SR04 (to prevent the timeout) which I think wouldn't work, I have thought of something similar but it would drag the Echo line high (which for the SR04 it only goes high for the return of the sensor data)
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-02-19 23:24
    I'm not at all familiar with the HC-SR04. More the Parallax Ping))).

    The PING))) is triggered by a short (3µs) pulse, and then after a delay of 750µs the ultrasound packet is emitted and starts the measurement pulse. The pulse ends either with the return echo, or with the timeout if there is no echo. Is that the same with the HC-SR04?

    Screen shot 2014-02-19 at 11.02.19 PM.png

    The circuit I posted would have to be modified a bit to handle both the output trigger and the input. I think. Mainly, eliminate the 10kΩ resistor I showed between the Stamp pin and the output, or reduce its value to ~100Ω.
  • GrimGrim Posts: 12
    edited 2014-02-20 08:07
    The HC-SR04 is pretty much identical apart from having the Echo and Trigger split into two pins. The major difference as I understand it is that the HC-SR04 is supposed to act the same for timeout conditions (from the datasheet) but it actually doesn't. It seems to just hang untill you want to use it again, using the PULSIN comman then just registers timeout and records a value of 0. I'll try your proposed solution but I'm not sure it'll work.
    Edit:
    I've just tried it and no joy, I think it's because this is 4 pin and the ping sensors are 3 pin.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-02-20 09:17
    Can you draw out a timing diagram of what happens on the two pins of the 'SR04?
  • GrimGrim Posts: 12
    edited 2014-02-20 09:45
    hcsr04.jpg

    It's pretty much identical but the output is on a separate pin. The problem is that if the timeout occurs the stamp gives back 0, and I don't think there is anoither way around this without a separate micro-controller to deal with the sensors.
    992 x 416 - 59K
  • AribaAriba Posts: 2,685
    edited 2014-02-20 09:49
    I don't know how fast the BS2 is, but I think it should be possible to measure the echo pulse with a software loop instead of PULSIN. So you can control the timeout.
    The following code triggers the sensor and then does a for-next loop for a fixed time which should match the pulse length of the max distance you will measure. In this loop it counts up the dist variable as long as the echo pin is high.
    If the sensor is still busy from the previous trigger, no new measurement is done, but a pause so that the timing is the same. In this case the old distance is used again.
    trig  PIN 0
    echo  PIN 1
    dist  VAR WORD
    i     VAR WORD
    
    do
      IF echo=0 THEN       'HC-SR04 ready?
        HIGH trig
        dist = 0
        LOW  trig
        FOR i=0 to 1000    'choose right endvalue for max distance
          IF echo=1 THEN dist=dist+1
        NEXT
        'dist value now depends on pulse length
      ELSE
        PAUSE 10           'same delay as measurement but keep old dist value
      ENDIF
      'use dist
    LOOP
    '
    
    It may even be possible to measure all 3 sensors at the same time in one loop:
    IF echo1=0 AND echo2=0 AND echo3=0 THEN
       HIGH trig1
       HIGH trig2
       HIGH trig3
       LOW  trig1
       LOW  trig2
       LOW  trig3
       dist1=0
       dist2=0
       dist3=0
       FOR i=0 to 400
         IF echo1=1 THEN dist1=dist1+1
         IF echo2=1 THEN dist2=dist2+1
         IF echo3=1 THEN dist3=dist3+1
       NEXT
     ELSE
       PAUSE 10
     ENDIF
    '
    

    Andy
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-02-20 10:07
    Andy,
    The Stamp is slow, a minimum of about 140 microseconds per token. A sequence like this,
    HIGH Trig
    dist = 0
    LOW Trig

    takes already about 400 microseconds, for a pulse that is supposed to be 4µs long. It's not a Propeller where individual Spin instructions take on the order of 4µs! The Stamp has a instruction (pulsout pin,2) that does in fact generate a 4µs output pulse. It takes the Stamp a couple hundred µs to turn around and be ready and waiting at its next instruction, (pulsin pin, edge, variable). The Ping))) and evidently the 'SR04 have a delay of almost 1ms between the trigger and the leading edge of the data pulse, so that gives the Stamp the necessary turnaround time.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-02-20 10:12
    Grim,
    Did you try the RC circuit on the line that receives data back from the 'SR04? The circuit is a differentiator, so if the values are chosen correctly, it would be impossible for the Stamp to linger around for the firmware timeout. Does the 'SR04 have a totem pole output (no pullup resistor)?
  • ercoerco Posts: 20,256
    edited 2014-02-20 10:25
    Grim wrote: »
    Is it an improvement from an electronics point of view to use the attiny on the sensors so that the commands to the servos have less possiblity of being delayed to the point of hitting the wall ( for example if for some odd reason all three sensors time out in a row?)

    I tend to think the opposite way, since the servos are the limiter and require a constant stream of control pulses. Using a servo controller (ie ServoPal, etc) to offload servo overhead leaves the BS2 free for less critical timing functions like reading sensors, making calculations and decisions as fast as it can. For simple mobile robots, the BS2 paired with a ServoPal is still plenty fast enough IMO.
  • GrimGrim Posts: 12
    edited 2014-02-20 10:31
    Andy,
    I've tried writing a similar program myself already, I got it to measure in about 30cm increments due to the speed of the BS2 - in this project 5mm is a lot!

    Tracy,
    I did try it, but I couldn't get it to work. I was trying to adjust your circuit so that after the timeout of the RC circuit the Echo (Stamp pin as you referenced it) goes low.
  • AribaAriba Posts: 2,685
    edited 2014-02-20 10:32
    Andy,
    The Stamp is slow, a minimum of about 140 microseconds per token. A sequence like this,
    HIGH Trig
    dist = 0
    LOW Trig

    takes already about 400 microseconds, for a pulse that is supposed to be 4µs long. It's not a Propeller where individual Spin instructions take on the order of 4µs! The Stamp has a instruction (pulsout pin,2) that does in fact generate a 4µs output pulse. It takes the Stamp a couple hundred µs to turn around and be ready and waiting at its next instruction, (pulsin pin, edge, variable). The Ping))) and evidently the 'SR04 have a delay of almost 1ms between the trigger and the leading edge of the data pulse, so that gives the Stamp the necessary turnaround time.
    Thank you Tracy for the info. I thoght it is slow but not so slow.
    Maybe this can work then, but the resolution of the distance is a bit low:
    do
      IF echo=0 THEN       'HC-SR04 ready?
        dist = 0
        PULSOUT trig,2
        FOR i=0 to 50    'choose right endvalue for max distance
          IF echo=1 THEN dist=dist+1
        NEXT
        'dist value now depends on pulse length
      ELSE
        PAUSE 10           'same delay as measurement but keep old dist value
      ENDIF
      'use dist
    LOOP
    '
    
    Andy
  • AribaAriba Posts: 2,685
    edited 2014-02-20 10:34
    Grim wrote: »
    Andy,
    I've tried writing a similar program myself already, I got it to measure in about 30cm increments due to the speed of the BS2 - in this project 5mm is a lot!

    Tracy,
    I did try it, but I couldn't get it to work. I was trying to adjust your circuit so that after the timeout of the RC circuit the Echo (Stamp pin as you referenced it) goes low.

    Sorry wrote while you posted.
    I see you tried it already - so forget my last post.

    Andy
  • GrimGrim Posts: 12
    edited 2014-02-20 10:35
    erco wrote: »
    I tend to think the opposite way, since the servos are the limiter and require a constant stream of control pulses. Using a servo controller (ie ServoPal, etc) to offload servo overhead leaves the BS2 free for less critical timing functions like reading sensors, making calculations and decisions as fast as it can. For simple mobile robots, the BS2 paired with a ServoPal is still plenty fast enough IMO.

    Thanks, I was thinking of it the other way around: The program executes fairly quickly (apart from the sensor polling), controlling the servos with the BS2 ensures that you don't end up with a random extra pulse sent to the motors ( accuracy here is a big deal)
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-02-21 00:30
    Tracy,
    I did try it, but I couldn't get it to work. I was trying to adjust your circuit so that after the timeout of the RC circuit the Echo (Stamp pin as you referenced it) goes low.

    I was curious to see if it would work with the Ping))). First off, I didn't know, or had forgotten, that the Ping))) itself times out at 20ms. If it doesn't hear an echo it drops its output line at the 20ms mark so that the Stamp can continue to service the servos. Give +1 to Parallax.

    Anyway, I was able to shorten the timeout even more by use of the RC.
    [SIZE=1][FONT=courier new]    Stamp pin  -------/\/\/\----o------/\/\/\------- Vss
                              110   |        510k
                                  ===== 0.01µF
                                    |
                                    |
                                PING))) signal
    [/FONT][/SIZE]
    
    Here are scope shots.
    --trigger pulse from BS2 followed by echo pulse from ping
    ----bottom, red, signal at ping pin
    ----top, yellow, signal at Stamp pin
    ----middle, orange, time expanded view of the trigger pulse.
    echo1ms.png
    echo20msShortened.png

    The first frame shows a short echo, yellow trace hardly distorted by the presence of the capacitor. The Stamp was indicating a raw count of around 300, and the value tracked changes in distance up to a raw value of around 4000 (8 milliseconds for echo return).

    The second frame is a 20ms pulse, timed out due to ping pointing into empty space. The bottom trace shows the 20ms echo return. But the Stamp times out earlier at about 8 ms, due to the decay of the signal at the Stamp pin down to the 1.4V threshold. Without the capacitor, the Stamp raw value would be ~10000, but with the capacitor, the maximum raw value is ~4000.
    640 x 480 - 7K
    640 x 480 - 8K
  • GrimGrim Posts: 12
    edited 2014-02-21 08:30
    Thanks, that looks exactly ike what I need! I'll have a go at trying it tomorow.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-02-21 15:05
    Let us know how it turns out. When I first tried it with the Ping))), it didn't work, in fact, the output of the Ping))) went crazy with repeat scans. After some head-scratching, It turned out I had connected the resistor to +Vdd instead of to Vss. Oops. Moved, it was good.

    The RC circuit should attach to the signal line that goes from the 'SR04 back to the Stamp.
  • GrimGrim Posts: 12
    edited 2014-02-22 01:44
    It works perfectly! I get a max range of about 1m but thats plenty for our needs and i'll have a play around with component sizes, thank-you so much for your help!
  • Hello Grim,

    I realize this is an old thread.
    I am wondering, it sounds like you got an HC-SR04 to work with a Parallax board, would you be able to tell me the code to get it to work.
    I'm a software developer, that part I can handle, but have only fairly recent "hobby" experience with Parallax, (Arduino, etc).
    I have two Parallax distance sensors but I would like to have a third and have a number of HC-SR04's that I plan to use with either Arduino or RaspPi.
    I have a maze I have a solution for and an ActivityBot that I worked with my son with last year and would like to get it working now better.

    Thank you much for what ever you may be able to tell me.

Sign In or Register to comment.