简体   繁体   中英

c++ class inheritance issue

I am having some troubles with c++ inheritance and I cannot find the error. I have class that implements a simple unicycle kinematic model.

namespace kinematics{

class Unicycle{
    // state variables
    geometry_msgs::Pose2D _state;   ///< position of the robot

    // robot parameters
    float _max_linear_vel;          ///< maximum linear velocity, saturation
    float _b;                       ///< output point offset

    // integration variables
    float _int_step;                ///< integration step

protected:
    void _updateOdometry(float dv, float tr);
    void _ioLinearization(float vx, float vy, double &d, double &t);

public:
    Unicycle();
    Unicycle(float is, float b, float vmax, geometry_msgs::Pose2D initS);
};

}

Since a differential drive kinematic model is an extension to the unicycle I would like to implement using inheritance.

#include "unicycle/Unicycle.h"

class DiffDrive: public kinematics::Unicycle {
    //robot parameters
    float _wheel_radius;        ///< wheel radius
    float _wheel_separation;    ///< distance between wheels

    void _wheelSpeed(float dv, float tr, float &rs, float &ls);

public:
    DiffDrive();
    DiffDrive(float wr, float ws, geometry_msgs::Pose2D initS,
        float ts, float vmax, float b);
};

I wrote the constructor in this way

DiffDrive::DiffDrive(float wr,
                     float ws,
                     geometry_msgs::Pose2D initS,
                     float ts,
                     float vmax,
                     float b)
    :Unicycle(ts,b,vmax,initS), _wheel_radius{wr}, _wheel_separation{ws}{
}

However when I use the function _wheelSpeed()

void DiffDrive::_wheelSpeed(float dv, float tr, float &rs, float &ls){
    ls = dv - _wheel_separation*tr/2.f;
    ls = ls/_wheel_radius;

    rs = _wheel_separation*tr/_wheel_radius + ls;
    std::cout << "PARS: " << _wheel_separation << " - " << _wheel_radius << std::endl;

    std::cout << "WHEELS: " << rs << " - " << ls << std::endl;
}

The value of _wheel_separation and _wheel_radius are different from what expected:

PARS: -179014 - 4.58631e-41

Even if the constructor is invoked with the following values:

wheel_radius = 0.02;
wheel_sep = 0.04;
_diff_drive = new DiffDrive(wheel_radius,wheel_sep,update_period,vel_max,offset);

Please help me understand what's wrong with my code.

Check out the differences between inheritance and composition.
Think of it this way: Is a differential drive a unicycle or is a differential drive a part of a unicycle? Doesn't inheritance make more sense in this way:

  • Cycle is a base class

  • Unicycle is a derived class

  • Tricycle is another derived class.

Differential drive is something that is a part of a unicycle.

I think if the design is correct, the rest will fall in place automatically. The values you are getting look like either uninitialised values or values of an object that was already destroyed (again uninitialised values.)

I figured out what was wrong. I remember that in Java it was allowed to invoke a constructor from another constructor so to hide some parameters and avoid to rewrite the entire initialization. For example:

constructor1(par 1, par2){
  par3 = 0;
  constructor2(par1,par2,par3);
}

I am not sure that this is correct, but that is what I thought. Therefore I implemented my constructor in the following way

DiffDrive::DiffDrive(const float wr,
                     const float ws,
                     float ts,
                     float vmax,
                     float b):Unicycle(ts,b,vmax){
    geometry_msgs::Pose2d initS;
    initS.x = 0;
    initS.y = 0;
    initS.theta = 0;

    DiffDrive(wr,ws,initS,ts,vmax,b);

}

Unfortunately this is not legal in C++, that returned the result of the instantiation of the outer constructor ignoring the inner one. I apologize if I didn't put this constructor in the previous question, but I simply forgot about it. I was my mistake. thanks for the help and sorry for wasting your time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM