Shop OBEX P1 Docs P2 Docs Learn Events
Need a recommendation — Parallax Forums

Need a recommendation

CannibalRoboticsCannibalRobotics Posts: 535
edited 2008-11-04 19:45 in Propeller 1
I have been unable to get my head wrapped around where a variable lives in the prop and how it is most efficiently referenced/modified from multiple cogs. Can someone recommend a good tech note on this aspect of the architecture/language.

Thanks,
Jim-

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-10-03 17:39
    Hi Jim,
    You need to be a little more specific with your request, are you talking about intraobject or interobject, are you talking about assembly to assembly, assembly to spin, or spin to spin?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-10-03 18:12
    Hello Jim.

    as long as all cogs are started by cognew from the SAME *.SPIN-File
    you can access them simply by the name of the variable
    here is a demo which shows that

    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      
    VAR
      long heart_stack[noparse][[/noparse] 20]
      long CogStack1[noparse][[/noparse] 20]
      long CogStack2[noparse][[/noparse] 20]
      
      long MyTestVar
    
    OBJ
      'heart : "heartbeat" little object from the obex making an LED blink so i can see Prop is working
      debug : "FullDuplexSerial"
    
    
    PUB Main
    'the FIRST PUB-Method inside a spinfile is ALWAYS the startpoint where the program starts to run
      'heart.Start(27, 200)
      debug.start(31, 30, 0, 9600)
      
      MyTestVar := 100
      debug.str(string("Start MyTestVar="))
      debug.dec(MyTestVar)
      debug.Tx(13)
    
      cognew(M1,@CogStack1)
      cognew(M2,@CogStack2)
      
      repeat
        waitcnt(clkfreq + cnt)
        debug.str(string("MyTestVar="))
        debug.dec(MyTestVar)
        debug.Tx(13)
    
    
    
    PUB M1
      repeat
        waitcnt(ClkFreq * 3 + cnt)
        MyTestVar := 1
    
    
    PUB M2
      repeat
        waitcnt(ClkFreq * 5 + cnt)
        MyTestVar := 2
    
    



    if you want to access variables ACROSS different objects
    define the variables in your top-object-file
    and pass the ADRESS of the variables by using the "@"-operator (the adress-operator

    VAR
      long MyVar1
      long Pointer_to_MyVar1
    
      long MyVar2
      long Pointer_to_MyVar2
    
    OBJ
      MyObj1 : "MyObject"
    
    PUB Main
      Pointer_to_MyVar1 := @MyVar1
      Pointer_to_MyVar2 := @MyVar2
      'then give the value of the pointer to the other object as a parameter 
      'of some kind of setup-method to the other object
      MyObj1.Start.....
      MyObj1.SetupPointers(Pointer_to_MyVar1,  Pointer_to_MyVar2)
    
    
    



    now INSIDE the file MyObject.spin

    there have to be variables for the pointervalues
    VAR
      long Ptr1
      long Ptr2
    
    PUB SetupPointers(p_Pointer1,  p_Pointer2)
      Ptr1 := p_Pointer1 'through this setup you can use the pointers in every PUB or PRI-method inside THIS object
      Ptr2 := p_Pointer2
    
    'and then you can access the variables inside THIS object by
    
      long[noparse][[/noparse]Ptr1] := ....
      long[noparse][[/noparse]Ptr2] := ....
    
    
    



    best regards

    Stefan
  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-04 19:45
    heathclf,
    What you've posted seems unnecessarily complicated, like why are you using pointers?

    You've got the simple case of a method (subroutine) that is running in its own cog with everything together in one object. Good!

    Here it is simpler:
    VAR
    Byte stack1[noparse][[/noparse]30], highTime
    
    CON
       maxHigh = 1200*80
       minHigh = 975*80
       totalTime = 20000*80
       servoPin = 0
    
    PUB main
       highTime := maxHigh
       cognew(PulseServo, @stack1)
    
       repeat until highTime < minHigh
          waitcnt(clkfreq + cnt)
          highTime := highTime - 100
    
    PUB PulseServo | myTime
    
    'This assumes that the pulseis going to an inverter (for 5VTTL conversion), and so the timing is reversed
    
       dira[noparse][[/noparse]servoPin]~~ ''Output Pins
       repeat
          myTime := highTime   ' reference it once so, if it's changed between the waitcnt's, the timing will still be correct
          waitcnt(totalTime - myTime + cnt)
          outa[noparse][[/noparse]servoPin]~
          waitcnt(myTime + cnt)
          outa[noparse][[/noparse]servoPin]~~
    

    Post Edited (Mike Green) : 11/4/2008 7:50:32 PM GMT
Sign In or Register to comment.