Making a servo react to range on an SRF005?

pjl83

Member
Hi all, me again :)

I've been playing with the 28x1 project board this week in preparation for me eventually building an avoidance robot based on the LMR starter robot. I've had the servo moving around and I've used the pulsin and pulsout with the debug command to display the range on the screen.

I then tried adding an if....then to move a servo when the range gets below 10cm. I'm getting a syntax error on the if....then line?

I have coppied most of the code from the lmr site with the exception of the moving servo bit (which is where it went wrong!)

Code:
symbol trig= 3 'define output pin for trigger pulse
symbol echo= 6 'define input pin for echo pulse
symbol range= w1 '16 bit word variable for range


main:
	servo 0, 75 'move servo to far left
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10Us)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recahrge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)	 
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	let range = range*10/58 ' multiply by 10 then divide by 58
	debug range 'display range via debug command
	if range= <10 then movehead
	goto main
	
movehead:
	servo 0, 225 'move servo to far right
	wait 3 'wait 3 seconds
	goto main
There's probably something obvious in there that I'm just not seeing :confused:

Thanks in advance
Paul
 

hippy

Ex-Staff (retired)
if range= <10 then movehead

It should be "=<" rather than with a space between. A bit like putting a space in "can not" when it's almost inevitably meant to be "cannot".

Some would argue it should be "<=", less than or equal, rather than "=<", equal or less than, but the PICAXE compiler accepts either as the same. The first will usually be most familiar to many programmers, the second will look odd or thought to be a mistake.
 

pjl83

Member
Thank you. That solved the error.

I have now lost the function of the SRF005. It no longer pulser like it did before, it just flashes once every cycle. The servo acts as if the range is less than 10 as it moves on each cycle. The window in the PE that shows the value (where I saw the range before) no longer shows any value?
 

pjl83

Member
If i take out all of the servo lines then the range finder works fine??

Code:
symbol trig= 3 'define output pin for trigger pulse
symbol echo= 6 'define input pin for echo pulse
symbol range= w1 '16 bit word variable for range


main:
	'servo 0, 75 'move servo to far left
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10Us)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recahrge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)	 
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	let range = range*10/58 ' multiply by 10 then divide by 58
	debug range 'display range via debug command
	'if range=<10 then movehead
	goto main
	
'movehead:
'	servo 0, 225 'move servo to far right
'	wait 3 'wait 3 seconds
'	goto main
:confused:
 

russbow

Senior Member
Your original code works fine once the if range= <10 then movehead
is corrected.

Because the value of w0 is < or = to 10 you will always call the movehead routine.

In the simulator, click the word radio button. then click the right arrow just above the central white box. This will increase the box value ( your srf value ). Take it up to 100 and you will see the wo value in the debug screen and notice that the sub routine is not called
 

pjl83

Member
That works fine in the simulator (although it's the w1 value that displays), but when downloaded to the board, the SRF doesn't function like it does without the servo lines in there. The LED flashes quickly and constantly without the servo lines, but once they're in there it only flashes once in every cycle of the code. I can't see what I have done that would change the function of the SRF.
 

russbow

Senior Member
the SRF will show a led pulse every time it is called.

This will be quite fast if your w1 is above threshold as the main programme will run on a continuous loop.

If however the threshhold calls the movehead routine things will slow right down. You have built in a 3 second delay before the return command to go back to the main loop.

So if your object is near you will continually invoke the 3 seconds as you have not taken steps to move away (your next part of code perhaps ? )
 

pjl83

Member
But there lies my problem, the object isn't within close range. And even if it was, why is there no value displayed in the window where there was before?

I wanted to do this to test that I could use an analogue input to trigger an output, I used the servo as it was the easiest to connect. I'll try the same thing with an LED tonight and see if strange things happen.
 

russbow

Senior Member
PJ. If your prog works in the sim, but not on the board, then your circuit has got to be suspect hasn't it.

This is your original prog, corrected and visually tidied up. It works perfect in the sim.

Code:
symbol trig= 3 'define output pin for trigger pulse
symbol echo= 6 'define input pin for echo pulse
symbol range= w1 '16 bit word variable for range


main:
	servo 0, 75 'move servo to far left
	
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10Us)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recahrge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)	 
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	
	let range = range*10/58 ' multiply by 10 then divide by 58
	
	debug range 'display range via debug command
	if range <= 10 then movehead
	goto main
	
movehead:
	servo 0, 225 'move servo to far right
	wait 3 'wait 3 seconds
	goto main
Your comment

I wanted to do this to test that I could use an analogue input to trigger an output,
suggests perhaps you should be considering the READADC command rather than worrying about interfacing the SRF. Your "test gear" need be nothing more than a 10K pot and a LED.

I used the SRF in my first bot, just lift the code from here if you want.

http://www.picaxeforum.co.uk/showthread.php?t=12091

The SRF part is

Code:
''''''''''''''   Use ultrasonic detect forward   '''''''''''''''''''''''

ping:
pulsout trig,2 		&#8216; produce 20uS trigger pulse (must be minimum of 10uS)
pulsin echo,1,range 	&#8216; measures the range in 10uS steps
pause 10 			&#8216; recharge period after ranging completes
				&#8216; now convert range to cm (divide by 5.8) or inches (divide by 14.8)
				&#8216; as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
let range = range * 10 / 58 &#8216; multiply by 10 then divide by 58

if range < 20 then gosub right
return




R.
 
Last edited:

hippy

Ex-Staff (retired)
On thing worth doing is trimming your code down to a simple loop which triggers the sensor, reads the return pulse and reports its value via DEBUG or SERTXD, non calculations - That way you can see exactly what is being returned, and can check the hardware is working.

If the results there are wrong everything else which relies on them will be also. If those results are right but something later is not what you are expecting that will indicate a narrow area where to look to see what is going wrong.
 

pjl83

Member
Thanks both, I will have another play tonight. It's just frustrating me that I get the value returned using debug with the calculations and all is working perfectly. I can also move a server around perfectly.

But when I try the 2 together with everything wired the same as the individual tests, I lose the function of the sensor. I'll try some different things tonight and try and narrow it down a bit.

Thanks again
Paul
 

hippy

Ex-Staff (retired)
Servos can draw considerable currents when they move, sometimes several amps, so I'm wondering if moving the servo is having some affect on the sensor causing it to return a wrong result ?

Adding a reasonable pause after moving the servo lets it moves and settle into position. You have a 3 second wait after moving the head but none when moving to the default position before firing the sensor. It may be worth adding a wait there; it will slow things down but might show if that's the problem.
 

pjl83

Member
Servos can draw considerable currents when they move, sometimes several amps, so I'm wondering if moving the servo is having some affect on the sensor causing it to return a wrong result ?

Adding a reasonable pause after moving the servo lets it moves and settle into position. You have a 3 second wait after moving the head but none when moving to the default position before firing the sensor. It may be worth adding a wait there; it will slow things down but might show if that's the problem.
Thanks Hippy,

This worked a treat ;) I tried it with 3 seconds at first and all worked fine. I've now got it down to pause 250 and it still works fine. I'll make a note of this for another time, it's a good lesson learned.

PJ. If your prog works in the sim, but not on the board, then your circuit has got to be suspect hasn't it.
No, not on this occasion.

Thanks again to both
Paul
 
Top