nýtt branch fyrir part3 og kóðar
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
|
||||
#include "rotary_encoder_driver.h"
|
||||
#include "motor_driver.h"
|
||||
#include "p_controller.h"
|
||||
//#include "p_controller.h"
|
||||
#include "pi_controller.h"
|
||||
#include "digital_out.h"
|
||||
|
||||
class State;
|
||||
@@ -24,7 +25,8 @@ private:
|
||||
|
||||
RotaryEncoderDriver& encoder;
|
||||
MotorDriver& motor;
|
||||
P_controller& controller;
|
||||
//P_controller& controller;
|
||||
PI_controller& controller;
|
||||
|
||||
Digital_out led;
|
||||
|
||||
@@ -76,6 +78,10 @@ public:
|
||||
void led_on();
|
||||
|
||||
void led_off();
|
||||
|
||||
void reset_controller();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,6 +10,9 @@ public:
|
||||
P_controller(double Kp);
|
||||
|
||||
double update(double ref, double actual);
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef PI_CONTROLLER_H
|
||||
#define PI_CONTROLLER_H
|
||||
|
||||
class PI_controller
|
||||
{
|
||||
private:
|
||||
double Kp;
|
||||
double Ti;
|
||||
double T;
|
||||
double integral;
|
||||
|
||||
public:
|
||||
PI_controller(
|
||||
double Kp,
|
||||
double Ti,
|
||||
double T
|
||||
);
|
||||
|
||||
double update(double ref, double actual);
|
||||
|
||||
void reset();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -15,7 +15,10 @@ void InitializationState::enter()
|
||||
|
||||
context->reset_speed_measurement();
|
||||
|
||||
context->reset_controller();
|
||||
|
||||
enter_time_ms = millis();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+9
-3
@@ -3,7 +3,8 @@
|
||||
|
||||
#include "rotary_encoder_driver.h"
|
||||
#include "motor_driver.h"
|
||||
#include "p_controller.h"
|
||||
//#include "p_controller.h"
|
||||
#include "pi_controller.h"
|
||||
|
||||
#include "motor_controller.h"
|
||||
|
||||
@@ -16,10 +17,15 @@ RotaryEncoderDriver encoder;
|
||||
|
||||
MotorDriver motor;
|
||||
|
||||
const double KP = 12.5;
|
||||
//const double KP = 12.5;
|
||||
|
||||
P_controller controller(KP);
|
||||
//P_controller controller(KP);
|
||||
|
||||
PI_controller controller(
|
||||
12.5, // Kp
|
||||
1.0, // Ti
|
||||
0.004 // T = 4 ms
|
||||
)
|
||||
|
||||
InitializationState initialization_state;
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
MotorController::MotorController(
|
||||
RotaryEncoderDriver& encoder,
|
||||
MotorDriver& motor,
|
||||
P_controller& controller,
|
||||
//P_controller& controller,
|
||||
PI_controller& controller;
|
||||
State& initialization,
|
||||
State& operational,
|
||||
State& stopped
|
||||
@@ -140,3 +141,8 @@ void MotorController::led_off()
|
||||
{
|
||||
led.set_lo();
|
||||
}
|
||||
|
||||
void MotorController::reset_controller()
|
||||
{
|
||||
controller.reset();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "pi_controller.h"
|
||||
|
||||
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 error = ref - actual;
|
||||
|
||||
double new_integral = integral + error * T;
|
||||
|
||||
double output =
|
||||
Kp * (error + new_integral / Ti);
|
||||
|
||||
// Saturation + anti-windup
|
||||
if (output > 100.0)
|
||||
{
|
||||
return 100.0;
|
||||
}
|
||||
|
||||
if (output < -100.0)
|
||||
{
|
||||
return -100.0;
|
||||
}
|
||||
|
||||
integral = new_integral;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
void PI_controller::reset()
|
||||
{
|
||||
integral = 0.0;
|
||||
}
|
||||
Reference in New Issue
Block a user