13 changed files with 160 additions and 208 deletions
+7 -3
View File
@@ -11,11 +11,13 @@ skinparam state {
state Initialization state Initialization
Initialization : Entry / Stop motor Initialization : Entry / Stop motor
Initialization : Entry / Initialize internal parameters Initialization : Do / Initialize internal parameters
Initialization : Entry / Reset speed measurement Initialization : Do / Reset speed measurement
Initialization : Do / Wait 2 seconds Initialization : Do / Wait 2 seconds
Initialization : Exit / Send Boot-up message Initialization : Exit / Send Boot-up message
state Operational state Operational
Operational : Entry / LED on Operational : Entry / LED on
Operational : Do / Run speed controller Operational : Do / Run speed controller
@@ -26,7 +28,9 @@ state Stopped
Stopped : Do / Blink LED at 2 Hz Stopped : Do / Blink LED at 2 Hz
Stopped : Exit / LED off Stopped : Exit / LED off
Initialization --> Operational : initialization complete
Initialization --> Operational : initialization\n complete
Operational --> Initialization : reset Operational --> Initialization : reset
Operational --> Stopped : fault detected Operational --> Stopped : fault detected
+18
View File
@@ -0,0 +1,18 @@
#ifndef CONTROLLER_H
#define CONTROLLER_H
class Controller
{
public:
virtual ~Controller()
{
}
virtual double update(double ref, double actual) = 0;
virtual void reset()
{
}
};
#endif
+1 -1
View File
@@ -7,7 +7,7 @@
class InitializationState : public State class InitializationState : public State
{ {
private: private:
static const unsigned long WAIT_MS = 2000; static const unsigned long WAIT_MS = 2000UL;
unsigned long enter_time_ms = 0; unsigned long enter_time_ms = 0;
+21 -3
View File
@@ -3,7 +3,9 @@
#include "rotary_encoder_driver.h" #include "rotary_encoder_driver.h"
#include "motor_driver.h" #include "motor_driver.h"
#include "p_controller.h" //#include "p_controller.h"
//#include "pi_controller.h"
#include "controller.h"
#include "digital_out.h" #include "digital_out.h"
class State; class State;
@@ -24,7 +26,9 @@ private:
RotaryEncoderDriver& encoder; RotaryEncoderDriver& encoder;
MotorDriver& motor; MotorDriver& motor;
P_controller& controller; //P_controller& controller;
//PI_controller& controller;
Controller& controller;
Digital_out led; Digital_out led;
@@ -35,13 +39,21 @@ public:
MotorController( MotorController(
RotaryEncoderDriver& encoder, RotaryEncoderDriver& encoder,
MotorDriver& motor, MotorDriver& motor,
P_controller& controller, //P_controller& controller,
Controller& controller,
State& initialization, State& initialization,
State& operational, State& operational,
State& stopped State& stopped
); );
// void set_states(
// State* initialization,
// State* operational,
// State* stopped
// );
void begin(); void begin();
void update(); void update();
@@ -68,9 +80,15 @@ public:
void reset_controller(); void reset_controller();
void led_on(); void led_on();
void led_off(); void led_off();
}; };
#endif #endif
+9 -5
View File
@@ -1,16 +1,20 @@
#ifndef P_CONTROLLER_H #ifndef P_CONTROLLER_H
#define P_CONTROLLER_H #define P_CONTROLLER_H
class P_controller #include "controller.h"
class P_controller : public Controller
{ {
protected: private:
double Kp; double Kp;
public: public:
P_controller(double Kp); P_controller(double Kp);
virtual ~P_controller() {}
virtual double update(double ref, double actual); double update(double ref, double actual) override;
virtual void reset() {}
}; };
#endif #endif
+14 -8
View File
@@ -1,20 +1,26 @@
#ifndef PI_CONTROLLER_H #ifndef PI_CONTROLLER_H
#define PI_CONTROLLER_H #define PI_CONTROLLER_H
#include "p_controller.h" #include "controller.h"
class PI_controller : public P_controller class PI_controller : public Controller
{ {
private: private:
static constexpr double U_MAX = 100.0; // % double Kp;
static constexpr double U_MIN = -100.0; // %
double Ti; double Ti;
double T; double T;
double integral; double integral;
public:
PI_controller(double Kp, double Ti, double T); public:
PI_controller(
double Kp,
double Ti,
double T
);
double update(double ref, double actual) override; double update(double ref, double actual) override;
void reset() override; void reset() override;
}; };
-7
View File
@@ -16,10 +16,3 @@ framework = arduino
monitor_speed = 115200 monitor_speed = 115200
test_framework = unity test_framework = unity
test_build_src = yes test_build_src = yes
; [env:native]
; platform = native
; test_framework = unity
; test_build_src = yes
; build_src_filter = -<*> +<p_controller.cpp> +<pi_controller.cpp>
+3
View File
@@ -15,7 +15,10 @@ void InitializationState::enter()
context->reset_speed_measurement(); context->reset_speed_measurement();
context->reset_controller();
enter_time_ms = millis(); enter_time_ms = millis();
} }
+9 -9
View File
@@ -1,11 +1,9 @@
// main.cpp er sleppt i test-build (pio test) svo setup()/loop() rekist ekki a prof
#ifndef PIO_UNIT_TESTING
#include <Arduino.h> #include <Arduino.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "rotary_encoder_driver.h" #include "rotary_encoder_driver.h"
#include "motor_driver.h" #include "motor_driver.h"
//#include "p_controller.h"
#include "pi_controller.h" #include "pi_controller.h"
#include "motor_controller.h" #include "motor_controller.h"
@@ -19,13 +17,15 @@ RotaryEncoderDriver encoder;
MotorDriver motor; MotorDriver motor;
const double Kp = 12.5; //const double KP = 12.5;
const double Ti = 1.0;
const double T = 0.004;
// P_controller controller(Kp); //P_controller controller(KP);
PI_controller controller(Kp, Ti, T);
PI_controller controller(
12.5, // Kp
1.0, // Ti
0.004 // T = 4 ms
);
InitializationState initialization_state; InitializationState initialization_state;
@@ -59,6 +59,7 @@ int main()
sei(); sei();
// motor_controller.set_states( // motor_controller.set_states(
// &initialization_state, // &initialization_state,
// &operational_state, // &operational_state,
@@ -92,4 +93,3 @@ int main()
// int main(){ // int main(){
// return 0; // return 0;
// } // }
#endif // PIO_UNIT_TESTING
+11 -6
View File
@@ -7,7 +7,9 @@
MotorController::MotorController( MotorController::MotorController(
RotaryEncoderDriver& encoder, RotaryEncoderDriver& encoder,
MotorDriver& motor, MotorDriver& motor,
P_controller& controller, //P_controller& controller,
//PI_controller& controller;
Controller& controller,
State& initialization, State& initialization,
State& operational, State& operational,
State& stopped State& stopped
@@ -58,6 +60,8 @@ void MotorController::transition_to(State& new_state)
current_state = &new_state; current_state = &new_state;
current_state->set_context(this);
current_state->enter(); current_state->enter();
} }
@@ -120,11 +124,6 @@ void MotorController::reset_speed_measurement()
encoder.reset_speed_measurement(); encoder.reset_speed_measurement();
} }
void MotorController::reset_controller()
{
controller.reset();
}
void MotorController::to_stopped() void MotorController::to_stopped()
{ {
transition_to(stopped_state); transition_to(stopped_state);
@@ -135,6 +134,11 @@ bool MotorController::fault_active()
return motor.fault_active(); return motor.fault_active();
} }
void MotorController::reset_controller()
{
controller.reset();
}
void MotorController::led_on() void MotorController::led_on()
{ {
led.set_hi(); led.set_hi();
@@ -145,3 +149,4 @@ void MotorController::led_off()
{ {
led.set_lo(); led.set_lo();
} }
-2
View File
@@ -8,8 +8,6 @@ void OperationalState::enter()
{ {
Serial.println("State: Operational"); Serial.println("State: Operational");
context->reset_controller();
context->led_on(); context->led_on();
} }
+44 -19
View File
@@ -1,31 +1,56 @@
#include "pi_controller.h" #include "pi_controller.h"
PI_controller::PI_controller(double Kp, double Ti, double T) : P_controller(Kp), Ti(Ti), T(T), integral(0.0)
PI_controller::PI_controller(
double Kp,
double Ti,
double T
)
: Kp(Kp),
Ti(Ti),
T(T),
integral(0.0)
{ {
} }
double PI_controller::update(double ref, double actual) double PI_controller::update(double ref, double actual)
{ {
double e = ref - actual; return 0.0;
double new_integral = integral + e * T;
double u = Kp * (e + new_integral / Ti);
if (u > U_MAX)
{
u = U_MAX;
}
else if (u < U_MIN)
{
u = U_MIN;
}
else
{
integral = new_integral;
}
return u;
// return 0.0;
} }
// 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()
{ {
integral = 0.0; integral = 0.0;
+14 -136
View File
@@ -1,150 +1,28 @@
#include <unity.h> #include <unity.h>
// #ifdef ARDUINO //#include "p_controller.h"
#include <Arduino.h>
// #endif
#include "p_controller.h"
#include "pi_controller.h" #include "pi_controller.h"
static const double KP = 2.0; static const double KP = 2.0;
static const double TI = 1.0; static const double KI = 1.0;
static const double T = 0.1; static const double T = 0.1;
static const double TOL = 1e-4; // double er 32-bita float a AVR static const double TOL = 1e-4; // Tolerance
// void setUp(void) {} void test_positive_error()
// void tearDown(void) {}
// Normal flow: engin villa -> ekkert úttak
void test_zero_error_gives_zero_output(void)
{ {
PI_controller c(KP, TI, T); PI_controller controller(KP, KI, T);
TEST_ASSERT_FLOAT_WITHIN(TOL, 0.0, c.update(10.0, 10.0));
double output = controller.update(10.0, 8.0);
TEST_ASSERT_DOUBLE_WITHIN(TOL, 4.4, output);
} }
// Normal flow: fyrsta skref u = Kp * (e + e*T/Ti)
void test_first_step(void)
{
PI_controller c(KP, TI, T);
double e = 5.0;
TEST_ASSERT_FLOAT_WITHIN(TOL, KP * (e + e * T / TI), c.update(e, 0.0));
}
// Normal flow: stöðug villa safnast upp, u_n = Kp * (e + n*e*T/Ti) int main()
void test_integral_accumulates(void)
{ {
PI_controller c(KP, TI, T);
double e = 5.0;
double u = 0.0;
for (int n = 1; n <= 5; n++)
{
u = c.update(e, 0.0);
TEST_ASSERT_FLOAT_WITHIN(TOL, KP * (e + n * e * T / TI), u);
}
}
// Normal flow: þegar villan er orðin 0 heldur integralið úttakinu uppi
void test_output_held_when_error_zero(void)
{
PI_controller c(KP, TI, T);
double e = 5.0;
for (int n = 0; n < 5; n++)
{
c.update(e, 0.0);
}
double E = 5 * e * T;
TEST_ASSERT_FLOAT_WITHIN(TOL, KP * E / TI, c.update(0.0, 0.0));
}
// Alt flow: villa langt utan getu -> mettun í +100 %
void test_saturates_high(void)
{
PI_controller c(KP, TI, T);
TEST_ASSERT_FLOAT_WITHIN(TOL, 100.0, c.update(10000.0, 0.0));
}
// Alt flow: mettun í -100 %
void test_saturates_low(void)
{
PI_controller c(KP, TI, T);
TEST_ASSERT_FLOAT_WITHIN(TOL, -100.0, c.update(-10000.0, 0.0));
}
// Alt flow: anti-windup. Eftir langa mettun á lítil neikvæð villa
// strax að gefa úttak undir mettun (integralið óx ekki)
void test_no_integral_windup(void)
{
PI_controller c(KP, TI, T);
for (int n = 0; n < 1000; n++)
{
c.update(10000.0, 0.0);
}
double u = c.update(0.0, 1.0); // e = -1
TEST_ASSERT_TRUE(u < 0.0);
}
// Alt flow: reset (stopped -> operational) hreinsar integralið
void test_reset_clears_integral(void)
{
PI_controller c(KP, TI, T);
for (int n = 0; n < 5; n++)
{
c.update(5.0, 0.0);
}
c.reset();
TEST_ASSERT_FLOAT_WITHIN(TOL, 0.0, c.update(0.0, 0.0));
}
// Polymorphism: kall í gegnum P_controller& gefur PI hegðun
void test_polymorphic_update(void)
{
PI_controller pi(KP, TI, T);
P_controller& c = pi;
double e = 5.0;
TEST_ASSERT_FLOAT_WITHIN(TOL, KP * (e + e * T / TI), c.update(e, 0.0));
}
// P_controller hegðar sér óbreytt
void test_p_controller_unchanged(void)
{
P_controller c(KP);
TEST_ASSERT_FLOAT_WITHIN(TOL, KP * 5.0, c.update(5.0, 0.0));
}
void setup()
{
// init();
delay(2000);
UNITY_BEGIN(); UNITY_BEGIN();
RUN_TEST(test_zero_error_gives_zero_output);
RUN_TEST(test_first_step); RUN_TEST(test_positive_error);
RUN_TEST(test_integral_accumulates);
RUN_TEST(test_output_held_when_error_zero); return UNITY_END();
RUN_TEST(test_saturates_high);
RUN_TEST(test_saturates_low);
RUN_TEST(test_no_integral_windup);
RUN_TEST(test_reset_clears_integral);
RUN_TEST(test_polymorphic_update);
RUN_TEST(test_p_controller_unchanged);
// return UNITY_END();
UNITY_END();
} }
void loop() {}
// #ifdef ARDUINO
// void setup()
// {
// delay(2000); // bida eftir serial tengingu eftir reset
// run_tests();
// }
// void loop()
// {
// }
// #else
// int main(int argc, char** argv)
// {
// return run_tests();
// }
// #endif