Putting Code into a COG
Discovery
Posts: 606
I could use some sample code that shows how to run a separate program in a COG. Actually, three programs will run in three separate COGs.
How is the coding accomplished?
Discovery
How is the coding accomplished?
Discovery
Comments
-Phil
http://learn.parallax.com/propeller-c-functions/multicore-example
If you are programing in Spin, I'd recommend using the Propeller Education Kit Labs Fundamentals book. It has lessons and code for using the cogs. You can download the pdf from this page:
http://www.parallax.com/downloads/propeller-education-kit-labs-fundamentals-text
Although the circuits use the Propeller Education Kit, it's easy enough to use with any Prop board. I use the quickstart and activity boards.
Hope this helps
Tom
-Phil
I am programming in "C".
I will get right on it.
Discovery
I wrote a test program composed of the Multicore Example as referenced above. Every character was checked and checked again...then again.
The output is a string of about twenty 5001's followed by about the same number of 5002's and this format continues to 5003, 5004, etc.
Any reason why it does that?
Discovery
Hi Discovery,
Did you hand-enter Multicore Example into a new project, or did you run the example program supplied with the SimpleIDE Learn folder?
If you hand-entered the code, could you please run Multicore Example.side from this folder: Documents\SimpleIDE\Learn\Examples\C Intro\Basics
....and tell us what happens?
I hand entered the code. After Looking at the While Loops I noticed why the strange output appeared. The display rate in the main program was faster than the adder loop rate so the pattern emerged. I made the pause values the same and the 5000 number increased by one as it should.
I used the same approach in the Multicore Example for my propeller development code and the COG worked fine. In my application, I designed the propeller COGs to operate with a math-coprocessor to handle floating point numbers, the main COG for my program, and three more COGs to run identical programs. These three identical programs use the input function to detect the rising edge of three square wave inputs, count the number of rising edges, and make the three summations available to the main COG program. I cannot get all three input counting cogs to work at the same time. I duplicated the one counting code three times in the main program but only the last referenced COG produces a measurement.
There must be a cleaver way to get the COGS to run separately but in all my reading of forums and tutorials...I just cannot find any solution. I am getting frustrated. When I write programs for the BS2 modules...it is real breeze to use the pull-down library that explains the functions and how they are used. Whoever built the BS2 P-Basic code reference deserves a pat on the back. That was outstanding. It is too bad that the same was not done for the Propeller C. I could really use some hands on help.
Discovery
Would an index of all available functions be helpful?
Today the functions are all described in the library page found under the SimpleIDE Help menu. The library page is also online here.
The BS2 has a very limited but useful set of utility functions. Having a limited set makes things much easier.
Yes
Yes
I have the Library available
Limited but very informative
My problem is to build an industrial robot control system using the propeller programmed in "C" that uses one COG for math co-processor functions, a second COG for the main control program, and three additional COGs that implement up/down counters that make the count variables available to the main program and receive separate inputs to zero the counters.
I was provided code that allowed me to set up a COG with the up counter code (currently lacking the control signals for down counting) and it worked. However, when I tried to start another COG with the same program...the first COG was shut down.
The program used a function start_cog() which does not show up in the library. Using start_cog two and three times does not work...only the last cog is working...it would appear.
The function that makes sense to me to use is cog_run() which says it will run a function code in the next available cog. But when I try this I get and error that I cannot correct. There must be a lot more to using the function than meets the eye.
What I really need is a short piece of code that shows precisely how to setup three COGs that work with passing global variable data between the main program and the three COG up/down counters. If you have setup two or more COGs that work, would you provide me the code so that I can learn how to implement it? Just looking at library functions isn't going to cut it.
Discovery
Oops...an error.
I got the cart before the horse...I did not mean start_cog, I meant cogstart().
Discovery
The code that I generated and loaded into the three COGs that act as up/down counters cannot all run at the same time. Each one runs correctly as the GOG is started but stops as the next COG is started.
How can this be corrected so that all three up/down counters operate at the same time?
Sincerely,
Discovery
Of course, to help us diagnose this issue you'll have to post your code.
Here's an example that runs 3 counters in parallel and then loops printing out their values:
Steve, I just installed the latest version of Madcap Flare to get going on that index. Stay tuned!
In the meantime, some test code is below with rudimentary versions of the features you explained. Here also is a temporary simpletools library fix (steps 1...7 for you). For the time being, please also avoid using the print function to display result of attempts to divide floating point values by 0.0.
(0) For others who want to try this, replace your learn folder with the 2/27/14 or newer version available here http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder.
(1) Use SimpleIDE's Open Project button to open Documents\SimpleIDE\Learn\Simple Libraries\Utility\libsimpletools\libsimpletools.side
(2) If the project manager pane is not visible along the left, click the Open Project Manager button (bottom-left corner of SimpleIDE window) to display it.
(3) Right click fpucog.c -> source/fpucog.c and select delete. (Or just click it and hit the delete key)
(4) Make sure the Memory Model field in the Project Options tab is set to CMM Main RAM Compact.
(5) Click the Build Project button.
(6) Make sure it compiled.
(7) Shut down all instances of SimpleIDE, then restart.
Thank you
Discovery
I am back and tried to run the ersmith code and the Lindsay code by hand entering the code into two programs. I received a compiler error on line 13 unsigned char STACK2[STACK_SIZE] for ersmith
and
an error on line 22 cog_run(squareWaves3, 50); for the Lindsay code.
Would you put these two programs on the mydocuments/simpleIDE/propeller GCC Demos folder so that I can run your code?
Sincerely,
Discovery
I will try copying the code.
Discovery
I just noticed the transcription errors. Three of them all semicolons.
The code runs fine.
Thank you for the suggestion.
Sincerely,
Discovery
[edited because I just noticed you managed to get it working -- glad you found the issues. In general it's helpful to post the exact compiler error when you get one -- that can help diagnose the issue.]
Regards,
Eric
For Andy Lindsay's code...I cannot find any error in transcription. The compiler will not compile cog_run.
Discovery
http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder
...do so and then try again. Make sure to restart SimpleIDE after updating the library and before re-running the code example.
If that doesn't make it work, copy all the error messages, and paste them into a reply on this thread. For best results in the forum post, add [noparse] [/noparse] just below them.
Example
[noparse] [/noparse]
Results
It'll look like this in the post.
Andy, I was able to find and correct the semicolon errors in my transcription of ERSMITH's code and run the code but the code you provided is much closer to the configuration that my controller requires.
If I can get your code to run, it should be a relatively easy task of inserting the additional control panel interface I/O to make the machine operate. I am just learning to program in "C" and find myself struggling to understand the commands that you and the other well seasoned "C" programmers use rather easily.
I tried to copy your code but I could only get portions...actually it took three copy segments to get the entire code to print onto paper where I made the transcription into my code.
What do you recommend as the best way for me to get your code into my machine?
Sincerely,
Discovery
I downloaded the Learn Folder files and began to perform the seven steps listed in your forum note. I was able to delete the function fpucog.c->source/fpucog.c.
However, I could not make sure that the memory model was CMM because there is no Project Options Tab.
I will assume that the ram memory is set to CMM and perform steps 5, 6, and 7.
Discovery
I performed all the steps except step 4 and cog_run(squareWaves3, 50); will not compile.
Build error status
c:55:18expected ')' before ';' token
c:58:3expected ';' before '}' token
Build Failed
I thought it was peculiar that there are no opening and closing brackets for the instructions contained within the FOR and IF functions but I am still a novice in 'C' programming. As a What If...I added the brackets to the FOR and IF functions and got the same build failures.
Discovery
The Build Status
Propeller Experiment(3).c:55:18expected ')' before ';' token
Propeller Experiment(3).c:58:3expected ';' before '}' token
Done.Build Failed!
I noticed in the first while function there are no opening and closing brackets in the FOR and IF functions. I added them and got the same result.
The compiler halts at cog_run(squareWaves3, 50);
I just don't see the problem.
Sincerely,
Discovery
Here is a link to a set of tutorials designed to take minimum time while guiding you through enough C language, circuits, and programming the Propeller to get up to speed with multicore projects.
http://learn.parallax.com/propeller-c-tutorials
The for and while commands operate on either a single statement, or a block of statements contained in braces { }.
From this, I'm seeing a number of potential issues after correcting any transcription errors. So, attached is a zip of the code from post #16.
(8) Open SimpleIDE and click Help -> About. Verify that you have SimpleIDE Version 0.9.45 or higher. If not, go to this page, and follow the instructions to update your software http://learn.parallax.com/propeller-c-set-simpleide. If you do update your software, you will also need to update your learn folder again, following the instructions here: http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder.
(9) Download Putting Code into a Cog.zip (attached).
(10) Unzip it to your desktop.
(11) Use SimpleIDE's Open Project button. Use the Open Project dialogue to find the "Putting Code into a Cog" folder on your desktop, and open "Control System Test and Display.side" from within that folder.
(12) After you have opened "Control System Test and Display.side" into SimpleIDE, click the Run with Terminal button.
If your board has LED's connected to P26, P27, and P5, they should blink fairly rapidly (at rates of 5, 6, and 7 Hz, and then double that after 5th SimpleIDE Terminal update). The SimpleIDE Terminal should display the cycles it counted at each pin. If you want to test externally applied signals, make sure to comment cog_run(squareWaves3, 50) by adding a // to the left of it. Then, re-run the program before applying the external signals.