Encoder fyrir position í int32, stöðuvél án null-pointera, nefndir fastar, LED gegnum context
This commit is contained in:
+18
-11
@@ -4,6 +4,7 @@
|
||||
#include "rotary_encoder_driver.h"
|
||||
#include "motor_driver.h"
|
||||
#include "p_controller.h"
|
||||
#include "digital_out.h"
|
||||
|
||||
class State;
|
||||
|
||||
@@ -11,15 +12,21 @@ class State;
|
||||
class MotorController
|
||||
{
|
||||
private:
|
||||
static constexpr double DEFAULT_REFERENCE_RPM = 40.0;
|
||||
static constexpr double PWM_PER_PERCENT = 255.0 / 100.0;
|
||||
static const uint8_t LED_PIN = 5; // PB5 = D13 = LED_BUILTIN
|
||||
|
||||
State* current_state;
|
||||
|
||||
State* initialization_state;
|
||||
State* operational_state;
|
||||
State& initialization_state;
|
||||
State& operational_state;
|
||||
|
||||
RotaryEncoderDriver& encoder;
|
||||
MotorDriver& motor;
|
||||
P_controller& controller;
|
||||
|
||||
Digital_out led;
|
||||
|
||||
double reference_rpm;
|
||||
|
||||
|
||||
@@ -27,13 +34,9 @@ public:
|
||||
MotorController(
|
||||
RotaryEncoderDriver& encoder,
|
||||
MotorDriver& motor,
|
||||
P_controller& controller
|
||||
);
|
||||
|
||||
|
||||
void set_states(
|
||||
State* initialization,
|
||||
State* operational
|
||||
P_controller& controller,
|
||||
State& initialization,
|
||||
State& operational
|
||||
);
|
||||
|
||||
|
||||
@@ -43,7 +46,7 @@ public:
|
||||
|
||||
void command(char c);
|
||||
|
||||
void transition_to(State* new_state);
|
||||
void transition_to(State& new_state);
|
||||
|
||||
|
||||
void to_initialization();
|
||||
@@ -56,6 +59,10 @@ public:
|
||||
void stop_motor();
|
||||
|
||||
void reset_speed_measurement();
|
||||
|
||||
void led_on();
|
||||
|
||||
void led_off();
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -4,37 +4,36 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "digital_in.h"
|
||||
//#include "digital_out.h"
|
||||
#include "timer_usec.h"
|
||||
|
||||
class RotaryEncoderDriver {
|
||||
static const uint8_t HIST_LEN = 4; // 4 x 4 ms = 16 ms gluggi fyrir rpm-utreikning
|
||||
public:
|
||||
static constexpr uint16_t SAMPLE_PERIOD_US = 4000; // Ts = 4 ms
|
||||
static constexpr float COUNTS_PER_REV = 1050.0f;
|
||||
|
||||
volatile uint8_t cycle;
|
||||
volatile int16_t count;
|
||||
volatile int16_t prev_count;
|
||||
private:
|
||||
static const uint8_t HIST_LEN = 4; // 4 x Ts = 16 ms gluggi fyrir rpm-utreikning
|
||||
|
||||
volatile int32_t count;
|
||||
volatile int16_t delta;
|
||||
volatile bool speed_ready;
|
||||
|
||||
volatile int16_t history[HIST_LEN];
|
||||
volatile int32_t history[HIST_LEN];
|
||||
volatile uint8_t hist_idx;
|
||||
|
||||
//Digital_out led;
|
||||
|
||||
Digital_in eA;
|
||||
Digital_in eB;
|
||||
|
||||
Timer_usec timer;
|
||||
|
||||
public:
|
||||
volatile int16_t delta;
|
||||
|
||||
RotaryEncoderDriver();
|
||||
|
||||
void begin();
|
||||
|
||||
void update();
|
||||
|
||||
int position();
|
||||
int32_t position();
|
||||
|
||||
void update_delta();
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ class MotorController;
|
||||
class State
|
||||
{
|
||||
protected:
|
||||
MotorController* context;
|
||||
MotorController* context = nullptr;
|
||||
|
||||
public:
|
||||
virtual ~State()
|
||||
|
||||
Reference in New Issue
Block a user