timmaxw / HitecDServo

Arduino library to program and control Hitec D-Series servos. (Not endorsed by Hitec.)
MIT License
7 stars 3 forks source link

HitecDServo

Arduino library to program and control Hitec D-series servos. (Not endorsed by Hitec.)

Hitec D-series servos have many settings that can be programmed using Hitec's proprietary programmer. This Arduino library was developed by reverse-engineering the programmer's serial protocol. The library can program all servo settings, including direction, speed, deadband, range, soft-start, fail-safe, power-limit, overload-protection, and sensitivity. The library can also control the servo's motion, and read back the actual servo position.

Note: This library is not endorsed by Hitec, not guaranteed to work, and could potentially even damage your servo.

Note 2: As an alternative to this library, this guide describes a way of using Hitec's official GUI application to program the servo even if you don't have a DPC-11 dongle. Hitec's official GUI supports a wider range of servo models than this library does.

Pull-up resistor

The library works with the servo attached to any digital pin, even if not PWM-capable. However, you must attach a 2-kiloohm pullup resistor between the digital pin and the +5V power supply. (If you're using a 3.3V microcontroller, then instead attach a 1-kiloohm pullup resistor between the digital pin and the +3.3V power supply.)

Diagram of pullup resistor

Using as a library

To program the servo settings from an Arduino sketch, declare a HitecDServo instance and use the writeSettings() method:

#include <HitecDServo.h>

HitecDServo servo;

void setup() {
  int result;

  Serial.begin(115200);

  int servoPin = 2;
  result = servo.attach(servoPin);

  /* Always check that the return value is HITECD_OK. If not, this can indicate
  a problem communicating with the servo. */
  if (result != HITECD_OK) { printError(result); }

  /* Reduce the servo's speed to 50% and reverse the direction. */
  HitecDSettings settings;
  settings.speed = 50;
  settings.counterclockwise = true;
  result = servo.writeSettings(settings);
  if (result != HITECD_OK) { printError(result); }

  /* After writeSettings(), we must wait 1000ms for the servo to reboot. */
  delay(1000);
}

void loop() {
  /* Sweep the servo back and forth between the end points */
  servo.writeTargetMicroseconds(850);
  delay(2000);
  servo.writeTargetMicroseconds(2150);
  delay(2000);
}

void printError(int result) {
  Serial.print("Error: ");
  Serial.println(hitecdErrToString(result));
  while (1) { }
}

For full documentation, see HitecDServo.h.

Using as a programmer

The Programmer example sketch turns your Arduino into an interactive servo programmer. Upload it to the Arduino, then use the Arduino Serial Monitor at 152000 baud to interactively read/write the settings of your Hitec D-series servo that's attached to the Arduino.

Details

Supported Hitec D-series servo models

Right now, the library has only been tested with a Hitec D485HW servo. Other models might work, but it's not guaranteed.

Want to help improve support for other Hitec D-series servo models? If you run the Programmer example sketch with an unsupported servo model, it will print some diagnostic information on the Arduino Serial Monitor. Please open a GitHub issue with the diagnostic information.

Supported Arduino models

The library has been tested with an Arduino Uno (5V, 16MHz) and a Sparkfun Pro Micro (3.3V, 8MHz).

Non-AVR architectures are not supported. (The library synchronously bit-bangs the serial protocol, and this depends on instruction cycle counts.)

Serial protocol details

See src/HitecDServoInternal.h for notes on the details of the serial protocol between the Hitec DPC-11 programmer and the servo. See extras/DPC11Notes.md for some additional notes about the behavior of the DPC-11 programmer.