Skip to content

Arduino

Chris Courson edited this page Sep 20, 2020 · 10 revisions

xArm and LeArm servo controller library for Arduino.

Version 1.1

Installation

View releases for zip that can be installed through the Arduino IDE.

xArmServoController class

xArmServoController(xArmMode mode, Stream &serial_port);

The xArmServoController class requires two parameters. The first parameter, mode defines the type of arm (xArm or LeArm) the class instance will interface with. The second parameter is the serial_port definition and may be HardwareSerial or SoftwareSerial. The baud rate must be set to 9600.

For HardwareSerial Arduino API, refer to Arduino Serial Communication.

For SoftwareSerial Arduino API, refer to Arduino SoftwareSerial Library.

xArmServoController initialization

HardwareSerial initialization example

#include <xArmServoController.h>

// define servo controller
xArmServoController xarm = xArmServoController(xArm, Serial1);

void setup() {
  // initialize serial port
  Serial1.begin(9600);
  // Your setup here.
}

void loop() {
  // Your code here.
}

SoftwareSerial initialization example

#include <SoftwareSerial.h>
#include <xArmServoController.h>

// define servo controller
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
xArmServoController xarm = xArmServoController(xArm, mySerial);

void setup() {
  // initialize serial port
  mySerial.begin(9600);
  // Your setup here.
}

void loop() {
  // Your code here.
}

xArmServo struct

struct xArmServo {
	uint8_t id;
	uint16_t position;
};

xArmServo Object Initialization

Set by properties example

#include <xArmServoController.h>

// define servo controller
xArmServoController xarm = xArmServoController(xArm, Serial1);
// define xArmServo object
xArmServo servo;

void setup() {
  // initialize serial port
  Serial1.begin(9600);

  // initialize xArmServo object
  servo.servo_id = 1;    // Set by property
  servo.position = 500;  // Set by property
}

void loop() {
  // set servo 1 to position 500
  xarm.setPosition(servo);

  // loop here indefinately
  while (true) {}
}

Set by array example

#include <xArmServoController.h>

// define servo controller
xArmServoController xarm = xArmServoController(xArm, Serial1);
// define xArmServo object
xArmServo servo;

void setup() {
  // initialize serial port
  Serial1.begin(9600);

  // initialize xArmServo object
  servo = {1, 500};  // Set by array initializer
}

void loop() {
  // set servo 1 to position 500
  xarm.setPosition(servo);

  // loop here indefinately
  while (true) {}
}

An array of servos set by array initializer example

#include <xArmServoController.h>

// define servo controller
xArmServoController xarm = xArmServoController(xArm, Serial1);
// define xArmServo object
xArmServo servo;

void setup() {
  // initialize serial port
  Serial1.begin(9600);

  // initialize an array xArmServo objects
  xArmServo servos[] = {{1, 500}, // Set by array initializer
                        {2, 500},
                        {3, 500},
                        {4, 500},
                        {5, 500},
                        {6, 500}};  
}

void loop() {
  // set servo 1 to position 500
  xarm.setPosition(servos);

  // loop here indefinately
  while (true) {}
}

Commands

Command/Link xArm LeArm
setPosition ✔️ ✔️
getPosition ✔️
servoOff ✔️
actionRun ✔️ ✔️
actionStop ✔️ ✔️
actionSpeed ✔️ ✔️
actionIsRunning ✔️
serialEvent ✔️
getBatteryVoltage ✔️ ✔️
beep ✔️ ✔️

License

All assets are under the GNU GENERAL PUBLIC LICENSE and in the public domain unless otherwise noted.