Question

I am attempting to spin a Stepper motor with a rabbit 4000 processor. I have a queue that will hold structs, and have created a function to create the sequence of binary bits need to run the 4 winding stepper motor. I am trying to pass the sequence from the function in side a struct to a producer that will fill the queue with it. The issue is I'm not getting the expected value put in the queue. Am i misunderstanding how to return a struct from a function, or do i need to assign the array values individually? The relevant chunks of code are:

typedef struct {
    char d[4];
    int  delayUs;
} mQueueEntry_t;


mQueueEntry_t setDirStep(int count0, int count1, int count2, int count3){

 mQueueEntry_t entry;

        entry.d[0] = (!((count0+1)%4));                 //1a
        entry.d[1] = (!((count1+1)%4));                 //1b
        entry.d[2] = (!((count2+1)%4));                 //2a
        entry.d[3] = (!((count3+1)%4));                 //2b

        entry.delayUs =10;
        printf("Breaking Funct with: %d,%d,%d,%d\n",entry.d[0], entry.d[1], entry.d[2], entry.d[3]);
}

// continually stuff a queue with a dedicated structure
void Producer(void* pdata){
    mQueueEntry_t entry;

    int count0 = 3;
    int count1=1;
    int count2=2;
    int count3=0;

    labQueue_t * q = (labQueue_t *) pdata;  // Note use of task data..
    printf("Hola Producer\n");
    while (1)
    {
        entry = setDirStep(count0, count1, count2, count3);
        printf("Values after funct call: %d,%d,%d,%d\n",entry.d[0], entry.d[1], entry.d[2], entry.d[3]);

        count0++;
        count1++;
        count2++;
        count3++;

        // send a copy of the element and
        switch ( labQueuePut( q, &entry) ){
        case LABQ_OK:
            printf("put %d,%d,%d,%d\n",entry.d[0], entry.d[1], entry.d[2], entry.d[3]);
            break;
        case LABQ_TIMEOUT:
            OSTimeDly(OS_TICKS_PER_SEC/10);
            break;
        case LABQ_FULL:
            OSTimeDly(OS_TICKS_PER_SEC/10);
            break;
        default:
            assert(0);
            break;
        }
        OSTimeDly(1);
      }

}

Screen output for program:

Hola Consumer
Hola Producer
Breaking Funct with: 1,0,0,0
Values after funct call: 235,0,24,23
received (1a= 0: 1b= 1: 2a= 0:2b= 0) 0
put 235,0,24,23
Breaking Funct with: 0,0,1,0
Values after funct call: 236,41,237,0
received (1a= 0: 1b= 0: 2a= 0:2b= 1) 1
put 236,41,237,0

My issue is with the values from breaking function to Values after funct call are expected to be the same.

Was it helpful?

Solution

Your setDirStep() is not returning a value, yet, you declared it as returning mQueueEntry_t. The values you read on this assignment will be invalid:

entry = setDirStep(count0, count1, count2, count3);

This should show a compiler warning, I'm surprised it doesn't.

To fix the error, add the necessary return statement in setDirStep():

return entry;

In the future, always make sure to compile with warnings turned on. If you're using gcc, all you need is -Wall.

The rest of the code looks ok

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top