2021-02-10 16:25:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "hardware/pio.h"
|
2022-04-13 20:12:44 +01:00
|
|
|
#include "common/pimoroni_common.hpp"
|
2021-02-10 16:25:25 +00:00
|
|
|
|
2022-04-22 16:06:54 +01:00
|
|
|
using namespace pimoroni;
|
|
|
|
|
|
|
|
namespace encoder {
|
2022-04-26 23:15:13 +01:00
|
|
|
static constexpr float MMME_CPR = 12.0f;
|
|
|
|
static constexpr float ROTARY_CPR = 24.0f;
|
2021-02-10 16:25:25 +00:00
|
|
|
|
|
|
|
class Encoder {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-05-05 16:54:02 +01:00
|
|
|
static constexpr float DEFAULT_COUNTS_PER_REV = ROTARY_CPR;
|
2022-04-17 16:16:59 +01:00
|
|
|
static const bool DEFAULT_COUNT_MICROSTEPS = false;
|
2022-04-13 11:29:40 +01:00
|
|
|
static const uint16_t DEFAULT_FREQ_DIVIDER = 1;
|
2021-02-10 16:25:25 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static const uint32_t STATE_A_MASK = 0x80000000;
|
|
|
|
static const uint32_t STATE_B_MASK = 0x40000000;
|
|
|
|
static const uint32_t STATE_A_LAST_MASK = 0x20000000;
|
|
|
|
static const uint32_t STATE_B_LAST_MASK = 0x10000000;
|
|
|
|
|
|
|
|
static const uint32_t STATES_MASK = STATE_A_MASK | STATE_B_MASK |
|
|
|
|
STATE_A_LAST_MASK | STATE_B_LAST_MASK;
|
|
|
|
|
|
|
|
static const uint32_t TIME_MASK = 0x0fffffff;
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Enums
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2022-04-17 16:16:59 +01:00
|
|
|
enum StepDir {
|
|
|
|
NO_DIR = 0,
|
|
|
|
INCREASING = 1,
|
|
|
|
DECREASING = -1,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum MicroStep : uint8_t {
|
|
|
|
MICROSTEP_0 = 0b00,
|
|
|
|
MICROSTEP_1 = 0b10,
|
|
|
|
MICROSTEP_2 = 0b11,
|
|
|
|
MICROSTEP_3 = 0b01,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Substructures
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-04-22 16:06:54 +01:00
|
|
|
class Capture {
|
2022-04-17 16:16:59 +01:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2022-04-19 15:44:31 +01:00
|
|
|
int32_t captured_count;
|
|
|
|
int32_t captured_delta;
|
|
|
|
float captured_frequency;
|
2022-04-17 16:16:59 +01:00
|
|
|
float counts_per_rev;
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-04-22 16:06:54 +01:00
|
|
|
Capture();
|
|
|
|
Capture(int32_t count, int32_t delta, float frequency, float counts_per_rev);
|
2022-04-17 16:16:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-04-19 15:44:31 +01:00
|
|
|
int32_t count() const;
|
|
|
|
int32_t delta() const;
|
|
|
|
float frequency() const;
|
|
|
|
|
2022-04-17 16:16:59 +01:00
|
|
|
float revolutions() const;
|
|
|
|
float degrees() const;
|
|
|
|
float radians() const;
|
|
|
|
|
|
|
|
float revolutions_delta() const;
|
|
|
|
float degrees_delta() const;
|
|
|
|
float radians_delta() const;
|
|
|
|
|
|
|
|
float revolutions_per_second() const;
|
|
|
|
float revolutions_per_minute() const;
|
|
|
|
float degrees_per_second() const;
|
|
|
|
float radians_per_second() const;
|
2021-02-10 16:25:25 +00:00
|
|
|
};
|
2022-04-13 20:12:44 +01:00
|
|
|
|
|
|
|
|
2021-02-10 16:25:25 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2022-04-13 20:12:44 +01:00
|
|
|
PIO pio;
|
|
|
|
uint sm;
|
|
|
|
pin_pair enc_pins;
|
2022-04-17 16:16:59 +01:00
|
|
|
uint enc_common_pin;
|
|
|
|
Direction enc_direction;
|
|
|
|
float enc_counts_per_rev;
|
2021-02-10 16:25:25 +00:00
|
|
|
|
2022-04-17 16:16:59 +01:00
|
|
|
const bool count_microsteps;
|
|
|
|
const uint16_t freq_divider;
|
|
|
|
const float clocks_per_time;
|
2021-02-10 16:25:25 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
2022-04-13 11:29:40 +01:00
|
|
|
|
2022-04-17 16:16:59 +01:00
|
|
|
volatile bool enc_state_a = false;
|
|
|
|
volatile bool enc_state_b = false;
|
|
|
|
volatile int32_t enc_count = 0;
|
|
|
|
volatile int32_t enc_cumulative_time = 0;
|
|
|
|
volatile int16_t enc_step = 0;
|
|
|
|
volatile int16_t enc_turn = 0;
|
|
|
|
|
|
|
|
volatile int32_t microstep_time = 0;
|
|
|
|
volatile StepDir step_dir = NO_DIR;
|
2021-02-10 16:25:25 +00:00
|
|
|
|
2022-04-17 16:16:59 +01:00
|
|
|
int32_t last_count = 0;
|
2022-04-25 12:28:42 +01:00
|
|
|
int32_t last_capture_count = 0;
|
2021-02-10 16:25:25 +00:00
|
|
|
|
2022-04-17 16:16:59 +01:00
|
|
|
bool initialised = false;
|
2022-04-13 20:12:44 +01:00
|
|
|
|
2021-02-10 16:25:25 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Statics
|
|
|
|
//--------------------------------------------------
|
2022-04-13 20:12:44 +01:00
|
|
|
static Encoder* encoders[NUM_PIOS][NUM_PIO_STATE_MACHINES];
|
|
|
|
static uint8_t claimed_sms[NUM_PIOS];
|
|
|
|
static uint pio_program_offset[NUM_PIOS];
|
|
|
|
static void pio_interrupt_handler(uint pio_idx);
|
|
|
|
static void pio0_interrupt_handler();
|
|
|
|
static void pio1_interrupt_handler();
|
2021-02-10 16:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-04-25 16:16:02 +01:00
|
|
|
Encoder(PIO pio, uint sm, const pin_pair &pins, uint common_pin = PIN_UNUSED, Direction direction = NORMAL_DIR,
|
2022-04-17 16:16:59 +01:00
|
|
|
float counts_per_rev = DEFAULT_COUNTS_PER_REV, bool count_microsteps = DEFAULT_COUNT_MICROSTEPS,
|
2021-02-10 16:25:25 +00:00
|
|
|
uint16_t freq_divider = DEFAULT_FREQ_DIVIDER);
|
|
|
|
~Encoder();
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
2022-04-13 20:12:44 +01:00
|
|
|
public:
|
2021-02-10 16:25:25 +00:00
|
|
|
bool init();
|
|
|
|
|
2022-04-13 20:12:44 +01:00
|
|
|
// For print access in micropython
|
|
|
|
pin_pair pins() const;
|
2022-04-17 16:16:59 +01:00
|
|
|
uint common_pin() const;
|
2022-04-13 20:12:44 +01:00
|
|
|
|
|
|
|
bool_pair state() const;
|
|
|
|
int32_t count() const;
|
2022-04-17 16:16:59 +01:00
|
|
|
int32_t delta();
|
|
|
|
void zero();
|
|
|
|
|
|
|
|
int16_t step() const;
|
|
|
|
int16_t turn() const;
|
|
|
|
|
2022-04-13 20:12:44 +01:00
|
|
|
float revolutions() const;
|
2022-04-17 16:16:59 +01:00
|
|
|
float degrees() const;
|
|
|
|
float radians() const;
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
|
|
|
Direction direction() const;
|
|
|
|
void direction(Direction direction);
|
|
|
|
|
2022-04-25 12:28:42 +01:00
|
|
|
float counts_per_rev() const;
|
|
|
|
void counts_per_rev(float counts_per_rev);
|
2021-02-10 16:25:25 +00:00
|
|
|
|
2022-04-22 16:06:54 +01:00
|
|
|
Capture capture();
|
2022-04-17 16:16:59 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
2021-02-10 16:25:25 +00:00
|
|
|
private:
|
2022-04-17 16:16:59 +01:00
|
|
|
void process_steps();
|
|
|
|
void microstep(int32_t time_since, bool up);
|
2021-02-10 16:25:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|