Apr 11, 2007
Controlling servos with Motion Sensor
At the beginning me and Linus Roune both had a go at controlling Servos with some sort of data we would get form a sensor. Linus got into some heavy stuff with Arduino I went to look for solution using Basic Stamp.
The servos we had were Parallax (Futaba) Servos so hooking them with Basic Stamp was easy, and I found the code on the web. What I didn’t pay attention to was the difference between Standard and Continuous Rotation type of servo.
Standard Servo Manual (.pdf)
http://www.parallax.com/dl/docs/prod/motors/stdservo.pdf
Continuous Rotation Servo Manual (.pdf)
http://www.parallax.com/dl/docs/prod/motors/crservo.pdf
For my data I used a Sharp sensor
http://www.acroname.com/robotics/info/examples/GP2D02-4/GP2D02-4.html
The sensor gives values only for “motion” or “no motion” This means the result when hooking it up to Servo is “full rotation” or “no rotation”.
As you see bellow I used Basic Stamp II, the code goes as follows:
‘ {$STAMP BS2}
‘ variable declarations
val02 VAR Word ‘ value where reading is stored
i VAR Byte ‘ count variable for loop
‘——————————————————–
‘ constant declarations
cl CON 14 ‘ pin 14 is output (clock)
dt CON 15 ‘ pin 15 is the input (data)
‘——————————————————–
‘ I/O pin setup for detector
INPUT dt ‘ make pin 15 the input
HIGH cl ‘ make pin 14 output and high
‘——————————————————–
‘ main loop
DEBUG “Start”, CR ‘ indicate beginning
main:
GOSUB read02 ‘ call measurement routine
DEBUG DEC val02, CR ‘ display the value
PAUSE 100 ‘ stall so display readable
FOR val02 = 200 TO 1200
PULSOUT Servo_pin,val02
PAUSE 50
NEXT
FOR val02 = 1200 TO 200
PULSOUT Servo_pin,val02
PAUSE 50
NEXT
GOTO main
END
‘——————————————————–
‘ subroutine read02
‘
‘ This subroutine takes a reading from the connected
‘ GP2D02 detector and stores the value in “val02”.
‘ Any two pins can be used. Set “dt” to the data line
‘ of the GP2D02 (pin 4 on JST connector, yellow wire).
‘ Set cl to the clock line of the GP2D02 (pin 2 on the
‘ JST connector, green wire).
read02:
LOW cl ‘ turn on detector for reading
rl:
IF IN15 = 0 THEN rl ‘ wait for input high
SHIFTIN dt, cl, MSBPOST, [val02]
HIGH cl ‘ turn detector off
PAUSE 1 ‘ let detector reset
RETURN
Servo_pin CON 0 ‘I/O pin that is connected to servo
start:
GOTO start