Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Grove tb6612 fng #4797

Merged
merged 45 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e67a0e5
add motor implementation
max246 May 8, 2023
4d8f309
fix issues
max246 May 9, 2023
bbef99b
fixes
max246 May 9, 2023
76eafdb
add owner
max246 May 9, 2023
06c51b2
add test
max246 May 9, 2023
cac1a98
update i2c
max246 May 9, 2023
159d6a8
rename
max246 May 9, 2023
4ef4257
fix lint
max246 May 9, 2023
2dfd136
fix lint
max246 May 9, 2023
85927b8
fix lint
max246 May 9, 2023
c8c4fc5
fix lint
max246 May 9, 2023
98b341f
remove lines
max246 May 9, 2023
715f589
add one line for lint
max246 May 9, 2023
954f991
add one line for lint
max246 May 9, 2023
0f93347
update fail message
max246 May 11, 2023
1018d3a
Merge branch 'dev' into FEATURE/grove-TB6612FNG
max246 May 12, 2023
52dce9e
remove name
max246 May 12, 2023
f862296
remove delay
max246 May 12, 2023
1c5b7e6
update import
max246 May 14, 2023
6b185be
fixes
max246 May 14, 2023
a74f305
only compile for arduino
max246 May 14, 2023
7ecc7f9
update limitation
max246 May 14, 2023
a737b2e
fixes
max246 May 14, 2023
e09ae06
move import lib
max246 May 14, 2023
426bf9f
Revert "update import"
max246 May 14, 2023
a5fb0a4
clean up test
max246 May 14, 2023
15fefb7
Merge branch 'dev' into FEATURE/grove-TB6612FNG
max246 May 14, 2023
678a26c
Update esphome/components/grove_i2c_motor/grove_i2c_motor.cpp
max246 May 14, 2023
0addf7a
Update esphome/components/grove_i2c_motor/grove_i2c_motor.cpp
max246 May 14, 2023
d45893a
Update esphome/components/grove_i2c_motor/grove_i2c_motor.cpp
max246 May 14, 2023
ad57281
tidy up code
max246 May 14, 2023
70826b4
fix parentd
max246 May 14, 2023
f396439
fix parent
max246 May 14, 2023
f42727e
Merge branch 'dev' into FEATURE/grove-TB6612FNG
max246 May 15, 2023
2fd6abd
Merge branch 'dev' into FEATURE/grove-TB6612FNG
max246 May 17, 2023
341cbc1
Merge branch 'dev' into FEATURE/grove-TB6612FNG
max246 Jul 8, 2023
6900fa5
add tests
max246 Jul 11, 2023
ee7bcf3
Merge branch 'dev' into FEATURE/grove-TB6612FNG
max246 Jul 11, 2023
386322f
fix test
max246 Jul 11, 2023
e6a0036
fix test
max246 Jul 11, 2023
b96d6e5
fix test
max246 Jul 11, 2023
9b73d0d
fix test
max246 Jul 11, 2023
8711736
fix test
max246 Jul 11, 2023
dcfb206
Update actions
jesserockz Jul 12, 2023
fe5be90
Update actions in test
jesserockz Jul 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ esphome/components/gp8403/* @jesserockz
esphome/components/gpio/* @esphome/core
esphome/components/gps/* @coogle
esphome/components/graph/* @synco
esphome/components/grove_i2c_motor/* @max246
esphome/components/growatt_solar/* @leeuwte
esphome/components/haier/* @Yarikx
esphome/components/havells_solar/* @sourabhjaiswal
Expand Down
151 changes: 151 additions & 0 deletions esphome/components/grove_i2c_motor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation

from esphome.const import (
CONF_ID,
CONF_CHANNEL,
CONF_SPEED,
CONF_DIRECTION,
CONF_ADDRESS,
)

DEPENDENCIES = ["i2c"]

CODEOWNERS = ["@max246"]

grove_i2c_motor_ns = cg.esphome_ns.namespace("grove_i2c_motor")
GROVE_TB6612FNG = grove_i2c_motor_ns.class_("GroveMotorDriveTB6612FNG", cg.Component)
GROVETB6612FNGMotorRunAction = grove_i2c_motor_ns.class_(
"GROVETB6612FNGMotorRunAction", automation.Action
)
GROVETB6612FNGMotorBrakeAction = grove_i2c_motor_ns.class_(
"GROVETB6612FNGMotorBrakeAction", automation.Action
)
GROVETB6612FNGMotorStopAction = grove_i2c_motor_ns.class_(
"GROVETB6612FNGMotorStopAction", automation.Action
)
GROVETB6612FNGMotorStandbyAction = grove_i2c_motor_ns.class_(
"GROVETB6612FNGMotorStandbyAction", automation.Action
)
GROVETB6612FNGMotorNoStandbyAction = grove_i2c_motor_ns.class_(
"GROVETB6612FNGMotorNoStandbyAction", automation.Action
)

DIRECTION_TYPE = {
"FORWARD": 1,
"BACKWARD": 2,
}

CONFIG_SCHEMA = cv.Schema(
{
cv.Required(CONF_ID): cv.declare_id(GROVE_TB6612FNG),
cv.Optional(CONF_ADDRESS, default=0x14): cv.i2c_address,
}
).extend(cv.COMPONENT_SCHEMA)


async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)

cg.add(var.set_address(config[CONF_ADDRESS]))

return var


@automation.register_action(
"grove_i2c_motor.motor_run",
GROVETB6612FNGMotorRunAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(GROVE_TB6612FNG),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=1),
cv.Required(CONF_SPEED): cv.int_range(min=0, max=255),
max246 marked this conversation as resolved.
Show resolved Hide resolved
cv.Required(CONF_DIRECTION): cv.enum(DIRECTION_TYPE, upper=True),
}
),
)
async def grove_i2c_motor_run_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)

template_channel = await cg.templatable(config[CONF_CHANNEL], args, int)
template_speed = await cg.templatable(config[CONF_SPEED], args, cg.uint16)
template_speed = (
template_speed if config[CONF_DIRECTION] == "FORWARD" else -template_speed
)
cg.add(var.set_chl(template_channel))
cg.add(var.set_speed(template_speed))
return var


@automation.register_action(
"grove_i2c_motor.motor_break",
GROVETB6612FNGMotorBrakeAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(GROVE_TB6612FNG),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=1),
}
),
)
async def grove_i2c_motor_break_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)

template_channel = await cg.templatable(config[CONF_CHANNEL], args, int)
cg.add(var.set_chl(template_channel))
return var


@automation.register_action(
"grove_i2c_motor.motor_stop",
GROVETB6612FNGMotorStopAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(GROVE_TB6612FNG),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=1),
}
),
)
async def grove_i2c_motor_stop_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)

template_channel = await cg.templatable(config[CONF_CHANNEL], args, int)
cg.add(var.set_chl(template_channel))
return var


@automation.register_action(
"grove_i2c_motor.standby",
GROVETB6612FNGMotorStandbyAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(GROVE_TB6612FNG),
}
),
)
async def grove_i2c_motor_standby_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)

return var


@automation.register_action(
"grove_i2c_motor.no_standby",
GROVETB6612FNGMotorNoStandbyAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(GROVE_TB6612FNG),
}
),
)
async def grove_i2c_motor_no_standby_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)

cg.add_library("https://github.com/Seeed-Studio/Grove_Motor_Driver_TB6612FNG", None)
return var
85 changes: 85 additions & 0 deletions esphome/components/grove_i2c_motor/grove_i2c_motor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "grove_i2c_motor.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"

namespace esphome {
namespace grove_i2c_motor {

static const char *const TAG = "GroveMotorDriveTB6612FNG";

void GroveMotorDriveTB6612FNG::dump_config() {
ESP_LOGCONFIG(TAG, "GroveMotorDriveTB6612FNG:");
ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address);
}

void GroveMotorDriveTB6612FNG::setup() {
ESP_LOGCONFIG(TAG, "Setting up Grove Motor Drive TB6612FNG ...");

motor->init(this->address);
}

void GroveMotorDriveTB6612FNG::standby() { motor->standby(); }

void GroveMotorDriveTB6612FNG::not_standby() { motor->notStandby(); }

void GroveMotorDriveTB6612FNG::set_i2c_addr(uint8_t addr) { motor->setI2cAddr(addr); }

void GroveMotorDriveTB6612FNG::dc_motor_run(uint8_t chl, int16_t speed) {
max246 marked this conversation as resolved.
Show resolved Hide resolved
motor_channel_type_t channel;
if (chl == 0)

Check failure on line 29 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
channel = MOTOR_CHA;
else

Check failure on line 31 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
channel = MOTOR_CHB;
motor->dcMotorRun(channel, speed);
}

void GroveMotorDriveTB6612FNG::dc_motor_brake(uint8_t chl) {
motor_channel_type_t channel;
if (chl == 0)

Check failure on line 38 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
channel = MOTOR_CHA;
else

Check failure on line 40 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
channel = MOTOR_CHB;
motor->dcMotorBrake(channel);
}

void GroveMotorDriveTB6612FNG::dc_motor_stop(uint8_t chl) {
motor_channel_type_t channel;
if (chl == 0)

Check failure on line 47 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
channel = MOTOR_CHA;
else

Check failure on line 49 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
channel = MOTOR_CHB;
motor->dcMotorStop(channel);
}

void GroveMotorDriveTB6612FNG::stepper_run(int mode, int16_t steps, uint16_t rpm) {
stepper_mode_type_t mode_type;

if (mode == 0)

Check failure on line 57 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
mode_type = FULL_STEP;
else if (mode == 1)

Check failure on line 59 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
mode_type = WAVE_DRIVE;
else if (mode == 2)

Check failure on line 61 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
mode_type = HALF_STEP;
else

Check failure on line 63 in esphome/components/grove_i2c_motor/grove_i2c_motor.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

statement should be inside braces
mode_type = MICRO_STEPPING;
motor->stepperRun(mode_type, steps, rpm);
}

void GroveMotorDriveTB6612FNG::stepper_stop() { motor->stepperStop(); }

void GroveMotorDriveTB6612FNG::stepper_keep_run(int mode, uint16_t rpm, bool is_cw) {
stepper_mode_type_t mode_type;

if (mode == 0)
mode_type = FULL_STEP;
else if (mode == 1)
mode_type = WAVE_DRIVE;
else if (mode == 2)
mode_type = HALF_STEP;
else
mode_type = MICRO_STEPPING;
motor->stepperKeepRun(mode_type, rpm, is_cw);
}

} // namespace grove_i2c_motor
} // namespace esphome
99 changes: 99 additions & 0 deletions esphome/components/grove_i2c_motor/grove_i2c_motor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/automation.h"

#include "Grove_Motor_Driver_TB6612FNG.h"

Check failure on line 7 in esphome/components/grove_i2c_motor/grove_i2c_motor.h

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 IDF

'Grove_Motor_Driver_TB6612FNG.h' file not found

namespace esphome {
namespace grove_i2c_motor {

class GroveMotorDriveTB6612FNG : public Component {
public:
void setup() override;
void dump_config() override;

void standby();

void not_standby();

void set_i2c_addr(uint8_t addr);

void dc_motor_run(uint8_t chl, int16_t speed);

void dc_motor_brake(uint8_t chl);

void dc_motor_stop(uint8_t chl);

void stepper_run(int mode, int16_t steps, uint16_t rpm);

void stepper_stop();

void stepper_keep_run(int mode, uint16_t rpm, bool is_cw);
void set_address(uint8_t addr) { this->address = addr; }

MotorDriver *motor{nullptr};
uint8_t address;
};

template<typename... Ts> class GROVETB6612FNGMotorRunAction : public Action<Ts...> {
public:
GROVETB6612FNGMotorRunAction(GroveMotorDriveTB6612FNG *motor) : motor_(motor) {}
TEMPLATABLE_VALUE(uint8_t, chl)
TEMPLATABLE_VALUE(uint16_t, speed)

void play(Ts... x) override {
auto chl = this->chl_.value(x...);
auto speed = this->speed_.value(x...);
this->motor_->dc_motor_run(chl, speed);
}

protected:
GroveMotorDriveTB6612FNG *motor_;
};
max246 marked this conversation as resolved.
Show resolved Hide resolved

template<typename... Ts> class GROVETB6612FNGMotorBrakeAction : public Action<Ts...> {
public:
GROVETB6612FNGMotorBrakeAction(GroveMotorDriveTB6612FNG *motor) : motor_(motor) {}
TEMPLATABLE_VALUE(uint8_t, chl)

void play(Ts... x) override { this->motor_->dc_motor_brake(this->chl_.value(x...)); }

protected:
GroveMotorDriveTB6612FNG *motor_;
};

template<typename... Ts> class GROVETB6612FNGMotorStopAction : public Action<Ts...> {
public:
GROVETB6612FNGMotorStopAction(GroveMotorDriveTB6612FNG *motor) : motor_(motor) {}
TEMPLATABLE_VALUE(uint8_t, chl)

void play(Ts... x) override { this->motor_->dc_motor_stop(this->chl_.value(x...)); }

protected:
GroveMotorDriveTB6612FNG *motor_;
};

template<typename... Ts> class GROVETB6612FNGMotorStandbyAction : public Action<Ts...> {
public:
GROVETB6612FNGMotorStandbyAction(GroveMotorDriveTB6612FNG *motor) : motor_(motor) {}

void play(Ts... x) override { this->motor_->standby(); }

protected:
GroveMotorDriveTB6612FNG *motor_;
};

template<typename... Ts> class GROVETB6612FNGMotorNoStandbyAction : public Action<Ts...> {
public:
GROVETB6612FNGMotorNoStandbyAction(GroveMotorDriveTB6612FNG *motor) : motor_(motor) {}

void play(Ts... x) override { this->motor_->not_standby(); }

protected:
GroveMotorDriveTB6612FNG *motor_;
};

} // namespace grove_i2c_motor
} // namespace esphome
23 changes: 12 additions & 11 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@
extends = common
lib_deps =
${common.lib_deps}
SPI ; spi (Arduino built-in)
Wire ; i2c (Arduino built-int)
ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt
esphome/ESPAsyncWebServer-esphome@2.1.0 ; web_server_base
fastled/FastLED@3.3.2 ; fastled_base
mikalhart/TinyGPSPlus@1.0.2 ; gps
freekode/TM1651@1.0.1 ; tm1651
glmnet/Dsmr@0.5 ; dsmr
rweather/Crypto@0.4.0 ; dsmr
dudanov/MideaUART@1.1.8 ; midea
tonia/HeatpumpIR@1.0.20 ; heatpumpir
SPI ; spi (Arduino built-in)
Wire ; i2c (Arduino built-int)
ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt
esphome/ESPAsyncWebServer-esphome@2.1.0 ; web_server_base
fastled/FastLED@3.3.2 ; fastled_base
mikalhart/TinyGPSPlus@1.0.2 ; gps
freekode/TM1651@1.0.1 ; tm1651
glmnet/Dsmr@0.5 ; dsmr
rweather/Crypto@0.4.0 ; dsmr
dudanov/MideaUART@1.1.8 ; midea
tonia/HeatpumpIR@1.0.20 ; heatpumpir
https://github.com/Seeed-Studio/Grove_Motor_Driver_TB6612FNG ; TB6612FNG

Check failure on line 68 in platformio.ini

View workflow job for this annotation

GitHub Actions / Run script/ci-custom

Trailing whitespace detected
build_flags =
${common.build_flags}
-DUSE_ARDUINO
Expand Down
5 changes: 5 additions & 0 deletions tests/test3.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,8 @@ cd74hc4067:

adc128s102:
cs_pin: GPIO12

grove_i2c_motor:
id: test_motor
name: motor_outside
address: 0x14
2 changes: 2 additions & 0 deletions tests/test3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,5 @@ daly_bms:
qr_code:
- id: homepage_qr
value: https://esphome.io/index.html

max246 marked this conversation as resolved.
Show resolved Hide resolved