Skip to content
This repository has been archived by the owner on Apr 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from allgood38/master
Browse files Browse the repository at this point in the history
Working Pololu Library
  • Loading branch information
QMAST committed Feb 9, 2013
2 parents 240f5c1 + 13eb867 commit fab8729
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
24 changes: 24 additions & 0 deletions PololuServo/doc.md
Original file line number Original file line Diff line number Diff line change
@@ -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);
}
```
59 changes: 59 additions & 0 deletions PololuServo/pololu_servo.cpp
Original file line number Original file line Diff line number Diff line change
@@ -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;
}
50 changes: 48 additions & 2 deletions PololuServo/pololu_servo.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,11 +16,57 @@
* Copyright (C) Stephen Cripps, 2013 * 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> #include <Arduino.h>


class PololuMSC { class PololuMSC {
private: 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; Stream* serialCom;
int reset_pin;

Stream* serialDebug;
int DEBUG;

public: 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.