Shop OBEX P1 Docs P2 Docs Learn Events
Help needed to "shorten" time between pulses — Parallax Forums

Help needed to "shorten" time between pulses

XypherXypher Posts: 10
edited 2009-12-15 10:09 in BASIC Stamp
I am writing a program to control a 4 Channel r/c toy.

However, the time taken for each cycle of 4 channels is too slow. The receiver needs to be updated every 20ms. 50 times a second. However each full PPM output takes 75-100ms at least. Partly due to RCTIME function. Is there anyway to control it more effectively? At the current level, its impossible to control any r/c toy I think.

Would PWMPAL be the best tool for this job?
Somebody said...
' {$STAMP BS2}
' {$PBASIC 2.5}

' Control servos with accelerometer


rawx VAR Word
rawy VAR Word
x VAR Word
y VAR Word
time VAR Word
throttle VAR Word
header VAR Word

DO
PULSIN 8, 1, rawx 'data from accelerometer
PULSIN 9, 1, rawy 'data from accelerometer

y =5 * rawy / 12 - 290 'formula to convert raw data to pulse to control servos

x =5 * rawx / 12 - 290 'formula to convert raw data to pulse to control servos

HIGH 8 'receive data from potentiometer to control motor
RCTIME 8, 1, time

throttle = 10 * time + 490 'convert data to pulse

PULSOUT 7, x
PULSOUT 7, x
PULSOUT 7, throttle
PULSOUT 7, y
header = 10000 - x - x - y - throttle 'calculate the pulse require to fill up remaining 20ms

PULSOUT 7, header 'pulse to reset



LOOP

Thanks.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-18 17:19
    A Stamp can only do one thing at a time and nothing is overlapped. With 4 R/C pulses, that's maybe as much as 10ms spent just generating the pulses themselves leaving only 10ms for other things. You have two accelerometers you want to read and a potentiometer and the time to read these has to come out of the 10ms that's left. You need to reduce the width of the "leftover" pulse to compensate. Remember that the units for both PULSIN and PULSOUT are 2us on a BS2. Your "leftover" pulse should be:

    header = 10000 -x - x - throttle - y - rawx - rawy - time

    You also need to check for no time left like:

    IF header > 0 THEN PULSOUT 7, 2*header
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-11-18 17:44
    Xypher,

    Is there any reason you need to read all three sensor outputs in every transmit cycle? Perhaps you could save time by rotating through them, reading just one per cycle.

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2009-11-19 05:07
    Xypher,

    Phil's suggestion is the way to do it in a single threaded environment. Basically you have an Index counter set to count to "N" and recycle...

    Pseudo Code
    
    Index = Index + 1
    
    If Index = 1
       PULSIN 8, 1, rawx 'data from accelerometer
       PULSIN 9, 1, rawy 'data from accelerometer
    
    If Index = 2
       y =5 * rawy / 12 - 290 'formula to convert raw data to pulse to control servos
       x =5 * rawx / 12 - 290 'formula to convert raw data to pulse to control servos
    
    If Index = 3
       HIGH 8 'receive data from potentiometer to control motor
       RCTIME 8, 1, time
    
    If Index = 4
       throttle = 10 * time + 490 'convert data to pulse
    
       Index = 0  ' Reset Index count
       Flag = 1   ' Flag ensures that we've completed the loop at least once before sending out pulses 
    
    If Flag = 1
       PULSOUT 7, x
       PULSOUT 7, x
       PULSOUT 7, throttle
       PULSOUT 7, y
       header = 10000 - x - x - y - throttle 'calculate the pulse require to fill up remaining 20ms
       PULSOUT 7, header 'pulse to reset
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • XypherXypher Posts: 10
    edited 2009-11-25 06:42
    Thanks Beau for the code.

    However, due to RC TIME, the pulse is still too long for my receiver. Is there any other way to control the motor without RC TIME?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-25 07:10
    Look at the chapter in the Stamp Manual on the RCTIME statement. You might be able to change the capacitor value to use a shorter time period for the RCTIME. You'll probably get less resolution, but it may be enough for your purpose. If not, you'll just have to use some kind of external analog to digital converter to measure the voltage at the pot wiper if it's connected between +5V and ground.
  • XypherXypher Posts: 10
    edited 2009-12-15 08:25
    waveform.jpg

    May I know why this kind of waveform appears instead of the usual pulsout waveform?
    Somebody said...
    DO

    PULSOUT 5,750
    PAUSE 12
    PULSOUT 5,700
    PAUSE 12
    PULSOUT 5,650
    PAUSE 12
    PULSOUT 5,600
    PAUSE 12

    LOOP

    I used this program to test the waveform but it still looks wrong.

    Thanks!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-12-15 10:09
    It looks like you forgot to connect your scope's ground lead to Vss, or else you've got the input set for AC coupling.

    -Phil
Sign In or Register to comment.