LoginSignup
3
2

More than 5 years have passed since last update.

Arduino サーボ制御 LXD-227 2台連携

Posted at

単体制御:https://qiita.com/kudo453/items/6f4510aea4e484e0a644

サーボを2台連携する。(LXD-227)

サーボを向かい合わせて配置した場合の制御方法。
DSC_6153.jpg

固定する前にサーボの基準角度を決めておく。

ANGLE_Nでサーボを固定する。これは制御用の基準角度になるので実際の制御範囲の中心である必要は無い。

シリアルモニターから数値を入力して作動テスト

if (Serial.available()) {
        pulse = Serial.parseInt();
        if (pulse < ANGLE_MIN)pulse = ANGLE_MIN;
        if (pulse > ANGLE_MAX)pulse = ANGLE_MAX;
    }

IDEのシリアルモニターから数値を入力して動かせるようなっている。
干渉しないように最小値、最大値を設定する。
パソコンから制御したいならこのままシリアル通信をすればいい。

制御例


#define SERVO_PIN1 8
#define SERVO_PIN2 9

#define CYCLE 22000
#define MIN_PULSE 1
#define ANGLE_MIN 900
#define ANGLE_MAX 1800
#define ANGLE_90 1360
#define ANGLE_0 1025
#define ANGLE_180 1695

#define ANGLE_N 1300


int pulse = ANGLE_MIN;//500-2100 (500-2500)
int _pulse = ANGLE_MAX;



void ServoWrite(int min_pulse) {
    if (_pulse - pulse > min_pulse) {
        _pulse -= min_pulse;
    }
    else if (_pulse - pulse < -min_pulse) {
        _pulse += min_pulse;
    }
    else {
        _pulse = pulse;
    }
    int _pulse2 = ANGLE_N * 2 - _pulse;
    digitalWrite(SERVO_PIN1, HIGH);
    delayMicroseconds(_pulse);
    digitalWrite(SERVO_PIN1, LOW);
    delayMicroseconds(CYCLE - _pulse);

    digitalWrite(SERVO_PIN2, HIGH);
    delayMicroseconds(_pulse2);
    digitalWrite(SERVO_PIN2, LOW);
    delayMicroseconds(CYCLE - _pulse2);


}

void ServoTest(int speed) {
    for (int i = 0; i < 150; i++) {
        ServoWrite(speed);
    }

}

void ServoSetup() {
    pinMode(SERVO_PIN1, OUTPUT);
    pinMode(SERVO_PIN2, OUTPUT);

    int speed = 20;

    pulse = ANGLE_MIN;
    ServoTest(speed);
    pulse = ANGLE_MAX;
    ServoTest(speed);
    pulse = ANGLE_0;
    ServoTest(speed);
    pulse = ANGLE_180;
    ServoTest(speed);

    pulse = ANGLE_90;

}

void setup() {
    Serial.begin(115200);
    ServoSetup();
    Serial.println("START");
}


void loop() {
    if (Serial.available()) {
        pulse = Serial.parseInt();
        if (pulse < ANGLE_MIN)pulse = ANGLE_MIN;
        if (pulse > ANGLE_MAX)pulse = ANGLE_MAX;
    }

    ServoWrite(MIN_PULSE);


}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2