From 3906d7954d66578a65f94d922a8f058e6eb3cf62 Mon Sep 17 00:00:00 2001 From: Erik Schembor Date: Mon, 20 Nov 2017 22:26:38 -0500 Subject: [PATCH] Add ability to drive the wheels with direct pwm duty --- include/create/create.h | 8 ++++++++ src/create.cpp | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/create/create.h b/include/create/create.h index 2143607..e4662f8 100644 --- a/include/create/create.h +++ b/include/create/create.h @@ -191,6 +191,14 @@ namespace create { */ bool driveWheels(const float& leftWheel, const float& rightWheel); + /** + * \brief Set the direct for the left and right wheels. + * \param leftWheel pwm in the range [-1, 1] + * \param rightWheel pwm in the range [-1, 1] + * \return true if successful, false otherwise + */ + bool driveWheelsPwm(const float& leftWheel, const float& rightWheel); + /** * \brief Set the forward and angular velocity of Create. * \param xVel in m/s diff --git a/src/create.cpp b/src/create.cpp index 1c6048f..950c33e 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -424,6 +424,27 @@ namespace create { } } + bool Create::driveWheelsPwm(const float& leftWheel, const float& rightWheel) + { + static const int16_t PWM_COUNTS = 255; + + if (leftWheel < -1.0 || leftWheel > 1.0 || + rightWheel < -1.0 || rightWheel > 1.0) + return false; + + int16_t leftPwm = roundf(leftWheel * PWM_COUNTS); + int16_t rightPwm = roundf(rightWheel * PWM_COUNTS); + + uint8_t cmd[5] = { OC_DRIVE_PWM, + rightPwm >> 8, + rightPwm & 0xff, + leftPwm >> 8, + leftPwm & 0xff + }; + + return serial->send(cmd, 5); + } + bool Create::drive(const float& xVel, const float& angularVel) { // Compute left and right wheel velocities float leftVel = xVel - ((model.getAxleLength() / 2.0) * angularVel);