【发布时间】:2021-08-02 05:15:29
【问题描述】:
我正在构建一个相当复杂的机械臂,因此我编写了一个具有继承的类来控制不同的伺服系统,而无需编写太多代码。这些类如下所示(有些东西被省略了):
在servoPart.h中:
#include <Servo.h>
#ifndef SERVO_PART_H
#define SERVO_PART_H
class ServoPart {
protected:
virtual void doJob() = 0;
private:
Servo servo;
public:
ServoPart(int pin, int minPWM, int maxPWM) {
servo.attach(pin, minPWM, maxPWM);
};
void setAngle(int angle) {
servo.write(angle);
};
int getPosition() {
return servo.read();
};
}
#endif
在base.h中:
#include "servoPart.h"
#ifndef BASE_H
#define BASE_H
class Base : public ServoPart {
private:
void doJob() {/* implementation */};
public:
Base(int pin, int stepsize = 5) : ServoPart(pin, 771, 2193) {
};
};
#endif
在 main.cpp 中:
#include <Arduino.h>
#include "base.h"
#define SERVO_BASE 9
Base base(SERVO_BASE);
void setup() {
Serial.begin(9600);
delay(500);
base.setAngle(80);
Serial.println(base.getPosition()); // <-- prints 80
delay(500);
base.setAngle(110);
Serial.println(base.getPosition()); // <-- prints 110
}
void loop() {
}
伺服似乎设置为 80/110,但是没有任何动作。如果我在main.cpp 中创建伺服对象并在那里使用servo.write(),伺服会移动,这意味着问题不在于伺服或连接/电路。是否有可能是我初始化基础的方式出现了故障?在设置函数之前使用Base base(SERVO_BASE)?
【问题讨论】:
-
你在哪里调用那个名为servo的私有Servo的构造函数?调用 attach 是不够的。
-
@Abel 在 Base 构造函数中:
Base(int pin, int stepsize) : ServoPart(pin, 771, 2193) { }。这不正确吗? -
你是在调用Servo伺服的构造函数时被问到的,而不是ServoPart构造函数的调用。
-
哦,感谢您的澄清!那在 ServoPart 类中,在私有部分:
Servo servo;和构造函数servo.attach(pin);。至少这就是我在 main.cpp 中使用伺服类时的做法。编辑:如果您看一下 Arduino IDE Servo 示例,他们会以同样的方式进行操作。使用Servo myservo;创建一个伺服,然后调用attach激活它
标签: c++ class inheritance arduino servo