Skip to content

Commit 56f86c1

Browse files
committed
implemented linear regression class
1 parent 0649fd6 commit 56f86c1

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

src/propulsion/main.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ void Main::run()
5555
break;
5656
case data::State::kCalibrating:
5757
if (state_processor_.isInitialised()) {
58+
// set coefficients for rpm regulator
59+
utils::math::Regression regression;
60+
// TODO:find actual x and y data (and probably get from json or something)
61+
regression.CaulculateCoefficients({1, 2, 3}, {1, 2, 3});
62+
std::vector<double> coefficients = regression.coefficients_;
63+
RpmRegulator rpm_regulator;
64+
rpm_regulator.setCoefficients(coefficients);
65+
5866
if (motor_data.module_status != data::ModuleStatus::kReady) {
5967
motor_data.module_status = data::ModuleStatus::kReady;
6068
data.setMotorData(motor_data);

src/propulsion/rpm_regulator.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ int32_t RpmRegulator::calculateRpm(const data::nav_t actual_velocity, const int3
1818
return std::max(target, 0);
1919
}
2020

21+
void RpmRegulator::setCoefficients(std::vector<double> coefficients)
22+
{
23+
coefficients_ = coefficients;
24+
}
25+
2126
int32_t RpmRegulator::calculateOptimalRpm(const data::nav_t actual_velocity)
2227
{
23-
// TODO: get regression coeffs from class!
24-
/*
25-
hyped::utils::math::Regression regression;
26-
const double beta1 = regression.coefficients.beta1;
27-
const double beta0 = regression.coefficients.beta0;
28+
double beta1 = coefficients_.at(0);
29+
double beta0 = coefficients_.at(1);
2830

2931
// polynomial values from simulation
3032
return std::round(beta1 * actual_velocity + beta0);
31-
*/
3233
return 0;
3334
}
3435

src/propulsion/rpm_regulator.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class RpmRegulator {
2020
*/
2121
int32_t calculateRpm(const data::nav_t actual_velocity, const int32_t actual_rpm);
2222

23+
/**
24+
* @brief sets the coefficients calculated on startup in utils to be stored
25+
* as private class members.
26+
*
27+
* @param actual_velocity
28+
*/
29+
void setCoefficients(std::vector<double> coeffients);
30+
2331
private:
2432
/**
2533
* @brief calculates the optimal rpm based off of the current velocity.
@@ -37,6 +45,9 @@ class RpmRegulator {
3745
* @return int32_t - the step with which to increase the rpm
3846
*/
3947
int32_t step(const int32_t optimal_rpm, const int32_t actual_rpm);
48+
49+
// coefficients vector of form {beta1, beta0}
50+
std::vector<double> coefficients_;
4051
};
4152

4253
} // namespace hyped::propulsion

src/utils/math/regression.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace hyped::utils::math {
44

5-
void Regression::GetCoeffs(const std::vector<double> x_data, const std::vector<double> y_data)
5+
Regression::Regression()
6+
{
7+
}
8+
9+
void Regression::CaulculateCoefficients(const std::vector<double> x_data,
10+
const std::vector<double> y_data)
611
{
712
double x_sum = 0;
813
double y_sum = 0;
@@ -27,8 +32,6 @@ void Regression::GetCoeffs(const std::vector<double> x_data, const std::vector<d
2732
double beta1 = s_xy / s_xx;
2833
double beta0 = y_mean - (beta1 - x_mean);
2934

30-
std::vector<double> coeffs{beta1, beta0};
31-
coefficients.beta0 = coeffs.at(1);
32-
coefficients.beta1 = coeffs.at(0);
35+
coefficients_ = {beta1, beta0};
3336
}
3437
} // namespace hyped::utils::math

src/utils/math/regression.hpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ namespace hyped::utils::math {
88
class Regression {
99
public:
1010
Regression();
11-
void GetCoeffs(std::vector<double> x_data, std::vector<double> y_data);
1211

13-
struct Coefficients {
14-
double beta0;
15-
double beta1;
16-
};
17-
Coefficients coefficients;
12+
/**
13+
* @brief calculates regressed coefficients using basic x-y linear regression.
14+
* only works for straight lines!
15+
*
16+
* @param x_data vector of x-data points
17+
* @param y_data vector of y-data points
18+
*/
19+
void CaulculateCoefficients(std::vector<double> x_data, std::vector<double> y_data);
1820

19-
private:
21+
// calculated coefficients from regression function
2022
std::vector<double> coefficients_;
2123
};
2224
} // namespace hyped::utils::math

0 commit comments

Comments
 (0)