Question about objects and sharing
JasonDorie
Posts: 1,930
I have a bunch of code that I'm trying to keep clean, by using small objects, and a few of them need to use the Float32 object.· That object starts a cog running the PASM portion.
My question is: can multiple objects share a single 'instance' of the Float32 object?· Is there a way to 'pass it around' in the code, like a C++ class?
I have something like this:
-·ControlLogic (uses Float32)
·· |
·· +--PID X· (also uses Float32)
·· |
·· +--PID Y· (also uses Float32)
Since the ControlLogic object is calling the functions in the 2 PID objects, I won't have concurrency issues.· Can I use the same instance of the Float32 object?
According to the manual:
"Symbols defined in an object are global to that object, but are not available outside that object."· It doesn't say anything about passing the objects themselves around.
Anyone done this, or is this unsupported?· I have a few ideas on how to accomplish this by hacking a new start function for Float32, but I was wondering if there was a more 'correct', Spin-friendly way.
Thanks,
Jason
·
My question is: can multiple objects share a single 'instance' of the Float32 object?· Is there a way to 'pass it around' in the code, like a C++ class?
I have something like this:
-·ControlLogic (uses Float32)
·· |
·· +--PID X· (also uses Float32)
·· |
·· +--PID Y· (also uses Float32)
Since the ControlLogic object is calling the functions in the 2 PID objects, I won't have concurrency issues.· Can I use the same instance of the Float32 object?
According to the manual:
"Symbols defined in an object are global to that object, but are not available outside that object."· It doesn't say anything about passing the objects themselves around.
Anyone done this, or is this unsupported?· I have a few ideas on how to accomplish this by hacking a new start function for Float32, but I was wondering if there was a more 'correct', Spin-friendly way.
Thanks,
Jason
·
Comments
I'll try altering the code to 'extract' out the ASM engine and see if I can get the three Float32 objects to use a single cog. It would be useful if the SPIN compiler would allow 'extern' on objects, or something along those lines so that existing objects could be more easily shared like this. I would guess this to be more of a syntax limitation than anything else. So far this is about the only thing about the Prop/Spin stuff that bugs me. That and Ctrl + Up/Down changing the font size instead of scrolling. [noparse]:)[/noparse]
Thanks
to me it's not clear how your code is organized
do you mean there is the main-spin-file
and in the main-spinfile there are defined
- an object controll-logic
- an object PID X
- an object PID Y
and each of this three objects has codelines
is this correct ?
If you change the definitions of the variable
to DAT-RAM-space you can use them from different objects
this technique is show in the serialmirror object from the obex
obex.parallax.com/objects/189/
best regards
Stefan
Stefan, to answer your question a little more specifically it's like this:
MainControlLogic
OBJ:
F : Float32
P : PIDRoutine
PIDRoutine
OBJ:
F : Float32
So I have a Float32 object in use by 'MainControlLogic', and one in each of the two 'PIDRoutine' objects, but 'MainControlLogic' calls the functions in 'PIDRoutine', so it's unnecessary to have 3 cogs running the Float32 processor, as one would be just as efficient in this case. For me at the moment it's ok because I have enough cogs around that it's not a big problem. On the other hand, I keep having to move where I define 'TV_Text' for debugging, because I can only have one instance of it on the designated pins, but I want debug prints in many places in my code. It would be nice to share that object too.
If the DAT section of SPIN code works the way I now think it does, this should be reasonably easy to set up by just creating a different 'Float32' object that puts the VARs in a DAT section instead, and then don't call Float32.start for the 2nd and 3rd instances of it. Does that sound like I've got it right?
Thanks guys,
Jason
For example I want to send a global varable array x[noparse][[/noparse] 25 ] to an object for further analysis from my main object. Can this be done?
Well, there's only one DAT section in a given object, even if it's split up in the source file. It can contain "static globals" and PASM code. When you start up a cog, you load it with 496 longs from the DAT section. Typically that's PASM code followed by some data (and then whatever follows it in hub memory). So as you say, whatever follows the PASM code becomes local to a given cog, in the sense that the cog works on its copy of that data. But in another sense, whatever follows the PASM code is still in hub memory, outside the cog. Hope that makes sense.
I can't speak to your proposed modification to float32.
You can't send the array directly, but you can send the address of the array. Use the @ operator to compute the address, then in your analysis method, use the "long[noparse]/noparse[noparse]/noparse" syntax to access your array (or "word[noparse]/noparse[noparse]/noparse" or "byte[noparse]/noparse[noparse]/noparse"). Here's an example: