Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9607a26f8f | ||
|
|
1d93605cc3 | ||
|
|
be4cf10d7e |
@@ -11,13 +11,11 @@ skinparam state {
|
|||||||
|
|
||||||
state Initialization
|
state Initialization
|
||||||
Initialization : Entry / Stop motor
|
Initialization : Entry / Stop motor
|
||||||
Initialization : Do / Initialize internal parameters
|
Initialization : Entry / Initialize internal parameters
|
||||||
Initialization : Do / Reset speed measurement
|
Initialization : Entry / 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
|
||||||
@@ -28,9 +26,7 @@ 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
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
#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 = 2000UL;
|
static const unsigned long WAIT_MS = 2000;
|
||||||
|
|
||||||
unsigned long enter_time_ms = 0;
|
unsigned long enter_time_ms = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,7 @@
|
|||||||
|
|
||||||
#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;
|
||||||
@@ -26,9 +24,7 @@ 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;
|
||||||
|
|
||||||
@@ -39,21 +35,13 @@ 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();
|
||||||
@@ -78,17 +66,9 @@ public:
|
|||||||
|
|
||||||
void reset_speed_measurement();
|
void reset_speed_measurement();
|
||||||
|
|
||||||
void reset_controller();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void led_on();
|
void led_on();
|
||||||
|
|
||||||
void led_off();
|
void led_off();
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
#ifndef P_CONTROLLER_H
|
#ifndef P_CONTROLLER_H
|
||||||
#define P_CONTROLLER_H
|
#define P_CONTROLLER_H
|
||||||
|
|
||||||
#include "controller.h"
|
class P_controller
|
||||||
|
|
||||||
class P_controller : public Controller
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
double Kp;
|
double Kp;
|
||||||
@@ -11,10 +9,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
P_controller(double Kp);
|
P_controller(double Kp);
|
||||||
|
|
||||||
double update(double ref, double actual) override;
|
double update(double ref, double actual);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#ifndef PI_CONTROLLER_H
|
|
||||||
#define PI_CONTROLLER_H
|
|
||||||
|
|
||||||
#include "controller.h"
|
|
||||||
|
|
||||||
class PI_controller : public 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) override;
|
|
||||||
|
|
||||||
void reset() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
+1
-2
@@ -14,5 +14,4 @@ board = nanoatmega328new
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
test_framework = unity
|
|
||||||
test_build_src = yes
|
|
||||||
|
|||||||
@@ -15,10 +15,7 @@ void InitializationState::enter()
|
|||||||
|
|
||||||
context->reset_speed_measurement();
|
context->reset_speed_measurement();
|
||||||
|
|
||||||
context->reset_controller();
|
|
||||||
|
|
||||||
enter_time_ms = millis();
|
enter_time_ms = millis();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+3
-10
@@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
#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 "motor_controller.h"
|
#include "motor_controller.h"
|
||||||
|
|
||||||
@@ -17,15 +16,10 @@ RotaryEncoderDriver encoder;
|
|||||||
|
|
||||||
MotorDriver motor;
|
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;
|
InitializationState initialization_state;
|
||||||
|
|
||||||
@@ -59,7 +53,6 @@ int main()
|
|||||||
|
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
|
|
||||||
// motor_controller.set_states(
|
// motor_controller.set_states(
|
||||||
// &initialization_state,
|
// &initialization_state,
|
||||||
// &operational_state,
|
// &operational_state,
|
||||||
|
|||||||
+26
-19
@@ -7,9 +7,7 @@
|
|||||||
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
|
||||||
@@ -60,8 +58,6 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,20 +120,8 @@ void MotorController::reset_speed_measurement()
|
|||||||
encoder.reset_speed_measurement();
|
encoder.reset_speed_measurement();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorController::to_stopped()
|
<<<<<<< HEAD
|
||||||
{
|
=======
|
||||||
transition_to(stopped_state);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MotorController::fault_active()
|
|
||||||
{
|
|
||||||
return motor.fault_active();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MotorController::reset_controller()
|
|
||||||
{
|
|
||||||
controller.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MotorController::led_on()
|
void MotorController::led_on()
|
||||||
{
|
{
|
||||||
@@ -149,4 +133,27 @@ void MotorController::led_off()
|
|||||||
{
|
{
|
||||||
led.set_lo();
|
led.set_lo();
|
||||||
}
|
}
|
||||||
|
>>>>>>> 1d93605cc37a81ea974c12e62c082be19acdb715
|
||||||
|
void MotorController::to_stopped()
|
||||||
|
{
|
||||||
|
transition_to(stopped_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MotorController::fault_active()
|
||||||
|
{
|
||||||
|
return motor.fault_active();
|
||||||
|
}
|
||||||
|
<<<<<<< HEAD
|
||||||
|
|
||||||
|
void MotorController::led_on()
|
||||||
|
{
|
||||||
|
led.set_hi();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MotorController::led_off()
|
||||||
|
{
|
||||||
|
led.set_lo();
|
||||||
|
}
|
||||||
|
=======
|
||||||
|
>>>>>>> 1d93605cc37a81ea974c12e62c082be19acdb715
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
#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)
|
|
||||||
{
|
|
||||||
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()
|
|
||||||
{
|
|
||||||
integral = 0.0;
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
#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