Help with addressing VAR
samsearcher
Posts: 13
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
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
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.