Shop OBEX P1 Docs P2 Docs Learn Events
Problem with Motor Code: please help!! — Parallax Forums

Problem with Motor Code: please help!!

crd15crd15 Posts: 12
edited 2009-04-14 02:40 in Robotics
I am running the Basic Stamp 2sx and I have a slight error in my code somewhere and can't seem to find it. I am running 4 dc motors off of a motor controller that has 2 switches. S1 controls forward and reverse and S2 controls left and right (tank style). Below is a copy of the code.

' {$STAMP BS2sx}
' {$PBASIC 2.5}

'this program tests the servo position

motor PIN 14
turn PIN 15

forward CON 1600
backward CON 2050
break CON 1850
right CON 1600
left CON 2100
turnbreak CON 1875

counter VAR BYTE
pulses VAR WORD
duration VAR WORD

'PULSOUT motor, break
'PULSOUT turn, turnbreak


main:

FOR counter = 1 TO 1
DEBUG "b"
PULSOUT motor, break
PULSOUT turn, turnbreak
PAUSE 2000
NEXT

FOR counter = 1 TO 1
DEBUG "f"
PULSOUT motor, forward
PULSOUT turn, break
PAUSE 4000
NEXT

FOR counter = 1 TO 1
DEBUG "L"
PULSOUT motor, break
PULSOUT turn, left
PAUSE 1000

NEXT

GOTO main


END




