Vars from one cog to another
Ok I am new.... To the whole prop world, and would like to say thank you for your time.
I have been reading and trying to wrap my head around this as much as I can but I just cant seem to get it. So here I am for help.
The simple question is how do I get variable Information from one cog to another.
Where I get lost is that the hub, must pass the cog, in order for it to take the data and then pass it on. In a quick infinite loop code how do I not miss this window? Or will it set it there and the hub will grab it next pass? I cant seem to find a command that would be Wait for Hub pickup.
I am sorry this is so Noob question, but Im just so lost now.
So regardless of if the hub picks it up or not, How do I use it.
So as for a coding example for the real side of it.
Object Timer to be loaded into cog 1
Object 2 to be loaded into cog 2.
Ok so now my boot program, or main program would look like this.
Now at this point there is no way it makes any sense that Use can access timer. According to the manual, cant use vars outside of that object.
So then maybe
But that doesnt seem to get me anywhere.
So what if I Use the return from the timer obj
So last thought was Repeat it, but then all I do is keep writing cog after cog and starting it.
and I still have not been able to use the value I got from the time obj.
So this is where I am at, I thank anyone who takes the time to read this for at least doing that, and if anyone can help me I would greatly appreciate it.
I am sorry this is so basic.
Thank you
TJ
I have been reading and trying to wrap my head around this as much as I can but I just cant seem to get it. So here I am for help.
The simple question is how do I get variable Information from one cog to another.
Where I get lost is that the hub, must pass the cog, in order for it to take the data and then pass it on. In a quick infinite loop code how do I not miss this window? Or will it set it there and the hub will grab it next pass? I cant seem to find a command that would be Wait for Hub pickup.
I am sorry this is so Noob question, but Im just so lost now.
So regardless of if the hub picks it up or not, How do I use it.
So as for a coding example for the real side of it.
Object Timer to be loaded into cog 1
// Timer.Spin Var Word Timestart, Timeend, Time Pub Timer // Have tried Pub Timer(Time) or Pub Timer |Time see further down Repeat Waitpeq(%000001,<|5, 0) '' Wait for the pin to go high, to start timing the high pulse. Timestart := Cnt '' Store the Time to one var. Waitpeq(%000000, <|5,0) '' Wait for the end of the pulse, low side Timeend := Cnt '' Store the end time Time := Timeend-timestart // So here is where im lost, How do I get this value out so I can use it in a different cog.
Object 2 to be loaded into cog 2.
// usetime.spin Pub Main // Have tried Pub main(Time) or Pub Main |Time see further down Repeat If Time >= 100 // So if the pulse is long enough do something. DO SOMETHING // anything I guess
Ok so now my boot program, or main program would look like this.
OBJ Timer : "Timer" Use : "Usetime" Var Word Time long Stack[noparse][[/noparse]9] Pub Start Cognew(Timer.timer, @Stack) '' Put this timer thing into a new cog. Hopefully 1. Cognew(Use.Main,@stack) '' Put this one into another cog, 2
Now at this point there is no way it makes any sense that Use can access timer. According to the manual, cant use vars outside of that object.
So then maybe
OBJ Timer : "Timer" Use : "Usetime" Var Word Time long Stack[noparse][[/noparse]9] Pub Start Cognew(Timer.timer(Time), @Stack) ''. So It would be a Var in a var?? Cognew(Use.Main(Time) ,@stack) '' Use this Var in Var I got.
But that doesnt seem to get me anywhere.
So what if I Use the return from the timer obj
OBJ Timer : "Timer" Use : "Usetime" Var Word Time long Stack[noparse][[/noparse]9] Pub Start Cognew(Timer.timer |Time , @Stack) '' I think I only get this variable 1 time... Not a constant update Cognew(Use.Main(Time),@stack) '' Try and use time
So last thought was Repeat it, but then all I do is keep writing cog after cog and starting it.
and I still have not been able to use the value I got from the time obj.
OBJ Timer : "Timer" Use : "Usetime" Var Word Time long Stack[noparse][[/noparse]9] Pub Start Repeat ''Use a whole bunch of cogs Cognew(Timer.timer|Time, @Stack) Cognew(Use.Main(Timer),@stack)
So this is where I am at, I thank anyone who takes the time to read this for at least doing that, and if anyone can help me I would greatly appreciate it.
I am sorry this is so basic.
Thank you
TJ
Comments
1. Please make the stack for the new cogs more than 9. You also need a different stack for each cog so you need
2. Unfourtantly, although it doesn't say it in the manual you can't do
The method in cogNew must always be in the same object. The way around this is to simply add a new method to the object that you want to run like this
Now you simply need to call the Start method. You should really also store the cogId and add a stop method but this is all in the propeller manual in the spin section.
3. Now to what your question is about. You need to learn about pointers and passing by reference. Have a read of my post in this thread http://forums.parallax.com/showthread.php?p=715624 about pointers. If that helps good, if it doesn't than we can discuss it some more here
4. Watch out for the difference between '>=' and '=>'. One is an assignment operator and the other is a comparative operator.
Good Luck
But wont each time I call the start method, I will place that code into a new cog? and start the repeat loop?
or does it skip that instruction knowing that its started?
I tried to keep it simple so I don't to lost.
When I call the Start method. It would excaute and return. I think, but would requre a start trigger each time.
What I think I want to do, Is start timer in Cog one and have it constantly looping, and updating the "Variable".
Then Cog 2, each time it gets to where ever it is in its loop, can read the Time from cog one's Variable. Regardless if has been updated or not, if not it would just get the previous value.
Maybe Im looking at this wrong.
If you put this in your main object than that is exactly what it will do.
So in cog1 this will start to run. It will start Timer in cog2, Use in cog3 and will then stop so you will have just 2 cogs running.
Note that I have added an extra parameter '@timerValue'. This is a pointer and will allow you to share the timerValue variable. But, you need to understand about pointers to use it.