Byrjaði á kóðum fyrir part 3 en lenti í veseni með test kóðann
This commit is contained in:
+2
-1
@@ -14,4 +14,5 @@ board = nanoatmega328new
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
test_framework = unity
|
||||||
|
test_build_src = yes
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ PI_controller controller(
|
|||||||
12.5, // Kp
|
12.5, // Kp
|
||||||
1.0, // Ti
|
1.0, // Ti
|
||||||
0.004 // T = 4 ms
|
0.004 // T = 4 ms
|
||||||
)
|
);
|
||||||
|
|
||||||
InitializationState initialization_state;
|
InitializationState initialization_state;
|
||||||
|
|
||||||
|
|||||||
+33
-29
@@ -13,39 +13,43 @@ PI_controller::PI_controller(
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double PI_controller::update(double ref, double actual)
|
double PI_controller::update(double ref, double actual)
|
||||||
{
|
{
|
||||||
double error = ref - actual;
|
return 0.0;
|
||||||
|
|
||||||
double new_integral =
|
|
||||||
integral + error * T;
|
|
||||||
|
|
||||||
double output =
|
|
||||||
Kp * (error + new_integral / Ti);
|
|
||||||
|
|
||||||
|
|
||||||
// Positive saturation
|
|
||||||
if (output > 100.0)
|
|
||||||
{
|
|
||||||
return 100.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Negative saturation
|
|
||||||
if (output < -100.0)
|
|
||||||
{
|
|
||||||
return -100.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Only save the integral if the controller
|
|
||||||
// is not saturated
|
|
||||||
integral = new_integral;
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// double PI_controller::update(double ref, double actual)
|
||||||
|
// {
|
||||||
|
// double error = ref - actual;
|
||||||
|
|
||||||
|
// double new_integral =
|
||||||
|
// integral + error * T;
|
||||||
|
|
||||||
|
// double output =
|
||||||
|
// Kp * (error + new_integral / Ti);
|
||||||
|
|
||||||
|
|
||||||
|
// // Positive saturation
|
||||||
|
// if (output > 100.0)
|
||||||
|
// {
|
||||||
|
// return 100.0;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// // Negative saturation
|
||||||
|
// if (output < -100.0)
|
||||||
|
// {
|
||||||
|
// return -100.0;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// // Only save the integral if the controller
|
||||||
|
// // is not saturated
|
||||||
|
// integral = new_integral;
|
||||||
|
|
||||||
|
// return output;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
void PI_controller::reset()
|
void PI_controller::reset()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#include <unity.h>
|
||||||
|
|
||||||
|
//#include "p_controller.h"
|
||||||
|
#include "pi_controller.h"
|
||||||
|
|
||||||
|
static const double KP = 2.0;
|
||||||
|
static const double KI = 1.0;
|
||||||
|
static const double T = 0.1;
|
||||||
|
static const double TOL = 1e-4; // Tolerance
|
||||||
|
|
||||||
|
void test_positive_error()
|
||||||
|
{
|
||||||
|
PI_controller controller(KP, KI, T);
|
||||||
|
|
||||||
|
double output = controller.update(10.0, 8.0);
|
||||||
|
|
||||||
|
TEST_ASSERT_DOUBLE_WITHIN(TOL, 4.4, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
RUN_TEST(test_positive_error);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user