Shop OBEX P1 Docs P2 Docs Learn Events
Inter cog communication, passing variables — Parallax Forums

Inter cog communication, passing variables

DizzyDizzy Posts: 9
edited 2008-03-09 21:35 in Propeller 1
Hello I've been feverishly trying to get variables to pass between objects in SPIN. What I have is an XBee object I've modified constantly polling for input packets on one cog and a dashboard cog that should be passed those packets. From the examples I've seen it seems like parent objects can modify variables in child objects instead of the other way around? Either way this is what I'm attempting in a nutshell:


Inside Dashboard Object
net.start(6,5,0,9600,@byte1,@byte2) <-This starts the Xbee module on pins 5&&6 @baud of 9600 passing the two packets I want the Xbee to update byte1 and byte2



---Inside Xbee object
byte packet

Pub Start (RXpin, TXPin, Mode, Baud, inByte1, inByte2)

repeat
    RxStrTime(5,@packet)
    inByte1 := packet[noparse][[/noparse]0]
    inByte2 := packet





I've also tried BYTE[noparse][[/noparse]@inByte1] := packet[noparse][[/noparse]0] and BYTE[noparse][[/noparse]inByte1] := packet[noparse][[/noparse]0]




For some reason in the text above the forum is deleting the brackets off of some of my stuff. packet is byte array of 3. the second line where inByte2 := packet is missing brackets. Please disregard syntax.
What is the correct syntax to get the Xbee object to be able to change the variables passed in from teh dashboard object?

Thanks,

Diz

