我创建了一个名为“rudder.cpp”的类及其“rudder.h” 我也有Arduino主代码。
1)Arduino主代码:调用方向舵类并移动伺服。
2)方向舵类:将伺服从0移动到180.(从扫描码http://arduino.cc/en/Tutorial/Sweep复制粘贴)
3)方向舵头:这包含rudder.cpp中的所有定义
我的问题是从 Rudder类调用move方法时,这只是扫描中的复制粘贴代码,我已确保 它在使用前正常工作
我注意到有一种不良行为正在发生。
1)伺服变热 2)伺服正在摇晃而不像扫描那样平稳移动
代码快照:
Arduino主文件
#include <Servo.h>
Rudder rudder = Rudder();
void setup()
{
}
void loop()
{
rudder.moveRudder();
}
Rudder.cpp文件:
#include "Rudder.h"
#include <Rudder.h>
#include <Servo.h>
Servo myServo;
Rudder::Rudder()
{
myServo.attach(9);
}
// Sweep code!
//
void Rudder::moveRudder()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Rudder.h
#ifndef Rudder_h
#define Rudder_h
#include "Arduino.h"
#include <Servo.h>
class Rudder
{
Public:
Rudder();
moveRudder();
private:
Servo myServo;
};
#endif
答案 0 :(得分:0)
问题解决了!有线!我必须附上myServo.attach(9);每次调用moveRudder()
时所以新代码是:
void Rudder::moveRudder()
{
myServo.attach(9); //Here the solution
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}