How can I start a cog with more than 1 function?
Hal Albach
Posts: 747
I would like to start a cog where the program that is loaded into it with, for example;
cog_run(&wheels, 15);
where the wheels function can call other functions in the cog.
Here I"m using the function "test" just to figure out how to have additional functions in a cog.
Using the Hammer Icon to compile the program returns the following;
I know I'm missing something or doing it incorrectly (obviously!) but the answer eludes me. I've searched the forum for several days and cannot locate how (or IF) this can be done.
I would be grateful for any assistance.
cog_run(&wheels, 15);
where the wheels function can call other functions in the cog.
// Motor COG Function void wheels() { // set forward direction // low(14); // dir left, low = fwd, high = rev // low(16); // dir right test(); while(1) // Forever loop { // turn on pwm to drivers pwm_set(13, 0, pwmR); // pwm_set(Pin, channel, dutyCycle) pwm_set(15, 1, pwmL); // // pause(100); int i; int temp = 0; left = right = 0; for(i = 0; i < 10; i++) { temp = count(22, span); // count pulses from LEFT encoder & accumulate in left += temp; // global variable "left" temp = count(23, span); // count pulses from RIGHT encoder & accumulate in right += temp; // global variable "right" } leftb = left / 10; // average the result rightb = right / 10; // // Adjust pwm for each wheel to match with the set point and display parms if(rightb > sp) pwmR -= cp; // if right is faster then slow it down if(rightb < sp) pwmR += cp; // if right slower than speed it up if(leftb < sp) pwmL += cp; // if left is slower then speed it up if(leftb > sp) pwmL -= cp ; // if left is faster slow it down } // ends while(1) void test() { low(14); // dir left, low = fwd, high = rev low(16); // dir right } // ends void test() } // ends wheels
Here I"m using the function "test" just to figure out how to have additional functions in a cog.
Using the Hammer Icon to compile the program returns the following;
C:\Users\Admin\AppData\Local\Temp\ccg3d39N.o: In function `_wheels': (.text+0x9): undefined reference to `_test' collect2: ld returned 1 exit status Done. Build Failed! Check source for bad function call or global variable name `_test'Both test and wheels are prototyped above Main.
I know I'm missing something or doing it incorrectly (obviously!) but the answer eludes me. I've searched the forum for several days and cannot locate how (or IF) this can be done.
I would be grateful for any assistance.
Comments
Not recommended practice though as nested functions is a GCC extension. Not standard C at all.
Strange but true.
One important thing to keep in mind is that the function running in another cog needs extra stack space for pushing and popping the function call and return addresses along with any local variables the function (blink_counter in this case) might declare. With that in mind, I increased the cog_run function's stacksize argument from 10 to 120. That's way more than necessary here, but it's best to be liberal with stack space while prototyping because bugs from undersized stacks can be real head-scratchers.
When you are ready to make a coding pass to reduce the program size, there's a Cog Stack Usage Test.side project in Documents\SimpleIDE\Learn\Examples\Multicore. If your code makes the function(s) running in another cog perform to their maximum extent, it will tell you how much stack space was actually used. You can take that number and reduce the cog_run function's stackSize argument accordiingly.
I was able to place that void test() function nearly anywhere in the source, at the end, or before Main(), or right after Main(), and it operated the same.
No, it is not the case. What gets loaded into the cog is an "interpreter" (as Chip calls it) just like in spin, and that interpreter runs "wheels()" and anything else wheels calls.
If you want C code to load in a COG to run, you would load it in a way similar to how PASM gets loaded. Actually the C code is compiled to PASM and that PASM is run in the COG.