Notaði fyrrum p_controller og bætti við virtual pi_controller
This commit is contained in:
@@ -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
|
||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@
|
|||||||
#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 "pi_controller.h"
|
||||||
|
#include "controller.h"
|
||||||
#include "digital_out.h"
|
#include "digital_out.h"
|
||||||
|
|
||||||
class State;
|
class State;
|
||||||
@@ -26,7 +27,8 @@ private:
|
|||||||
RotaryEncoderDriver& encoder;
|
RotaryEncoderDriver& encoder;
|
||||||
MotorDriver& motor;
|
MotorDriver& motor;
|
||||||
//P_controller& controller;
|
//P_controller& controller;
|
||||||
PI_controller& controller;
|
//PI_controller& controller;
|
||||||
|
Controller& controller;
|
||||||
|
|
||||||
Digital_out led;
|
Digital_out led;
|
||||||
|
|
||||||
@@ -37,7 +39,8 @@ 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
|
||||||
@@ -75,11 +78,15 @@ public:
|
|||||||
|
|
||||||
void reset_speed_measurement();
|
void reset_speed_measurement();
|
||||||
|
|
||||||
|
void reset_controller();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void led_on();
|
void led_on();
|
||||||
|
|
||||||
void led_off();
|
void led_off();
|
||||||
|
|
||||||
void reset_controller();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#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
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
double Kp;
|
double Kp;
|
||||||
@@ -9,7 +11,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
P_controller(double Kp);
|
P_controller(double Kp);
|
||||||
|
|
||||||
double update(double ref, double actual);
|
double update(double ref, double actual) override;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
#ifndef PI_CONTROLLER_H
|
#ifndef PI_CONTROLLER_H
|
||||||
#define PI_CONTROLLER_H
|
#define PI_CONTROLLER_H
|
||||||
|
|
||||||
class PI_controller
|
#include "controller.h"
|
||||||
|
|
||||||
|
class PI_controller : public Controller
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
double Kp;
|
double Kp;
|
||||||
double Ti;
|
double Ti;
|
||||||
double T;
|
double T;
|
||||||
|
|
||||||
double integral;
|
double integral;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -16,9 +19,9 @@ public:
|
|||||||
double T
|
double T
|
||||||
);
|
);
|
||||||
|
|
||||||
double update(double ref, double actual);
|
double update(double ref, double actual) override;
|
||||||
|
|
||||||
void reset();
|
void reset() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -8,7 +8,8 @@ MotorController::MotorController(
|
|||||||
RotaryEncoderDriver& encoder,
|
RotaryEncoderDriver& encoder,
|
||||||
MotorDriver& motor,
|
MotorDriver& motor,
|
||||||
//P_controller& controller,
|
//P_controller& controller,
|
||||||
PI_controller& controller;
|
//PI_controller& controller;
|
||||||
|
Controller& controller,
|
||||||
State& initialization,
|
State& initialization,
|
||||||
State& operational,
|
State& operational,
|
||||||
State& stopped
|
State& stopped
|
||||||
@@ -59,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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,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();
|
||||||
@@ -142,7 +150,3 @@ void MotorController::led_off()
|
|||||||
led.set_lo();
|
led.set_lo();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorController::reset_controller()
|
|
||||||
{
|
|
||||||
controller.reset();
|
|
||||||
}
|
|
||||||
+10
-2
@@ -1,5 +1,6 @@
|
|||||||
#include "pi_controller.h"
|
#include "pi_controller.h"
|
||||||
|
|
||||||
|
|
||||||
PI_controller::PI_controller(
|
PI_controller::PI_controller(
|
||||||
double Kp,
|
double Kp,
|
||||||
double Ti,
|
double Ti,
|
||||||
@@ -17,22 +18,29 @@ double PI_controller::update(double ref, double actual)
|
|||||||
{
|
{
|
||||||
double error = ref - actual;
|
double error = ref - actual;
|
||||||
|
|
||||||
double new_integral = integral + error * T;
|
double new_integral =
|
||||||
|
integral + error * T;
|
||||||
|
|
||||||
double output =
|
double output =
|
||||||
Kp * (error + new_integral / Ti);
|
Kp * (error + new_integral / Ti);
|
||||||
|
|
||||||
// Saturation + anti-windup
|
|
||||||
|
// Positive saturation
|
||||||
if (output > 100.0)
|
if (output > 100.0)
|
||||||
{
|
{
|
||||||
return 100.0;
|
return 100.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Negative saturation
|
||||||
if (output < -100.0)
|
if (output < -100.0)
|
||||||
{
|
{
|
||||||
return -100.0;
|
return -100.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Only save the integral if the controller
|
||||||
|
// is not saturated
|
||||||
integral = new_integral;
|
integral = new_integral;
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
|||||||
Reference in New Issue
Block a user