Call subroutine from within (spin) cog?
Zoot
Posts: 2,227
I have a robot program on a Propeller 1 I'm working on.
The main program uses a PWM object that launches it's own cog.
The main program then launches another cog that does some RTC stuff, updating of ramping values, etc.
Can the second cog call methods in the original PWM object? In other words, can the new spin code cog call subroutines from the main program? It compiles, but don't know if this will work in practice.
e.g.
The main program uses a PWM object that launches it's own cog.
The main program then launches another cog that does some RTC stuff, updating of ramping values, etc.
Can the second cog call methods in the original PWM object? In other words, can the new spin code cog call subroutines from the main program? It compiles, but don't know if this will work in practice.
e.g.
OBJ pwm : "pwm.spin" ' launches a cog, but also has some methods for updating PWM duty values and such PUB Main pwm.start cognew( monitor_cog, @stack) repeat ' do more stuff PUB monitor_cog | tmp1 repeat tmp1++ if tmp1 > 100 tmp1~ pwm.duty( ledPin, tmp1, 1000 ) ' this method is in the pwm object declared in the main program waitcnt( clkfreq / 50 + cnt ) ' can you do that from within the cog?(also, I can't figure out how to post code on the new forums)
Comments
Use HTML tags from the "Show source" button at the right of the toolbar <pre><code>my code here</code></pre>
[code]
Any cog can "communicate" with any other cog through hub RAM. Probably, you can do what you want since the PWM core code is already running in another cog. The easiest thing would be to give it a try and if it works, great! However, you could also look through pwm.spin and find pwm.duty. If that method doesn't use any stateful variables (variables that were modified in the Main cog), you'd be good to go.
Some motor control objects just pulse the enable pin from PASM but the direction pins are set with Spin methods. The same sort thing happens with one of the OLED display objects. Most the pins are controlled from PASM but the command/data pin and the reset pin are controlled from Spin.
The safest thing to do is to make calls to the PWM object from the same cog which called the Start method. If you attach an archive of your program we can let you know if you need to worry about which cog calls the object.
I just wasn't sure if you could even "run" external subroutines from within a spin cog.
Somewhat related: is it correct that the spin interpreter itself uses up a cog? So if I have 6 spin cogs that I've set in motion, I am really using 7 cogs, because one runs the intrepreter?
-Phil
The Spin interpreter requires some stack space in the hub which is why the address of stack space is added to "cognew" commands launching Spin code but not PASM code.