For my chassis, I would like to let it decelerate while moving towards a certain position. I don’t know if I should be using a linear motion profile controller (okapi), since it seems to also have an acceleration period at the start. I tried writing a piece of code that applies acceleration depending on a supplied function, but I haven’t tested it yet. Can anyone help me verify what I wrote? Or is it even necessary to achieve what I want?
The accel parameter takes in errors from the target in encoder ticks, and returns an acceleration in rpm/s.
accelerateTo(QLength itarget, double iinitialVel,
std::function<double(int, int)> accel) {
const double maxVelocity = toUnderlyingType(gearsetRatioPair.internalGearset);
setMaxVelocity(maxVelocity);
const int newTarget = itarget.convert(meter) * scales.straight * gearsetRatioPair.ratio;
const QTime dt = 10_ms;
const double timeRatio = dt.convert(second);
double currentVel = iinitialVel;
const auto encStartVals = chassisModel->getSensorVals();
int leftError = newTarget;
int rightError = newTarget;
while (!settledUtil->isSettled(leftError) && !settledUtil->isSettled(rightError)) {
chassisModel->left(currentVel / maxVelocity);
chassisModel->right(currentVel / maxVelocity);
currentVel += accel(leftError, rightError) * timeRatio;
const auto enc = chassisModel->getSensorVals();
leftError = newTarget - enc[0];
rightError = newTarget - enc[1];
rate->delayUntil(dt);
}
chassisModel->stop();
}