Shop OBEX P1 Docs P2 Docs Learn Events
coordination between motors — Parallax Forums

coordination between motors

AnnoyingAnnoying Posts: 50
edited 2009-09-20 02:48 in General Discussion
I need to finish·a project so I would really appreciate help with this. I have two motors attached to two separate BS2s, with an accelerometer, RF Transmitter/Receiver modules, and an HB-25 motor controller·attached to each BS2. I want the motors to alternate turning in this way: once the transmitted accelerometer value from one·BS2·is above a value, the motor receiving the value·turns on until the received value is below a certain value. Then this motor stops and the other one turns until the accelerometer value it receives meets a similar condition. They repeat in this fashion, so it's off,on,off...for one motor and on,off,on...for the other motor. I can't get the motors to coordinate in this way and I know the problem has something to do with timing and delays. Once the accelerometer condition is met, I can see in the debug window that the program is out of the current loop as it should be, but it doesn't enter the next loop. Does anyone see what is wrong with this code? Thank you.


'{$STAMP BS2}
'{$PBASIC 2.5}

y1 VAR Word··· 'accelerometer value from·motor's own·BS2

y2 VAR Word·· 'accelerometer value from other BS2



Main:

SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]· 'receive other BS2's accelerometer value


IF y2 >3120 THEN

PULSOUT 3, 1000· 'motor turns

DO UNTIL y2 <1930

SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]

LOOP


PULSIN 5, 1, y1 'get accelerometer value from motor's own BS2
PULSOUT 3, 750 'stop turning the motor

DO UNTIL y1 > 3100
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]· 'send out accelerometer value so other motor will turn (its code is just like the previous loop)
PULSIN 5, 1, y1

LOOP

'Repeat process:

PULSOUT 3, 1000
DO UNTIL y2 > 3120
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]

LOOP


PULSOUT 3, 750
DO UNTIL y1 < 1880
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
PULSIN 5,1, y1
LOOP

ENDIF


GOTO Main

note: I have put the PULSOUT commands within the DO LOOPs but this hasn't worked well either.

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2009-09-16 07:04
    I changed up your code a bit so that I could follow it easier, maybe it'll help to spot the problem. I'm sleepy.

    G'nite.

    Rich H

    
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    
    y1 VAR Word    'accelerometer value from motor's own BS2
    
    y2 VAR Word   'accelerometer value from other BS2
    
    
    
    Main:
    
    GOSUB GetOtherValue
    
    IF y2 >3120 THEN
    GOSUB RunMotor
    
    DO UNTIL y2 <1930
    GOSUB GetOtherValue
    LOOP
    
    GOSUB GetOwnValue
    
    GOSUB StopMotor
    
    DO UNTIL y1 > 3100
    GOSUB SendValue
    GOSUB GetOwnValue
    LOOP
    
    
    'Repeat process:
    
    
    GOSUB RunMotor
    
    DO UNTIL y2 > 3120
    GOSUB GetOtherValue
    LOOP
    
    GOSUB StopMotor
    
    DO UNTIL y1 < 1880
    GOSUB SendValue
    GOSUB GetOwnValue
    LOOP
    
    ENDIF
    
    
    GOTO Main
    
    GetOtherValue:
    SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]  'receive other BS2's accelerometer value
    RETURN
    
    RunMotor:
    PULSOUT 3, 1000  'motor turns
    RETURN
    
    StopMotor:
    PULSOUT 3, 750 'stop turning the motor
    RETURN
    
    GetOwnValue:
    PULSIN 5, 1, y1 'get accelerometer value from motor's own BS2
    RETURN
    
    SendValue:
    SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
    RETURN
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Servo Boss, a 12 channel servo tester kit from Gadget Gangster.
  • AnnoyingAnnoying Posts: 50
    edited 2009-09-18 23:53
    thank you so much for simplifying the code! it helped but I still have a slight problem. this is a portion of the code for the other motor:

    IF y2 <1920 THEN

    GOSUB StopMotor

    DO UNTIL y2 > 3120

    GOSUB GetOwnValue
    GOSUB SendValue

    LOOP

    GOSUB GetOtherValue

    GOSUB RunMotor

    DO UNTIL y1 > 3000
    DEBUG ? y1
    GOSUB GetOtherValue
    LOOP

    DEBUG "hi"
    GOSUB StopMotor
    ....................

    So this code works fine until after the last loop. For some reason when y1 > 3000, it exits the loop and no longer prints the value of y1 in the debug window, which is good,·but it does not print "hi" on the debug screen after this and the motor never stops. Do you know why this would happen?
  • FranklinFranklin Posts: 4,747
    edited 2009-09-19 02:23
    If you would attach your whole program using the attachment manager it would help You have gosubs in your code but they aren't where we can see the flow.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • dev/nulldev/null Posts: 381
    edited 2009-09-19 22:46
    Could it be that the other side is looping and not sending data? Can you debug both Stamps at the same time? If not, you can use a LED or LCD to indicate that the other side is looping... Just an idea.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • AnnoyingAnnoying Posts: 50
    edited 2009-09-20 00:00
    I actually got it to work!! turns out the HB-25 initialization code at the beginning was not allowing the program to loop a second time, so I fixed it. thank you though!
  • FranklinFranklin Posts: 4,747
    edited 2009-09-20 02:48
    Attaching your working code might help others.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.