0

This is my program:

#include "Arduino.h"
#include "MotorDriver.h"
#include "encoders.h"

//Define the global variables to configure the motors

//Right Motor Configuration Variables
int motR_pins[3] = {4, 15, 18};     //Define the Motor Pins
int motR_sign = -1;                 //Define the motor rotation sign

//Left Motor configuration variables
int motL_pins[3] = {2, 12, 0};
int motL_sign = 1;

//Encoders
Encoders Encr;
Encoders Encl;
int signal_R=-1;
int signal_L=1;

MotorDriver Mr;
MotorDriver Ml;

//Setup
void setup()
{
  //Set up the Motors
  //Setup the Right Motor object
  Mr.SetBaseFreq(5000);                                             //PWM base frequency setup
  Mr.SetSign(motR_sign);                                            //Setup motor sign
  Mr.DriverSetup(motR_pins[0], 0, motR_pins[1], motR_pins[2]);      //Setup motor pins and channel
  Mr.MotorWrite(0);                                                 //Write 0 velocity to the motor when initialising

  //Setup the Left Motor object
  Ml.SetBaseFreq(5000);
  Ml.SetSign(motL_sign);
  Ml.DriverSetup(motL_pins[0], 1, motL_pins[1], motL_pins[2]);
  Ml.MotorWrite(0);

  //Encoder setup
  Encr.EncodersSetup(34, 36);
  Encl.EncodersSetup(35, 39);
  
  //Begin Serial Communication
  Serial.begin(9600);
}

long positionLeft  = -999;
long positionRight = -999;

//Loop
void loop()
{

  Mr.MotorWrite(-0.5);                       //Set Velocity percentage to the Motors (-1 to 1)
  Ml.MotorWrite(0.4);

  long newLeft, newRight;
  newLeft = Encl.readenc(signal_L);
  newRight = Encr.readenc(signal_R);
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    positionLeft = newLeft;
    positionRight = newRight;                                //Delay before next loop iteration
}

This is my library which is supposed to read the rpm values and change them to the linear values so that I can work on PID implementation later on:

#ifndef Encoders_h
#define Encoders_h
#include <Arduino.h>

class Encoders
{
  private:
  int PinA;
  int PinB;
  float current_time=0;
  int sample=10;
  float ticks=1632.67;
  float previous_time=0;
  float pinAStateCurrent = LOW;
  float pinAStateLast = LOW;
  float rotation = 0;
  float counter = 0;
  public:
  Encoders();
  void EncodersSetup(int A, int B)
  {
    PinA=A;
    PinB=B;
  };
  
  
  float readenc(int enc_signal)
  {
    pinMode(PinA, INPUT);
    pinMode(PinB, INPUT);
    pinAStateCurrent = digitalRead(PinA);
    if ((digitalRead(PinA)) == HIGH)
    {
        update();
    }
    else
    {
        update();
    }
    current_time=millis();
    if (current_time-previous_time > sample)
    {
      rotation = (counter*enc_signal)/ticks;
      rotation = (rotation * 1000) / (current_time-previous_time);
      previous_time=current_time;
      counter=0;
    }
    pinAStateLast = pinAStateCurrent;
    return rotation*0.1*3.1415;
  };
  void update()
  {
    if((pinAStateLast == LOW) && (pinAStateCurrent == HIGH))
    {
        if (digitalRead(PinB) == HIGH)
        {
            counter++;
        }
        else
        {
            counter--;
        }
    }
  };
};
#endif

I'm getting errors which I can't understand:

sketch\task2.ino.cpp.o:(.literal.startup._GLOBAL__sub_I_motR_pins+0x4): undefined reference to `Encoders::Encoders()'
sketch\task2.ino.cpp.o: In function `_GLOBAL__sub_I_motR_pins':
C:\Users\hubno\OneDrive\Pulpit\ESP - reports\TD3\task2/task2.ino:67: undefined reference to `Encoders::Encoders()'
C:\Users\hubno\OneDrive\Pulpit\ESP - reports\TD3\task2/task2.ino:17: undefined reference to `Encoders::Encoders()'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board ESP32 Dev Module.

I can't notice anything wrong in the lines 17 and 67. The MotorDriver part should be alright since it was provided externally and it has been tested before and it proved to be working. So I guess the problem must be with my implementation of encoder library. I would be grateful for any help.

Hubson
  • 11
  • 5
  • 2
    you have either include `Encoders.cpp` into the project or link the application with a library that contains the class. Including `"encoders.h` isn't enough. You have declaration of the class but miss its implementation. – folibis May 03 '21 at 11:27
  • C++ error messages can be a bit cryptic, in this case the default constructor `Encoders::Encoders` is not linked with your program. This could be because you never define it, or because you don't link the library in which it is defined. – asynts May 03 '21 at 11:37
  • You show a header file that *declares* your `Encoders` constructor but doesn't *define* it. It looks like you didn't include the file that defines it in your project. – lurker May 03 '21 at 11:43
  • 1
    Add a `{}` pair to the constructor: `public Encoders();` to `public Encoders() {}`. Unrelated, at the closing `}` of each function, drop the `;`. – Paul T. May 03 '21 at 12:14

1 Answers1

1

You have declared the default constructor for Encoders but not actually defined it anywhere. Either define it yourself or remove the declaration and let the compiler do it for you.
See Difference between a definition and a declaration.
You may also refer to Paul T.'s comment.

Kitswas
  • 1,134
  • 1
  • 13
  • 30