Is there a way to make a variable global to the child?
lardom
Posts: 1,659
I changed a value of a variable from 1 to 0 in a loop in the parent object. I passed the address of that variable to the child and created another loop to monitor that variable and watched the result with an LED. The child object could see the initial value but would not respond to changes that were made in the parent unless I used the "@" symbol again. I was looking for a way to let the child know when the parent had finished executing. Is there a way to do this?

Comments
I wrote an object where the Ping acts as a motion sensor. The problem is that a breeze can also trigger a scan. I want a method as reliable as monitoring wheel encoders. I'm using steppers. I want the motors to write values to a "universal" global address that could be seen by the 'servo/Ping child.
I'm stuck on how to update object "two".
obj 'ONE two : "two" var word ref pub main ref := 1 ' two.start(1) ' waitcnt(clkfreq/2+cnt) two.It_Works(@ref) ' two.It_Works(1) repeat ref := 0 ' two.It_Works(@ref) ' waitcnt(clkfreq/2+cnt) ref := 1 ' two.It_Works(ref) ' waitcnt(clkfreq/2+cnt)var 'TWO word refAddr long stack[10] pub start(a) cognew(It_Works(a), @stack+1) pub It_Works(n) dira[17]~~ refAddr := n ' repeat outa[17] := (word[refAddr]) waitcnt(clkfreq/4+cnt) outa[17] := (word[refAddr]) waitcnt(clkfreq*7+cnt) { repeat refAddr := n if refAddr == 1 outa[17]~~ ' waitcnt(clkfreq/2+cnt) if refAddr == 0 outa[17]~ ' waitcnt(clkfreq/2+cnt) }What's the reason for having the servo / PING handling in a separate cog (from the main one)? Is the servo and PING functioning autonomously? What about accounting for the motion of the robot? How's that done? If you're stopping the robot to avoid motion artifact, why not do the scan in the main cog? If you want the servo and PING to function autonomously, you do indeed want to have motion information passed on to the servo / PING cog via shared variables.
It works but it's unreliable. I hope that makes sense.
In a nutshell; When the motors finish executing I want them to copy "1" to a universal global address which the Ping object can see.
Who sets the flag values to zero?
When you call the start method for the motor, you pass it the address of that motor's flag long. The start routine saves the address in a global variable (VAR) for the object. When the start routine does its cognew, it doesn't have to do anything special because the new cog's code can directly access the global variable that holds the flag's address. I don't recommend passing the address to a separate method because the address should be known before you start up the new cog.
' Main object VAR long flagRight, flagLeft OBJ right, left : "motorControl" PUB main | isMoving right.start(@flagRight, 1) ' initialize right motor left.start(@flagLeft, -1) ' initialize left motor ping.start(@flagRight,@flagLeft) ' ping can see whether motor is running repeat isMoving := ping.doScan ' here you decide what to do right.doNewCommand( ???) ' initiate a movement with left motor left.doNewCommand( ???) ' initiate a movement with right motor ' you can wait here for both movements to finish if you want ' Object for right and left motors: VAR long saveAddr, saveCommand, stack[20] PUB start( flagAddr, direction) saveAddr := flagAddr ' save flag address saveCommand := 0 ' "do nothing" command long[saveAddr] := 1 ' make it look like motor is ready cognew(motorMethod(direction),@stack) PUB doNewCommand( command) repeat until long[saveAddr] ' wait for motor ready saveCommand := command long[saveAddr] := 0 ' signal new command is ready PUB motorMethod( directionSign) repeat while long[saveAddr] ' wait for new command ' do stuff based on value of saveCommand and directionSign long[saveAddr] := 1 ' signal we're doneThis works for me:
obj 'ONE two : "two" var word ref pub main two.[COLOR="#FF0000"]Start[/COLOR](@ref) repeat ref := 0 ' waitcnt(clkfreq+cnt) ref := 1 ' waitcnt(clkfreq+cnt)var 'TWO word refAddr long stack[10] pub start(a) cognew(It_Works(a), [COLOR="#FF0000"]@stack[/COLOR]{0}) pub It_Works(n) dira[17]~~ refAddr := n repeat outa[17] := (word[refAddr]) waitcnt(clkfreq/4+cnt) outa[17] := (word[refAddr]) waitcnt(clkfreq*7+cnt)It is a big deal when people take the time to clarify concepts by writing code for me. Understanding a concept is better than simply memorizing syntax. Thank you.