PDA

View Full Version : Your RC Servo Code


Jawahar
06-17-04, 11:39 PM
This is with respect to RM Dumse's code for an RC Servo application. The code is at the end of this post.

The code did not work when it sends out the same value of the signal it took in. But when I scaled the outgoing value to twice the incoming value, it worked.

addition to your code:
200 100 */ ( doubling the pulse width just after reading it.)

Can you explain why it works this way?



*******RM Dumse's Code******************
Whatever signal it took in on

TA0
TA1
TA2

It turns around and sends out again on

PWMA0
PWMA1
PWMA2

Hope that helps.




SCRUB

HEX

VARIABLE CH EEWORD

: CHVAR3
P@ CH @ +
; EEWORD

CHVAR3 RCIN EEWORD
CHVAR3 RCOUT EEWORD

: CH0 0 CH ! ; EEWORD
: CH1 1 CH ! ; EEWORD
: CH2 2 CH ! ; EEWORD

D09 CONSTANT MINPOS EEWORD ( ~.59 mS
2DCB CONSTANT MAXPOS EEWORD ( ~2.38 ms
MAXPOS MINPOS + 2/ CONSTANT MIDPOS EEWORD
MAXPOS MINPOS - CONSTANT RCRANGE EEWORD
2710 CONSTANT RCSCALE# EEWORD ( 10,000dec

: INIT
7FFF PWMA0 PWM-PERIOD ( 76 Hz UPDATE RATE

MIDPOS PWMA0 PWM-OUT ( MIDPOS
MIDPOS PWMA1 PWM-OUT ( MIDPOS
MIDPOS PWMA2 PWM-OUT ( MIDPOS

TA0 ACTIVE-HIGH
TA1 ACTIVE-HIGH
TA2 ACTIVE-HIGH

TA0 SET-PWM-IN
TA1 SET-PWM-IN
TA2 SET-PWM-IN
; EEWORD

: RC
( A SINGLE CHANNEL OF PWM IN AND OUT
TA0 CHK-PWM-IN ?DUP
IF
CH0
RCIN !
( PROCESS AS NEEDED HERE
RCIN @ RCOUT !
( PROCESS AS NEEDED HERE
RCOUT @ PWMA0 PWM-OUT
TA0 SET-PWM-IN
( CR ." CH 0 " DUP .
THEN

TA1 CHK-PWM-IN ?DUP
IF
CH1
RCIN !
( PROCESS AS NEEDED HERE
RCIN @ RCOUT !
( PROCESS AS NEEDED HERE
RCOUT @ PWMA1 PWM-OUT
TA1 SET-PWM-IN
( CR ." CH 1 " DUP .
THEN

TA2 CHK-PWM-IN ?DUP
IF
CH2
RCIN !
( PROCESS AS NEEDED HERE
RCIN @ RCOUT !
( PROCESS AS NEEDED HERE
RCOUT @ PWMA2 PWM-OUT
TA2 SET-PWM-IN
( CR ." CH 2 " DUP .
THEN

; EEWORD

MACHINE-CHAIN PIGRC
CH @
RC
CH !
END-MACHINE-CHAIN EEWORD

: STARTUP
INIT
( XXXX SET-STATE
CR ." STARTING UP " CR
EVERY C350 CYCLES SCHEDULE-RUNS PIGRC
; EEWORD

: X INIT BEGIN RC ?TERMINAL UNTIL ; EEWORD

( 3C00 AUTOSTART STARTUP
( SAVE-RAM

*****************End of Code*****************

RMDumse
06-18-04, 03:31 PM
Originally posted by Jawahar
The code did not work when it sends out the same value of the signal it took in. But when I scaled the outgoing value to twice the incoming value, it worked.

Can you explain why it works this way?


Yes, this looks like what I threw together for Prof. Huff. It was to read the output of an RC receiver, and then retransmit the same length pulses to the RC Servos.

I'm surprized by the factor of 2 difference. Obviously. If I'd suspected that, I'd have allowed for it. I'm not sure if it might not be a revision issue. What version IsoMax(TM) do you have on your IsoPod(TM)?

In any case, if it is indeed a factor of two, you can scale it as you showed, but there are easier ways to get 2x. In fact the word 2* is included in the language which does a very fast Arithmetic Shift Left of a 16-bit value on the stack. So here's how I'd modify my example code, assuming the 2x problem is not a passing thing with an older revision (which I still need to check into).

TA0 CHK-PWM-IN ?DUP
IF
CH0
RCIN !
( PROCESS AS NEEDED HERE
RCIN @ RCOUT !
( PROCESS AS NEEDED HERE
RCOUT @ 2* PWMA0 PWM-OUT
TA0 SET-PWM-IN
( CR ." CH 0 " DUP .
THEN


Oh! I think I know what's going on here. PWM-OUT works on a scale of 0-FFFF. TA0 CHK-PWM-IN returns the same value as the modulo time base of the PWM out hardware works in, 0-7FFF. So the PWM-OUT word is essentially doing a "1/2" scaling. Sure. This isn't a revision problem. It's just how the PWM-OUT word does its scaling. So just add the 2* before delivering the value to PWM-OUT.

Jawahar
06-18-04, 09:47 PM
That sets it right!

Actually I used "200 100 */" as I wanted two decimal places precision. I experimented scales ranging from 1.05 to 2.10. It was after all that trial and error that I figured the factor was 2.

Thanks for the reasoning why!