5 years, 6 months ago.

Error with function pointer!

Hi, I'm doing a couple of functions. The first is "move" in which I want to move a servo and the second function is "move_leg" in which I want to move several servos to perform the movement of a leg of a hexapod (one leg has 3 servos). Compiling it gives me an error in the first call of the move switch, which says the following: "No suitable conversion function from" SoftPWM "to" SoftPWM * "exits". Could the solution include "&"? I refer to this: mueve (& pata1 [0]);

I do not know if it would be fine.

The code is the following:

SoftPWM pata1[] = {(PB_8),(PB_9),(PB_7)};

void mueve(SoftPWM pata[]){ int i; int pata_si = 560; pata[i].pulsewidth_us(pata_si); }

void move_leg(char pos){ switch(pos){ case 'A': Avanza mueve(pata1[0]); mueve(pata1[1]); mueve(pata1[2]); break; .........................................

}

1 Answer

5 years, 6 months ago.

Hello Jose,

You can try the following code:

SoftPWM pata1[] = { SoftPWM(PB_8), SoftPWM(PB_9), SoftPWM(PB_7) }; // Call the constructor for each element to create it!

void mueve(SoftPWM* softPWM)  // Takes a pointer to a SoftPWM object (e.g. pointer to an element of pata1 array)
{
    int pata_si = 560;
    softPWM->pulsewidth_us(pata_si);
}

void move_leg(char pos)
{
    switch (pos) {
        case 'A':
            mueve(&pata1[0]);  // Pass the address of the pata1 array element (i.e. pointer to a SoftPWM object)
            mueve(&pata1[1]);
            mueve(&pata1[2]);
            break;
            
            //...
}

ADVISE: To have a more readable code published here (on mbed pages) try to mark it up as below (with <<code>> and <</code>> tags, each on separate line at the begin and end of your code)

<<code>>
text of your code
<</code>>

Accepted Answer