2022-03-28 22:46:58 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
2022-04-25 16:16:02 +01:00
|
|
|
#include "common/pimoroni_common.hpp"
|
2022-03-28 22:46:58 +01:00
|
|
|
|
2022-04-25 16:16:02 +01:00
|
|
|
using namespace pimoroni;
|
2022-03-28 22:46:58 +01:00
|
|
|
|
2022-04-25 16:16:02 +01:00
|
|
|
namespace motor {
|
2022-04-05 16:53:36 +01:00
|
|
|
|
2022-04-09 01:41:42 +01:00
|
|
|
enum DecayMode {
|
|
|
|
FAST_DECAY = 0, //aka 'Coasting'
|
|
|
|
SLOW_DECAY = 1, //aka 'Braking'
|
|
|
|
};
|
2022-03-28 22:46:58 +01:00
|
|
|
|
2022-04-09 01:41:42 +01:00
|
|
|
class MotorState {
|
2022-03-28 22:46:58 +01:00
|
|
|
//--------------------------------------------------
|
|
|
|
// Constants
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-04-05 16:53:36 +01:00
|
|
|
static constexpr float DEFAULT_SPEED_SCALE = 1.0f; // The standard motor speed scale
|
2022-04-25 16:16:02 +01:00
|
|
|
static constexpr float DEFAULT_ZEROPOINT = 0.0f; // The standard motor deadzone
|
|
|
|
static constexpr float DEFAULT_DEADZONE = 0.05f; // The standard motor deadzone
|
2022-04-09 01:41:42 +01:00
|
|
|
|
2022-04-05 16:53:36 +01:00
|
|
|
static const DecayMode DEFAULT_DECAY_MODE = SLOW_DECAY; // The standard motor decay behaviour
|
2022-03-28 22:46:58 +01:00
|
|
|
static constexpr float DEFAULT_FREQUENCY = 25000.0f; // The standard motor update rate
|
2022-04-05 16:53:36 +01:00
|
|
|
static constexpr float MIN_FREQUENCY = 10.0f;
|
|
|
|
static constexpr float MAX_FREQUENCY = 50000.0f;
|
2022-04-04 20:00:03 +01:00
|
|
|
|
2022-03-28 22:46:58 +01:00
|
|
|
static constexpr float ZERO_PERCENT = 0.0f;
|
|
|
|
static constexpr float ONEHUNDRED_PERCENT = 1.0f;
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2022-04-04 20:00:03 +01:00
|
|
|
float motor_speed;
|
|
|
|
float last_enabled_duty;
|
|
|
|
bool enabled;
|
2022-04-05 16:53:36 +01:00
|
|
|
|
|
|
|
// Customisation variables
|
|
|
|
Direction motor_direction;
|
|
|
|
float motor_scale;
|
2022-04-25 16:16:02 +01:00
|
|
|
float motor_zeropoint;
|
2022-04-05 16:53:36 +01:00
|
|
|
float motor_deadzone;
|
2022-03-28 22:46:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
MotorState();
|
2022-04-25 16:16:02 +01:00
|
|
|
MotorState(Direction direction, float speed_scale, float zeropoint, float deadzone);
|
2022-03-28 22:46:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-04-04 20:00:03 +01:00
|
|
|
float enable_with_return();
|
|
|
|
float disable_with_return();
|
2022-03-28 22:46:58 +01:00
|
|
|
bool is_enabled() const;
|
2022-04-05 16:53:36 +01:00
|
|
|
|
2022-03-28 22:46:58 +01:00
|
|
|
float get_duty() const;
|
2022-04-09 01:41:42 +01:00
|
|
|
float get_deadzoned_duty() const;
|
2022-03-28 22:46:58 +01:00
|
|
|
float set_duty_with_return(float duty);
|
|
|
|
|
|
|
|
float get_speed() const;
|
|
|
|
float set_speed_with_return(float speed);
|
|
|
|
|
2022-04-04 20:00:03 +01:00
|
|
|
//--------------------------------------------------
|
|
|
|
|
|
|
|
float stop_with_return();
|
2022-04-05 16:53:36 +01:00
|
|
|
float full_negative_with_return();
|
|
|
|
float full_positive_with_return();
|
2022-03-28 22:46:58 +01:00
|
|
|
float to_percent_with_return(float in, float in_min = ZERO_PERCENT, float in_max = ONEHUNDRED_PERCENT);
|
|
|
|
float to_percent_with_return(float in, float in_min, float in_max, float speed_min, float speed_max);
|
|
|
|
|
2022-04-05 16:53:36 +01:00
|
|
|
//--------------------------------------------------
|
|
|
|
|
|
|
|
Direction get_direction() const;
|
|
|
|
void set_direction(Direction direction);
|
|
|
|
|
|
|
|
float get_speed_scale() const;
|
|
|
|
void set_speed_scale(float speed_scale);
|
|
|
|
|
2022-04-25 16:16:02 +01:00
|
|
|
float get_zeropoint() const;
|
|
|
|
void set_zeropoint(float zeropoint);
|
|
|
|
|
2022-04-09 01:41:42 +01:00
|
|
|
float get_deadzone() const;
|
|
|
|
float set_deadzone_with_return(float deadzone);
|
2022-04-05 16:53:36 +01:00
|
|
|
|
2022-03-28 22:46:58 +01:00
|
|
|
//--------------------------------------------------
|
|
|
|
static int32_t duty_to_level(float duty, uint32_t resolution);
|
|
|
|
|
|
|
|
static float map_float(float in, float in_min, float in_max, float out_min, float out_max);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|