grovemd: allow seperate directions for each DC motor

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson 2015-03-18 12:20:19 -06:00 committed by Mihai Tudor Panu
parent d380658b40
commit 970d6a083f
4 changed files with 24 additions and 15 deletions

View File

@ -39,13 +39,13 @@ int main(int argc, char **argv)
// set direction to CW and set speed to 50%
cout << "Spin M1 and M2 at half speed for 3 seconds" << endl;
motors->setDirection(upm::GroveMD::DIR_CW);
motors->setMotorDirections(upm::GroveMD::DIR_CW, upm::GroveMD::DIR_CW);
motors->setMotorSpeeds(127, 127);
sleep(3);
// counter clockwise
cout << "Reversing M1 and M2 for 3 seconds" << endl;
motors->setDirection(upm::GroveMD::DIR_CCW);
motors->setMotorDirections(upm::GroveMD::DIR_CCW, upm::GroveMD::DIR_CCW);
sleep(3);
//! [Interesting]

View File

@ -33,7 +33,8 @@ function start()
{
// set direction to CW and set speed to 50%
console.log("Spin M1 and M2 at half speed for 3 seconds");
my_MotorDriver_obj.setDirection(groveMotorDriver_lib.GroveMD.DIR_CW);
my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CW,
groveMotorDriver_lib.GroveMD.DIR_CW);
my_MotorDriver_obj.setMotorSpeeds(127, 127);
}
}
@ -44,7 +45,8 @@ function reverse()
{
// counter clockwise
console.log("Reversing M1 and M2 for 3 seconds");
my_MotorDriver_obj.setDirection(groveMotorDriver_lib.GroveMD.DIR_CCW);
my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CCW,
groveMotorDriver_lib.GroveMD.DIR_CCW);
}
}

View File

@ -108,12 +108,13 @@ bool GroveMD::setPWMFrequencyPrescale(uint8_t freq)
return writePacket(SET_PWM_FREQ, freq, GROVEMD_NOOP);
}
bool GroveMD::setDirection(DIRECTION_T dir)
bool GroveMD::setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB)
{
uint8_t dir = ((dirB & 0x03) << 2) | (dirA & 0x03);
return writePacket(SET_DIRECTION, dir, GROVEMD_NOOP);
}
bool GroveMD::enableStepper(DIRECTION_T dir, uint8_t speed)
bool GroveMD::enableStepper(STEP_DIRECTION_T dir, uint8_t speed)
{
return writePacket(STEPPER_ENABLE, dir, speed);
}

View File

@ -79,10 +79,15 @@ namespace upm {
STEPPER_NUM_STEPS = 0x1c
} REG_T;
// legal directions
typedef enum { DIR_CCW = 0x0a,
DIR_CW = 0x05
} DIRECTION_T;
// legal directions for the stepper
typedef enum { STEP_DIR_CCW = 0x0a,
STEP_DIR_CW = 0x05
} STEP_DIRECTION_T;
// legal directions for individual DC motors
typedef enum { DIR_CCW = 0x02,
DIR_CW = 0x01
} DC_DIRECTION_T;
/**
* grovemd constructor
@ -130,22 +135,23 @@ namespace upm {
bool setPWMFrequencyPrescale(uint8_t freq=0x03);
/**
* For controlling DC motors, set the direction
* For controlling DC motors, set the directions of motors A & B
*
* @param dir direction, CW or CCW
* @param dirA direction for motor A, DIR_CW or DIR_CCW
* @param dirB direction for motor B, DIR_CW or DIR_CCW
* @return true if command successful
*/
bool setDirection(DIRECTION_T dir);
bool setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB);
/**
* For controlling a stepper motor, set a direction, speed and
* then enable.
*
* @param dir direction, CW or CCW
* @param dir direction, STEP_DIR_CW or STEP_DIR_CCW
* @param speed motor speed. Valid range is 1-255, higher is slower.
* @return true if command successful
*/
bool enableStepper(DIRECTION_T dir, uint8_t speed);
bool enableStepper(STEP_DIRECTION_T dir, uint8_t speed);
/**
* For controlling a stepper motor, stop the stepper motor.