0

I'm trying to use Arduino to control a robot base with 3 motors on it. I have a class to control each class individuality called SimpleMotor, and want a class to control the 3 SimpleMotors called Driver.

I'd like to be able to initialize 3 SimpleMotors, then initialize a Driver object with the motor objects. The following is my code and the error messages: Code: class SimpleMotor { public: int motorSpeed = 127; //value from 0-255 int hallPin; int enablePin; int in1Pin; int in2Pin;

    SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin) {        
      enablePin = ePin;
      in1Pin = d1Pin;
      in2Pin = d2Pin;
    }

    void SimpleMotor::Direction(bool dir) { //true is cw, false is ccw
      if (dir) {
        digitalWrite(in1Pin, HIGH);
        digitalWrite(in2Pin, LOW);
      }
      else {
        digitalWrite(in1Pin, LOW);
        digitalWrite(in2Pin, HIGH);
      }
    }
    
    void SimpleMotor::WriteSpeed(int motorPower){
      if(motorPower < 0)  {Direction(0);}
      else  {Direction(1);}

      analogWrite(enablePin, motorPower);
    }

    void SimpleMotor::Stop(){
      analogWrite(enablePin, 0);
    }
};

class Driver{
  friend class SimpleMotor;
  public:
    SimpleMotor m1;
    SimpleMotor m2;
    SimpleMotor m3;    

    Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3) {
      m1 = motor1;
      m2 = motor2;
      m3 = motor3;
    }

    void Driver::Spin(int stickX){
      if(stickX<0){
        m1.Direction(0);
        m2.Direction(0);
        m3.Direction(0);
      }
      else{
        m1.Direction(1);
        m2.Direction(1);
        m3.Direction(1);
      }

      m1.WriteSpeed((int) floor(stickX/4));
      m2.WriteSpeed((int) floor(stickX/4));
      m3.WriteSpeed((int) floor(stickX/4));

    }

};

Errors:

HolonomicDriver:46:80: error: no matching function for call to 'SimpleMotor::SimpleMotor()'

     Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3) {

                                                                                ^

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate: SimpleMotor::SimpleMotor(int, int, int)

     SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin) {

     ^~~~~~~~~~~

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note:   candidate expects 3 arguments, 0 provided

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(const SimpleMotor&)

 class SimpleMotor {

       ^~~~~~~~~~~

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note:   candidate expects 1 argument, 0 provided

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(SimpleMotor&&)

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note:   candidate expects 1 argument, 0 provided

HolonomicDriver:46:80: error: no matching function for call to 'SimpleMotor::SimpleMotor()'

     Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3) {

                                                                                ^

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate: SimpleMotor::SimpleMotor(int, int, int)

     SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin) {

     ^~~~~~~~~~~

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note:   candidate expects 3 arguments, 0 provided

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(const SimpleMotor&)

 class SimpleMotor {

       ^~~~~~~~~~~

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note:   candidate expects 1 argument, 0 provided

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(SimpleMotor&&)

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note:   candidate expects 1 argument, 0 provided

HolonomicDriver:46:80: error: no matching function for call to 'SimpleMotor::SimpleMotor()'

     Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3) {

                                                                                ^

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate: SimpleMotor::SimpleMotor(int, int, int)

     SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin) {

     ^~~~~~~~~~~

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note:   candidate expects 3 arguments, 0 provided

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(const SimpleMotor&)

 class SimpleMotor {

       ^~~~~~~~~~~

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note:   candidate expects 1 argument, 0 provided

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(SimpleMotor&&)

c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note:   candidate expects 1 argument, 0 provided

exit status 1

Thanks!

2
  • The short answer is yes. You're experiencing a different issue, likely just a syntactical error. The first error, for example, tells you that SimpleMotor has no default constructor. In fact, that's what all three errors are about. Use the initialization section when implementing constructors. Commented Jun 28, 2020 at 2:23
  • It also seems that you don't need to use friend here if the methods of SimpleMotor are public. Commented Jun 28, 2020 at 3:42

2 Answers 2

0

As @sweenish has already mentioned, you need to fix the errors related to the constructor. You have 2 ways:

1.Add a default constructor:

SimpleMotor::SimpleMotor() = default;

which is a shorter equivalent of

SimpleMotor::SimpleMotor() : enablePin{0}, in1Pin{0}, in2Pin{0}
{
}

This approach has a drawback of redundant construction + assignment, and you will also have 2 copies of each motors, where each can control your robot, one in main function and one in your Driver object:

// in main
SimpleMotor motor1(1,2,3); // can control your motor
Driver driver(motor1, ...);
// in Driver class
SimpleMotor m1; // here we create m1{0, 0, 0} with default constructor
// in Driver constructor
m1 = motor1; // assign motor1 to m1. m1 can also control your motor

2.Use pointers (or references) in Driver:

class Driver {
public:
    std::shared_ptr<SimpleMotor> m1;
    std::shared_ptr<SimpleMotor> m2;
    std::shared_ptr<SimpleMotor> m3;    
        
    Driver::Driver(std::shared_ptr<SimpleMotor> motor1, std::shared_ptr<SimpleMotor> motor2, std::shared_ptr<SimpleMotor> motor3) {
        m1 = motor1;
        m2 = motor2;
        m3 = motor3;
    }
};

Then you will also need to change the way you create SimpleMotors, for example:

auto m1 = std::make_shared<SimpleMotor>(1, 2, 3);
auto m2 = std::make_shared<SimpleMotor>(5, 6, 7);
auto m3 = std::make_shared<SimpleMotor>(4, 8, 9);

Driver d(m1, m2, m3);

Of course, you could use raw pointers or uniques_ptr instead. You may find more info on shared_ptr in the here: https://en.cppreference.com/w/cpp/memory/shared_ptr

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks everyone for your help!

Adding:

SimpleMotor::SimpleMotor() = default;

in my SimpleMotor class did work.

I really appreciate all the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.