diff --git a/src/adafruitms1438/adafruitms1438.cxx b/src/adafruitms1438/adafruitms1438.cxx index 54352191..90012dbc 100644 --- a/src/adafruitms1438/adafruitms1438.cxx +++ b/src/adafruitms1438/adafruitms1438.cxx @@ -35,6 +35,21 @@ using namespace std; AdafruitMS1438::AdafruitMS1438(int bus, uint8_t address) : m_pca9685(new PCA9685(bus, address)) +{ + initAdafruitMS1438(); +} + +AdafruitMS1438::AdafruitMS1438(std::string initStr) : m_pca9685(new PCA9685(initStr)) +{ + initAdafruitMS1438(); +} + +AdafruitMS1438::~AdafruitMS1438() +{ + delete m_pca9685; +} + +void AdafruitMS1438::initAdafruitMS1438() { setupPinMaps(); @@ -56,11 +71,6 @@ AdafruitMS1438::AdafruitMS1438(int bus, uint8_t address) : stepConfig(STEPMOTOR_M34, 200); } -AdafruitMS1438::~AdafruitMS1438() -{ - delete m_pca9685; -} - void AdafruitMS1438::initClock(STEPMOTORS_T motor) { gettimeofday(&m_stepConfig[motor].startTime, NULL); @@ -146,12 +156,12 @@ void AdafruitMS1438::setMotorSpeed(DCMOTORS_T motor, int speed) { if (speed < 0) speed = 0; - + if (speed > 100) speed = 100; float percent = float(speed) / 100.0; - + // make sure that the FullOn bit is turned off, or the speed setting // (PWM duty cycle) won't have any effect. m_pca9685->ledFullOn(m_dcMotors[motor].pwm, false); @@ -162,7 +172,7 @@ void AdafruitMS1438::setMotorSpeed(DCMOTORS_T motor, int speed) void AdafruitMS1438::setStepperSpeed(STEPMOTORS_T motor, int speed) { - m_stepConfig[motor].stepDelay = 60 * 1000 / + m_stepConfig[motor].stepDelay = 60 * 1000 / m_stepConfig[motor].stepsPerRev / speed; } @@ -291,14 +301,14 @@ void AdafruitMS1438::stepperSteps(STEPMOTORS_T motor, unsigned int steps) if (m_stepConfig[motor].stepDirection == 1) { - if (m_stepConfig[motor].currentStep >= + if (m_stepConfig[motor].currentStep >= m_stepConfig[motor].stepsPerRev) m_stepConfig[motor].currentStep = 0; } else { if (m_stepConfig[motor].currentStep <= 0) - m_stepConfig[motor].currentStep = + m_stepConfig[motor].currentStep = m_stepConfig[motor].stepsPerRev; } diff --git a/src/adafruitms1438/adafruitms1438.hpp b/src/adafruitms1438/adafruitms1438.hpp index 30a1eee0..3e080fb5 100644 --- a/src/adafruitms1438/adafruitms1438.hpp +++ b/src/adafruitms1438/adafruitms1438.hpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "pca9685.hpp" @@ -36,7 +37,7 @@ #define ADAFRUITMS1438_DEFAULT_I2C_ADDR 0x60 namespace upm { - + /** * @brief Adafruit Motor Shield * @defgroup adafruitms1438 libupm-adafruitms1438 @@ -104,11 +105,23 @@ namespace upm { */ AdafruitMS1438(int bus, uint8_t address = ADAFRUITMS1438_DEFAULT_I2C_ADDR); + /** + * Instantiates AdafruitMS1438 based on a given string. + * + * @param initStr string containing specific information for AdafruitMS1438 initialization. + */ + AdafruitMS1438(std::string initStr); + /** * AdafruitMS1438 destructor */ ~AdafruitMS1438(); + /** + * AdafruitMS1438 initialization method + */ + void initAdafruitMS1438(); + /** * Returns the number of milliseconds elapsed since initClock(...) * was last called. @@ -126,7 +139,7 @@ namespace upm { /** * Sets the PWM period. Note: this applies to all PWM channels. * - * @param hz Sets the PWM period + * @param hz Sets the PWM period */ void setPWMPeriod(float hz); @@ -182,7 +195,7 @@ namespace upm { * @param dir Direction to set the motor in */ void setMotorDirection(DCMOTORS_T motor, DIRECTION_T dir); - + /** * Sets the direction of a stepper motor, clockwise or counterclockwise * @@ -190,7 +203,7 @@ namespace upm { * @param dir Direction to set the motor in */ void setStepperDirection(STEPMOTORS_T motor, DIRECTION_T dir); - + /** * Sets a stepper motor configuration *