Printing in COGS?
I recently ran into an issue working with cogs and printing to the terminal and writing to the XBEE terminal. While the writeline and printf lines work inside of the int main(), I'm guessing this would be cog0, they ceased functioning when added to the other cogs.
This is a sample of the code i'm
I have tried finding information in learn.parallax and read some posts on this forum but cannot seem to find a solution. Without a book to refer to like the Boe-Bot or reference material beyond a sites.google entry for the propeller C files I am lost on how to solve this issue.
This is a sample of the code i'm
int main()
{
//Start up XBEE
xb = fdserial_open(rx_pin, tx_pin, 0, baud);
int cog_scan_id = cogstart(&scanner, NULL, stack, sizeof(stack));
while(1)
{
//Does other stuff
}
}
void scanner(void *par)
{
while(1)
{
int i;
for(i = 0;i<4;i++) //pins 0-3
{
q[i] = qti(i);
pause(1);
}
//TODO: Identify Changes.
print("test\n");
sprintf(buf, "[QTI_DEBUG] %d %d %d %d", q[3], q[2], q[1], q[0]);
writeLine(xb, buf);
}
}
I have tried finding information in learn.parallax and read some posts on this forum but cannot seem to find a solution. Without a book to refer to like the Boe-Bot or reference material beyond a sites.google entry for the propeller C files I am lost on how to solve this issue.

Comments
int main() { int cog_scan_id = cogstart(&scanner, NULL, stack, sizeof(stack)); while(1) { //Does other stuff } } void scanner(void *par) { //Start up XBEE here xb = fdserial_open(rx_pin, tx_pin, 0, baud); while(1) { int i; for(i = 0;i<4;i++) //pins 0-3 { q[i] = qti(i); pause(1); } //TODO: Identify Changes. print("test\n"); sprintf(buf, "[QTI_DEBUG] %d %d %d %d", q[3], q[2], q[1], q[0]); writeLine(xb, buf); } }Welcome to the forums.
/* Simple output on multiple cogs with no locking (chars can get mixed). */ #include "simpletools.h" #include "fdserial.h" /* global fdserial device used by all cogs */ fdserial *xb; /* forward declarations to get rid of warnings */ void scanner(void *par); void spitter(void *par); /* plenty of stack space */ int stack[200]; int stack2[200]; int main() { xb = fdserial_open(31, 30, 0, 115200); dprint(xb, "startup\n"); cogstart(&scanner, NULL, stack, sizeof(stack)); cogstart(&spitter, NULL, stack2, sizeof(stack2)); while(1) { pause(200); dprint(xb, "main\n"); } return 0; } void scanner(void *par) { int q[4]; char buf[80]; int i = 0; while(1) { i++; q[i%4] = i; dprint(xb, "scanner\n"); sprint(buf, "[DEBUG] %d %d %d %d\n", q[3], q[2], q[1], q[0]); writeLine(xb, buf); pause(50); } } void spitter(void *par) { while(1) { dprint(xb, "spitter\n"); pause(150); } }/* adapted from Learn.C "Multicore Example.c" two cogs each controlling an LED at different blink rates*/ // PROBLEM TO TROUBLESHOOT: only result on terminal is "Starting main" from cog 0. Nothing from cogs 1,2 #include "simpletools.h" //includes cogstart() void blink1000(void *par); // Forward declaration. Blink duration 1000 pin 26 void blink500(void *par); // Forward declaration. Blink duration 500 pin 27 unsigned int stack1000[40 + 25]; // Stack vars for cog 1 unsigned int stack500[40 + 25]; // Stack vars for cog 2 int main(){ print("Start main"); // test print() cogstart(&blink1000, NULL, stack1000, sizeof(stack1000)); cogstart(&blink500, NULL, stack500, sizeof(stack500)); while(1){} //main does nothing more than launch } void blink1000(void *par){ print("Start blink1000"); // test print() while(1){ high(26);pause(1000);low(26);pause(1000); } } void blink500(void *par){ print("Start blink500"); // test print() while(1){ high(27); pause(500);low(27); pause(500); } }Function print() does not use a separate cog unless it is re-opened as fdserial. We are adding some things to make that easier to do.
Add this code.
Result: I get on the terminal "Start Main" followed by "SSttaarrtt bblliinnkk1500000" - it alternated the characters output from blink1000 and blink500. Sort of cool, but not desired.
My guess is cog1 and cog2 are using same port? In a hazy way I am thinking make two ports available? Currently is just the one re-directed from debug port?
Thanks.
/* adapted from Learn.C "Multicore Example.c" two cogs each controlling an LED at different blink rates // PROBLEM TO TROUBLESHOOT: only result on terminal is "Starting main" from cog 0. Nothing from cogs 1,2 */ #include "simpletools.h" //includes cogstart() #include "fdserial.h" void blink500(void *par); // Forward declaration. Blink duration 500 pin 27 unsigned int stack1000[40 + 25]; // Stack vars for cog 1 unsigned int stack500[40 + 25]; // Stack vars for cog 2 int main(){ extern text_t *dport_ptr; // default debug port pointer gets reassigned to fdserial. simpleterm_close(); dport_ptr = (fdserial_open(31,30,0,115200)); void blink1000(void *par); // Forward declaration. Blink duration 1000 pin 26 print("Start main"); // test print() cogstart(&blink1000, NULL, stack1000, sizeof(stack1000)); cogstart(&blink500, NULL, stack500, sizeof(stack500)); while(1){} //main does nothing more than launch } void blink1000(void *par){ print("\nStart blink1000"); // test print() while(1){ high(26);pause(1000);low(26);pause(1000); } } void blink500(void *par){ print("\nStart blink500"); // test print() while(1){ high(27); pause(500);low(27); pause(500); } }John,
This is where you need to use mutex locks to ensure that output is coherent. I don't have any examples at the moment. Generally, one acquires a lock, does work, and then releases the lock.
Maybe you should ask Andy for some support in this.
My objective is a short guide for people coming from PBASIC to C. I am comfortable noting that certain things are hard in C - accept it and work around.
Andy is working these weeks on some teaching materials so I'll hold off on pursuing this with him.
Only a couple of topics left on my list for the guide and then ver 01 posted for feedback (and tested in a Prop class I'm trying on a trial run). Hopefully a lot of answers will be found there and less posted here. Most importantly I want someone from STAMP to be able to take the guide and have pretty quick success with Prop C.