Shop OBEX P1 Docs P2 Docs Learn Events
Easy problem...I think — Parallax Forums

Easy problem...I think

agrossagross Posts: 5
edited 2007-12-03 05:27 in Propeller 1
I am using the propeller in conjunction with the tilt axis accelerometer to control a small R/C toy with tank style steering I have all of the wiring correct and was able to test using some basic outa[noparse][[/noparse]pin] commands. I am now trying to replicate movement of the tank with if and elseif statements based on conditions, an example would be; if the tilt of the x sxis is greater than 300 pulse high on pin 1 and 2 (left and right motor) to go forward. So here is the problem, when I use the conditional if statements I can only get two movements out of the R/C toy. When I try to add any more than that I lose control and all I can do is turn left or right. I think the problem is coming from the else if statement I used after the if statement to stop the movement when the condition is no longer met but I can't seem to wrap my head around where my logic is wrong. below is the segment of code that drives the toy as it is within my code if you see what I can't let me know because it seems like such an easy problem to fix I just can't figure out what's causing it. Also as a side note when I try to use the elseif statement I get an error that says "expected variable or expression" even though there is one there can anyone explain this, I should also add most of my programming background is in JAVA so that could be an issue depending on how the elseif works in the spin editor.

if yaxis > 300
forward
elseif yaxis < 300
FSTOP
if yaxis < -300
reverse
elseif yaxis > -300
RSTOP
if xaxis > 300
right
elseif xaxis < 300
rightStop
if xaxis < -300
left
elseif xaxis > -300
leftStop


PUB forward
outa := 1
outa := 1
PUB reverse
outa := 1
outa[noparse][[/noparse]6] := 1
PUB left
outa := 1
outa := 1
PUB right
outa := 1
outa[noparse][[/noparse]6] := 1
PUB FSTOP
outa := 0
outa := 0
PUB RSTOP
outa := 0
outa[noparse][[/noparse]6] := 0
PUB rightStop
outa := 0
outa[noparse][[/noparse]6] := 0
PUB leftStop
outa := 0
outa := 0

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-02 00:31
    You'll have to resubmit your program with the proper tags. Use [noparse][[/noparse] code ] and [noparse][[/noparse] /code ] without the extra spaces to set off the code and to maintain the spacing and indenting. Also note that the message formatter strips out numbers in square brackets like [noparse][[/noparse] nnn ] with nnn = 1,2,3, and possibly others. The solution for that is to edit the message adding spaces after the opening bracket.
  • danieldaniel Posts: 231
    edited 2007-12-02 00:35
    I see 2 things I'd take a closer look at.

    If the value is exactly 300 or -300, nothing happens.

    I suspect that you need to use a better set of boundary tests: essentially each axis seems to have 4 conditions of interest. Use a case statement or different tests in the if statements. Something like:

    [b]if[/b] x > 300 then
        doSomething1
    [b]elseif[/b] x > 0 then
        doSomething2
    [b]elseif[/b] x > -300 then
        doSomething3
    [b]else[/b]
        doSomething4
    
    
    




    But, perhaps I misunderstood what you're asking.

    Daniel

    edit: code fragment not meant to be PropSpin.
    edit2: reformatted using PhiPi's formatter.

    Post Edited (daniel) : 12/2/2007 12:42:09 AM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-02 00:36
    You can also use the code formatter at www.phipi.com/format, which will take care of the brackets for you.

    -Phil
  • hippyhippy Posts: 1,981
    edited 2007-12-02 03:13
    A reply with quotes seems to show up the intended indentation, so ...

    if yaxis > 300
      forward
    elseif yaxis < 300
      FSTOP
    
    
    



    That's pretty much an if-then-else rather than an operation with hysterisis. You probably want to alter the elseif value to something positive but lower ( eg, 150 ). Likewise with the other threeselectors.
  • agrossagross Posts: 5
    edited 2007-12-02 03:30
    The 300 is a constant and the xaxis and yaxis is a variable, if I try to use elseif conditions I never seem to get past the first elseif statement meaning, I can go forward but not back, left or right it just stops checking the other conditions. below is the formatted code sorry about the first post.


          [b]if[/b] yaxis > 300
                forward
             [b]elseif[/b] yaxis < 300
                 FSTOP
             [b]if[/b] yaxis < -300
                 reverse
             [b]elseif[/b] yaxis > -300
                 RSTOP    
             'if xaxis > 300
                'right
             'elseif xaxis < 300
                'rightStop
             'if xaxis < -300
                'left
             'elseif xaxis > -300
                'leftStop
    
    [b]PUB[/b] forward
            [b]outa[/b][noparse][[/noparse]3&#093; := 1
            [b]outa[/b][noparse][[/noparse]4&#093; := 1
    [b]PUB[/b] reverse
            [b]outa[/b][noparse][[/noparse]5&#093; := 1
            [b]outa[/b][noparse][[/noparse]6&#093; := 1 
    [b]PUB[/b] left
            [b]outa[/b][noparse][[/noparse]5&#093; := 1
            [b]outa[/b][noparse][[/noparse]4&#093; := 1
    [b]PUB[/b] right
            [b]outa[/b][noparse][[/noparse]3&#093; := 1
            [b]outa[/b][noparse][[/noparse]6&#093; := 1
    [b]PUB[/b] FSTOP
            [b]outa[/b][noparse][[/noparse]3&#093; := 0
            [b]outa[/b][noparse][[/noparse]4&#093; := 0
    [b]PUB[/b] RSTOP
            [b]outa[/b][noparse][[/noparse]5&#093; := 0
            [b]outa[/b][noparse][[/noparse]6&#093; := 0
    [b]PUB[/b] rightStop
            [b]outa[/b][noparse][[/noparse]3&#093; := 0
            [b]outa[/b][noparse][[/noparse]6&#093; := 0
    [b]PUB[/b] leftStop
            [b]outa[/b][noparse][[/noparse]5&#093; := 0
            [b]outa[/b][noparse][[/noparse]4&#093; := 0
    
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-02 03:41
    I know that the ELSEIF is supposed to have the same indent as the IF that starts the statement. I would expect that the compiler would treat your ELSEIF as an error since it's indented relative to the IF. If the first IF fails, all of the following statements indented to the right of the IF will be ignored.
  • agrossagross Posts: 5
    edited 2007-12-02 18:32
    Thanks for all the help so far, I've gotten the elsif statements to work but I am having a problem with my turn left and right subroutines conflicting with my forward and reverse subroutines. Because this is tank style steering to turn I just have to run one motor forward and the the other in reverse but my stop subroutines are holding up the show. So when I want to go forward the rightStop routine holds my left motor from doing anything while the left motor kicks in and drives the thing in a circle. Is there any other way to get these routines to play nice?
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-12-02 21:28
    why not use the case syntax?
  • agrossagross Posts: 5
    edited 2007-12-02 22:25
    I COULD KISS YOU!!! i thought originally that the case condition wouldn't work but I figured I had nothing to lose by going back and re-reading the syntax material and it works PERFECTLY!!! thanks for the epiphany!!
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-12-03 05:27
    deep curtsey: 'Well shucks Ma'am"
Sign In or Register to comment.