PDA

View Full Version : servo control


alphaone
09-15-02, 08:01 PM
could someone please give an example of pulsing servos. it is not clear from the manual, and i am new to forth.
thanks....
-phil

RMDumse
09-16-02, 08:19 AM
Could you give a little more detail on the servos you wish to control? Probably you are thinking of RC servos, but since the IsoPod(TM) is capable of doing those as well as DC motor and brushless DC motors as well, "servo" control is kind of a broader subject than you might expect.

Anyway, here is some code I used in the Hexapod Walker which had 18 RC servos controlling the shoulder (2) and knee (1) and two for the sonar head. It uses the 12 PWM outputs, and configures the timers as PWM outputs as well.

This is early code written for V0.2. V0.3 (release imminent) has some words making PWM easier, but this example goes straight to the hardware registers and gives full control. Adjust the LOSTOP and HISTOP values for your RC servo limits. Over driving the RC servos can strip gears, so if in question back up to the 1ms LO (4E2) and 2ms HI (9C4) stops.

HEX

: HALFSPEEDCPU F413 FA1 ! 82 F00 ! ; ( DIV BY 2

HALFSPEEDCPU

C3 CONSTANT PWMCTL# ( DIV BY 8
( VARIABLE LOSTOP 4E2 LOSTOP !
VARIABLE LOSTOP 2E4 LOSTOP !

( VARIABLE HISTOP 9C4 HISTOP !
VARIABLE HISTOP B9A HISTOP !

VARIABLE PER# 61A8 PER# ! ( 61A8 for 50 Hz, 5161 for 60 Hz

VARIABLE SCALE# 3E8 SCALE# !

: MIDWAY HISTOP @ LOSTOP @ + 2/ ;

: 8* 2* 2* 2* ; ( 8 * WITHOUT THE SPEED PENALTY

: SETPWM ( val n -- )
DUP 6 U<
IF
E00
ELSE
6 -
E20
THEN
>R R@ 6 + + ! R@ @ DROP PWMCTL# R> !
;

: SETTMRCMP ( val n -- ) 8* D00 + 2DUP 1+ ! PER# @ ROT - SWAP ! ;

: RCINIT
8000 E03 !
0 E0D !
0 E0E !
000E E0F ! ( CENTER ALIGN FOR DIV BY 2
PER# @ E05 !
8000 E23 !
0 E2D !
0 E2E !
000E E2F ! ( CENTER ALIGN FOR DIV BY 2
PER# @ E25 !

C 0
DO
MIDWAY I SETPWM
LOOP

C 0
DO
3824 I 8* D06 + ! ( DIVIDE BY 16 SAME AS PWM ASSUME HALFSPEED CPU
MIDWAY I SETTMRCMP
0001 I 8* D07 + !
LOOP
;

: RC ( COUNT FROM 0 TO SCALE#, THEN 0-11D PWM #
SWAP 0 MAX SCALE# @ MIN HISTOP @ LOSTOP @ - SCALE# @ */ LOSTOP @ + SWAP
0 MAX 1C MIN
DUP C U<
IF
SETPWM
ELSE
C - SETTMRCMP
THEN
;

: RC? ( COUNT --- SCALED VALUE
0 MAX 1C MIN
DUP C U<
IF
DUP 6 U< IF E06 ELSE E20 THEN +
ELSE
C - 8* D01 +
THEN
@ LOSTOP @ - SCALE# @ HISTOP @ LOSTOP @ - */
;

alphaone
09-16-02, 11:02 AM
thanks!
sorry you are correct it is R/C servos...
i apologise for the abruptness of the first question i was being harried by a three year old.
how does the isopod know which servo to pulse, i guess there is an address in the command?
thanks again
-phil

RMDumse
09-17-02, 12:02 AM
Yes, you pass the PWM setting from 0 to SCALE#, and the number of the PWM to set. 0-11D for hardware PWM channels then 12D to 22D for the timers TMRA0,1,2,3 TMRB0,1,2,3 TMRC0,1