Skip to content

Commit 20ed0b1

Browse files
authored
Use average dt values for velocity calculation (#60)
1 parent fd1d0a2 commit 20ed0b1

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

include/create/create.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
3737
#include <memory>
3838
#include <string>
3939
#include <unistd.h>
40+
#include <deque>
4041

4142
#include "create/serial_stream.h"
4243
#include "create/serial_query.h"
@@ -81,6 +82,8 @@ namespace create {
8182
float totalRightDist;
8283
bool firstOnData;
8384
std::chrono::time_point<std::chrono::steady_clock> prevOnDataTime;
85+
std::deque<float> dtHistory;
86+
uint8_t dtHistoryLength;
8487

8588
Matrix poseCovar;
8689

@@ -332,6 +335,14 @@ namespace create {
332335
*/
333336
bool playSong(const uint8_t& songNumber) const;
334337

338+
/**
339+
* \brief Set dtHistoryLength parameter.
340+
* Used to configure the size of the buffer for calculating average time delta (dt).
341+
* between onData calls, which in turn is used for velocity calculation.
342+
* \param dtHistoryLength number of historical samples to use for calculating average dt.
343+
*/
344+
void setDtHistoryLength(const uint8_t& dtHistoryLength);
345+
335346
/**
336347
* \return true if a left or right wheeldrop is detected, false otherwise.
337348
*/

src/create.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace create {
4141
poseCovar = Matrix(3, 3, 0.0);
4242
requestedLeftVel = 0;
4343
requestedRightVel = 0;
44+
dtHistoryLength = 100;
4445
data = std::shared_ptr<Data>(new Data(model.getVersion()));
4546
if (model.getVersion() == V_1) {
4647
serial = std::make_shared<SerialQuery>(data);
@@ -167,8 +168,22 @@ namespace create {
167168
deltaYaw = wheelDistDiff / model.getAxleLength();
168169
}
169170

170-
measuredLeftVel = leftWheelDist / dt;
171-
measuredRightVel = rightWheelDist / dt;
171+
// determine average dt over window
172+
dtHistory.push_front(dt);
173+
174+
if (dtHistory.size() > dtHistoryLength){
175+
dtHistory.pop_back();
176+
}
177+
178+
float dtHistorySum = 0;
179+
for (auto it = dtHistory.cbegin(); it != dtHistory.cend(); ++it)
180+
{
181+
dtHistorySum += *it;
182+
}
183+
auto dtAverage = dtHistorySum / dtHistory.size();
184+
185+
measuredLeftVel = leftWheelDist / dtAverage;
186+
measuredRightVel = rightWheelDist / dtAverage;
172187

173188
// Moving straight
174189
if (fabs(wheelDistDiff) < util::EPS) {
@@ -183,10 +198,10 @@ namespace create {
183198
totalLeftDist += leftWheelDist;
184199
totalRightDist += rightWheelDist;
185200

186-
if (fabs(dt) > util::EPS) {
187-
vel.x = deltaDist / dt;
201+
if (fabs(dtAverage) > util::EPS) {
202+
vel.x = deltaDist / dtAverage;
188203
vel.y = 0.0;
189-
vel.yaw = deltaYaw / dt;
204+
vel.yaw = deltaYaw / dtAverage;
190205
} else {
191206
vel.x = 0.0;
192207
vel.y = 0.0;
@@ -593,6 +608,10 @@ namespace create {
593608
return serial->send(cmd, 2);
594609
}
595610

611+
void Create::setDtHistoryLength(const uint8_t& dtHistoryLength) {
612+
this->dtHistoryLength = dtHistoryLength;
613+
}
614+
596615
bool Create::isWheeldrop() const {
597616
if (data->isValidPacketID(ID_BUMP_WHEELDROP)) {
598617
return (GET_DATA(ID_BUMP_WHEELDROP) & 0x0C) != 0;

0 commit comments

Comments
 (0)