Shop OBEX P1 Docs P2 Docs Learn Events
Printing in COGS? — Parallax Forums

Printing in COGS?

egeisegeis Posts: 5
edited 2014-01-14 13:36 in Propeller 1
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
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

  • DL7PNPDL7PNP Posts: 18
    edited 2013-11-11 04:08
    I never programmed the Propeller in C, but i believe you may have to start the fdserial object in your scanner cog, so try this:
    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);  
      }
    }
    
  • jazzedjazzed Posts: 11,803
    edited 2013-11-11 14:15
    Following DL7PNP example ... (well sort of).

    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);
      }
    }
    
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-01-13 05:32
    I'm having same problem of using print() in non-main cogs but nothing appearing in terminal from cogs other than main. I can't fit above answer to my situation because there is no print object (like xb object) to move. Much thanks and your answer will be leveraged in a short doc I'm writing for folks moving from STAMP to Prop C
    /* 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);
      }
    }
    
  • ersmithersmith Posts: 6,054
    edited 2014-01-13 12:23
    Does print() use another cog to print the data, like FullDuplexSerial does? If not, then that's the problem right there -- if two cogs both try to manipulate the same pin (the serial output pin, in this case) the results are almost always wrong.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-01-13 12:35
    Interesting idea. To test I removed print() from main() and put a print() in Blink500(). Result: nothing on terminal.
  • jazzedjazzed Posts: 11,803
    edited 2014-01-13 12:41
    John, Eric's statement still applies.

    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.
    // add to top of your file
    #include "fdserial.h"
    
    // add this to the beginning of main
    //
    extern text_t *dport_ptr; // default debug port pointer gets reassigned to fdserial.
    simpleterm_close();
    dport_ptr = (fdserial_open(31,30,0,115200));
    
    
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-01-13 13:52
    Much thanks, especially for supply of code along with the concept. I gave it a try (but don't understand each part yet). Code copypaste below to be sure I did it right.
    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);
      }
    }
    
  • jazzedjazzed Posts: 11,803
    edited 2014-01-14 13:21
    Much thanks, especially for supply of code along with the concept. I gave it a try (but don't understand each part yet). Code copypaste below to be sure I did it right.
    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.

    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.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-01-14 13:36
    Thanks Jazzer.
    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.
Sign In or Register to comment.