Use average dt values for velocity calculation (#60)

This commit is contained in:
tim-fan 2021-04-19 18:24:43 +12:00 committed by GitHub
parent fd1d0a220f
commit 20ed0b16ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View file

@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <memory> #include <memory>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include <deque>
#include "create/serial_stream.h" #include "create/serial_stream.h"
#include "create/serial_query.h" #include "create/serial_query.h"
@ -81,6 +82,8 @@ namespace create {
float totalRightDist; float totalRightDist;
bool firstOnData; bool firstOnData;
std::chrono::time_point<std::chrono::steady_clock> prevOnDataTime; std::chrono::time_point<std::chrono::steady_clock> prevOnDataTime;
std::deque<float> dtHistory;
uint8_t dtHistoryLength;
Matrix poseCovar; Matrix poseCovar;
@ -332,6 +335,14 @@ namespace create {
*/ */
bool playSong(const uint8_t& songNumber) const; bool playSong(const uint8_t& songNumber) const;
/**
* \brief Set dtHistoryLength parameter.
* Used to configure the size of the buffer for calculating average time delta (dt).
* between onData calls, which in turn is used for velocity calculation.
* \param dtHistoryLength number of historical samples to use for calculating average dt.
*/
void setDtHistoryLength(const uint8_t& dtHistoryLength);
/** /**
* \return true if a left or right wheeldrop is detected, false otherwise. * \return true if a left or right wheeldrop is detected, false otherwise.
*/ */

View file

@ -41,6 +41,7 @@ namespace create {
poseCovar = Matrix(3, 3, 0.0); poseCovar = Matrix(3, 3, 0.0);
requestedLeftVel = 0; requestedLeftVel = 0;
requestedRightVel = 0; requestedRightVel = 0;
dtHistoryLength = 100;
data = std::shared_ptr<Data>(new Data(model.getVersion())); data = std::shared_ptr<Data>(new Data(model.getVersion()));
if (model.getVersion() == V_1) { if (model.getVersion() == V_1) {
serial = std::make_shared<SerialQuery>(data); serial = std::make_shared<SerialQuery>(data);
@ -167,8 +168,22 @@ namespace create {
deltaYaw = wheelDistDiff / model.getAxleLength(); deltaYaw = wheelDistDiff / model.getAxleLength();
} }
measuredLeftVel = leftWheelDist / dt; // determine average dt over window
measuredRightVel = rightWheelDist / dt; dtHistory.push_front(dt);
if (dtHistory.size() > dtHistoryLength){
dtHistory.pop_back();
}
float dtHistorySum = 0;
for (auto it = dtHistory.cbegin(); it != dtHistory.cend(); ++it)
{
dtHistorySum += *it;
}
auto dtAverage = dtHistorySum / dtHistory.size();
measuredLeftVel = leftWheelDist / dtAverage;
measuredRightVel = rightWheelDist / dtAverage;
// Moving straight // Moving straight
if (fabs(wheelDistDiff) < util::EPS) { if (fabs(wheelDistDiff) < util::EPS) {
@ -183,10 +198,10 @@ namespace create {
totalLeftDist += leftWheelDist; totalLeftDist += leftWheelDist;
totalRightDist += rightWheelDist; totalRightDist += rightWheelDist;
if (fabs(dt) > util::EPS) { if (fabs(dtAverage) > util::EPS) {
vel.x = deltaDist / dt; vel.x = deltaDist / dtAverage;
vel.y = 0.0; vel.y = 0.0;
vel.yaw = deltaYaw / dt; vel.yaw = deltaYaw / dtAverage;
} else { } else {
vel.x = 0.0; vel.x = 0.0;
vel.y = 0.0; vel.y = 0.0;
@ -593,6 +608,10 @@ namespace create {
return serial->send(cmd, 2); return serial->send(cmd, 2);
} }
void Create::setDtHistoryLength(const uint8_t& dtHistoryLength) {
this->dtHistoryLength = dtHistoryLength;
}
bool Create::isWheeldrop() const { bool Create::isWheeldrop() const {
if (data->isValidPacketID(ID_BUMP_WHEELDROP)) { if (data->isValidPacketID(ID_BUMP_WHEELDROP)) {
return (GET_DATA(ID_BUMP_WHEELDROP) & 0x0C) != 0; return (GET_DATA(ID_BUMP_WHEELDROP) & 0x0C) != 0;