Shop OBEX P1 Docs P2 Docs Learn Events
Help with addressing VAR — Parallax Forums

Help with addressing VAR

samsearchersamsearcher Posts: 13
edited 2007-04-12 20:41 in Propeller 1
I have read about addressing in the prop manual but it's not clear to me just
how to do it. I would like to pass a value from a child object back to the parent

example
The motor object controls the motor with input from the motorminder object
which is started in a new cog by the motor object. What I don't understand
is where to use the @ symbol

"note: this is not complete code, just·would like to know if addressing is correct"


Motor object

OBJ
· Hall : "Motor_Minder"

VAR· Long RevPeriod, HallCount
PUB StartFWD(spInputCMPS, distance) : Success
·· Stop
·· Success := (Cog := cognew (FWD(spInputCMPS, distance), @MotorStack)+1)

PUB FWD(spInputCMPS, distance)

· Hall.start( 0, @RevPeriod, @HallCount) 'this is the part of what·don't understand, should I use the @ symbol here or not
················································· ··· 'I'm trying to get the Motor_Minder object to write a value back to
················································ ···· 'VAR Long RevPeriod, HallCount··in the ·motor object

·{more code here,
·· FWD object uses values in VAR Long RevPeriod and HallCount
·· that are changed by the motor_mider object that is running in another cog.
·· I think this is ok because if I understand correctly only one cog at a time can read or
·· write a long in main ram·}

*****************************************************************************
Motor_Mider object
"note: this is not complete code, just·would like to know if addressing is correct"

PUB Start(encoderPin, addrPer, addrRevs) : result
· '************************
· 'Note:
· 'Not sure if I should change addrPer and addrRevs to
··[url=mailto:'@RevPeriod]'@RevPeriod[/url] and @ HallCount
· '************************
· stop································································································· '**********************
· cog := cognew(mindMotor(encoderPin, addrPer, addrRevs), @MindStack) + 1··'should addrPer, addrRevs be changed to @RevPeriod, @HallCount
· result := cog······················································································· '**********************
························································································ '************************
PUB mindMotor(encoderPin, adrPW, adrRevs) |· period,· oldCount· ' should adrPW and adrRevs be changed to @RevPeriod, @HallCount
{{···················································································· '*************************
·· measures period of motor in counter A
·· counts revolutions in counter B
·· updates period and rev counter variables
}}
· oldcount := 0 ' initialize oldcount
· dira[noparse][[/noparse]encoderPin]~
· 'start revolution counter in counter B
· frqb := 1
· ctrb := 0···· ' stop counter
· phsb := 0···· ' zero counter
· ctrb := (%01010 << 26 ) | (encoderPin)···· ' count positive going edges in counter B
· repeat·····
· while oldcount == phsb···················· ' sync to start of revolutin
· oldcount := 0
· repeat
·
··· frqa :=· 1
··································· '**************
··· long[noparse][[/noparse]adrRevs] := phsb···' Should this be changed to long[noparse][[/noparse]@RevPeriod]
··································· '**************

··· oldcount := phsb
··· ctra := (%11111 << 26 )················· ' count always in counter A
··· phsa := 0······························· ' zero count
·················
··· repeat
·····
··· while oldcount == phsb·················· ' count during revolution
······
··· ctra := (%00000 << 26 )··· ' stop counting
··· period := ( phsa· / (clkfreq / 1_000_000) + 1) / 1000

······································

··································· '*****************
··· long[noparse][[/noparse]adrPW] := period··'Should this be changed to long[noparse][[/noparse]@HallCount]
··································· '*****************
··· oldcount := phsb

I have tried variations of the above but at this point I'm totally confused.
I have thought about combining the two objects to make the addressing easier
but would like to keep them seperated if possible and would like to clear this up
in my mind!
I have looked for·examples but have only been able to find examples that
pass a value from the parent object·to the child object·not from the child back to the parent
thanks all

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-04-12 20:22
    At first glance it appears that you are declaring the pointers correctly, you do not declare an argument to a function·as a pointer via the @ symbol. So when you write PUB function(a, b, c), you do not include the @ symbol, you know which are pointers and which are values. Once you create a pointer by using the @ symbol, it stays a pointer, you don't need to use the @ symbol abain when passing it as an argument (if you did you would be creating a pointer to a pointer which is not what you want).

    So in a nutshell, the only time you use an @ symbol is when you create the pointer, not when you use it. So Hall.start function call is the only place where the @ symbol should appear in the code you posted.

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

    Parallax, Inc.
  • samsearchersamsearcher Posts: 13
    edited 2007-04-12 20:41
    Paul thanks for the reply. It makes sense now.
Sign In or Register to comment.