accessing 1 object from 2 programs
TC
Posts: 1,019
Hello all,
I am drawing a blank here, and am in need of some advice. I am going to try to explain this the best I can.
My main object (parent) will start 2 cogs. One cog is reading temperature, and the other cog is performing the PID calculation and the simple PWM controlling. What I want is the temperature cog to pass 2 forms of temperature data, one form to the parent(current full temperature data for displaying), and the other form(shorten temperature data) to the PID/PWM cog. But I also must be able to pass values to the PID/PWM cog.
I hope that makes sense.
Thanks
TC
I am drawing a blank here, and am in need of some advice. I am going to try to explain this the best I can.
My main object (parent) will start 2 cogs. One cog is reading temperature, and the other cog is performing the PID calculation and the simple PWM controlling. What I want is the temperature cog to pass 2 forms of temperature data, one form to the parent(current full temperature data for displaying), and the other form(shorten temperature data) to the PID/PWM cog. But I also must be able to pass values to the PID/PWM cog.
I hope that makes sense.
Thanks
TC
Comments
Var long temp1, temp2
Cognew(@stack, @temp1)
In the cog, access the temp1 data with
Pub main(tmp)
long[tmp] := 1. 'Temp1
Long[tmp+4] := 1. "Temp2
This assumes it is a long data. Otherwise use word or byte. You can access other data on the same line by indexing using +4 etc.
Send any cog the same pointer and any cog can read or write the value
-Phil
And I just realized I forgot some much needed information in my first post.
Each cog is controlling 2 things. The temperature cog controls 2 MAX31855 with 2 32-bit values. I can make the parent read the 32-bit value, and the adjusted values from the MAX31855 (thermocouple, Cold Junction, and errors). The PID/PWM cog has 2 PID's it runs through, and the output of the PID goes to the PWM that controls the heaters. I can adjust the PWM of the heaters individually from the parent.
But I need the PID/PWM cog to get the thermocouple values from the MAX31855's and run the PID about once a second. The parent can request the current MAX31855 data at anytime. *edit* But the parent needs to be able to send the "set" temperature to the PID.
cognew(main(@temp1), @stack) 'start a cog doing something
cognew(PID(@temp1), @stack2) 'start a cog doing something
cognew(PID2(@temp1), @stack3) 'start a cog doing something
In the cog, access the temp1 data with
There are various ways to do what you want. The above is just examples to show you how you can read or write any variable from any cog. IF you want to write bytes, it will be the same but only use byte[someparamter] := 1 as an example. You can modify any preexisting object to accept a parameter that is a pointer, except that if the object is using PASM, there is more work to do to get the pointer into the PASM code plus have the PASM doing some extra work to rdlong or wrlong.
Don't tell anyone, but when I have a bunch of variables that need to be shared, I just make a monster parent object and dispense with child objects except for low level drivers.
Those are some good examples, thanks for sharing.
I wont tell.... many. Muahahah
I personally like to have A object for each task. If I need to control an LED, I make an object that its only job is to control the LED. I do that because I can get lost in large amount of code. So I split the code into smaller sections that I do not have to have open if I do not need to look at them. Then I would just call the object from the parent object. This is going to help with the way I do code. Right now I have to tell my child objects to change things. But now I can just have the parent change the RAM value, and the child will just look at the RAM location.
I know I don't. I just helps me to stay on tract, and so I don't get lost.
Ideally, one would want to organize code with object but there are times when doing so can create more work than it may be worth.
A few years ago I was trying to make a GPS logger. The examples I found in the OBEX used cogs very inefficiently. By moving many of the child objects into the parent object, I was able to reduce the number of cogs used by four.
I left the CON and VAR sections next to the section of code they were used with. These extra CON and VAR sections made it much easier to find my way around in the code. Here's what the "Summary" view looks like when using the smallest font.
Sure it would have been nice to have the code broken into objects but I decided I would rather spend the time it would require to do so some other way.
The Propeller Tool also has bookmarks to make sections of code easier to find.
For the record, I don't leave my code the way I am doing it. I only do it the way I am doing it for creating my program. Once I am happy with what I have, I will open up the child object file, change the name (add underscore before name) then copy and paste the child object to the parent. Then I will change a few things. Most of the time all I have to do is change the dot (name "."name) to a underscore (name"_"name). My way has a large flaw that I know of, it uses a lot of ram. I'm ok with that for getting the program working, but not the final code.
I have a total of 19 variables that are being shared between the parent and one child object.
These variables are being used mostly by the child object. But I want to be able to have the parent object to be able to read and write the contents of the variables.
I am using what everyone has suggested, but I am starting to get lost keeping tract of what pointer I should use (index+4, index+8, index+32, etc..)
I tried to do
But it does not work since "index" is a variable.
Does anyone have any ideas that would make my life a lot better with trying to manage all these variables?
Thanks
TC
I am trying to do the 2 PID algorithms, but with all these variables, I am getting lost keeping tract of the offset(I think that what it is called) of the variable I need. Take this for example;
This line of code is "Upper_error := Upper_Set_Value - Upper_Current_value
I am use to having variable names I can put in code, not " long[_index+16] ". I am just wondering if there is a better way of doing it.
Hope that makes more sense.
Just a hacked together example of using real names versus indexing.
As usual with kuroneko's post you really need to read the fine print.
so change
to get more readability into it
Enjoy!
Mike
What I am going to do is, in the parent object I will declare 2 variables that will be shared.
The one child object will just write its values to the variables in the parent object.
Then in the other child object, I would have
And in this child object, I will have methods that will "return" the contents of a variable that are needed. And it also will have the address of the 2 variables that are in the parent object.