1

我做了一个名为“rudder.cpp”的类及其“rudder.h”我也有Arduino主代码。

1)Arduino主代码:调用舵类,移动舵机。

2) 舵类:将舵机从 0 移动到 180。(从 Sweep 代码http://arduino.cc/en/Tutorial/Sweep复制粘贴)

3) rudder header:这包含 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 
  } 
}

方向舵.h

#ifndef Rudder_h
#define Rudder_h

#include "Arduino.h"
#include <Servo.h>

class Rudder
{
   Public:
   Rudder();
   moveRudder();
   private:
   Servo myServo;

};

#endif
4

1 回答 1

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 
  } 
}
于 2013-08-10T10:50:23.037 回答