Hi Ben,
Thanks for your reply.
The complete working code is a bit big to post here, so I took some time to find some demos. One hardware version running on pin 1 (hardware serial) en one that uses
pin 4.
Hardware version:
/*******************************************\
Servo Controller code
\********************************************/
void setup()
{
Serial.begin(9600);
}
void loop()
{
// Use the SetPosition command to do stuff.
// Takes values between 0 and 5000
SetServo(1, 2500);
}
// Set a servo position. Returns 0 on success, -1 on error
// Uses the Pololu protocol, so the jumper has to be off of the controller
// Makes use of "Absolute Position" command
// VALID POSITION VALUES: 0-5000
int SetServo(int servo, int position)
{
byte data1 = 0;
byte data2 = 0;
// Check to make sure the servo is right
if (servo <0 || servo >8)
return -1;
// Check to make sure position is within bounds
if (position < 0 || position > 5000)
return -1;
// Controller actually takes values between 500 and 5500,
// so just add 500
position+=500;
// Calculate data bytes from position
data2 = position & B01111111;
data1 = position >> 7;
// Start Byte
Serial.print(0x80, BYTE);
// Device ID
Serial.print(0x01, BYTE);
// Command: 0x04 is set absolute position
Serial.print(0x04, BYTE);
// Servo number
Serial.print((byte)servo, BYTE);
// First data byte
Serial.print(data1, BYTE);
// Second data byte
Serial.print(data2, BYTE);
// Everything seems ok, return 0
return (0);
}
SoftSerial version:
/*******************************************\
Pololu Serial Servo Controller code
\********************************************/
#include <SoftwareSerial.h>
// Define pins you're using for serial communication
// Don't need to use pins 0 and 1
#define RXPIN 3
#define TXPIN 4
// Set up the new serial port to transmit data
// You only need to connect the TXPIN and Ground
// to the Pololu controller...RXPIN is unused
// (Pololu incapable of sending data back to arduino)
SoftwareSerial pololu(RXPIN, TXPIN);
void setup()
{
// define pin modes for tx, rx:
pinMode(RXPIN, INPUT);
pinMode(TXPIN, OUTPUT);
Serial.begin(9600);
pololu.begin(9600);
}
void loop()
{
// Use the SetPosition command to do stuff.
// Takes values between 0 and 5000
SetServo(1, 2500);
}
// Set a servo position. Returns 0 on success, -1 on error
// Uses the Pololu protocol, so the jumper has to be off of the controller
// Makes use of "Absolute Position" command
// VALID POSITION VALUES: 0-5000
int SetServo(int servo, int position)
{
byte data1 = 0;
byte data2 = 0;
// Check to make sure the servo is right
if (servo <0 || servo >8)
return -1;
// Check to make sure position is within bounds
if (position < 0 || position > 5000)
return -1;
// Controller actually takes values between 500 and 5500,
// so just add 500
position+=500;
// Calculate data bytes from position
data2 = position & B01111111;
data1 = position >> 7;
// Start Byte
pololu.print(0x80, BYTE);
// Device ID
pololu.print(0x01, BYTE);
// Command: 0x04 is set absolute position
pololu.print(0x04, BYTE);
// Servo number
pololu.print((byte)servo, BYTE);
// First data byte
pololu.print(data1, BYTE);
// Second data byte
pololu.print(data2, BYTE);
// Everything seems ok, return 0
return (0);
}
I’ve tested both version using an oscilloscope monitoring the Tx pin, and the output pattern looks the same. The odd thing is
that with the hardware version the pololu servo controller board lights up a green LED but with the
SoftSerial version the orange LED lights up and the RED LED blinks, no servo is moving.
Arjan