Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be4cf10d7e |
@@ -11,30 +11,17 @@ skinparam state {
|
||||
|
||||
state Initialization
|
||||
Initialization : Entry / Stop motor
|
||||
Initialization : Do / Initialize internal parameters
|
||||
Initialization : Do / Reset speed measurement
|
||||
Initialization : Entry / Initialize internal parameters
|
||||
Initialization : Entry / Reset speed measurement
|
||||
Initialization : Do / Wait 2 seconds
|
||||
Initialization : Exit / Send Boot-up message
|
||||
|
||||
|
||||
|
||||
state Operational
|
||||
Operational : Entry / LED on
|
||||
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
|
||||
|
||||
@@ -20,7 +20,6 @@ private:
|
||||
|
||||
State& initialization_state;
|
||||
State& operational_state;
|
||||
State& stopped_state;
|
||||
|
||||
RotaryEncoderDriver& encoder;
|
||||
MotorDriver& motor;
|
||||
@@ -37,18 +36,10 @@ public:
|
||||
MotorDriver& motor,
|
||||
P_controller& controller,
|
||||
State& initialization,
|
||||
State& operational,
|
||||
State& stopped
|
||||
State& operational
|
||||
);
|
||||
|
||||
|
||||
// void set_states(
|
||||
// State* initialization,
|
||||
// State* operational,
|
||||
// State* stopped
|
||||
// );
|
||||
|
||||
|
||||
void begin();
|
||||
|
||||
void update();
|
||||
@@ -62,10 +53,6 @@ public:
|
||||
|
||||
void to_operational();
|
||||
|
||||
void to_stopped();
|
||||
|
||||
bool fault_active();
|
||||
|
||||
|
||||
void run_speed_controller();
|
||||
|
||||
|
||||
@@ -9,14 +9,10 @@ public:
|
||||
void begin();
|
||||
|
||||
void forward();
|
||||
|
||||
void reverse();
|
||||
|
||||
void stop();
|
||||
|
||||
void set(double pwm);
|
||||
|
||||
bool fault_active(); // Fault detection for part 2
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,25 +0,0 @@
|
||||
#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
|
||||
+1
-11
@@ -9,7 +9,6 @@
|
||||
|
||||
#include "initialization_state.h"
|
||||
#include "operational_state.h"
|
||||
#include "stopped_state.h"
|
||||
|
||||
|
||||
RotaryEncoderDriver encoder;
|
||||
@@ -25,7 +24,6 @@ InitializationState initialization_state;
|
||||
|
||||
OperationalState operational_state;
|
||||
|
||||
StoppedState stopped_state;
|
||||
|
||||
// Stöðurnar verða að vera skilgreindar á undan motor_controller
|
||||
MotorController motor_controller(
|
||||
@@ -33,8 +31,7 @@ MotorController motor_controller(
|
||||
motor,
|
||||
controller,
|
||||
initialization_state,
|
||||
operational_state,
|
||||
stopped_state
|
||||
operational_state
|
||||
);
|
||||
|
||||
|
||||
@@ -54,13 +51,6 @@ int main()
|
||||
sei();
|
||||
|
||||
|
||||
// motor_controller.set_states(
|
||||
// &initialization_state,
|
||||
// &operational_state,
|
||||
// &stopped_state
|
||||
// );
|
||||
|
||||
|
||||
motor_controller.begin();
|
||||
|
||||
|
||||
|
||||
@@ -9,13 +9,11 @@ MotorController::MotorController(
|
||||
MotorDriver& motor,
|
||||
P_controller& controller,
|
||||
State& initialization,
|
||||
State& operational,
|
||||
State& stopped
|
||||
State& operational
|
||||
)
|
||||
: current_state(nullptr),
|
||||
initialization_state(initialization),
|
||||
operational_state(operational),
|
||||
stopped_state(stopped),
|
||||
encoder(encoder),
|
||||
motor(motor),
|
||||
controller(controller),
|
||||
@@ -24,22 +22,9 @@ 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();
|
||||
@@ -120,15 +105,6 @@ 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::led_on()
|
||||
{
|
||||
|
||||
@@ -43,16 +43,10 @@ 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;
|
||||
@@ -122,9 +116,3 @@ void MotorDriver::set(double pwm)
|
||||
ain2_pwm(duty);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool MotorDriver::fault_active()
|
||||
{
|
||||
return !(PIND & (1 << PIND4));
|
||||
}
|
||||
|
||||
@@ -11,26 +11,9 @@ 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();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user