ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
m5stack_unit_bldc.h
Go to the documentation of this file.
1#pragma once
2
7#ifdef USE_NUMBER
9#endif
10#ifdef USE_SELECT
12#endif
13#ifdef USE_SENSOR
15#endif
16#ifdef USE_TEXT_SENSOR
18#endif
19
21
22// I2C register map, from M5Stack's "Unit BLDC I2C Protocol" datasheet and the M5UnitBLDC library
23// (https://github.com/m5stack/M5Unit-BLDC). Multi-byte values are little-endian.
24enum : uint8_t {
25 REG_MODE = 0x00, // 1B, 0: open loop, 1: closed loop
26 REG_PWM = 0x10, // 2B, uint16_t duty 0-2047
27 REG_READBACK_RPM = 0x20, // 4B, read only, float
28 REG_READBACK_FREQ = 0x30, // 4B, read only, float Hz
29 REG_SET_RPM = 0x40, // 4B, float, closed loop only
30 REG_PID = 0x50, // 12B, 3x int32_t, each gain * 100
31 REG_DIRECTION = 0x60, // 1B, 0: forward, 1: backward
32 REG_MOTOR_CONFIG = 0x70, // 2B, motor model, pole pairs
33 REG_MOTOR_STATUS = 0x80, // 1B, read only, 0: standby, 1: running, 2: error
34 REG_SAVE_TO_FLASH = 0xF0, // 1B, write 1 to persist model, pole pairs and PID
35};
36
38// Values match the direction select's option order
39enum class Direction : uint8_t { DIRECTION_FORWARD = 0, DIRECTION_BACKWARD = 1 };
41
44 public:
45 M5StackUnitBldc(ControlMode mode, Direction initial_direction, MotorModel model, uint8_t pole_pairs,
46 bool save_to_flash)
47 : current_direction_(initial_direction),
49 initial_direction_(initial_direction),
50 motor_model_(model),
51 pole_pairs_(pole_pairs),
52 save_to_flash_(save_to_flash) {}
53
54 void setup() override;
55 void update() override;
56 void dump_config() override;
57
59 void set_pid(int32_t p, int32_t i, int32_t d) {
60 this->pid_[0] = p;
61 this->pid_[1] = i;
62 this->pid_[2] = d;
63 this->has_pid_ = true;
64 }
65
66#ifdef USE_SENSOR
69#endif
70#ifdef USE_TEXT_SENSOR
71 SUB_TEXT_SENSOR(status)
72#endif
73#ifdef USE_NUMBER
75 SUB_NUMBER(command)
76 void control_command(float value) {
77 if (this->write_command(value))
78 this->command_number_->publish_state(value);
79 }
80#endif
81#ifdef USE_SELECT
83 void control_direction(size_t index) {
84 if (this->write_direction(static_cast<Direction>(index)))
85 this->direction_select_->publish_state(index);
86 }
87#endif
88
91 bool write_command(float value);
93
94 protected:
95 static constexpr uint32_t DIRECTION_CHANGE_POLL_ID = 0;
96
97 template<typename T> bool read_reg_(uint8_t reg, T *value) {
98 return this->read_bytes(reg, reinterpret_cast<uint8_t *>(value), sizeof(T));
99 }
100 template<typename T> bool write_reg_(uint8_t reg, const T &value) {
101 return this->write_bytes(reg, reinterpret_cast<const uint8_t *>(&value), sizeof(T));
102 }
103 bool write_command_raw_(float value);
105 bool read_command_raw_(float *value);
107
108 int32_t pid_[3]{};
110 // Last command, restored after the stop a direction change needs
112 Direction current_direction_; // what the unit drives, so selecting it again does not stop the motor
116 const uint8_t pole_pairs_;
117 const bool save_to_flash_;
118 bool has_pid_{false};
119 // While a direction change waits for the motor to stop, commands are only cached
122#ifdef USE_TEXT_SENSOR
123 uint8_t last_status_{0xFF};
124#endif
125};
126
127} // namespace esphome::m5stack_unit_bldc
BedjetMode mode
BedJet operating mode.
uint16_le_t frequency
Definition bl0942.h:6
This class simplifies creating components that periodically check a state.
Definition component.h:532
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
Hub for one M5Stack M5Unit-BLDC brushless motor driver.
SUB_SENSOR(rpm) SUB_SENSOR(frequency) SUB_TEXT_SENSOR(status) SUB_NUMBER(command) void control_command(float value)
The PWM duty number in open loop, the target RPM number in closed loop.
static constexpr uint32_t DIRECTION_CHANGE_POLL_ID
void set_pid(int32_t p, int32_t i, int32_t d)
PID gains as the device stores them, each multiplied by 100.
bool write_reg_(uint8_t reg, const T &value)
SUB_SELECT(direction) void control_direction(size_t index)
bool write_command(float value)
Send the PWM duty (open loop) or target RPM (closed loop).
M5StackUnitBldc(ControlMode mode, Direction initial_direction, MotorModel model, uint8_t pole_pairs, bool save_to_flash)
FanDirection direction
Definition fan.h:5
uint32_t value
Definition hal.cpp:41
static void uint32_t