Try Embedded

Position a servo motor with a pulse/direction drive

Move an industrial servo motor to exact angles through its drive, and use the drive's INP and ALM outputs to know when it has arrived or failed.

Motors · Advanced

In a real product: CNC machine axis (Industrial): An industrial servo moves to exact positions and reports alarms.

A stepper moves blindly: if it misses steps, nothing notices. An industrial servo motor has an encoder, and its drive runs a closed position loop. It compares where the motor is with where it was told to be, and pushes it there. To the controller, a servo drive in position mode looks like a stepper drive: pulses and a direction. It also reports back. INP says the motor has arrived, and ALM says something is wrong.

What you'll learn

  • Pulse/direction position control and the electronic gear (pulses per revolution)
  • Following error: why the motor lags behind the pulses while it moves
  • Using the drive's servo-on (SON) input and its INP and ALM outputs

How it works

The drive takes a 24–48 V supply. U, V, W carry the motor's power. ENC is the encoder cable, the feedback that closes the loop. Without it the drive shows AL.01 and refuses to run. The control connector (CN1) is opto-isolated, wired common cathode, so a pin HIGH turns an input on:

  • SON (pin 5) turns the servo on. The display changes from rdy to run.
  • Each PUL pulse (pin 3) moves the command 1/1000 of a turn (the drive's pulses per revolution), the way DIR (pin 4) says.
  • INP and ALM are outputs: transistors that conduct between + and −. With − to GND and + on a pin with a pull-up, the pin reads LOW while INP is on (in position) or ALM is on (an alarm).

While moving, the motor trails the command by its following error: speed ÷ the loop gain. After the last pulse it closes the gap, and INP turns on once it is within 10 encoder counts. The sketch times that settling. If the error ever passes 3 turns, the drive alarms with AL.02. Turning SON off and on clears an alarm.

Try this

  • Type an angle such as -45 or 720 in the Serial Monitor and watch the drive's display and the motor.
  • Unplug the ENC wire and restart: read the alarm on pin 8, and print it.
  • Set the drive to 10000 pulses a turn. The motor turns much more slowly for the same pulse rate. Why?

sketch.ino

// A servo motor on a pulse/direction servo drive (1000 pulses a turn), wired common cathode: PUL+ pin 3, DIR+ pin 4,
// SON+ pin 5, INP+ pin 7 and ALM+ pin 8 (the outputs pull them to GND), and all the - terminals to GND.
// It visits a list of angles; type an angle in the Serial Monitor to go there instead.
#include <AccelStepper.h>

const int PUL = 3, DIR = 4, SON = 5, INP = 7, ALM = 8;
const long PULSES_PER_TURN = 1000;
const int ANGLES[] = { 90, 180, -90, 0 };

AccelStepper servo(AccelStepper::DRIVER, PUL, DIR);
int next = 0;

void goTo(long degrees) {
  Serial.print("Moving to ");
  Serial.print(degrees);
  Serial.println(" deg");
  servo.moveTo(degrees * PULSES_PER_TURN / 360);
  servo.runToPosition();                 // sends the pulses, with acceleration
  unsigned long sent = millis();
  while (digitalRead(INP) == HIGH && digitalRead(ALM) == HIGH) {}  // wait for the motor to catch up
  if (digitalRead(ALM) == LOW) return;
  Serial.print("  in position, settled ");
  Serial.print(millis() - sent);
  Serial.println(" ms after the last pulse");
}

void setup() {
  Serial.begin(115200);
  pinMode(SON, OUTPUT);
  pinMode(INP, INPUT_PULLUP);
  pinMode(ALM, INPUT_PULLUP);
  servo.setMaxSpeed(4000);     // pulses a second: 4 turns a second
  servo.setAcceleration(8000);
  digitalWrite(SON, HIGH);     // servo on
  delay(50);
  Serial.println(digitalRead(ALM) == LOW ? "Drive alarm: check the encoder cable and the supply" : "Servo on");
}

void loop() {
  if (digitalRead(ALM) == LOW) {
    Serial.println("ALARM - resetting with SON");
    digitalWrite(SON, LOW);
    delay(100);
    digitalWrite(SON, HIGH);
    delay(1000);
    return;
  }
  if (Serial.available()) {
    goTo(Serial.parseInt());
    while (Serial.available()) Serial.read();
  } else {
    goTo(ANGLES[next]);
    next = (next + 1) % 4;
    delay(700);
  }
}

Lessons behind it