Shop OBEX P1 Docs P2 Docs Learn Events
Help with Accelermoter/Servo Control — Parallax Forums

Help with Accelermoter/Servo Control

RubrChickenRubrChicken Posts: 39
edited 2009-02-16 00:56 in Propeller 1
I'm having great fun (and success) in the Propeller world, OBEX is my best friend! Anyway, I'm trying to have proportional servo control based on the accelerometer. I get a value called size that goes from 0 (horizontal) to 45 (vertical) and a value called deg which goes 0-360. I used Servo32v3 for servo control. My servo has 1000 as the bottom and 2000 as the top, I've tested this elsewhere. I used a method called ChooseE to choose the direction the servo will move, 1 is down (1500-1000) and 2 is up (1500-2000). I have the up side working, but the down part doesn't! I have an update accel method, and it is straight from the example that came with it.

My code:
 repeat
       waitcnt(cnt + 1_000_000)           'To make sure you can read the TV
       !outa[noparse][[/noparse]23]                          'If you don't have a TV this says everythings working fine.


        term.dec(deg)
        term.str(string("            "))       'These four commands format and output text to the TV
        term.dec(size)
        term.str(string(13))


        elevator := chooseE
        if elevator == 1
           outa[noparse][[/noparse]17] := 1
           SERVO.Set(5,2000)


        if elevator == 2
           outa[noparse][[/noparse]18] := 1
            SERVO.Set(5,(size*500)/45+1500)


        else
            outa[noparse][[/noparse]17] := 0
             outa[noparse][[/noparse]18] := 0
            SERVO.Set(5,1500)                 




Update the accelerometer (this is run in its own cog)


PUB UpdateAccel | raw, mg, G_scale, temp
            G_scale := 2000/(256/3)
  repeat
          raw := acc.ro                                       'Get raw value for acceleration
          raw := raw / clk_scale                              'the magnitude, really
          size := raw / G_scale                                   'scale mg value for screen display              '
          temp := acc.theta >> 19                              'scale 32-bit value to a 13-bit value
          deg := temp*360/8192
          if cnt // 3000000 ==  0
             dira[noparse][[/noparse]18]~~
             !outa[noparse][[/noparse]dira]
                                                               




ChooseE
pub ChooseE

        if deg > 90
            if deg < 270
                 term.str(string("down"))
                 return 1
            else
        else
        if deg < 90
             term.str(string("up"))
             return 2
        if deg > 270
                term.str(string("up"))
                return 2
        else
            return 3           






Thanks!!

Comments

  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-02-15 21:59
    My guess from an initial glance at the code is that you're missing an ELSE statement after the first IF. Assuming the value of elevator == 1, your code as written says:

    if elevator == 1
      'do some stuff. (this stuff will happen)
    
    

    Then the code does this:
    if elevator == 2
      '(it's not, it's 1, so this stuff is skipped)
    ELSE
      'do different stuff.
    
    

    The 'different stuff' will happen, because elevator is 1, not 2, and this erases the result from the very first IF.

    You can change your code to use explicit IFs for all three cases, OR use ELSEIF, like this:
    if elevator==1
      '(stuff 1)
    elseif elevator == 2
      '(stuff 2)
    else
      '(stuff 3)
    
    

    Jason

    Post Edited (JasonDorie) : 2/15/2009 10:04:34 PM GMT
  • RubrChickenRubrChicken Posts: 39
    edited 2009-02-15 22:09
    Oh... I get it! I'm used to java and c++ where you can delaminate different parts of the statement with {}. I was assuming the if statement would end if i had something on the same tab level as the original statement, but I guess not. I'll see if that works on my setup. Thanks again!
  • RubrChickenRubrChicken Posts: 39
    edited 2009-02-15 22:20
    Perfect! That's great! Your poetry is wonderifisiful.
  • Bill DrummondBill Drummond Posts: 54
    edited 2009-02-16 00:56
    would this work
    PUB ChoosE
      case degree
       90..270:
         term.str(string("down"))
         return 1
       0..89 :
        term.str(string("up"))
         return 2
       271..360 :
         term.str(string("up"))
          return 2
       return 3
    
Sign In or Register to comment.