Post Edited (Dizzy) : 3/8/2008 5:58:39 PM GMT

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2008-03-08 18:09
    You should have had 1 hit!

    Look:
    As C SPIN uses call-by-value (-> look-up what that means)
    So the "value" of your "dummy" parameters is a pointer to byte1 and to byte2.

    Assigning to "dummy" parameters just changes them locally; this is done by many programmers, for different reasons;
    generally to save a local variable or an assignment... This has no effect outside the routine

    @dummyParameter is just a pointer to that dummy parameter - not of much use in your case


    BYTE[noparse][[/noparse]inByte1] is the method of choice! It is called "de-referentiation" and the most important way to access to vectors from sub-routines
    or over object borders. Referencing and Derefenecing are most important techniques in all machine language like applications.
    There is no ambitious piece of code working without it, and understanding ALL OF ITS ASPECTS is needed to make the step
    from the VERY BEGINNER to the ADVANCED BEGINNER smile.gif
  • DizzyDizzy Posts: 9
    edited 2008-03-08 18:11
    Yeah I know what pointers are. You're saying BYTE[noparse][[/noparse]inByte1] := packet[noparse][[/noparse]0] should in effect change the value in the dashboard object?
  • hippyhippy Posts: 1,981
    edited 2008-03-08 18:18
    In your Dashboard object you pass the address of byte1 and byte2 to the XBee Start method, so within that method inByte1 and inByte2 point to the variables you ultimately want to use ( byte1 and byte2 ). Thus "BYTE[noparse][[/noparse] inByte1 ] := packet[noparse][[/noparse] 0 ]" should assign the value in packet[noparse][[/noparse] 0 ] to the variable pointed to by inByte1 which would be byte1.

    Have you defined byte1 as BYTE ? You may need to use WORD[noparse][[/noparse] ] or LONG[noparse][[/noparse] ] otherwise ?

    You haven't actually explained what isn't working with your code. Are you sure that RxStrTime() is even returning ? Have you tried trimming your code back to find what does work and doesn't ? You can try removing the call to RxStrTime(), force known values intp packet[noparse]/noparse and check they get passed back. For a simple observer who doesn't know what you code is it's only possible to make stabs in the dark. Your mechanism for passing addresses looks okay so the problem is likely somewhere else.
  • DizzyDizzy Posts: 9
    edited 2008-03-08 18:21
    Yep, problem is I run the same functions in the XBEe, outputting anything received to the LED's on my protoboard and that works. The second I try to decouple and let the two objects work together I get no response. Thanks for your posts now I know I at least have the correct syntax and can start barking up a different tree in my code
  • Chuck RiceChuck Rice Posts: 210
    edited 2008-03-08 19:07
    I struggled with this too. What I Finally got to work was:

    ===== Main Object =====

    var
      byte map [noparse][[/noparse] Scanner#cMaxRows+1 ]
    
    pub main
      rc := scanner.start(cServoPin,cSonarTrigger,cSonarData,@map)
    
    pub showScanMap | j,n,c
          repeat j from 0 to Scanner#cMaxRows
          x = map[noparse][[/noparse]j]                                        ' use the value 
    
    




    ===== Scanner Object =====

    con
      cMaxRows                      = 20
    
    var
      long  map
    
    pub start(theServoPin,theSonarTrigger,theSonarData,theMap) | rc
      map          := themap
      scanCog := cognew(doPan,@Stack)
    
    pri doPan |  pulse, row, distance
      repeat row from 0 to cMaxRows
        distance := doPing    
        byte[noparse][[/noparse]map][noparse][[/noparse]row] := distance
    
    



    The important thing is that the pointer that gets passed in is really an
    address, so I reserve a long variable called map that I use in the
    scanner object to reference the byte using

    byte [noparse][[/noparse] map ][noparse][[/noparse] row ] := distance

    In my case I am using an array of bytes, so I have the second reference of [noparse][[/noparse] row ]. -Chuck
  • RinksCustomsRinksCustoms Posts: 531
    edited 2008-03-09 18:07
    Chuck Rice said...
    I struggled with this too. What I Finally got to work was:

    ===== Main Object =====

    var
      byte map [noparse][[/noparse] Scanner#cMaxRows+1 ]
    
    pub main
      rc := scanner.start(cServoPin,cSonarTrigger,cSonarData,@map)
    
    pub showScanMap | j,n,c
          repeat j from 0 to Scanner#cMaxRows
          x = map[noparse][[/noparse]j]                                        ' use the value 
    
    




    ===== Scanner Object =====

    con
      cMaxRows                      = 20
    
    var
      long  map
    
    pub start(theServoPin,theSonarTrigger,theSonarData,theMap) | rc
      map          := themap
      scanCog := cognew(doPan,@Stack)
    
    pri doPan |  pulse, row, distance
      repeat row from 0 to cMaxRows
        distance := doPing    
        byte[noparse][[/noparse]map][noparse][[/noparse]row] := distance
    
    



    The important thing is that the pointer that gets passed in is really an
    address, so I reserve a long variable called map that I use in the
    scanner object to reference the byte using

    byte [noparse][[/noparse] map ][noparse][[/noparse] row ] := distance

    In my case I am using an array of bytes, so I have the second reference of [noparse][[/noparse] row ]. -Chuck
    So let me see if I understand this correctly.· Your calling doPing to get distance from the ping sensor, then store the low word of the long distance into [noparse][[/noparse]row][noparse][[/noparse]map] respecively (LSB-MSB) as two bytes. Was there bit shifting going on before the doPing passes the distance long back to the calling method where it then has the upper word turnicated and stores the lower word as two consecutive bytes in the form of (LSB-MSB) [noparse][[/noparse]row][noparse][[/noparse]map]· respectively?

    If so, that would be a very efficient way to pass multiple word or byte "packets" back to the calling method. I am propably wrong in my thinking, but that's how it looks to me anyway.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    E3 = Thought

    http://folding.stanford.edu/·- Donating some CPU/GPU downtime just might lead to a cure for cancer! My team stats.
  • deSilvadeSilva Posts: 2,967
    edited 2008-03-09 18:20
    No.
    This
    byte[noparse][[/noparse]map][noparse][[/noparse]row] := distance
    


    copies EIGHT BITS to the byte at address "map+row" - and nothing else!
  • Chuck RiceChuck Rice Posts: 210
    edited 2008-03-09 21:35
    RinksCustoms said...
    So let me see if I understand this correctly. Your calling doPing to get distance from the ping sensor, then store the low word of the long distance into [noparse][[/noparse]row][noparse][[/noparse]map] respecively (LSB-MSB) as two bytes. Was there bit shifting going on before the doPing passes the distance long back to the calling method where it then has the upper word turnicated and stores the lower word as two consecutive bytes in the form of (LSB-MSB) [noparse][[/noparse]row][noparse][[/noparse]map] respectively?

    If so, that would be a very efficient way to pass multiple word or byte "packets" back to the calling method. I am propably wrong in my thinking, but that's how it looks to me anyway.

    no, if you wanted to pass back two bytes you would need to use


    word[noparse][[/noparse]map][noparse][[/noparse]row] := distance
    



    And define map in the main as a Word Array. I guess it is confusing that I used map in both objects. The scanner object should have defined that map variable as mapPointer. This pointer could have been a word I think, but a long is ok. I only pass back 20 bytes since my sonar only returns a number from 6 to 200. So 20 readings fit into 20 bytes. -Chuck-

    .
Sign In or Register to comment.