Permalink
Browse files

Merge pull request #1 from allgood38/master

Working Pololu Library
  • Loading branch information...
2 parents 240f5c1 + 13eb867 commit fab8729033330b69d2ab26f7ec2ebc6c6ac34e25 @QMAST committed Feb 9, 2013
Showing with 131 additions and 2 deletions.
  1. +24 −0 PololuServo/doc.md
  2. +59 −0 PololuServo/pololu_servo.cpp
  3. +48 −2 PololuServo/pololu_servo.h
View
@@ -0,0 +1,24 @@
+To use the Pololu Micro Servo Controller library.
+
+ ```c
+ #include <pololu_servo.h>
+
+ static const int pololu_reset_pin = 3;
+ static const int MOTOR_ONE = 7;
+
+ PololuMSC* pololu;
+
+ void setup() {
+ Serial1.begin(9600);
+ pinMode(pololu_reset_pin, OUTPUT);
+ pololu = new PololuMSC(&Serial1, pololu_reset_pin);
+ pololu->restart();
+ }
+
+ void loop() {
+ pololu->setPosition(MOTOR_ONE, 250);
+ delay(500);
+ pololu->setPosition(MOTOR_ONE, 0);
+ delay(500);
+ }
+ ```
@@ -0,0 +1,59 @@
+#include "pololu_servo.h"
+
+PololuMSC::PololuMSC(Stream* serialIn, int reset_pin) {
+ for( int i = 0; i < this->SERVO_SLOTS; i++ ) {
+ this->available_servos[i].current_value = SERVO_DEFAULT;
+ this->available_servos[i].is_attached = false;
+ }
+
+ this->DEBUG = false;
+
+ this->serialCom = serialIn;
+ this->reset_pin = reset_pin;
+}
+
+int PololuMSC::restart() {
+ // reset the pololu
+ digitalWrite(this->reset_pin, HIGH);
+ delay(this->RESET_HIGH_WAIT);
+ digitalWrite(this->reset_pin, LOW);
+ delay(this->RESET_LOW_WAIT);
+
+ // send initialization byte
+ this->serialCom->write(0xFF);
+
+ return 0;
+}
+int PololuMSC::setPosition(int motorID, int newValue) {
+ if ( newValue > 254 || newValue < 0
+ || motorID < 0 || motorID > this->SERVO_SLOTS - 1 ) {
+ if ( this->DEBUG ) {
+ this->serialDebug->println("Invalid setPosition Values");
+ }
+ return -1;
+ }
+ this->available_servos[motorID].current_value = newValue;
+ this->available_servos[motorID].is_attached = true;
+
+ this->serialCom->write(0xFF);
+ this->serialCom->write(motorID);
+ this->serialCom->write(newValue);
+
+ return 0;
+}
+
+int PololuMSC::debugSet(Stream* newSerial) {
+ if ( newSerial == NULL ) {
+ return -1;
+ }
+ this->serialDebug = newSerial;
+ this->DEBUG = true;
+
+ return 0;
+}
+
+int PololuMSC::debugOff() {
+ this->DEBUG = false;
+ this->serialDebug = NULL;
+ return 0;
+}
View
@@ -16,11 +16,57 @@
* Copyright (C) Stephen Cripps, 2013
*/
+/* Pololu Micro Servo controller class
+ *
+ * Basically the servo controller only needs to have serial commands sent
+ * to it, in accord with the mode of operation you choose. In this case,
+ * the library is using the Mini SSC II commands as opposed to the Pololu
+ * specific commands, which you can read about in the documentation
+ * posted on the wiki.
+ */
+
+#ifndef pololu_servo_h
+#define pololu_servo_h
+
#include <Arduino.h>
class PololuMSC {
private:
+ static const int RESET_HIGH_WAIT = 100;
+ static const int RESET_LOW_WAIT = 1000;
+ static const int SERVO_SLOTS = 8;
+ static const int SERVO_DEFAULT = 127;
+
+ struct servo {
+ int current_value;
+ bool is_attached;
+ };
+ struct servo available_servos[SERVO_SLOTS];
+
+ // To check for a high current situation
+ // No hardware to implement this as of yet
+ int power_supply_current;
+
Stream* serialCom;
+ int reset_pin;
+
+ Stream* serialDebug;
+ int DEBUG;
+
public:
- PololuMSC(Stream*);
-}
+ /** One instance of an initialised serial port and the pin for
+ * the reset on the Pololu
+ */
+ PololuMSC(Stream*, int);
+
+ /** This device has no error reporting, if behaviour becomes
+ * suspect, restart device.
+ */
+ int restart();
+ int setPosition(int motorID, int newValue);
+
+ int debugSet(Stream*);
+ int debugOff();
+};
+
+#endif

0 comments on commit fab8729

Please sign in to comment.