连续伺服旋转Arduino

时间:2014-04-25 03:12:00

标签: arduino

我有一个连续旋转伺服,我需要顺时针和逆时针。我发现中点位于100,而我发现最合适的顺时针和逆时针速度分别为110和85。按下按钮A时伺服器顺时针旋转,按下B时伺服器逆时针旋转。

我想要一种方法,一旦释放按钮,以指数速率减慢伺服速度,这样就可以说从110到100,或者在t(用户可定义的)秒内从85到100。有没有人知道的功能可以帮助我实现这个目标?或者这样做的方式。详细的答案赞赏,因为我是一个Arduino noob。

#include <Servo.h>

Servo myservo; // create servo object to control a servo

// CONSTANTS

// PINS
const int crServo = 12; // sets pin 12 as servo
const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
const int ledPinB = 4; // sets pin 10 as LED
const int ledPinG = 5; // sets pin 10 as LED
const int ledPinR = 6; // sets pin 10 as LED

// SERVO PROPERTIES
const int crSpeedDefault = 100; // is the stay still position, motor should not turn
const int crSpeedCW = 110; // turns the motor full speed clockwise
const int crSpeedCC = 85; // turns the motor full speed counter-clockwise

// SET BUTTON STATES
int buttonStateCW = 0; //sets button 1 as off
int buttonStateCC = 0; // sets button 2 as off

void setup()
{
  myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
  pinMode (buttonPinCW, INPUT); // sets button as input
  pinMode (buttonPinCC, INPUT); // sets button as input
  pinMode (ledPinB, OUTPUT); // sets led as output
  pinMode (ledPinG, OUTPUT); // sets led as output
  pinMode (ledPinR, OUTPUT); // sets led as output
  myservo.write(crSpeedDefault); // default servo to crSpeedDefault
  startup();
}

int startup() {
  blinker(2, ledPinB);
  blinker(1, ledPinG);
  blinker(1, ledPinR);
}

void blinker(int count, int pin) {
    for ( int x = 0; x < count; x++ )
    {
      digitalWrite(pin, HIGH);
      delay(1000);
      digitalWrite(pin, LOW);
      delay(1000);
    }
}

void loop()
{
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    digitalWrite(ledPinR, HIGH);
    myservo.write(crSpeedCW);
    // counterclockwise rotation
  } 
  else if (buttonStateCC == HIGH) {
    digitalWrite(ledPinG, HIGH);
    myservo.write(crSpeedCC);
  } 
  else {
    myservo.write(crSpeedDefault);
    digitalWrite(ledPinR, LOW); 
    digitalWrite(ledPinG, LOW);     // turn the LED off by making the voltage LOW
  }
}

更新了RMI的回答

#include <Servo.h>

Servo myservo; // create servo object to control a servo

// CONSTANTS

// PINS
const int crServo = 12; // sets pin 12 as servo
const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
const int ledPinB = 4; // sets pin 10 as LED
const int ledPinG = 5; // sets pin 10 as LED
const int ledPinR = 6; // sets pin 10 as LED

const int t = 1;  // slow down

// SERVO PROPERTIES
const int crSpeedDefault = 100; // is the stay still position, motor should not turn
const int crSpeedCW = 107; // turns the motor full speed clockwise
const int crSpeedCC = 87; // turns the motor full speed counter-clockwise

// SET BUTTON STATES
int buttonStateCW = 0; //sets button 1 as off
int buttonStateCC = 0; // sets button 2 as off

void setup()
{
  myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
  pinMode (buttonPinCW, INPUT); // sets button as input
  pinMode (buttonPinCC, INPUT); // sets button as input
  pinMode (ledPinB, OUTPUT); // sets led as output
  pinMode (ledPinG, OUTPUT); // sets led as output
  pinMode (ledPinR, OUTPUT); // sets led as output
  myservo.write(crSpeedDefault); // default servo to crSpeedDefault
  startup();
}

int startup() {
  //blinker(2, ledPinB);
  //blinker(1, ledPinG);
  //blinker(1, ledPinR);
}

void blinker(int count, int pin) {
  for (int x = 0; x < count; x++)
  {
    digitalWrite(pin, HIGH);
    delay(1000);
    digitalWrite(pin, LOW);
    delay(1000);
  }
}

void loop()
{
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    digitalWrite(ledPinR, HIGH);
    float speed = crSpeedCW;
    Serial.print("CLOCKWISE-ROTATION \n");
    for (int i = 0; i < t * 5; i++) {
      speed += ((float)crSpeedDefault - speed)/ 10;
      Serial.print(speed);
      Serial.print("\n");
      myservo.write((int)speed);
      delay(100);
    }
    myservo.write(crSpeedCW);
  } 
  else if (buttonStateCC == HIGH) {
      digitalWrite(ledPinG, HIGH);
      float speed = crSpeedCC;
      Serial.print("COUNTER-CLOCKWISE-ROTATION \n");
      for (int i = 0; i < t * 5; i++) {
        speed += ((float)crSpeedDefault - speed) / 10;
        Serial.print(speed);
        Serial.print("\n");
        myservo.write((int)speed);
        delay(100);
      }
      myservo.write(crSpeedCC);
    } 
  else {
    myservo.write(crSpeedDefault);
    digitalWrite(ledPinR, LOW); 
    digitalWrite(ledPinG, LOW);     // turn the LED off by making the voltage LOW
  }
}

1 个答案:

答案 0 :(得分:3)

试试这个。

void loop()
{
  ...
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    digitalWrite(ledPinR, HIGH);
    float speed = crSpeedCW;
    for (int i = 0; i < t * 5; i++) {
        speed -= (speed - (float)crSpeedDefault) / 10;
        myservo.write((int)speed);
        delay(200);
    }
    myservo.write(crSpeedCW);
  } 
  else if (buttonStateCC == HIGH) {
    digitalWrite(ledPinG, HIGH);
    // Do the same here in reverse
    myservo.write(crSpeedCC);
  } 
  else {
    ...
  }
}

这会以指数方式降低速度。您必须调整硬编码数字以适合您的范围。