Shop OBEX P1 Docs P2 Docs Learn Events
unpack array in spin — Parallax Forums

unpack array in spin

stargazer2050stargazer2050 Posts: 89
edited 2012-11-18 07:52 in General Discussion
Need syntax to unpack array in spin

Repeat var from o to 5
Get result

Put them in individual variables, var1, var2 ...

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-11-17 06:48
    This is a duplicate post. Please do not post the same question twice it is against forum rules.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-17 08:05
    I agree with Mike that this is a duplicate question, but I think I'd prefer the other thread to be used for questions about the RC Receiver object.

    This should have been asked in the Propeller forum.

    The answer of course is still the same which Mike gave.
    Mike G wrote: »
    Use an index to access an array element.
      elem1 := myarray[0]
      elem2 := myarray[1]
    

    The answer to this:
    Need syntax to unpack array in spin

    Repeat var from o to 5
    Get result

    Put them in individual variables, var1, var2 ...

    is you can't from within a loop like that. Well you can if you use pointers but then you might as well leave it in an array. To use unique variable names for each element, you need to do it the way Mike demonstrated.

    What I do, is leave it in an array and have meaningful constant names to refer to the various elements.

    For example, the program I'm working on right now, I use:
    CON
      ' channel names
      #0, THROTTLE_CHANNEL, X_CHANNEL, Y_CHANNEL, ROTATE_CHANNEL, AUX_CHANNEL, GEAR_CHANNEL
    

    (The "#0" means assign the first constant listed to zero and increment each constant from there.) Then I access the elements with these constants:
    PUB MainLoop  | localIndex, localX, localY
      repeat
        repeat localIndex from 0 to RX_CHANNELS - 1
          channel[localIndex] := Rc.getrc(localIndex)
          if ||channel[localIndex] =< DEAD_BAND
            channel[localIndex] := 0
          
        rotation := channel[[B]ROTATE_CHANNEL[/B]]
        localX := F.Ffloat(-channel[[B]X_CHANNEL[/B]]) 
        localY := F.Ffloat(channel[[B]Y_CHANNEL[/B]])
        fAngle := F.Atan2(localY, localX)
        localX := F.Fmul(localX, localX) 
        localY := F.Fmul(localY, localY)
        localX := F.Fadd(localX, localY) 
        fMagnitude := F.FSqr(localX)
        
        FComputeWheelSpeeds(fMagnitude, fAngle, rotation)
        
        RampWheels
        ProportionalOnlyPid
        
    
  • stargazer2050stargazer2050 Posts: 89
    edited 2012-11-17 09:21
    Ok, tried this out, am getting
    Need expression term with '0 to rx_channels'

    Instruction or variable with 'channel'

    Then you're using functions of some sort for some reason.

    Well, i tried. If it gets a workin please let me know.

    Thankyou both very very.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-17 09:55
    Ok, tried this out, am getting
    Need expression term with '0 to rx_channels'

    Instruction or variable with 'channel'

    Then you're using functions of some sort for some reason.

    Well, i tried. If it gets a workin please let me know.

    Thankyou both very very.

    This was an example to show why don't really need to unpack an array. I was showing you the way I name things in general. I didn't include enough of the program to compile it.

    This line of code could be thought to "unpack" one of the elements of the array:
    rotation := channel[ROTATE_CHANNEL]
    

    But it isn't really need, since I could just use "channel[ROTATE_CHANNEL]" instead of "rotation" in any of my calculations.

    channel[ROTATE_CHANNEL] is the same as channel[3], but I think it's easier for me to understand the meaning of the channel[ROTATE_CHANNEL] than channel[3] when I look back at the program.
  • stargazer2050stargazer2050 Posts: 89
    edited 2012-11-17 10:08
    you do it the competent programmer's way. here's the fun way
    PUB Start
    ' result is used a temporary variable
      Debug.Start(DEBUG_BAUD)
      
      waitcnt(clkfreq * 3 + cnt)  ' Time to open serial window.
      
      result := Rc.BuildMask(FIRST_RECEIVER_PIN, FIRST_RECEIVER_PIN + 1, FIRST_RECEIVER_PIN + 2, {
              } FIRST_RECEIVER_PIN + 3, FIRST_RECEIVER_PIN + 4, FIRST_RECEIVER_PIN + 5)
      'result := Rc.BuildMask(FIRST_RECEIVER_PIN, FIRST_RECEIVER_PIN + 2, FIRST_RECEIVER_PIN + 4, {
      '        } FIRST_RECEIVER_PIN + 6, FIRST_RECEIVER_PIN + 8, -1)
      Rc.Start(result)
      Debug.Clear
      'Debug.Position(TITLE_X, TITLE_Y)
      'Debug.str(string("RC Receiver Demo"))
     ' repeat result from 0 to Rc#POSSIBLE_CHANNELS - 1
     '   Debug.Position(DISPLAY_X, DISPLAY_Y + result)                                       
     '   Debug.str(string("channel #"))
     '   Debug.dec(result)
     
      repeat
        repeat result from 0 to Rc#POSSIBLE_CHANNELS - 1
          pulseLength[result] := Rc.Get(result)
           if pulseLength[result] <> previousPulseLength[result]
            ' update pulse length display if the value has changed
             'Debug.Position(READING_X, DISPLAY_Y + result)   
             'Debug.dec(pulseLength[result])
            'Debug.str(string(" us    ")) 
             previousPulseLength[result] := pulseLength[result]
               if result == 0
                 rcVar1 := pulseLength[result]
               if result == 1
                 rcVar2 := pulseLength[result]
               if result == 2
                 rcVar3 := pulseLength[result]
               if result == 3
                 rcVar4 := pulseLength[result]
               if result == 4
                 rcVar5 := pulseLength[result]
               if result == 5
                 rcVar6 := pulseLength[result]                       
             Debug.dec(rcVar1)
             Debug.str(string("    "))         
             Debug.dec(rcVar2)
             Debug.str(string("    "))
             Debug.dec(rcVar3)
             Debug.str(string("    "))
             Debug.Clear   
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-17 14:23
    I just don't see the need for "rcVar1" etc.

    Why is "rcVar1" better than "pulseLength[0]"? You could even rename the array "pulseLength" to "rcVar". If you don't like using a zero index add one to the array size and start your numbering at one instead of zero. If you rename and start with one instead of zero then you'd just use "rcVar[1]" instead of "rcVar1". This lets you access each variable with loops and all sorts of other advantages.

    One of the reasons to use an array is so you don't have to do silly inefficient things like having rcVar1, rcVar2, rcVar3, etc. If for some reason you want a second set of data use another array and use "longmove(@rcVar, @pulseLength, NUMBER_OF_LONGS_TO_COPY)".
  • stargazer2050stargazer2050 Posts: 89
    edited 2012-11-18 07:52
    Ah, thats fantastic. I can understand the 2nd one.

    Thanks again!!
Sign In or Register to comment.