Pulsin Timeout and HC SR04 problems
Grim
Posts: 12
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?
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
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
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.
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.
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.
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
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.
Things aren't looking so grim, Grim!
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?
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Ω.
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.
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.
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. It may even be possible to measure all 3 sensors at the same time in one loop:
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.
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)?
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.
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.
Maybe this can work then, but the resolution of the distance is a bit low: Andy
Sorry wrote while you posted.
I see you tried it already - so forget my last post.
Andy
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)
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.
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.
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.
The RC circuit should attach to the signal line that goes from the 'SR04 back to the Stamp.
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.