2022-03-28 22:46:58 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/pwm.h"
|
2022-04-05 16:53:36 +01:00
|
|
|
#include "common/pimoroni_common.hpp"
|
2022-03-28 22:46:58 +01:00
|
|
|
#include "motor_state.hpp"
|
|
|
|
|
2022-04-05 16:53:36 +01:00
|
|
|
using namespace pimoroni;
|
|
|
|
|
2022-03-28 22:46:58 +01:00
|
|
|
namespace motor {
|
|
|
|
|
|
|
|
class Motor2 {
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2022-04-05 16:53:36 +01:00
|
|
|
pin_pair motor_pins;
|
2022-03-28 22:46:58 +01:00
|
|
|
MotorState state;
|
|
|
|
pwm_config pwm_cfg;
|
|
|
|
uint16_t pwm_period;
|
|
|
|
float pwm_frequency;
|
2022-04-11 13:35:53 +01:00
|
|
|
DecayMode motor_mode;
|
2022-03-28 22:46:58 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Constructors/Destructor
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2022-04-09 01:41:42 +01:00
|
|
|
Motor2(const pin_pair &pins, Direction direction = NORMAL, float speed_scale = MotorState::DEFAULT_SPEED_SCALE,
|
|
|
|
float deadzone = MotorState::DEFAULT_DEADZONE, float freq = MotorState::DEFAULT_FREQUENCY, DecayMode mode = MotorState::DEFAULT_DECAY_MODE);
|
2022-03-28 22:46:58 +01:00
|
|
|
~Motor2();
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
|
|
|
bool init();
|
|
|
|
|
|
|
|
// For print access in micropython
|
2022-04-05 16:53:36 +01:00
|
|
|
pin_pair pins() const;
|
2022-03-28 22:46:58 +01:00
|
|
|
|
|
|
|
void enable();
|
|
|
|
void disable();
|
|
|
|
bool is_enabled() const;
|
|
|
|
|
2022-04-04 20:00:03 +01:00
|
|
|
float duty() const;
|
|
|
|
void duty(float duty);
|
2022-03-28 22:46:58 +01:00
|
|
|
|
|
|
|
float speed() const;
|
|
|
|
void speed(float speed);
|
|
|
|
|
|
|
|
float frequency() const;
|
|
|
|
bool frequency(float freq);
|
|
|
|
|
2022-04-05 16:53:36 +01:00
|
|
|
//--------------------------------------------------
|
2022-03-28 22:46:58 +01:00
|
|
|
|
|
|
|
void stop();
|
2022-04-05 16:53:36 +01:00
|
|
|
void coast();
|
2022-04-11 13:35:53 +01:00
|
|
|
void brake();
|
2022-04-05 16:53:36 +01:00
|
|
|
void full_negative();
|
|
|
|
void full_positive();
|
2022-03-28 22:46:58 +01:00
|
|
|
void to_percent(float in, float in_min = MotorState::ZERO_PERCENT, float in_max = MotorState::ONEHUNDRED_PERCENT);
|
|
|
|
void to_percent(float in, float in_min, float in_max, float speed_min, float speed_max);
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
2022-04-05 16:53:36 +01:00
|
|
|
|
2022-04-09 01:41:42 +01:00
|
|
|
Direction direction() const;
|
|
|
|
void direction(Direction direction);
|
2022-04-05 16:53:36 +01:00
|
|
|
|
|
|
|
float speed_scale() const;
|
|
|
|
void speed_scale(float speed_scale);
|
|
|
|
|
2022-04-09 01:41:42 +01:00
|
|
|
float deadzone() const;
|
|
|
|
void deadzone(float deadzone);
|
2022-04-05 16:53:36 +01:00
|
|
|
|
2022-04-11 13:35:53 +01:00
|
|
|
DecayMode decay_mode();
|
|
|
|
void decay_mode(DecayMode mode);
|
|
|
|
|
2022-04-05 16:53:36 +01:00
|
|
|
//--------------------------------------------------
|
2022-03-28 22:46:58 +01:00
|
|
|
private:
|
2022-04-09 01:41:42 +01:00
|
|
|
void apply_duty(float duty, DecayMode mode);
|
2022-03-28 22:46:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|