' {$STAMP BS2sx} ' {$PBASIC 2.5} '------------------------------------------------------ ' Servo Random Movement Generator ' By Vern Graner 10-26-2004 ' Any questions to vern@graner.com ' R-10.25b '------------------------------------------------------ ' -Added code to alter the stepping "speed" using an ' array with a seperate "step" rate for each servo ' -Added code to accel/decel servo seek speed ' -Added code to place motion limits on each servo ' -Added "controlled" start/stop '------------------------------------------------------ ' Hardware setup: ' Servos connected to pins 0-3 ' RC time POT ON PIN 7 (used BSAC values) 'Declare Variables '------------------------------------------------------ SERVO VAR Nib 'Store the servo number I VAR Nib 'Standard "for" counter RESULT VAR Word 'Store the random number result RAMP VAR Bit (4) 'Store speed ramp direction SPEED VAR Byte (4) 'Seek speed for the servos CUR VAR Word (4) 'Array to hold the current servo position DEST VAR Word (4) 'Array to hold the destination servo position 'initialize the seek speed for each servo '------------------------------------------------------ FOR I = 0 TO 3 SPEED(0)=1 NEXT I 'Set the "start position" for each servo '------------------------------------------------------ GOSUB LIMITS 'Position servos within their "safe" area FOR I = 0 TO 3 '(usually center). by "fetching" the value CUR(I) = DEST(I) ' from the "limits" subroutine and pre-stuffing NEXT ' them in to the "CURrent" value for each servo ' Use RCTIME POT position to generate a random seed '------------------------------------------------------ HIGH 7 PAUSE 1 RCTIME 7, 1, RESULT 'Main Loop that "seeks" servos to destination '------------------------------------------------------ DO SERVO=SERVO+1 'cycle through the four servos IF SERVO=4 THEN PAUSE 20 'AFTER sending four pulses to the four servos SERVO = 0 'wait for 20us to allow the pulse to ENDIF 'be "digested" by servo IF RAMP(SERVO) = 1 THEN 'Based on "ramp" direction, either increase SPEED(SERVO)=SPEED(SERVO)+1 'or decrease the servo seek speed by 1 ELSE SPEED(SERVO)=SPEED(SERVO)-1 ENDIF SPEED(SERVO)=SPEED(SERVO) MAX 50 'Enforce a maximum and minimum seek speed SPEED(SERVO)=SPEED(SERVO) MIN 10 IF CUR(SERVO)=DEST(SERVO) THEN 'If the servo has reached GOSUB FetchNewDest 'its destination then fetch ENDIF 'a new destination IF CUR(SERVO)DEST(SERVO) THEN 'Check to be sure the SPEED increment CUR(SERVO)=DEST(SERVO) 'does NOT "overshoot" the destination ENDIF 'value ENDIF IF CUR(SERVO)>DEST(SERVO) THEN 'Same as above, but decrement CUR(SERVO)=CUR(SERVO)-SPEED(SERVO) IF CUR(SERVO)