I understand that it will never get to the end because its in a never ending loop. That is not my problem. When i download the program to the board, the first break works perfectly. The motors do not move. Every time after that, the break command will not work when it comes back from the first loop. It goes into a turning command. Is there a way to reset the frequency i am sending to the motors after every loop so that is starts fresh again. I need a way to clear out the signal thats being sent so that it will break after the loop. If anyone can help please let me know. Kisses.

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-04-09 21:35
    Are you sure that one pulse is enough to return the system to a given state? Which controller are you using?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • crd15crd15 Posts: 12
    edited 2009-04-10 14:20
    I actually am still trying to figure it out. In what way would you send multiple purposes: a for loop? I am really new at basic and alright at programming. I am using the Sabertooth 2x5 Microcontroller in the second mode which is for micro controllers. There are three wires coming off the back of it. I have one for ground, the other is SW1 that controls forward and reverse and SW2 controlling left and right. Any help getting this work would be greatly appreciated. I am working on it everyday for 10 hours a day but find myself stuck at this point.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-04-10 15:43
    Are you sure the S1 and S2 inputs only require a pulse to operate? I would guess (without the benefit of having any data on this controller) that they would need high/low inputs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • crd15crd15 Posts: 12
    edited 2009-04-10 21:26
    Actually I was looking at it today and you are right. I looked at their website and it said something about

    high (input pin)
    pause (1000 uS to 2000uS)
    low (input pin)

    is there a way anyone could send a sample code on how that would look in basic. I am not asking anyone to do my work for me but i am just can't seem to get it right. I tried that today but on the Basic Stamp 2sx the timing is all off even though the oscilloscope tells me its sending a 1500 uS pulse but it's not stoping.
  • FranklinFranklin Posts: 4,747
    edited 2009-04-11 04:17
    Which controller?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • crd15crd15 Posts: 12
    edited 2009-04-11 18:06
    oh sorry. Its the sabertooth 2x5. I have been trying to program it but just can't seem to get the right signal out. The motors will start turning but then all of a sudden jump to a really high speed. I'll keep trying to work on it but if someone could send a simple simple program to turn the pin high, send the signal, and then turn it low. I am new at the stamp and although its pretty basic, i just can't seem to figure this one part out.
  • GWJaxGWJax Posts: 267
    edited 2009-04-11 18:10
    It would be easier to help you if you post your routine here so we can see where you are going wrong and help you out.

    Jax
    edit:
    never mind, I just seen it at the top post, I'll look it over for you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • GWJaxGWJax Posts: 267
    edited 2009-04-11 21:05
    Just read the manual and I hope this works for you, Please test it out and let me know what it does.

    Jax

    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}

    'this program tests the servo position
    '=====================================================================
    ' Turn the switches on the sabertooth 2x5 to 1, 2, 3, 4, 5, 6
    ' off, on, on, on, on, off
    '

    Motor PIN 14 ' High controls foward and Low controls backwards
    Turn PIN 15 ' High controls CC and Low controls CCW

    '=====================================================================
    ' This program should control 2 motors in a mix mode at the same time
    ' , in the modethe motors do not come to a complete stop or rest or
    ' rather it will continue to move foward, backward, left and right
    ' If one wants to control stopping the motors program it the same way
    ' you would control a SERVO using the PULSOUT command and set switch 6
    ' to the on possition.
    '

    Main:

    GOSUB Foward
    GOSUB Backwards
    GOSUB CC
    GOSUB CCW

    GOTO Main

    Foward:

    DEBUG CLS, " Forward ", CR
    HIGH Motor
    PAUSE 2000
    RETURN

    Backwards:

    DEBUG " Backwards ", CR
    LOW Motor
    PAUSE 2000
    RETURN

    CC:

    DEBUG " CC ", CR
    HIGH Turn
    PAUSE 2000
    RETURN

    CCW:

    DEBUG " CCW ", CR
    LOW Turn
    PAUSE 2000
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • crd15crd15 Posts: 12
    edited 2009-04-11 22:05
    Thanks a ton for doing that. I won't be able to test it until monday morning because my robot is in the engineering lab. I will let you know though if it works. I read the manual over and over again and every other little piece of documentation on it as well for some reason i couldnt figure it out. I have everything else on the robot programmed perfectly, just that was the only thing. I will try it out and let you know. Again, thanks a ton!

    I was also looking at the code and your saying if I want to stop the motor. Just set pin 6 to on and pulsout the 1500 to stop the motors. Would I essentially be changing the whole code to the pulsout commands like the servo or just for the stop routine?

    Post Edited (crd15) : 4/11/2009 10:11:32 PM GMT
  • GWJaxGWJax Posts: 267
    edited 2009-04-11 23:57
    Once the switch is turned on then the program must be revamped back to using it like the you would with servos so yes the routines would need to be changed.

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • crd15crd15 Posts: 12
    edited 2009-04-13 21:46
    Hey i just wanted to thank everyone. I got them working. I tried your code jax but for some reason it still did not work. I did a lot of research over the weekend and got the timing right and also tested them as servos and everything is working perfectly. Here is a sample code for anyone else using the sabertooth and the basic stamp together. This routine makes a basic square. switch 4 is on in this example.

    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}

    left PIN 14 ' controls left motors
    right PIN 15 ' controls right motors

    counter VAR WORD

    Main:

    GOSUB Forward
    GOSUB CC
    GOSUB Forward
    GOSUB CC
    GOSUB forward
    GOSUB cc
    GOSUB forward
    GOSUB break

    GOTO Main


    '
    'subroutines
    '


    Forward:

    DEBUG " forward ", CR
    FOR counter = 1 TO 150
    PULSOUT 14, 1500
    PULSOUT 15, 1500
    PAUSE 20
    NEXT
    RETURN

    Backwards:

    DEBUG " Backwards ", CR
    FOR counter = 1 TO 150
    PULSOUT 14, 2200
    PULSOUT 15, 2200
    PAUSE 20
    NEXT
    RETURN

    CC:

    DEBUG " CC ", CR
    FOR counter = 1 TO 75
    PULSOUT 14, 1675
    PULSOUT 15, 2075
    PAUSE 20
    NEXT
    RETURN

    CCW:

    DEBUG " CCW ", CR
    FOR counter = 1 TO 75
    PULSOUT 14, 2075
    PULSOUT 15, 1675
    PAUSE 20
    NEXT
    RETURN

    break:

    DEBUG " stop ", CR
    FOR counter = 1 TO 150
    PULSOUT 14, 1875
    PULSOUT 15, 1875
    PAUSE 20
    NEXT
    PAUSE 2000
    RETURN

    There are several other routines in there for reverse and ccw turning but i did not include them in the subroutine. Thanks again everyone
  • GWJaxGWJax Posts: 267
    edited 2009-04-13 23:39
    Great to hear that you got it working using the servo commands. I had my doubts about my code so I reprogrammed it with the servo commands but since you have done it already there is no need for me to post it. Congrats on your programming skills.

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • crd15crd15 Posts: 12
    edited 2009-04-14 02:40
    ha my programming skills are pretty elementary. I have some good logic, it just takes me awhile to figure out everything. Your code helped a lot, since i used yours as the framework and just tried with the servo control. Thanks again though.
Sign In or Register to comment.