Reading a variable in another cog started by an Object
xanadu
Posts: 3,347
I seem to be confused on global VARs. I have the PE kit book in front of me, and have done the examples related to cogs and variables. I have written code in the past that seemed to work fine, but I cannot get this simple example to work for some reason.
This is the main program, it counts up from 0 to 10 on the PST. It is also supposed to display the variable from the Object below.
This is my test object that produces a variable that counts down from 10.
Thanks for any help.
This is the main program, it counts up from 0 to 10 on the PST. It is also supposed to display the variable from the Object below.
'' Cogs and VARs version 1 Main Program OBJ PST : "Parallax Serial Terminal" OBJ1 : "Object1" CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long mainnum long obj1num PUB Main '---Init Vars--- mainnum := 0 '---Start PST--- pst.Start(115200) waitcnt(clkfreq * 2 + cnt) pst.str(string("PST Online")) pst.newline '---Begin Object 1--- OBJ1.Start '---PST VARs from this object, and new cog in Object 1--- repeat until mainnum => 10 pst.str(string("Main:")) pst.dec(mainnum) pst.newline obj1num := obj1.go pst.str(string("Obj1:")) pst.dec(obj1num) pst.newline mainnum := mainnum + 1 waitcnt(clkfreq * 1 + cnt) '---End Program--- pst.str(string("End Program"))
This is my test object that produces a variable that counts down from 10.
'' Cogs and VARs version 1 Object 1 OBJ CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long stack[100] long obj1num PUB Start cognew(Go,@stack[0]) PUB Go obj1num := 10 repeat until obj1num =< 0 obj1num := obj1num - 1 waitcnt(clkfreq * 1 + cnt)
Thanks for any help.
Comments
You are mixing two concepts here. What is the goal you want to archive?
You are starting a parallel process counting its own variable down from 10 to zero. One time. then this cog finishes.
you also call obj1num := obj1.go
thus calling the same thing once from main. And return the RESULT which is simple 0 since you do not set RESULT in GO it will return RESULT as 0.
All expected behavior.
Give a example of what your expected output in PST should look like and I may be able to build a small example for you.
Enjoy!
Mike
The child object has no knowledge of the variables in the parent object unless the address of the variable is passed to the child. Variables in Spin don't care about cogs. Only objects make variables inaccessible to other methods.
Naming the variables the same name in the parent and child don't do anything towards having the variable shared.
Here's my take on your objects.
Parent:
I added a "b" to the end of the child object's name. Make sure and name the object appropriately.
Here's the child object.
This example shows how the child object can change the value of a variable in the parent object. To access the value of a variable in the child object you'd use a different technique. I'll modify this example to show you an example of accessing a child's variable and post it soon.
Edit (4/20/14): Corrected typo. I had used "accessible" when I should have used "inaccessible".
The variable is passed with the method "GetNumber".
Here's the child (I removed some white space so it doesn't take up as much space in the forum code block).
This technique is probably the most common way of passing variables from child to parent.
Another technique, similar to the first example, is to pass the address of the child variable back to the parent.
Edit: Somehow I missed Mike's earlier reply. I doubt Mike will mind my not waiting for xanadu's clarification before proceeding with an example.
This one returns the address of the child's variable with the Start method.
The child object is almost the same as last time. The main difference is the address of the variable of interest is returned in the Start method. Since the address is returned, the method "GetNumber" is no longer needed.
Just to reiterate, cogs don't have any effect on how variables are accessed in Spin. The trick comes when you want to share variables among several objects.
@Mike, sorry for cutting in line.
I guess it's time for me to stop procrastinating and get back to work.
Thank you. My goal is to find a bare bones example of how to access global variables produced by other cogs in other objects. All of this is part of building my robot in the projects section. I have a PWM object and an encoder object (both not written by me) producing variables I'd like to be able to access from my main program which I am writing personally. I'd like to just leave the objects I'm not writing fully intact, and call on them when needed.
In order to do that I have decided to stop messing with that code, and write my own very simple object that calls another cog, and makes that cog produce a variable, then display it in my main program. So the inoperative example I posted was supposed to be a minimal code example of passing around variables.
The output should look like this. My main object (main1) counting up while the child object (obj1) counts down.
In my altitude hold project which works as far as sharing the variable goes in the main program I have this:
I can display the "balt" var on the pst no problemo.
Main Object:
That pulls the variable from the altimeter object with no problems. I can't seem to wrap my head around why it won't work the same way in my code above:
Altimeter Object:
I will run this example now and see if I can understand it better than what I am trying to do. Thanks again.
Every Spin Medthod has a implicit existing local variable named RESULT. In opposite to all local vars you define it is set to zero by default.
If you do not end your Method with a explicit RETURN someVar a RETURN RESULT is the default behavior.
Here now comes the crude part. If you do not like the name RESULT for thet return Variable you also can rename it.
look at
PUB altitude(mb) : alt | i, p, p2, x, qm, q0, q1, q2
here alt is renaming result.
: alt
all other local vars are defined after the |
| i, p, p2, x, qm, q0, q1, q2
so now PUB altitude(mb) has a implicit 'RETURN alt' at the end.
and that is the value you get by calling
balt := alt.altitude(alt.average_press) / 30
in your code above you just return 0 since RESULT is not set to anything else.
Duane showed a solution for alternate output.
Enjoy!
Mike
Main
Object 1:
PST Window Result:
Oh right, like Duane said twice already... I changed that and now it works.
Thanks everyone for your help, I much appreciate being unstuck on this one!
Wow. It took me about 2 weeks to get the meaning of that footer. I guess. It is irritating me for a while now.
So when you finish that school you talked about going to in that other thread you will be a professional meat servo?
Enjoy!
Mike
I guess depending on how you use the word professional hahaha. The other day I thought I had a servo going bad because the autopilot was climbing way over the pre-selected altitude. Sure enough I had the wrong barometric pressure programmed in. Semi-pro move for sure...