-1

I am busy with the firmware of a custom motorcontroller. For protyping i use the nucleo L432 with the mbed os.

The communication with the master controller is organized with the CAN bus. I would have a interupt routine when there is data received. This code work very good when placed in the main file but i would like to implement this code in a seperate class. But when i try to compile this solution i keep receiving the follow error from the compiler: Error: Nonstandard form for taking the address of a member function in "Lib/CANimp.cpp", Line: 19, Col: 18

Does anyone have a solution for this?

**CANimp.h**

#define CANIMP_H

#include "mbed.h"

class CANimp {

    public:
        CANimp(PinName can_rd, PinName can_td);

        void send(void);    

    private:
        CAN can1;
        void rcv(void);
};


#endif

**CANimp.cpp**
#include "CANimp.h"
#include "mbed.h"

void CANimp::rcv() {
    CANMessage msg1;
    can1.read(msg1);
    if(msg1.data[0] == 1)
    {
         float speed = msg1.data[1] / 255.0;
         printf("set speed to %f \n", speed);
         //Motordriver.setPWM(speed);
    }
    printf("Message received: %d  %d\n\n", msg1.data[0], msg1.data[1]);
}

CANimp::CANimp(PinName can_rd, PinName can_td) : can1(can_rd, can_td)
{
    can1.frequency(250000);
    can1.attach(&rcv, CAN::RxIrq);
}

void CANimp::send() {
    pc.printf("send()\n");
    char temp[8];
    temp[0] = 1;
    temp[1] = 255;
    temp[2] = 50;
    if (can1.write(CANMessage(1338, temp, 3))) {
        //pc.printf("wloop()\n");
        //pc.printf("Message sent: %d\n", counter);
        wait(0.02);
    } 
}
  • Try replacing `can1.attach(&rcv, CAN::RxIrq);` with `can1.attach(&CANimp::rcv, CAN::RxIrq);`. – Fureeish Mar 09 '19 at 22:28
  • Possible duplicate of [How do you pass a function of a class as a parameter to another function of the same class](https://stackoverflow.com/questions/3506234/how-do-you-pass-a-function-of-a-class-as-a-parameter-to-another-function-of-the) – Fureeish Mar 09 '19 at 22:29

2 Answers2

1

You want to pass a callback. Try this: can1.attach(callback(this, &CANimp::rcv), CAN::RxIrq);

You can find Mbed API and examples here: https://os.mbed.com/docs/mbed-os/v5.9/reference/callback.html

Kentaro Okuda
  • 1,557
  • 2
  • 12
  • 16
0

You are trying to use the address/reference operator (&) on a method:

can1.attach(&rcv, CAN::RxIrq);

Functions can have their address taken or be assigned to pointers, but not methods which actually represent an object and function. You can declare a standalone function that knows where an object is, like this:

void example() {
    CANimp::rcv()
}

Then pass example instead, since it's a function and can have it's address taken. You can also use C++ lambdas depending on how the library accepts function pointers:

can1.attach([](){
  CANimp::recv();
}, CAN::RxIrq);
Kristopher Ives
  • 5,838
  • 7
  • 42
  • 67
  • 2
    Methods do not represent "*and object and a function*". They are functions, which have implicit first argument that's a pointer to the object they are *called on*, the `this` pointer. One can reference a method. [Example here](https://godbolt.org/z/UayVim). – Fureeish Mar 09 '19 at 22:22