Shop OBEX P1 Docs P2 Docs Learn Events
Question about my BS2 Code — Parallax Forums

Question about my BS2 Code

noleboticnolebotic Posts: 8
edited 2011-02-21 15:32 in BASIC Stamp
Hello,
I'd be grateful if someone could help me figure out an issue I ran into tonight with my BS2 code. I am working on a DC motor powered project using a solarbotics LM298 motor driver and a small servo that will eventually rotate an IR LED and IR sensor for obstacle detection. I started off my code by using the example code that came with the motor driver kit. It works well on its own, driving the motors in which ever direction I code. The problem arises when I add on the code to drive the servo clockwise and back. If I use the GOSUB FORWARD or GOSUB LEFT option, the servo jitters really bad. When I use the GOSUB BACKWARD or GOSUB RIGHT the servo rotates back and forth perfectly as the 2 dc motors turn as they should. I have tried every type of trial and error option I could think of:

-I used a separate power supply for the servo instead of the 5V regulated tap on the motor driver. It didn't help.
-I used a DO-LOOP command instead of FOR b1= 1-30 NEXT. It didn't help
-I used a different BS2 pin. It didn't help
-I put the servo code as a subroutine. It didn't help

I have read the BOE-BOT manual, and the stampworks manual and searched the forums and couldn't find anything. I pasted my code below. Any help is much appreciated.



'{$STAMP BS2}
'ALISE SOURCE CODE



'
Pin Allocations

'Motor control
Enable_Right CON 13 'Enable high, disable low
Enable_Left CON 13 'Enable high, disable low

'
Direction Truth Table
'L1 & L3 | L2 & L4 | M1,M2,M3,M4 Outputs
'
|
|
M1a_Direction CON 9 ' HIGH | HIGH | Outputs = High (Motor Break)
M1b_Direction CON 10 ' LOW | LOW | Outputs = LOW (Motor Coast)
M2a_Direction CON 11 ' HIGH | LOW | Current flows POS.(Direction 1)
M2b_Direction CON 12 ' LOW | HIGH | Current flows Neg.(Direction 2)
'


Temp VAR WORD 'Temp register for manipulating bits

'
Start of main program
MAIN:

FOR B1 = 1 TO 30
PULSOUT 1,900 'secondary motion
PAUSE 20
NEXT
FOR B1 = 1 TO 30
PULSOUT 1,400 'Primary motion
PAUSE 20
NEXT
GOSUB FORWARD
GOTO MAIN 'Go back and start again

'
Subroutines
LEFT:
HIGH Enable_Right 'Turn on right motor
HIGH Enable_Left 'Turn on left motor
HIGH M1a_Direction 'This comination makes the right
LOW M1b_Direction 'motor turn clockwise
HIGH M2a_Direction 'This combination makes the left
LOW M2b_Direction 'motor turn Counterclockwise
RETURN 'return from this subroutine to the main
'program

RIGHT:
HIGH Enable_Right 'Make sure motors are still on
HIGH Enable_Left
LOW M1a_Direction 'This combination makes the right motor
HIGH M1b_Direction 'turn counterclockwise
LOW M2a_Direction 'This combination makes the left motor
HIGH M2b_Direction 'turn clockwise
RETURN

FORWARD:
HIGH Enable_Right 'Make sure motors are still on
HIGH Enable_Left
LOW M1a_Direction 'This combination makes the right motor
HIGH M1b_Direction 'turn counterclockwise
HIGH M2a_Direction 'This combination makes the left motor
LOW M2b_Direction 'turn counterclockwise
RETURN

BACKWARD:
HIGH Enable_Right 'Make sure motors are still on
HIGH Enable_Left 'This combination makes the right motor
HIGH M1a_Direction 'turn clockwise
LOW M1b_Direction 'This combination makes the left motor
LOW M2a_Direction 'turn clockwise
HIGH M2b_Direction
RETURN

GOTO main 'We should never reach this point, but
'it's a good failsafe

