Controlling a servo without using "SERVO"

Goeytex

Senior Member
I am sure this has been covered in the forums but thought it a good idea to post this
as a Code Snippet.

What this bit of demo code does is control a servo full range using a 10K pot and the pulsout
command rather than the Picaxe "servo" and "servopos" commands.

So why do it this way instead of using SERVO ? Mainly because of the conflicts that
can occur between "servo" and other commands that use the internal timer. These
conflicts can cause seemingly random glitches and/or jitter. Using Pulsout in a
timed loop eliminates any possibility of jitter or glitches cause by timer conflicts.

In this snippet it eliminates glitches caused by a conflict between READADC and SERVO.

Code:
#Picaxe 08M
setfreq m8

symbol Servo1 = 4       [COLOR=#008000]'Output[/COLOR]
symbol Position = W0  [COLOR=#008000] 'Pulse width[/COLOR]
symbol delay = b2       [COLOR=#008000]'Off time [/COLOR]

let delay = 33
 
do
pulsout servo1, position

[COLOR=#008000]'===========================================[/COLOR]
readadc10 1, position
position = position * 5 / 17 + 180  [COLOR=#008000]'scale the value + offset[/COLOR]

[COLOR=#008000]' In this areas stuff can be done such as receive
' serial data, etc  that would normall interfere
' with SERVO.  However much time this  takes should
' be subtracted from "delay" to keep the off time
' between pulses close  to 20 ms. Many servos
' can operate between 5  and 40 ms, so the 20 ms
' is not carved  in stone.
'===========================================[/COLOR]

pause delay
loop
 

Bill.b

Senior Member
Hi I also found that other commands effected the servo command, in my case it was the IR commands. The following
is a portion of code for a free roaming dot to control the panning of a SFR005 ultrasonic scanner.


Code:
'this is a part of a free roaming bot program (bot120  picaxe 20x2).  the following section controls the pan of 
'of a SRF005 ultrasonic scanner.
'the servo uses pulse out command in place of the usual servo commands.  this is
'because this bot also used IR commands for control. when the servo commands where used, an IR command would
'cause the servo move to the extreem left.  then pulse out command rectified this problem.

 
Symbol trig 		= C.4			'front range sensor ADC input
Symbol SERVO1           = b.0
Symbol leftPath         = w13             'Obstruction value on left side
Symbol rightPath        = w14             'Obstruction value on right side
Symbol range            = w12	
Symbol SERVOLeft 		= 150
Symbol SERVORight 	= 350
Symbol SERVOCent 		= 260






DistanceMessure:

	pulsout trig,2        		' produces about 20uS pulse (must be minimum of 10uS)
   	pulsin trig,1,range  		' measures the range in 10uS steps
    	pause 10               		' SRF04 mandatory 10mS recharge period after ranging completes
    	let range = range * 10 /58/2 	' multiply by 10 then divide by 58 then by 2 (8meg) 
   	If range > 20 then 
		return
	endif
	let pinsb =%00001010    			'stop bot
	pause 200
	let pinsb =%10100000
	let pinsb =%00001010 
	for b50 = 1 to 10				'pulse output to servo 1
		pulsout SERVO1,SERVOLeft	' Turn head to look left
		pause 20
	next               			' Turn head to look left
      Pause 200                          	' Wait for servo to move
      pulsout trig,2         			' produces about 20uS pulse (must be minimum of 10uS)
    	pulsin trig,1,leftPath   		' measures the range in 10uS steps
    	pause 10               			' SRF04 mandatory 10mS recharge period after ranging completes
    	let leftPath = leftPath * 10 /58/2 	' multiply by 10 then divide by 62 
    	for b50 = 1 to 20				'pulse output to servo 1
		pulsout b.0,SERVORight		' Turn head to look right
		pause 20
	next   
        Pause 200             		' Wait for servo to move
        pulsout trig,2         		' produces about 20uS pulse (must be minimum of 10uS)
    	pulsin trig,1,rightPath    		' measures the range in 10uS steps
    	pause 10               			' SRF04 mandatory 10mS recharge period after ranging completes
    	let rightPath = rightPath * 10 /58/2 ' multiply by 10 then divide by 62 
	for b50 = 1 to 20				'pulse output to servo 1
		pulsout SERVO1,SERVOCent	' Turn head to look forward
		pause 20
	next 
      If leftPath < rightPath Then 
        	gosub turnleft               ' Turn in the direction that has more room
      Else
          	gosub turnright
      End If 
       
	return

Regards Bill
 

Goeytex

Senior Member
Hey Bill,

That's very good ! And deserves its own thread.

Can you please create another snippit thread and post your code there instead ?

Thanks !
 
Last edited:
Top