0

Sorry if this question been posted but I couldn't find the one that matches my issue. I couldn't pass my struct values from the main file to a class. Any ideas? I got this:

error: no matching function for call to 'ODriveArduino::SetParam(R_motor_params&)'

Main.cpp:

#include ODriveArduino.h
ODriveArduino odrive(Serial1);

struct L_motor_params{
 int axis=1;
 float vel_Gain= 2.5f/10000.0f ;
 float pos_Gain= 40.0f;
 float vel_Int_Gain= 0.0f/10000.0f;
};
struct R_motor_params{
 int axis=0;
 float vel_Gain= 2.75f/10000.0f ;
 float pos_Gain= 40.0f;
 float vel_Int_Gain= 0.0f/10000.0f;
};

void setup() {
  struct R_motor_params M;
  odrive.SetParam(M);
}

ODriveArduino.h:

class ODriveArduino {

private:

    struct motor_Params{
      int axis;
      float vel_Gain;
      float pos_Gain;
      float vel_Int_Gain;
    };

public:
    ODriveArduino(Stream& serial);

    // Commands
    void SetParam(struct motor_Params M);

 }

ODriveArduino.cpp:

#include "ODriveArduino.h"
template<class T> inline Print& operator <<(Print &obj,     T arg) { obj.print(arg);    return obj; }
template<>        inline Print& operator <<(Print &obj, float arg) { obj.print(arg, 4); return obj; }

ODriveArduino::ODriveArduino(Stream& serial)
    : serial_(serial) {}

void ODriveArduino::SetParam(struct motor_Params M){
  Serial1 << "w axis" << M.axis << ".motor.config.pos_gain " << M.pos_Gain << '\n';
  Serial1 << "w axis" << M.axis << ".motor.config.vel_gain " << M.vel_Gain << '\n';
  Serial1 << "w axis" << M.axis << ".motor.config.vel_integrator_gain " << M.vel_Int_Gain << '\n';
}
3
  • you have 3 struct types motor_Params, R_motor_Params, L_motor_Params, but you want the L and R to be variables of type motor_Params – Juraj Apr 2 '20 at 4:44
  • @Juraj R_motor_Params and L_motor_Params have slightly different values. I want to pass them to SetParam. I figured, to do that, I need to have an identical struct in ODriveArduino.h so that SetParam knows what to expect. Obviously that didn't work. I'm not sure what I'm missing. – zaraku27 Apr 2 '20 at 5:55
  • motor_params L_motor_params; creates a variable of type motor_params1 named L_motor_params – Juraj Apr 2 '20 at 7:20
1

It looks like you're trying to implement polymorphism without implementing polymorphism.

What you have done is create three completely independent structures and assumed that they will all work together just because they are the same size, with similar names, and fields named the same. That's not the case. They are all completely separate.

Instead you want just one structure but to use it multiple times to create different variables:

// from ODriveArduino.h, but move *outside* the class
struct motor_Params{
      int axis;
      float vel_Gain;
      float pos_Gain;
      float vel_Int_Gain;
};

// in your code
struct motor_Params left_params = { 
    1, 2.5f/10000.0f, 40.0f, 0.0f/10000.0f
};

struct motor_Params right_params = {
    0, 2.75f/10000.0f, 40.0f, 0.0f/10000.0f
};

// later
  odrive.SetParam(left_params);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.