ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
grove_tb6612fng.cpp
Go to the documentation of this file.
1#include "grove_tb6612fng.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome {
6namespace grove_tb6612fng {
7
8static const char *const TAG = "GroveMotorDriveTB6612FNG";
9
10static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_BRAKE = 0x00;
11static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STOP = 0x01;
12static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_CW = 0x02;
13static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_CCW = 0x03;
14static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STANDBY = 0x04;
15static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_NOT_STANDBY = 0x05;
16static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_RUN = 0x06;
17static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_STOP = 0x07;
18static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_KEEP_RUN = 0x08;
19static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_SET_ADDR = 0x11;
20
22 ESP_LOGCONFIG(TAG, "GroveMotorDriveTB6612FNG:");
23 LOG_I2C_DEVICE(this);
24}
25
27 if (!this->standby()) {
28 this->mark_failed();
29 return;
30 }
31}
32
34 uint8_t status = 0;
35 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STANDBY, &status, 1) != i2c::ERROR_OK) {
36 ESP_LOGW(TAG, "Set standby failed!");
37 this->status_set_warning();
38 return false;
39 }
40 return true;
41}
42
44 uint8_t status = 0;
45 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_NOT_STANDBY, &status, 1) != i2c::ERROR_OK) {
46 ESP_LOGW(TAG, "Set not standby failed!");
47 this->status_set_warning();
48 return false;
49 }
50 return true;
51}
52
54 if (addr == 0x00 || addr >= 0x80) {
55 return;
56 }
57 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_SET_ADDR, &addr, 1) != i2c::ERROR_OK) {
58 ESP_LOGW(TAG, "Set new i2c address failed!");
59 this->status_set_warning();
60 return;
61 }
62 this->set_i2c_address(addr);
63}
64
65void GroveMotorDriveTB6612FNG::dc_motor_run(uint8_t channel, int16_t speed) {
66 speed = clamp<int16_t>(speed, -255, 255);
67
68 buffer_[0] = channel;
69 if (speed >= 0) {
70 buffer_[1] = speed;
71 } else {
72 buffer_[1] = (uint8_t) (-speed);
73 }
74
75 if (speed >= 0) {
76 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_CW, buffer_, 2) != i2c::ERROR_OK) {
77 ESP_LOGW(TAG, "Run motor failed!");
78 this->status_set_warning();
79 return;
80 }
81 } else {
82 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_CCW, buffer_, 2) != i2c::ERROR_OK) {
83 ESP_LOGW(TAG, "Run motor failed!");
84 this->status_set_warning();
85 return;
86 }
87 }
88}
89
91 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_BRAKE, &channel, 1) != i2c::ERROR_OK) {
92 ESP_LOGW(TAG, "Break motor failed!");
93 this->status_set_warning();
94 return;
95 }
96}
97
99 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STOP, &channel, 1) != i2c::ERROR_OK) {
100 ESP_LOGW(TAG, "Stop dc motor failed!");
101 this->status_set_warning();
102 return;
103 }
104}
105
107 uint8_t cw = 0;
108 // 0.1ms_per_step
109 uint16_t ms_per_step = 0;
110
111 if (steps > 0) {
112 cw = 1;
113 }
114 // stop
115 else if (steps == 0) {
116 this->stepper_stop();
117 return;
118 } else if (steps == INT16_MIN) {
119 steps = INT16_MAX;
120 } else {
121 steps = -steps;
122 }
123
124 rpm = clamp<uint16_t>(rpm, 1, 300);
125
126 ms_per_step = (uint16_t) (3000.0 / (float) rpm);
127 buffer_[0] = mode;
128 buffer_[1] = cw; //(cw=1) => cw; (cw=0) => ccw
129 buffer_[2] = steps;
130 buffer_[3] = (steps >> 8);
131 buffer_[4] = ms_per_step;
132 buffer_[5] = (ms_per_step >> 8);
133
134 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_RUN, buffer_, 1) != i2c::ERROR_OK) {
135 ESP_LOGW(TAG, "Run stepper failed!");
136 this->status_set_warning();
137 return;
138 }
139}
140
142 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_STOP, nullptr, 1) != i2c::ERROR_OK) {
143 ESP_LOGW(TAG, "Send stop stepper failed!");
144 this->status_set_warning();
145 return;
146 }
147}
148
150 // 4=>infinite ccw 5=>infinite cw
151 uint8_t cw = (is_cw) ? 5 : 4;
152 // 0.1ms_per_step
153 uint16_t ms_per_step = 0;
154
155 rpm = clamp<uint16_t>(rpm, 1, 300);
156 ms_per_step = (uint16_t) (3000.0 / (float) rpm);
157
158 buffer_[0] = mode;
159 buffer_[1] = cw; //(cw=1) => cw; (cw=0) => ccw
160 buffer_[2] = ms_per_step;
161 buffer_[3] = (ms_per_step >> 8);
162
163 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_KEEP_RUN, buffer_, 4) != i2c::ERROR_OK) {
164 ESP_LOGW(TAG, "Write stepper keep run failed");
165 this->status_set_warning();
166 return;
167 }
168}
169} // namespace grove_tb6612fng
170} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void stepper_keep_run(StepperModeTypeT mode, uint16_t rpm, bool is_cw)
void dc_motor_run(uint8_t channel, int16_t speed)
void stepper_run(StepperModeTypeT mode, int16_t steps, uint16_t rpm)
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:25
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:140
int speed
Definition fan.h:1
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7