首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与我自己的用于从编码器读取值的Arduino库相关的错误

与我自己的用于从编码器读取值的Arduino库相关的错误
EN

Stack Overflow用户
提问于 2021-05-03 18:58:19
回答 1查看 39关注 0票数 0

这是我的程序:

代码语言:javascript
复制
#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
}

这是我的库,它应该读取rpm值并将它们更改为线性值,以便我稍后可以处理PID实现:

代码语言:javascript
复制
#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

我得到了一些我不能理解的错误:

代码语言:javascript
复制
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.

我没有注意到第17行和第67行有任何错误。MotorDriver部分应该是正常的,因为它是从外部提供的,而且之前已经过测试,并且被证明是有效的。所以我猜问题一定出在我的编码库的实现上。如果有任何帮助,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2021-05-03 22:39:07

您已经声明了Encoders的默认构造函数,但实际上并没有在任何地方定义它。要么自己定义它,要么删除声明,让编译器为您做这件事。

参见Difference between a definition and a declaration

你也可以参考Paul T.的评论。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67367792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档