Shop OBEX P1 Docs P2 Docs Learn Events
assign functions to cogs? — Parallax Forums

assign functions to cogs?

p02osmajp02osmaj Posts: 14
edited 2014-06-21 21:22 in Learn with BlocklyProp
Hello everyone!

How can I assign funcitions/tasks to one cog?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-04-23 07:45
    Give us a little more of an idea of what you want to do. Are you using C or Spin or something else?

    Have you looked at the LMM C toggle example?
  • SRLMSRLM Posts: 5,045
    edited 2014-04-23 07:53
    Assuming that you're using PropellerGCC (since this is in the Learn forum) you can use cogstart:

    PropGCC cogstart: https://sites.google.com/site/propellergcc/documentation/libraries/propeller-h-library#TOC-cogstart
    Tutorial: http://learn.parallax.com/propeller-c-functions/multicore-example
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2014-04-23 08:53
    Thanks for answering, Mike & SRLM.

    We are adding a simpler approach to the simpletools library as well. I'll have a series of short tutorials available for it sometime next week, and will post here when it is ready.
  • p02osmajp02osmaj Posts: 14
    edited 2014-04-28 14:49
    Yes that's right! Thank you all!!
  • p02osmajp02osmaj Posts: 14
    edited 2014-05-20 15:30
    do you have any example of a program that use the i2c library?
    I have written this code and gime a lot of errors the SimpleIde. I don't know is because the simplei2c.h ...
    thank u!



    #include "simpletools.h"
    #include "simplei2c.h"
    #define numero_servos 3


    i2c *i2c_pointer;


    void funcion_expansores (void);


    int main(void)
    {
    i2c *i2c_storage;
    i2c_pointer = i2c_open(i2c_storage, 37, 38, 0);


    int pin = 16;
    int stacksize = sizeof(_thread_state_t)+sizeof(int)*25;
    int *stack = (int*) malloc(stacksize);
    int cog;
    // initialize wait time to 50ms
    wait_time = CLKFREQ/20;
    // start the cog
    cog = cogstart(funcion_expansores, (void*) pin, stack, stacksize);






    funcion_expansores();




    return 0;
    }












    //Se asigna un cog a esta funcion


    void funcion_expansores (void) {


    /*Se declara un array de enteros donde se almacenaran los bytes de cada expansor*/
    unsigned int byte_expansor[numero_servos];
    /*Se declara un array de enteros que se usa para indicar si ha habido cambios en los byte de los expansores*/
    unsigned int cambio_byte[numero_servos];
    /*Se declara un array de enteros donde guardar si ha habido una "interrupcion" en el expansor y hay que leer
    de este. IMPORTANTE: esta se
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2014-05-20 16:07
    We are adding a simpler approach to the simpletools library as well. I'll have a series of short tutorials available for it sometime next week, and will post here when it is ready.

    We posted the Multicore Approaches tutorial here: http://learn.parallax.com/multicore-approaches

    This tutorial uses newly-released libraries, so you must update your Learn folder: http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder
  • p02osmajp02osmaj Posts: 14
    edited 2014-05-21 03:04
    okk thank u ;)
    why the compiler continue give me errors even in parts of code that are commented: /*....*/ ??
  • jazzedjazzed Posts: 11,803
    edited 2014-05-21 08:03
    p02osmaj wrote: »
    okk thank u ;)
    why the compiler continue give me errors even in parts of code that are commented: /*....*/ ??

    In SimpleIDE?

    It is an unintended consequence sometimes of not knowing where the error is (like a linker error). This annoys me too and should be fixed.

    BTW, just for context the IDE is not the compiler. It simply tries to interpret the compiler and other program results.

    Sorry for the trouble.
  • p02osmajp02osmaj Posts: 14
    edited 2014-05-21 08:22
    then if the SimpleIde no work correctly there is no way to check the code?

    and are there examples of the use of simplei2c.h?

    Thank you all
  • jazzedjazzed Posts: 11,803
    edited 2014-05-21 08:37
    p02osmaj wrote: »
    then if the SimpleIde no work correctly there is no way to check the code?

    and are there examples of the use of simplei2c.h?

    Thank you all


    Check the build status window. It gives all details of building.

    All SimpleIDE learn libraries have test code in them. The libsimplei2c has most of it's test code commented out, but you should be able to use the examples there to learn more about the library. I strongly recommend reading the documentation for the library also. Documentation is in your Documents/SimpleIDE/Learn/Simple Libraries Index.html or on line here.

    Also, the simpletools library has some eeprom code that uses simplei2c.h.
  • p02osmajp02osmaj Posts: 14
    edited 2014-06-17 09:33
    hi all again

    I have this problem. I want to pass a parameter to a function through the function cogstart, and it's a struct parameter.
    Is is could be possible pass a struct parameter?
    if so, how can I do it?

    Thank you!
  • p02osmajp02osmaj Posts: 14
    edited 2014-06-17 15:07
    PD: thew compiler shows me, too few arguments in function cogstart
  • edited 2014-06-17 18:10
    Here is an example that uses two instances of a structure to configure two cogs to execute the same code in different ways (different light blinking rates in this case). This uses LEDs connected to the Activity Board's P26 and P27 Propeller I/O pins.
    /*
      cogrun + struct example
    */
    
    #include "simpletools.h"                      // Include simpletools library
    
    void blinker(void *par);                      // Forward declare blinker function
    
    typedef struct blink_st {                     // blink_st structure
      int pinLed;
      int timeLed;
    } blink_t;                                    // ...assigned blink_t data type
    
    int cog26, cog27;                             // Cog variables
    int stack26[43 + 20];                         // Cog stack arrays
    int stack27[43 + 20];
    
    int main()                                    // Main function
    {
      blink_t p26 = {26, 100};                    // Create two structure instances
      blink_t p27 = {27, 123};
      
      // Launch different cogs with behaviors defined by different data 
      // structures
      cog26 = cogstart(&blinker, &p26, stack26, sizeof stack26);
      cog27 = cogstart(&blinker, &p27, stack27, sizeof stack27);
      
      pause(3000);                                // Watch for 3 s
      
      while(input(27) == 1);                      // Finish high signal
      p27.timeLed = p27.timeLed / 2;              // Double one rate
      
      pause(3000);                                // for 3 more s
      
      cogstop(cog27);                             // Stop cogs
      cogstop(cog26);
    }
    
    
    void blinker(void *par)                       // Function runs in other cog
    {                                             // Receives struct address through par
      blink_t *bp = (blink_t *) par;              // Cast address as pointer to blink_t structure
      while(1)                                    // Cog's main loop
      {
        high(bp->pinLed);                         // LED on using stuct pointer
        pause(bp->timeLed);                       // Delay with struct pointer
        low(bp->pinLed);                          // Etc...
        pause(bp->timeLed);
      }
    }  
    
  • p02osmajp02osmaj Posts: 14
    edited 2014-06-18 02:58
    Thank you!
    I have solved one error, but I can't with this. I copy only the important part of code
    struct parametros_posicionamiento { /*se definen los parametros que seran necesarios para el posicionamiento
    de los servos en una estructura*/
    int numero_eje;
    int puesta_cero_t_on;
    int puesta_cero_t_off;
    };
    .
    .
    .
    int main(void)
    {

    struct parametros_posicionamiento pos_eje_1; //Variables estructura que contienen los parametros necesarios
    struct parametros_posicionamiento pos_eje_2; //para realizar la puesta a cero de los ejes
    struct parametros_posicionamiento pos_eje_3;
    .
    .
    .

    case 3: /*estado 3: se ha confirmado encendido de servos y se van a posicionar en referencia*/


    cogstart(&funcion_posicionamiento, &pos_eje_1, stack2, stacksize);
    cogstart(&funcion_posicionamiento, &pos_eje_2, stack3, stacksize);
    cogstart(&funcion_posicionamiento, &pos_eje_3, stack4, stacksize);
    .
    .
    .
    }
    .
    .
    .

    void funcion_posicionamiento (void *par)
    {


    parametros_posicionamiento *eje = (parametros_posicionamiento *) par ;
    int aux=0;
    while (aux != 7 ) { // 7 = 00000111
    pulse_out(eje.numero_eje, eje.puesta_cero_t_on);
    pause(eje.puesta_cero_t_off);
    aux = byte_expansor[eje.numero_eje-1] && 7; //porque empieza en 0
    //compruebe tiempo y genere alarma
    }
    pos_ok++;
    }
    the error is exactly this:
    struct parametros_pulsos generar_pulsos = (struct parametros_pulsos)arg;
  • edited 2014-06-19 11:50
    It's a lot simpler if you type-define your structure. Also, using structType.structElement will not work with the pointer in the function that runs in another cog. You have to use structType->structElement.

    Here is your code, adjusted to compile:
    #include "simpletools.h"
    
    int stack2[43 + 40];
    int stack3[43 + 40];
    int stack4[43 + 40];
    
    typedef struct parametros_posicionamiento { /*se definen los parametros que seran necesarios para el posicionamiento
    de los servos en una estructura*/
    int numero_eje;
    int puesta_cero_t_on;
    int puesta_cero_t_off;
    } par_pos;
    
    void funcion_posicionamiento(void *par);
    
    int main(void)
    {
      par_pos pos_eje_1; //Variables estructura que contienen los parametros necesarios
      par_pos pos_eje_2; //para realizar la puesta a cero de los ejes
      par_pos pos_eje_3;
      
      cogstart(&funcion_posicionamiento, &pos_eje_1, stack2, sizeof stack2);
      cogstart(&funcion_posicionamiento, &pos_eje_2, stack3, sizeof stack3);
      cogstart(&funcion_posicionamiento, &pos_eje_3, stack4, sizeof stack4);
    }
    
    void funcion_posicionamiento(void *par)
    {
      par_pos *eje = (par_pos *) par ;
      int aux=0;
      while (aux != 7 ) 
      { // 7 = 00000111
        pulse_out(eje->numero_eje, eje->puesta_cero_t_on);
        pause(eje->puesta_cero_t_off);
        //aux = byte_expansor[eje->numero_eje-1] && 7; //porque empieza en 0
        //compruebe tiempo y genere alarma
      }  
      //pos_ok++;
    }
    

    P.S. I cannot vouch for runtime errors, but it appears to be free of compiler errors.
  • p02osmajp02osmaj Posts: 14
    edited 2014-06-19 16:08
    Yes that's exactly right, I was working and I could solve it, but thank you!

    I have a different error, that appear me in this type functions declarations instructions:
    int delta_calcAngleYZ(float x0, float yo, float z0, float &theta) {
    .............
    I don't know if it's because the "&" or what else...?

    the part of code of this is the following:
    int delta_calcAngleYZ(float x0, float yo, float z0, float theta) { float y1 = -0.5 * 0.57735 * f; // f/2 * tg 30
    yo -= 0.5 * 0.57735 * e; // shift center to edge
    // z = a + b*y
    float a = (x0*x0 + yo*yo + z0*z0 +rf*rf - re*re - y1*y1)/(2*z0);
    float b = (y1-yo)/z0;
    // discriminant
    float d = -(a+b*y1)*(a+b*y1)+rf*(b*b*rf+rf);
    if (d < 0) return -1; // non-existing point
    float yj = (y1 - a*b - sqrt(d))/(b*b + 1); // choosing outer point
    float zj = a + b*yj;
    theta = 180.0*atan(-zj/(y1 - yj))/pi + ((yj>y1)?180.0:0.0);
    return 0;
    }


    // inverse kinematics: (x0, yo, z0) -> (theta1, theta2, theta3)
    // returned status: 0=OK, -1=non-existing position

    int delta_calcInverse(float x0, float yo, float z0, float &theta1, float &theta2, float &theta3) {
    theta1 = theta2 = theta3 = 0;
    int status = delta_calcAngleYZ(x0, yo, z0, theta1);
    if (status == 0) status = delta_calcAngleYZ(x0*cos120 + yo*sin120, yo*cos120-x0*sin120, z0, theta2);
    // rotate coords to +120 deg
    if (status == 0) status = delta_calcAngleYZ(x0*cos120 - yo*sin120, yo*cos120+x0*sin120, z0, theta3);
    // rotate coords to -120 deg
    return status;
    }

    }

    Thank you Andy ;)
  • edited 2014-06-19 16:17
    If you are passing a pointer to theta, it should be float *theta. If you are passing the value to theta, it should be float theta.
  • p02osmajp02osmaj Posts: 14
    edited 2014-06-20 09:48
    Thank you Andy. I have tried that you said but doesn't work.
    I don't know if you understood that I want.
    I have write a code more clear and clean with the same idea.

    This code does work in other compiler but doesn't work with the SimpleIde Compiler
    I have tried with "*" too
    #include "simpletools.h" // Include simple tools


    int delta_calcAngleYZ(float x0, float y0, float z0, float &theta) {
    theta = x0+y0+z0;
    return 0;
    }

    int main() // Main function
    {
    float resultado;

    delta_calcAngleYZ(4.0,1.0,2.0,resultado);

    while(1) {
    printf(" %f",resultado);
    }
    return 0;
    }
  • jac_goudsmitjac_goudsmit Posts: 418
    edited 2014-06-21 09:44
    p02osmaj wrote: »
    Yes that's exactly right, I was working and I could solve it, but thank you!

    I have a different error, that appear me in this type functions declarations instructions:


    I don't know if it's because the "&" or what else...

    Note: the "&" operator is only valid in C++. Either rename your source file from something.c to something.cpp, or use the "*" operator instead of the "&" operator to make the error go away.

    ===Jac
  • jazzedjazzed Posts: 11,803
    edited 2014-06-21 10:09
    Note: the "&" operator is only valid in C++. Either rename your source file from something.c to something.cpp, or use the "*" operator instead of the "&" operator to make the error go away.

    ===Jac

    To clarify ....

    The & operator in a function signature variable declaration is not valid in C. The pointer * operator should be used instead.

    somefunction(int *var); // valid in C/C++ ... specifies the type is a pointer (address of a variable passed to function).
    somefunction(int &var); // valid only in C++ ... specifies the type is a reference (alias of a variable passed to function).

    When calling somefunction(int *var) ... the & can used by the caller to say use address of variable (pointer to variable).


    For C:

    void somefunction(int *var)
    {
    }

    int myvar = 0;
    somefunction(&myvar);


    For C++:

    void somefunction(int *var)
    {
    }

    void somefunction(int &var)
    {
    }

    int myvar = 0;
    somefunction(&myvar);
    somefunction(myvar);
  • edited 2014-06-21 21:16
    Here is one way to modify your code to make it work. In this case, instead of making the function update a memory address with a result, it returns the result value.
    #include "simpletools.h" // Include simple tools
    
    float delta_calcAngleYZ(float x0, float y0, float z0) 
    {
      float theta = x0+y0+z0;
      return theta;
    }
    
    int main() // Main function
    {
      float resultado;
    
      resultado = delta_calcAngleYZ(4.0,1.0,2.0);
    
      print(" %f",resultado);
    
      return 0;
    } 
    
  • edited 2014-06-21 21:22
    Another approach would be to pass the memory location to the function and let the function save the result at that memory location.
    #include "simpletools.h" // Include simple tools
    
    void delta_calcAngleYZ(float x0, float y0, float z0, float *theta) 
    {
      *theta = x0+y0+z0;
    }
    
    int main() // Main function
    {
      float resultado;
    
      delta_calcAngleYZ(4.0,1.0,2.0, &resultado);
    
      print(" %f",resultado);
    
      return 0;
    } 
    

    P.S. In Propeller C, you can reduce the size of a program that displays floating point values by using print instead of printf.
Sign In or Register to comment.