Shop OBEX P1 Docs P2 Docs Learn Events
Variable overwriting another variable — Parallax Forums

Variable overwriting another variable

McadieuxMcadieux Posts: 5
edited 2009-02-24 20:37 in Propeller 1
I'm new to spin so hopefully this isn't something really stupid I am doing wrong. The problem I am having is when I write to one of my variables it is actually overwriting a different variable.

This is my variable definition block:

VAR
  
  long centerPos, servoPos, Dot
  long temporary, newPos, degMax, degMin
  
  byte Cog





This is where the temp variable is used:
PUB setDeg(servo, deg) 
  temporary := (controller.getHighLimit(servo) - controller.getLowLimit(servo)) / DOT[noparse][[/noparse]servo]
  newPos := (deg * temporary) + centerPos[noparse][[/noparse]servo]
  servoPos[noparse][[/noparse]servo] := newPos
  controller.Set(servo, servoPos[noparse][[/noparse]servo]) 




The problem is instead of the number being assigned to temp it is sent to DOT. This has me stumped.

Comments

  • Ken PetersonKen Peterson Posts: 806
    edited 2009-02-24 00:53
    Why are you indexing a variable that is not defined as an array?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • rokickirokicki Posts: 1,000
    edited 2009-02-24 00:53
    Your "servoPos" is an array of one element, indexed starting at 0.

    My guess is your server argument is set to *1*, not *0*.

    Always, for cases like this, please attach full code.
  • McadieuxMcadieux Posts: 5
    edited 2009-02-24 01:11
    The brackets don't seem to show up my variable block is actually:


    VAR

    long centerPos[noparse][[/noparse]10], servoPos[noparse][[/noparse]10], Dot[noparse][[/noparse]10]
    long temporary, newPos, degMax, degMin

    byte Cog 'Holds the cog that the controller was launched into
  • GeorgeCollinsGeorgeCollins Posts: 132
    edited 2009-02-24 04:04
    I think I would need to see more code to know for sure, but a guess would be that you are finding that the value you expect to be in temporary is also in Dot[noparse][[/noparse]10], which == temporary

    That may not be what is going on, but I think we need to see more code to know for sure.
  • McadieuxMcadieux Posts: 5
    edited 2009-02-24 05:33
    Ok here is the complete code, it's kind of messy. Its probably not the best most optimized way of doing things but im trying to get used to spin. The variable called cheapfix was my way of fixing the code for now. Instead of overwriting my last DOT variable it overwrites cheapfix. The reason temp is global is so that I could use viewpoint to view it since the code was acting funny. Thanks for the help so far.

    {{ Output.spin }}
    VAR
      
      long centerPos[noparse][[/noparse]10], servoPos[noparse][[/noparse]10], Dot[noparse][[/noparse]10]
      long cheapfix, temp, newPos[noparse][[/noparse]10], degMax, degMin
      long reverseStatus[noparse][[/noparse]10]
      
      byte Cog 'Holds the cog that the controller was launched into
    
    OBJ
      controller : "MyServo32v3"
         
    
    
    PUB setup(pinStart, pinEnd) | pinrange
    {{Sets all pins between start and end to output and sets initial position to servo center.
    It also sets default end point limits to avoid servo damage.
    This must be called before servo START}}
    
      repeat pinrange from pinStart to pinEnd 'Preset ALL servo's to center position
        controller.Set(pinrange,1000)
        controller.setHighLimit(pinrange, 2250)
        controller.setLowLimit(pinrange, 225)
     
    
    PUB start
    {{Starts the Controller must be called AFTER setup and before all other commands}}
      controller.Start
    
      
    PUB setCenter(servo, pos)
      {{Set the center position of each servo. This becomes 0 Deg's}}
      centerPos[noparse][[/noparse]servo] := pos
      servoPos[noparse][[/noparse]servo] := pos
      controller.Set(servo, centerPos[noparse][[/noparse]servo])
    
    PUB center(servo)
      servoPos[noparse][[/noparse]servo] := centerPos[noparse][[/noparse]servo]
      controller.Set(servo, centerPos[noparse][[/noparse]servo])
      
    PUB setDeg(servo, deg) 
     {{Sets the servo using degrees must be between degMax and degMin}}
      temp := (controller.getHighLimit(servo) - controller.getLowLimit(servo))/ DOT[noparse][[/noparse]servo] 'Calculate how many degrees per ms
      newPos[noparse][[/noparse]servo] := (deg * temp) + centerPos[noparse][[/noparse]servo]
      servoPos[noparse][[/noparse]servo] := newPos[noparse][[/noparse]servo]
      controller.Set(servo, servoPos[noparse][[/noparse]servo]) '
     
    
    PUB setDot(servo, deg)
    {{This tells the controller how many degrees of travel the servo is able to move. This must be adjusted
    if the end point limits are changed}}
      Dot[noparse][[/noparse]servo] := deg  
    
    PUB getPos(servo) : position
    {{return the current position of the servo}}
      position := servoPos[noparse][[/noparse]servo]
    
    
    
    PUB setPos(servo, pos)
    {{Set the position of the servo to a specific point. The controller will still stay between the endpoints}}
      servoPos[noparse][[/noparse]servo] := pos
      controller.Set(servo, servoPos[noparse][[/noparse]servo]) '
    
    
    
    PUB increasePos(servo, val) | tem
    {{Increase the servo position by a specific ms value}}
      tem := servoPos[noparse][[/noparse]servo] + val
      servoPos[noparse][[/noparse]servo] := tem
      controller.Set(servo, servoPos[noparse][[/noparse]servo]) '
    
    
    PUB decreasePos(servo, val) | tem
    {{Decrease the servo position by a specific ms value}}
      tem := servoPos[noparse][[/noparse]servo] - val
      servoPos[noparse][[/noparse]servo] := tem
      controller.Set(servo, servoPos[noparse][[/noparse]servo])
    
    
    PUB setHighLimit(servo, value)
    {{Sets the High END POINT of Servo}}
      controller.setHighLimit(servo, value)
    
    
    PUB setLowLimit(servo, value)
    {{Sets the low end point of servo}}
      controller.setLowLimit(servo, value)   
    
    PUB reverse(servo, status)
      reverseStatus[noparse][[/noparse]servo] := status
    
    
    
    
    
    
    
    

    Post Edited (Mcadieux) : 2/24/2009 5:48:11 AM GMT
  • rokickirokicki Posts: 1,000
    edited 2009-02-24 05:41
    Please, *full* code. It really does help.
  • mparkmpark Posts: 1,305
    edited 2009-02-24 15:22
    Mcadieux,

    If you want help, you have to post your entire program, not just output.spin. The problem might lie in the code that calls the methods in output.spin.
  • McadieuxMcadieux Posts: 5
    edited 2009-02-24 17:40
    Ok, here is all the code, sorry for the delay. My 6 month old was sick last night and wasn't really letting me get anything done. Now it seems to be acting normal, but I haven't changed anything. Here is the code anyway. I know it isn't optimized, but it is more about trying to get the language down. I believe the problem may have been how I was interpreting viewport. I was thinking of an array[noparse][[/noparse]10] as 1 - 10 instead of 0-9.

    {{ Output.spin }}
    VAR
      
      long centerPos[noparse][[/noparse]10], servoPos[noparse][[/noparse]10], Dot[noparse][[/noparse]10]
      long cheapfix, temp, newPos[noparse][[/noparse]10], degMax, degMin
      long reverseStatus[noparse][[/noparse]10]
      
      byte Cog 'Holds the cog that the controller was launched into
    
    OBJ
      controller : "MyServo32v3"
      vp:"Conduit"    
    
    
    PUB setup(pinStart, pinEnd) | pinrange
    {{Sets all pins between start and end to output and sets initial position to servo center.
    It also sets default end point limits to avoid servo damage.
    This must be called before servo START}}
      vp.share(@Dot, @newPos)
    
      repeat pinrange from pinStart to pinEnd 'Preset ALL servo's to center position
        controller.Set(pinrange,1000)
        controller.setHighLimit(pinrange, 2250)
        controller.setLowLimit(pinrange, 225)
     
    
    PUB start
    {{Starts the Controller must be called AFTER setup and before all other commands}}
      controller.Start
    
      
    PUB setCenter(servo, pos)
      {{Set the center position of each servo. This becomes 0 Deg's}}
      centerPos[noparse][[/noparse]servo] := pos
      servoPos[noparse][[/noparse]servo] := pos
      controller.Set(servo, centerPos[noparse][[/noparse]servo])
    
    PUB center(servo)
      servoPos[noparse][[/noparse]servo] := centerPos[noparse][[/noparse]servo]
      controller.Set(servo, centerPos[noparse][[/noparse]servo])
      
    PUB setDeg(servo, deg) 
     {{Sets the servo using degrees must be between degMax and degMin}}
      temp := (controller.getHighLimit(servo) - controller.getLowLimit(servo))/ DOT[noparse][[/noparse]servo] 'Calculate how many degrees per ms
    
      if reverseStatus[noparse][[/noparse]servo] == 0
        newPos[noparse][[/noparse]servo] := centerPos[noparse][[/noparse]servo] +(deg * temp)
      if reverseStatus[noparse][[/noparse]servo] == 1
        newPos[noparse][[/noparse]servo] := centerPos[noparse][[/noparse]servo] - (deg * temp)
    
      servoPos[noparse][[/noparse]servo] := newPos[noparse][[/noparse]servo]
      controller.Set(servo, servoPos[noparse][[/noparse]servo]) '
     
    
    PUB setDot(servo, deg)
    {{This tells the controller how many degrees of travel the servo is able to move. This must be adjusted
    if the end point limits are changed}}
      Dot[noparse][[/noparse]servo] := deg  
    
    PUB getPos(servo) : position
    {{return the current position of the servo}}
      position := servoPos[noparse][[/noparse]servo]
    
    
    
    PUB setPos(servo, pos)
    {{Set the position of the servo to a specific point. The controller will still stay between the endpoints}}
      servoPos[noparse][[/noparse]servo] := pos
      controller.Set(servo, servoPos[noparse][[/noparse]servo]) '
    
    
    
    PUB increasePos(servo, val) | tem
    {{Increase the servo position by a specific ms value}}
      tem := servoPos[noparse][[/noparse]servo] + val
      servoPos[noparse][[/noparse]servo] := tem
      controller.Set(servo, servoPos[noparse][[/noparse]servo]) '
    
    
    PUB decreasePos(servo, val) | tem
    {{Decrease the servo position by a specific ms value}}
      tem := servoPos[noparse][[/noparse]servo] - val
      servoPos[noparse][[/noparse]servo] := tem
      controller.Set(servo, servoPos[noparse][[/noparse]servo])
    
    
    PUB setHighLimit(servo, value)
    {{Sets the High END POINT of Servo}}
      controller.setHighLimit(servo, value)
    
    
    PUB setLowLimit(servo, value)
    {{Sets the low end point of servo}}
      controller.setLowLimit(servo, value)   
    
    PUB setReverse(servo, status)
      reverseStatus[noparse][[/noparse]servo] := status
    
    






    {{main}}
    VAR
      long  stack[noparse][[/noparse]10]
      long temp
    CON
                      
      _xinfreq = 5_000_000                      
      _clkmode = xtal1 + pll16x
    
    
    OBJ
    
      controller : "ServoController0.6"
    
      
    
    PUB Main
      controller.setup(1,1)
      controller.start
      controller.setCenter(1,1000)
      controller.setDOT(1, 160)
    
      
      repeat
        controller.setCenter(1,1000)
        waitcnt(clkfreq * 3 + cnt)
        controller.setDeg(1,45)
        waitcnt(clkfreq * 3 + cnt)
        controller.setCenter(1,1000)
        waitcnt(clkfreq * 3 + cnt)
        controller.setDeg(1,-45)
        waitcnt(clkfreq * 3 + cnt)
    
    
    




    The MyServo32v3 object is just your standard Servo32v3 object. I deleted cheapfix temporary and made temp back to a local variable and it seems to work fine now. I may have just been doing something stupid last night. Thanks for trying to help, sorry I was brain dead about posting code last night.
  • McadieuxMcadieux Posts: 5
    edited 2009-02-24 20:26
    Ok, I figured out what I did. It wasn't temp overwriting DOT it was DOT overwriting temp. I was writing past the end of the array. Considering I know C++ this was a dumb mistake. Thanks for trying to help me.
  • Ken PetersonKen Peterson Posts: 806
    edited 2009-02-24 20:37
    I was going to suggest that at first, as you always have to be conscious of that with indexed variables.· But since you said temporary was overwritten instead of Dot, that wouldn't have explained it.

    Glad you figured it out.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
Sign In or Register to comment.