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';
}
1 Answer 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);
motor_Params
,R_motor_Params
,L_motor_Params
, but you want the L and R to be variables of typemotor_Params
R_motor_Params
andL_motor_Params
have slightly different values. I want to pass them toSetParam
. I figured, to do that, I need to have an identical struct in ODriveArduino.h so thatSetParam
knows what to expect. Obviously that didn't work. I'm not sure what I'm missing.motor_params L_motor_params;
creates a variable of typemotor_params1
namedL_motor_params