'

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-02-19 20:31
    Start by reducing your code to just the servo code. Where are the servos attached and how are they powered? Attach you new, pruned code and we can look at it.
  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-02-20 02:45
    What is the rest of the code?
    What you have quoted will only run the Gosub Forward routine, I don't see the logic for using the other directions.

    On a similar note I had designed a program that rotated an ultrasonic sensor from left to centre, then to the right, back to centre and then to the left and repeat. The program took distance readings and compared it to the last readings from that direction to make a choice to where to go.

    I had a similar issue but mine was the drive motors jerked rather than run smoothly, it was due to the main program running the servo rather than the drive motors. I could not fix it without either updating it with a ServoPAL or going to the Propellor chip since the BasicStamp was a little limited in this regard.
  • noleboticnolebotic Posts: 8
    edited 2011-02-20 06:32
    Hi Franklin-
    The sevo rotation code is pasted below. It is connected to pin#1 on my BS2 (I tried other pins too, but the problem still remains) I am powering the servo using the regulated 5V tap on the solarbotics CMD motor driver. the specs for this motor driver can be found here:
    http://www.solarbotics.com/products/k_cmd/specs/

    I also tried using a completely separate 5V power supply that I've successfully used for servos in the past, and the problem still persists.

    Here is the servo code from my program:

    FOR B1 = 1 TO 30
    PULSOUT 1,900 'secondary motion
    PAUSE 20
    NEXT
    FOR B1 = 1 TO 30
    PULSOUT 1,400 'Primary motion
    PAUSE 20
    NEXT

    Thank you very much for your assistance and feedback!

    Nole.
  • noleboticnolebotic Posts: 8
    edited 2011-02-20 06:38
    Hello Clive,
    Good point you have regarding the rest of my code. I haven't written it yet. All I have is the GOSUB forward routine for right now until I figue why the servo is locking up on that subroutine but not the backward subroutine. Like your project, i too want to eventually have distance comparisons and then call the appropriate LEFT/RIGHT/FORWARD/BACKWARD subroutine based on the measurements. The other routines are listed because I wanted everyone to see the other subroutine options I plan to be using in this program. Why would the servo lock up and jitter on the forward and left routines, but rotate perfectly on the backward and right subroutines? There must be a simple explanation to this- I sure I made some kind of beginner's mistake here. I really appreciate your response and feedback!

    Nole.
  • Mike GMike G Posts: 2,702
    edited 2011-02-20 06:53
    Servos require a pulse at least every 20ms to stay energized and hold position. Your code pauses for 20ms which is right at the limit. I would start with decreasing the pause and testing. Try to isolate the servo as best you can so you don't have other devices affecting the results. If the servo work fine by itself then you'll have to look at servo control signal with the motors attached.

    Later, I would think about replacing the pauses with running code. Why waste clock cycles.
  • noleboticnolebotic Posts: 8
    edited 2011-02-20 07:50
    Hi Mike,
    I just tried your suggestions, and unfortunately the issue still exists. I tried several different pause lengths < and >20ms. I also isolated the servo as well, and I can get the servo to rotate 180 degrees back and forth no problem with the motors off. I also isolated the power supply and it works fine with either power supply. It seems very odd to me that the servo can rotate just fine with the motors running on the backward and right subroutines, but the servo locks up and jitters with the forward and left subroutines. I appreciate your feedback on this!

    Thanks,
    Nole.
  • ercoerco Posts: 20,256
    edited 2011-02-20 08:43
    'Motor control
    Enable_Right CON 13 'Enable high, disable low
    Enable_Left CON 13 'Enable high, disable low
    ...
    HIGH Enable_Right 'Turn on right motor
    HIGH Enable_Left 'Turn on left motor


    It looks like you meant to have your left & right motors enabled by 2 different pins, but in your code, they are both enabled by pin 13. You may have intended to use pin 14 for your left motor. Check if that's part of your problem.

    I know I'm old school, but I much prefer a simple 1995-era "high 13" (and undefined B0 and W0 variables, for that matter) to the modern overdefined, "more flexible, looks-like-C" code that makes for longer, IMHO less readable programs and increases the chances that errors creep in. Keep it BASIC!
  • noleboticnolebotic Posts: 8
    edited 2011-02-20 12:33
    Hi Erco,
    I had number the 13 pin doubled because the solarbotics brand l298cmd motor driver instructions suggested that you tie the 2 enable lines together on the driver board and send them to 1 pin on on the basic stamp. What's interesting though, is how solarbotic's example BS2 code suggests your send all lines on the motor driver board to an individual pin on the stamp, So I took your advice and followed the solarbotic example code exactly and re-wired the lines coming from the driver board, and there was no change at all. The FORWARD and LEFT subroutine still makes the servo lock up and jitter, while the BACKWARD and RIGHT subroutines allow the servo to rotate 180deg perfectly back and forth while the motors spin per their code. I have pasted the code below. Please note that this code is an exact copy of solarbotic's example code from their website, with the addition of the simple servo rotation code by me. As always, thank you very much for your review!


    '{$STAMP BS2}
    ' This program assumes that CMD motor controller is connected to the following BS2 pins;
    ' Pin7 = E 1-2
    ' Pin8 = E 3-4
    ' Pin9 = L1
    ' Pin10= L2
    ' Pin11= L3
    ' Pin12= L4
    ' Be sure to connect at least 6v to the power input and connect the motors
    ' to the outputs.


    '
    Pin Allocations

    'Motor control
    Enable_Right CON 7 'Enable high, disable low
    Enable_Left CON 8 'Enable high, disable low

    '
    Direction Truth Table
    'L1 & L3 | L2 & L4 | M1,M2,M3,M4 Outputs
    '
    |
    |
    M1a_Direction CON 9 ' HIGH | HIGH | Outputs = High (Motor Break)
    M1b_Direction CON 10 ' LOW | LOW | Outputs = LOW (Motor Coast)
    M2a_Direction CON 11 ' HIGH | LOW | Current flows POS.(Direction 1)
    M2b_Direction CON 12 ' LOW | HIGH | Current flows Neg.(Direction 2)
    '

    Temp VAR Word 'Temp register for manipulating bits


    '
    Start of main program
    MAIN:
    'Servo code to constantly rotate servo clockwise and counterclockwise
    FOR B1 = 1 TO 30
    PULSOUT 1,900 'secondary motion
    PAUSE 20
    NEXT
    FOR B1 = 1 TO 30
    PULSOUT 1,400 'Primary motion
    PAUSE 20
    NEXT

    'The next part of the code is for the motor driver

    GOSUB FORWARD
    GOTO MAIN 'Go back and start again

    '
    Subroutines
    LEFT:
    HIGH Enable_Right 'Turn on right motor
    HIGH Enable_Left 'Turn on left motor
    HIGH M1a_Direction 'This comination makes the right
    LOW M1b_Direction 'motor turn clockwise
    HIGH M2a_Direction 'This combination makes the left
    LOW M2b_Direction 'motor turn Counterclockwise
    RETURN 'return from this subroutine to the main
    'program

    RIGHT:
    HIGH Enable_Right 'Make sure motors are still on
    HIGH Enable_Left
    LOW M1a_Direction 'This combination makes the right motor
    HIGH M1b_Direction 'turn counterclockwise
    LOW M2a_Direction 'This combination makes the left motor
    HIGH M2b_Direction 'turn clockwise
    RETURN

    FORWARD:
    HIGH Enable_Right 'Make sure motors are still on
    HIGH Enable_Left
    LOW M1a_Direction 'This combination makes the right motor
    HIGH M1b_Direction 'turn counterclockwise
    HIGH M2a_Direction 'This combination makes the left motor
    LOW M2b_Direction 'turn counterclockwise
    RETURN

    BACKWARD:
    HIGH Enable_Right 'Make sure motors are still on
    HIGH Enable_Left 'This combination makes the right motor
    HIGH M1a_Direction 'turn clockwise
    LOW M1b_Direction 'This combination makes the left motor
    LOW M2a_Direction 'turn clockwise
    HIGH M2b_Direction
    RETURN

    GOTO main 'We should never reach this point, but
    'it's a good failsafe

    '
  • ercoerco Posts: 20,256
    edited 2011-02-20 14:30
    Looks like turning your right motor forward (in your FORWARD and LEFT subroutines) may be causing the servo problem. Check your wiring there, and then comment out the right motor commands in those routines and see if your servo jitter stops. You need to isolate the problem, one step at a time.
  • noleboticnolebotic Posts: 8
    edited 2011-02-20 15:34
    Great catch Erco. I commented out the HIGH M2a line in the FORWARD and LEFT subroutines. The right motor does not spin when calling the FORWARD or LEFT subroutines after that, and the servo works perfectly. I checked the wiring but did not see any anomalies, everything is going where it should with no breaks or shorts. So I know now thanks to you,that it's the right motor spinning forward that is causing the servo lock up and jitter. But where to from here considering I checked the wiring? Thanks a lot for walking me thru this.

    Nole
  • ercoerco Posts: 20,256
    edited 2011-02-20 16:43
    Sounds like a hardware problem, either wiring, power, or motor driver. Hard to troubleshoot via these text interchanges. You could try swapping L/R motors or sides of the motor controller to further isolate.
  • noleboticnolebotic Posts: 8
    edited 2011-02-21 15:24
    Very strange indeed. The motor driver works just fine on its own using all directions. I guess I'll have throw in the towel on this one and just not use the servo to rotate my sensor array. Anyone else out there have any ideas? Thanks again to everyone for your thoughts!

    Thanks,
    Nole
  • Mike GMike G Posts: 2,702
    edited 2011-02-21 15:32
    This is where an o-scope comes in handy. You could have some problems on the servo power or signal line.
Sign In or Register to comment.