Shop OBEX P1 Docs P2 Docs Learn Events
Motor control software question — Parallax Forums

Motor control software question

DavidNymanDavidNyman Posts: 52
edited 2012-09-02 10:15 in General Discussion
I have the following line of code in a subroutine:

Left_hand_turn:
FOR index = 0 TO 100
HIGH 15: LOW 14
HIGH 11: LOW 12
PULSOUT 13,index + 500
PULSOUT 10,index + 500
NEXT
'IF(string = "B")THEN GOTO Left_hand_turn
RETURN

I'm trying to get the program to stay in the subroutine until the variable "string" no longer = "B".
Basically the joystick has snapped back to the center and the turn routine is no longer needed.
I tried the second to last line in an effort to accomplish this but it just leaves the motor running instead of stopping when the joystick centers.
Can anyone suggest a easy way to fix this?
Thanks in advance!

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-09-01 07:23
    Perhaps another line of code IF(string =! "B) GOTO....

    Of course you have to either have the GOTO telling the motor to stop or to continue in a straight line rather than a turn.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-09-01 07:27
    As usual with showing only fragments of a larger program and without any description of the context (hardware, other software, etc.), it's hard to give advice.

    You've got a FOR / NEXT loop that does some PULSOUTs, presumably to affect a motor controller. You've got some variable which is somehow magically set. If the variable is "B", then the program repeats the FOR / NEXT loop. If it's not "B", the subroutine exits and you want the motor to stop.

    Well, you have to do something to make the motor stop. Commonly, motor controllers continue to make the motor go at the same speed until they're instructed somehow to do something else. Presumably, there's a PULSOUT setting that will cause the motor to stop. For something like Parallax's HB-25 controller, that's a pulse with a width of 1.5ms. Using a BS2, that would be a PULSOUT <pin>,750.
  • DavidNymanDavidNyman Posts: 52
    edited 2012-09-01 07:38
    Sorry guys,
    Here's the slave program, please forgive the mistakes, I'm not very good at programming.
    The stamp is tied to an RS-485 network as a slave and receives commands from another BS2 as the master. It's working well now, I just need to get the motor command to stay in the sub routine until I let go of the joystick.
    I'm using this for my "joystick"
    http://www.parallax.com/Store/Accessories/HumanInterfaceDevices/tabid/822/CategoryID/90/List/0/SortField/0/Level/a/ProductID/615/Default.aspx
    I'm using Pololu VNH3SP30 motor controllers.

    '============ Variables ==========
    string VAR Byte 'Used to hold the " command"
    index VAR Word 'used to hold the ramping variable
    '============ Initialize==========
    OUTPUT 2 'Make pin 2 the LED connected pin.
    OUTPUT 0 'Make pin 0 an output pin.
    LOW 0 'Put the SN75176 into receive mode.
    '============ Begin Program ======
    loop1:
    DEBUG HOME, "string = ",DEC string, CR 'Display data
    PAUSE 50 'wait a half second.
    SERIN 1,16468,60000,loop1,[string] 'Read a byte of data coming in.
    IF(string = "B")THEN GOSUB Left_hand_turn 'If B, then ...
    IF(string = "C")THEN GOSUB Right_hand_turn 'If C, then ...
    IF(string = "D")THEN GOSUB forward 'If D, then ...
    IF(string = "E")THEN GOSUB backward 'If E, then ...
    GOTO loop1

    '===========[Sub routine]=====================
    Left_hand_turn:
    FOR index = 0 TO 100
    HIGH 15: LOW 14
    HIGH 11: LOW 12
    PULSOUT 13,index + 500
    PULSOUT 10,index + 500
    NEXT
    IF(string =! "B")GOTO Left_hand_turn
    RETURN

    Right_hand_turn:
    FOR index = 0 TO 100
    HIGH 14: LOW 15
    HIGH 12: LOW 11
    PULSOUT 13,index + 500
    PULSOUT 10,index + 500
    NEXT
    RETURN

    Forward:
    FOR index = 0 TO 100
    HIGH 15: LOW 14
    HIGH 12: LOW 11
    PULSOUT 13,index + 500
    PULSOUT 10,index + 500
    NEXT
    RETURN

    Backward:
    FOR index = 0 TO 100
    HIGH 14: LOW 15
    HIGH 11: LOW 12
    PULSOUT 13,index + 500
    PULSOUT 10,index + 500
    NEXT
    RETURN



    Here is the master program so far:





    '========== Variables =============

    a VAR Word
    '========== Initialize ============
    OUTPUT 0 'Make pin 0 an output
    HIGH 0 'Put the SN75176 into transmit mode
    '========== Main Loop =============
    a=30
    start:
    IF(IN2<>0)AND (IN3<>0)AND (IN4<>0) AND (IN5<>0) THEN SEROUT 1,16468,1,["a"] 'Send an "A" over the rs485 network when joystick centers.
    PAUSE a
    IF(IN2<>1)THEN SEROUT 1,16468,1,["B"] 'Send a "B" over the RS485 network
    PAUSE a
    IF(IN3<>1)THEN SEROUT 1,16468,1,["C"] 'Send a "C" over the RS485 network
    PAUSE a
    IF(IN4<>1)THEN SEROUT 1,16468,1,["D"] 'Send a "D" over the RS485 network
    PAUSE a
    IF(IN5<>1)THEN SEROUT 1,16468,1,["E"] 'Send a "E" over the RS485 network
    GOTO start:
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-09-01 09:27
    Phip is absolutely right. You have to do something to make the motor stop.

    I don't know which command should be sent over the RS385 to stop the motor, but can you sense the neutral position of the joystick and have that be a condition that sends such a command?

    You have 5 lines (IN0 through IN4). Seems that IN0 is centered, but you seem to not be using IN0 or IN1 in your code. And you have IN5. Either you have shifts all the labels to suit your preference or your wiring and code are confused.
  • DavidNymanDavidNyman Posts: 52
    edited 2012-09-01 09:40
    The motors stop now when the joystick is released to center. The subroutine holds the motor drivers attention until the FOR Next loop runs out.
    This all is working very nicely. The motors pause for a few milliseconds now once the For Next loop runs out as the program jumps back up to run the main loop then once it sees joystick is still in the same position, returns to the subroutine.
    I'd like to eliminate that pause and stay in the subroutine as long as the joystick is being held to enable motion.
    IN0 & IN1 work the 75176 as laid out in the code.
    If all the contacts on the joystick are in the open position, the character A is transmitted out over the network. Once a contact is made on the joystick the corresponding letter is transmitted over the network.
    Again, it's all working correctly now and I have a nice 100' piece of Cat5 in between the master and slave BS2s. The brief pause in the motor operation is what I'd like some help with.

    This all goes into a DIY ROV. I've got a fiber optic transmitter and receiver pair that send my video topside via a fiber cable.
    The slave BS2 goes into the ROV and also has a HM55 compass that will transmit back up via another set of 75176 chips so I don't have to worry about conflicting signals over my RS485 network. The whole project is coming together nicely. My tether will have a piece of Cat 5 for the two networks and the fiber optic for the video.
  • DavidNymanDavidNyman Posts: 52
    edited 2012-09-02 10:03
    I appreciate the responses. I still don't have an answer.
    Can anyone help me?
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-09-02 10:15
    I think the only way to do what you want is to be able to detect the joystick position within the for-next loop. That may not be practical with the current set up. I'm thinking you need a motor controller that doesn't need constant attention.
Sign In or Register to comment.