Skip to content

Commit

Permalink
fixup! RateControl/PositionScratchController: use std::unique_ptr, Po…
Browse files Browse the repository at this point in the history
…llingControlProxy etc.
  • Loading branch information
ronso0 committed Dec 27, 2024
1 parent 100a520 commit 5b63d77
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 74 deletions.
3 changes: 0 additions & 3 deletions src/engine/controls/ratecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ RateControl::RateControl(const QString& group, UserSettingsPointer pConfig)
&RateControl::slotControlRatePermUpSmall,
Qt::DirectConnection);




// Scratch enable toggle
m_pScratch2Enable->set(0);

Expand Down
103 changes: 58 additions & 45 deletions src/engine/positionscratchcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,62 @@
#include "util/math.h"
#include "util/time.h"

VelocityController::VelocityController()
: m_last_error(0.0),
m_p(0.0),
m_d(0.0) {
}
class VelocityController {
public:
VelocityController()
: m_last_error(0.0),
m_p(0.0),
m_d(0.0) {
}
void setPD(double p, double d) {
m_p = p;
m_d = d;
}

void VelocityController::setPD(double p, double d) {
m_p = p;
m_d = d;
}
void reset(double last_error) {
m_last_error = last_error;
}

void VelocityController::reset(double last_error) {
m_last_error = last_error;
}
double observation(double error) {
// Main PD calculation
m_last_error = m_p * error + m_d * (error - m_last_error);
return m_last_error;
}

double VelocityController::observation(double error) {
// Main PD calculation
m_last_error = m_p * error + m_d * (error - m_last_error);
return m_last_error;
}
private:
double m_last_error;
double m_p, m_d;
};

RateIIFilter::RateIIFilter()
: m_factor(1.0),
m_last_rate(0.0) {
}
class RateIIFilter {
public:
RateIIFilter()
: m_factor(1.0),
m_last_rate(0.0) {
}

void RateIIFilter::setFactor(double factor) {
m_factor = factor;
}
void setFactor(double factor) {
m_factor = factor;
}

void RateIIFilter::reset(double last_rate) {
m_last_rate = last_rate;
}
void reset(double last_rate) {
m_last_rate = last_rate;
}

double RateIIFilter::filter(double rate) {
if (fabs(rate) - fabs(m_last_rate) > -0.1) {
m_last_rate = m_last_rate * (1 - m_factor) + rate * m_factor;
} else {
// do not filter strong decelerations to avoid overshooting
m_last_rate = rate;
double filter(double rate) {
if (fabs(rate) - fabs(m_last_rate) > -0.1) {
m_last_rate = m_last_rate * (1 - m_factor) + rate * m_factor;
} else {
// do not filter strong decelerations to avoid overshooting
m_last_rate = rate;
}
return m_last_rate;
}
return m_last_rate;
}

private:
double m_factor;
double m_last_rate;
};

namespace {

Expand All @@ -69,6 +82,14 @@ constexpr double kTimeToStop = 1.0;

PositionScratchController::PositionScratchController(const QString& group)
: m_group(group),
m_pScratchEnable(std::make_unique<ControlObject>(
ConfigKey(group, QStringLiteral("scratch_position_enable")))),
m_pScratchPos(std::make_unique<ControlObject>(
ConfigKey(group, QStringLiteral("scratch_position")))),
m_pMainSampleRate(std::make_unique<ControlProxy>(
ConfigKey(QStringLiteral("[App]"), QStringLiteral("samplerate")))),
m_pVelocityController(std::make_unique<VelocityController>()),
m_pRateIIFilter(std::make_unique<RateIIFilter>()),
m_isScratching(false),
m_inertiaEnabled(false),
m_prevSamplePos(0),
Expand All @@ -84,19 +105,11 @@ PositionScratchController::PositionScratchController(const QString& group)
m_p(1),
m_d(1),
m_f(0.4) {
m_pScratchEnable = std::make_unique<ControlObject>(
ConfigKey(group, QStringLiteral("scratch_position_enable")));

m_pScratchPos = std::make_unique<ControlObject>(
ConfigKey(group, QStringLiteral("scratch_position")));

m_pMainSampleRate = std::make_unique<ControlProxy>(
ConfigKey(QStringLiteral("[App]"), QStringLiteral("samplerate")));
m_pMainSampleRate->connectValueChanged(this,
&PositionScratchController::slotSampleRateChanged);
}

m_pVelocityController = std::make_unique<VelocityController>();
m_pRateIIFilter = std::make_unique<RateIIFilter>();
PositionScratchController::~PositionScratchController() {
}

void PositionScratchController::slotSampleRateChanged(double sampleRate) {
Expand Down
31 changes: 5 additions & 26 deletions src/engine/positionscratchcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,16 @@

class ControlObject;
class ControlProxy;

class VelocityController {
public:
VelocityController();

void setPD(double p, double d);
void reset(double last_error);
double observation(double error);

private:
double m_last_error;
double m_p, m_d;
};

class RateIIFilter {
public:
RateIIFilter();

void setFactor(double factor);
void reset(double last_rate);
double filter(double rate);

private:
double m_factor;
double m_last_rate;
};
class VelocityController;
class RateIIFilter;

class PositionScratchController : public QObject {
Q_OBJECT
public:
PositionScratchController(const QString& group);
// required for the forward-declarations of uniquq_pointers of
// VelocityController and RateIIFilter
~PositionScratchController();

void process(double currentSample,
double releaseRate,
Expand Down

0 comments on commit 5b63d77

Please sign in to comment.