c++ - 与我自己的 Arduino 库相关的错误,用于从编码器读取值
问题描述
这是我的程序:
#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 实现:
#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
我收到了我无法理解的错误:
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 部分应该没问题,因为它是从外部提供的,并且之前已经过测试并且证明可以正常工作。所以我想问题一定出在我对编码器库的实现上。如果有任何帮助,我将不胜感激。
解决方案
您已经声明了默认构造函数,Encoders
但实际上并未在任何地方定义它。要么自己定义它,要么删除声明,让编译器为你做。
请参阅定义和声明之间的区别。
您也可以参考Paul T.的评论。
推荐阅读
- string - 字符串方法,如 replace 在 %{ } ognl 标记内的 JSP 中不起作用
- javascript - 如何从 firebase 实时数据库中获取特定文档的数据?
- r - 确认直方图中低于 X 值的数据点
- java - 通过 FormPanel POST 时如何发送附加参数?
- perl - Xpath 获取元素名称
- android - Android recyclerView 滚动不流畅,有很多视图要绑定在旧设备上
- android - 是否可以使用 android 读取 aws-ssm 参数存储值?
- c# - 共享部分视图始终使用相同的控制器,即使它是通过另一个控制器的视图呈现的
- python - 如何从 FilteredSelectMultiple 小部件接收用户输入?
- powershell - 使用带有 PowershellGet 和 Azure AD SAML 身份验证的本地 Artifactory PowerShell 库