ESPHome 2026.6.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
6
7static const char *const TAG = "GroveMotorDriveTB6612FNG";
8
9static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_BRAKE = 0x00;
10static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STOP = 0x01;
11static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_CW = 0x02;
12static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_CCW = 0x03;
13static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STANDBY = 0x04;
14static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_NOT_STANDBY = 0x05;
15static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_RUN = 0x06;
16static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_STOP = 0x07;
17static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_KEEP_RUN = 0x08;
18static const uint8_t GROVE_MOTOR_DRIVER_I2C_CMD_SET_ADDR = 0x11;
19
21 ESP_LOGCONFIG(TAG, "GroveMotorDriveTB6612FNG:");
22 LOG_I2C_DEVICE(this);
23}
24
26 if (!this->standby()) {
27 this->mark_failed();
28 return;
29 }
30}
31
33 uint8_t status = 0;
34 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STANDBY, &status, 1) != i2c::ERROR_OK) {
35 ESP_LOGW(TAG, "Set standby failed!");
36 this->status_set_warning();
37 return false;
38 }
39 return true;
40}
41
43 uint8_t status = 0;
44 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_NOT_STANDBY, &status, 1) != i2c::ERROR_OK) {
45 ESP_LOGW(TAG, "Set not standby failed!");
46 this->status_set_warning();
47 return false;
48 }
49 return true;
50}
51
53 if (addr == 0x00 || addr >= 0x80) {
54 return;
55 }
56 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_SET_ADDR, &addr, 1) != i2c::ERROR_OK) {
57 ESP_LOGW(TAG, "Set new i2c address failed!");
58 this->status_set_warning();
59 return;
60 }
61 this->set_i2c_address(addr);
62}
63
64void GroveMotorDriveTB6612FNG::dc_motor_run(uint8_t channel, int16_t speed) {
65 speed = clamp<int16_t>(speed, -255, 255);
66
67 buffer_[0] = channel;
68 if (speed >= 0) {
69 buffer_[1] = speed;
70 } else {
71 buffer_[1] = (uint8_t) (-speed);
72 }
73
74 if (speed >= 0) {
75 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_CW, buffer_, 2) != i2c::ERROR_OK) {
76 ESP_LOGW(TAG, "Run motor failed!");
77 this->status_set_warning();
78 return;
79 }
80 } else {
81 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_CCW, buffer_, 2) != i2c::ERROR_OK) {
82 ESP_LOGW(TAG, "Run motor failed!");
83 this->status_set_warning();
84 return;
85 }
86 }
87}
88
90 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_BRAKE, &channel, 1) != i2c::ERROR_OK) {
91 ESP_LOGW(TAG, "Break motor failed!");
92 this->status_set_warning();
93 return;
94 }
95}
96
98 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STOP, &channel, 1) != i2c::ERROR_OK) {
99 ESP_LOGW(TAG, "Stop dc motor failed!");
100 this->status_set_warning();
101 return;
102 }
103}
104
106 uint8_t cw = 0;
107 // 0.1ms_per_step
108 uint16_t ms_per_step = 0;
109
110 if (steps > 0) {
111 cw = 1;
112 }
113 // stop
114 else if (steps == 0) {
115 this->stepper_stop();
116 return;
117 } else if (steps == INT16_MIN) {
118 steps = INT16_MAX;
119 } else {
120 steps = -steps;
121 }
122
123 rpm = clamp<uint16_t>(rpm, 1, 300);
124
125 ms_per_step = (uint16_t) (3000.0 / (float) rpm);
126 buffer_[0] = mode;
127 buffer_[1] = cw; //(cw=1) => cw; (cw=0) => ccw
128 buffer_[2] = steps;
129 buffer_[3] = (steps >> 8);
130 buffer_[4] = ms_per_step;
131 buffer_[5] = (ms_per_step >> 8);
132
133 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_RUN, buffer_, 6) != i2c::ERROR_OK) {
134 ESP_LOGW(TAG, "Run stepper failed!");
135 this->status_set_warning();
136 return;
137 }
138}
139
141 uint8_t status = 0;
142 if (this->write_register(GROVE_MOTOR_DRIVER_I2C_CMD_STEPPER_STOP, &status, 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 esphome::grove_tb6612fng
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
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) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:139
int speed
Definition fan.h:3
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14