Selection of objects at run time
ftkalcevic
Posts: 10
Is it possible to select which object you want loaded into cogs (and the number) at runtime?
I am writing a simple pwm motor controller.· It sends a signal to the motor driver to run the motor.· The signal can be PWM+Direction, Step/Dir, or the two halves of an H-Bridge.· I'd like to be able to write a .spin file (in PASM) for each individual type.· Then, via serial communications, the host pc can configure the type of output, and the main cog can load the one that is required.
Can this be done?
Thanks,
Frank
I am writing a simple pwm motor controller.· It sends a signal to the motor driver to run the motor.· The signal can be PWM+Direction, Step/Dir, or the two halves of an H-Bridge.· I'd like to be able to write a .spin file (in PASM) for each individual type.· Then, via serial communications, the host pc can configure the type of output, and the main cog can load the one that is required.
Can this be done?
Thanks,
Frank
Comments
If all the different code fits in the prop chip than it should fairly easy to do. You can use a trick like this to make it easy. However, if you do this than the methods that you call have to be have the same number of arguments and be in the same order for all of the objects.
Post Edited (stevenmess2004) : 12/11/2009 11:54:24 PM GMT
These are small routines, so they should all fit into memory at the same time.
stevenmess2004, what happens with any VAR data that is declared in an object.· If I spin up more than one instance, will they all point to the same VAR data?· Will I need to manage main memory myself?·
Thanks,
Frank
·
-Phil
In this code there will be one copy of the spin code and dat variables for pwmobject and two copies of the var variables for pwmobject. other object has its own complete copy of its spin, var variables and dat variables.
(well at least most of the time. If the code in otherobject is exactly the same as pwmobject it will be treated as if it was another copy of pwmobject.)
Will these instances share the same VAR from the obj1?
Do I need to allocate the data myself? ....
Post Edited (ftkalcevic) : 12/12/2009 12:15:09 AM GMT
The Memory endups looking something like this
This memory layout remains fixed throughout the life of the program and you can't change it. When I use instance in spin I mean a reference to an object such as obj1 or obj2. It doesn't matter what cog an object is running in, when you access any of the VAR or DAT variables they will still reference the same thing.
Local variables are a different matter though. Because local variables live on the stack and each cog has it's own stack the local variables will be different for each cog.
If you do want to use different data in each cog than you will need to either create different instances (not just start the same instance in a different cog) or write your code to use pointers and then pass in different pointers. Don't forget that you can have an array of objects that will all have their own VAR sections. You could do something like this.
How can I decided the number and types of objects I run at runtime?
I can see this as one solution....
Here I allocate storage for the maximum number of each instance.· COGS are only started when Start is called.· This wastes lots of memory.
My other idea, the confusing post above, was to define one object instance of each type, then pass in a pointer to some space allocated to it.· So the object would be...
The VAR data_ptr, is only used in starting the·PASM in the COG.· It is a pointer to shared memory, stored in COG memory.· It is then up to me to ensure there is enough longs (shared memory)·allocated for the object.
And I can create COGS by...
Just saves a little bit of code and you don't have to worry about it being long aligned.
Is the spin code changing the DAT section in main memory, amd then copying it into the COG?·
·
What I do - mainly to save RAM - is that I put all my PASM-drivers (keyboard, tv, SD ...) into upper part of the 64kB EEPROM. The SPIN-part of the drivers has been changed to load it into a temporary global buffer and start the COG. So, there is no PASM in a DAT section of the drivers any more.
But with this it's also possible to select a driver during startup or replace it during runtime.