Compare commits
7
Commits
Isak_part1
...
Isak_part3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36fce02ff2 | ||
|
|
d9ded44895 | ||
|
|
48f2d1aa43 | ||
|
|
3c5cedc413 | ||
|
|
fa30d35401 | ||
|
|
484686f8d1 | ||
|
|
d96da75bdf |
@@ -23,8 +23,18 @@ state Operational
|
||||
Operational : Do / Run speed controller
|
||||
Operational : Exit / Stop motor and LED off
|
||||
|
||||
state Stopped
|
||||
Stopped : Entry / Stop motor
|
||||
Stopped : Do / Blink LED at 2 Hz
|
||||
Stopped : Exit / LED off
|
||||
|
||||
|
||||
|
||||
Initialization --> Operational : initialization\n complete
|
||||
Operational --> Initialization : reset
|
||||
Operational --> Stopped : fault detected
|
||||
|
||||
Stopped --> Initialization : reset
|
||||
Stopped --> Operational : set operational
|
||||
|
||||
@enduml
|
||||
|
||||
@@ -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
|
||||
{
|
||||
private:
|
||||
static const unsigned long WAIT_MS = 2000;
|
||||
static const unsigned long WAIT_MS = 2000UL;
|
||||
|
||||
unsigned long enter_time_ms = 0;
|
||||
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
|
||||
#include "rotary_encoder_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"
|
||||
|
||||
class State;
|
||||
@@ -20,10 +22,13 @@ private:
|
||||
|
||||
State& initialization_state;
|
||||
State& operational_state;
|
||||
State& stopped_state;
|
||||
|
||||
RotaryEncoderDriver& encoder;
|
||||
MotorDriver& motor;
|
||||
P_controller& controller;
|
||||
//P_controller& controller;
|
||||
//PI_controller& controller;
|
||||
Controller& controller;
|
||||
|
||||
Digital_out led;
|
||||
|
||||
@@ -34,12 +39,21 @@ public:
|
||||
MotorController(
|
||||
RotaryEncoderDriver& encoder,
|
||||
MotorDriver& motor,
|
||||
P_controller& controller,
|
||||
//P_controller& controller,
|
||||
Controller& controller,
|
||||
State& initialization,
|
||||
State& operational
|
||||
State& operational,
|
||||
State& stopped
|
||||
);
|
||||
|
||||
|
||||
// void set_states(
|
||||
// State* initialization,
|
||||
// State* operational,
|
||||
// State* stopped
|
||||
// );
|
||||
|
||||
|
||||
void begin();
|
||||
|
||||
void update();
|
||||
@@ -53,6 +67,10 @@ public:
|
||||
|
||||
void to_operational();
|
||||
|
||||
void to_stopped();
|
||||
|
||||
bool fault_active();
|
||||
|
||||
|
||||
void run_speed_controller();
|
||||
|
||||
@@ -60,9 +78,17 @@ public:
|
||||
|
||||
void reset_speed_measurement();
|
||||
|
||||
void reset_controller();
|
||||
|
||||
|
||||
|
||||
void led_on();
|
||||
|
||||
void led_off();
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,10 +9,14 @@ public:
|
||||
void begin();
|
||||
|
||||
void forward();
|
||||
|
||||
void reverse();
|
||||
|
||||
void stop();
|
||||
|
||||
void set(double pwm);
|
||||
|
||||
bool fault_active(); // Fault detection for part 2
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,9 @@
|
||||
#ifndef P_CONTROLLER_H
|
||||
#define P_CONTROLLER_H
|
||||
|
||||
class P_controller
|
||||
#include "controller.h"
|
||||
|
||||
class P_controller : public Controller
|
||||
{
|
||||
private:
|
||||
double Kp;
|
||||
@@ -9,7 +11,10 @@ private:
|
||||
public:
|
||||
P_controller(double Kp);
|
||||
|
||||
double update(double ref, double actual);
|
||||
double update(double ref, double actual) override;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
#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
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef STOPPED_STATE_H
|
||||
#define STOPPED_STATE_H
|
||||
|
||||
#include "state.h"
|
||||
|
||||
class StoppedState : public State
|
||||
{
|
||||
private:
|
||||
unsigned long previous_time; // Used for blinking LED
|
||||
bool led_state; // Ditto
|
||||
|
||||
public:
|
||||
StoppedState();
|
||||
|
||||
void enter() override;
|
||||
|
||||
void update() override;
|
||||
|
||||
void command(char c) override;
|
||||
|
||||
void exit() override;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
+2
-1
@@ -14,4 +14,5 @@ board = nanoatmega328new
|
||||
framework = arduino
|
||||
|
||||
monitor_speed = 115200
|
||||
|
||||
test_framework = unity
|
||||
test_build_src = yes
|
||||
|
||||
@@ -15,7 +15,10 @@ void InitializationState::enter()
|
||||
|
||||
context->reset_speed_measurement();
|
||||
|
||||
context->reset_controller();
|
||||
|
||||
enter_time_ms = millis();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+20
-4
@@ -3,27 +3,35 @@
|
||||
|
||||
#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"
|
||||
|
||||
#include "initialization_state.h"
|
||||
#include "operational_state.h"
|
||||
#include "stopped_state.h"
|
||||
|
||||
|
||||
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;
|
||||
|
||||
OperationalState operational_state;
|
||||
|
||||
StoppedState stopped_state;
|
||||
|
||||
// Stöðurnar verða að vera skilgreindar á undan motor_controller
|
||||
MotorController motor_controller(
|
||||
@@ -31,7 +39,8 @@ MotorController motor_controller(
|
||||
motor,
|
||||
controller,
|
||||
initialization_state,
|
||||
operational_state
|
||||
operational_state,
|
||||
stopped_state
|
||||
);
|
||||
|
||||
|
||||
@@ -51,6 +60,13 @@ int main()
|
||||
sei();
|
||||
|
||||
|
||||
// motor_controller.set_states(
|
||||
// &initialization_state,
|
||||
// &operational_state,
|
||||
// &stopped_state
|
||||
// );
|
||||
|
||||
|
||||
motor_controller.begin();
|
||||
|
||||
|
||||
|
||||
@@ -7,13 +7,17 @@
|
||||
MotorController::MotorController(
|
||||
RotaryEncoderDriver& encoder,
|
||||
MotorDriver& motor,
|
||||
P_controller& controller,
|
||||
//P_controller& controller,
|
||||
//PI_controller& controller;
|
||||
Controller& controller,
|
||||
State& initialization,
|
||||
State& operational
|
||||
State& operational,
|
||||
State& stopped
|
||||
)
|
||||
: current_state(nullptr),
|
||||
initialization_state(initialization),
|
||||
operational_state(operational),
|
||||
stopped_state(stopped),
|
||||
encoder(encoder),
|
||||
motor(motor),
|
||||
controller(controller),
|
||||
@@ -22,9 +26,22 @@ MotorController::MotorController(
|
||||
{
|
||||
initialization_state.set_context(this);
|
||||
operational_state.set_context(this);
|
||||
stopped_state.set_context(this);
|
||||
}
|
||||
|
||||
|
||||
// void MotorController::set_states(
|
||||
// State* initialization,
|
||||
// State* operational,
|
||||
// State* stopped
|
||||
// )
|
||||
|
||||
// initialization_state = initialization;
|
||||
// operational_state = operational;
|
||||
// stopped_state = stopped;
|
||||
|
||||
|
||||
|
||||
void MotorController::begin()
|
||||
{
|
||||
led.init();
|
||||
@@ -43,6 +60,8 @@ void MotorController::transition_to(State& new_state)
|
||||
|
||||
current_state = &new_state;
|
||||
|
||||
current_state->set_context(this);
|
||||
|
||||
current_state->enter();
|
||||
}
|
||||
|
||||
@@ -105,14 +124,29 @@ void MotorController::reset_speed_measurement()
|
||||
encoder.reset_speed_measurement();
|
||||
}
|
||||
|
||||
void MotorController::to_stopped()
|
||||
{
|
||||
transition_to(stopped_state);
|
||||
}
|
||||
|
||||
bool MotorController::fault_active()
|
||||
{
|
||||
return motor.fault_active();
|
||||
}
|
||||
|
||||
void MotorController::reset_controller()
|
||||
{
|
||||
controller.reset();
|
||||
}
|
||||
|
||||
void MotorController::led_on()
|
||||
{
|
||||
led.set_hi();
|
||||
led.set_hi();
|
||||
}
|
||||
|
||||
|
||||
void MotorController::led_off()
|
||||
{
|
||||
led.set_lo();
|
||||
led.set_lo();
|
||||
}
|
||||
|
||||
|
||||
@@ -43,10 +43,16 @@ void MotorDriver::begin()
|
||||
{
|
||||
// Arduino D3 = PD3 = AIN1
|
||||
// Arduino D11 = PB3 = AIN2
|
||||
// Arduino D4 = PD4 = FLT
|
||||
|
||||
DDRD |= (1 << DDD3);
|
||||
DDRB |= (1 << DDB3);
|
||||
|
||||
DDRD &= ~(1 << DDD4);
|
||||
|
||||
PORTD |= (1 << PORTD4); // Enable pull-up resistor on D4 for part 2
|
||||
|
||||
|
||||
// Timer2 Fast PWM
|
||||
TCCR2A = 0;
|
||||
TCCR2B = 0;
|
||||
@@ -116,3 +122,9 @@ void MotorDriver::set(double pwm)
|
||||
ain2_pwm(duty);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool MotorDriver::fault_active()
|
||||
{
|
||||
return !(PIND & (1 << PIND4));
|
||||
}
|
||||
|
||||
@@ -11,9 +11,26 @@ void OperationalState::enter()
|
||||
context->led_on();
|
||||
}
|
||||
|
||||
// Part 1
|
||||
|
||||
//void OperationalState::update()
|
||||
//{
|
||||
// context->run_speed_controller();
|
||||
//}
|
||||
|
||||
// Part 2
|
||||
|
||||
void OperationalState::update()
|
||||
{
|
||||
if (context->fault_active())
|
||||
{
|
||||
Serial.println("Fault detected");
|
||||
|
||||
context->to_stopped();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
context->run_speed_controller();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "stopped_state.h"
|
||||
#include "motor_controller.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
StoppedState::StoppedState()
|
||||
: previous_time(0),
|
||||
led_state(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void StoppedState::enter()
|
||||
{
|
||||
Serial.println("State: Stopped");
|
||||
|
||||
context->stop_motor();
|
||||
|
||||
led_state = false;
|
||||
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
previous_time = millis();
|
||||
}
|
||||
|
||||
// Controlling the blinking of the LED @ 2Hz
|
||||
|
||||
void StoppedState::update()
|
||||
{
|
||||
unsigned long current_time = millis();
|
||||
|
||||
if (current_time - previous_time >= 250)
|
||||
{
|
||||
previous_time = current_time;
|
||||
|
||||
led_state = !led_state;
|
||||
|
||||
digitalWrite(
|
||||
LED_BUILTIN,
|
||||
led_state ? HIGH : LOW
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Commands for Stopped [ r = reset and o for operational]
|
||||
|
||||
void StoppedState::command(char c)
|
||||
{
|
||||
if (c == 'r' || c == 'R')
|
||||
{
|
||||
Serial.println("Command: reset");
|
||||
|
||||
context->to_initialization();
|
||||
}
|
||||
|
||||
else if (c == 'o' || c == 'O')
|
||||
{
|
||||
Serial.println("Command: set operational");
|
||||
|
||||
context->to_operational();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void StoppedState::exit()
|
||||
{
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
@@ -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