@zaowu357/lib-tb6612 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ [
2
+ {
3
+ "kind": "category",
4
+ "name": "TB6612",
5
+ "colour": "#FF6600",
6
+ "icon": "iconfont icon-motor",
7
+ "contents": [
8
+ {"kind": "label", "text": "初始化"},
9
+ {
10
+ "kind": "block",
11
+ "type": "tb6612_init",
12
+ "fields": {"VAR": "motor1"}
13
+ },
14
+ {"kind": "label", "text": "运动控制"},
15
+ {
16
+ "kind": "block",
17
+ "type": "tb6612_forward",
18
+ "inputs": {
19
+ "SPEED": {"shadow": {"type": "math_number", "fields": {"NUM": "200"}}}
20
+ }
21
+ },
22
+ {
23
+ "kind": "block",
24
+ "type": "tb6612_backward",
25
+ "inputs": {
26
+ "SPEED": {"shadow": {"type": "math_number", "fields": {"NUM": "200"}}}
27
+ }
28
+ },
29
+ {"kind": "block", "type": "tb6612_stop"},
30
+ {"kind": "block", "type": "tb6612_brake"}
31
+ ]
32
+ }
33
+ ]
package/block.json ADDED
@@ -0,0 +1,130 @@
1
+ [
2
+ {
3
+ "type": "tb6612_init",
4
+ "message0": "init TB6612 motor %1 IN1 %2 IN2 %3 PWM %4 STBY %5",
5
+ "args0": [
6
+ {
7
+ "type": "field_input",
8
+ "name": "VAR",
9
+ "text": "motor1"
10
+ },
11
+ {
12
+ "type": "field_dropdown",
13
+ "name": "IN1",
14
+ "options": "${board.digitalPins}"
15
+ },
16
+ {
17
+ "type": "field_dropdown",
18
+ "name": "IN2",
19
+ "options": "${board.digitalPins}"
20
+ },
21
+ {
22
+ "type": "field_dropdown",
23
+ "name": "PWM",
24
+ "options": "${board.digitalPins}"
25
+ },
26
+ {
27
+ "type": "field_dropdown",
28
+ "name": "STBY",
29
+ "options": "${board.digitalPins}"
30
+ }
31
+ ],
32
+ "previousStatement": null,
33
+ "nextStatement": null,
34
+ "colour": "#FF6600",
35
+ "tooltip": "Initialize a TB6612 motor driver channel with IN1, IN2, PWM and STBY pins"
36
+ },
37
+ {
38
+ "type": "tb6612_stby",
39
+ "message0": "TB6612 standby %1",
40
+ "args0": [
41
+ {
42
+ "type": "field_dropdown",
43
+ "name": "LEVEL",
44
+ "options": [["enable (HIGH)", "HIGH"], ["disable (LOW)", "LOW"]]
45
+ }
46
+ ],
47
+ "previousStatement": null,
48
+ "nextStatement": null,
49
+ "colour": "#FF6600",
50
+ "tooltip": "Set TB6612 STBY pin HIGH to enable or LOW to disable both motors"
51
+ },
52
+ {
53
+ "type": "tb6612_forward",
54
+ "message0": "%1 forward speed %2",
55
+ "args0": [
56
+ {
57
+ "type": "field_variable",
58
+ "name": "VAR",
59
+ "variable": "motor1",
60
+ "variableTypes": ["TB6612"],
61
+ "defaultType": "TB6612"
62
+ },
63
+ {
64
+ "type": "input_value",
65
+ "name": "SPEED",
66
+ "check": ["Number"]
67
+ }
68
+ ],
69
+ "previousStatement": null,
70
+ "nextStatement": null,
71
+ "colour": "#FF6600",
72
+ "tooltip": "Run motor forward at the given speed (0-255)"
73
+ },
74
+ {
75
+ "type": "tb6612_backward",
76
+ "message0": "%1 backward speed %2",
77
+ "args0": [
78
+ {
79
+ "type": "field_variable",
80
+ "name": "VAR",
81
+ "variable": "motor1",
82
+ "variableTypes": ["TB6612"],
83
+ "defaultType": "TB6612"
84
+ },
85
+ {
86
+ "type": "input_value",
87
+ "name": "SPEED",
88
+ "check": ["Number"]
89
+ }
90
+ ],
91
+ "previousStatement": null,
92
+ "nextStatement": null,
93
+ "colour": "#FF6600",
94
+ "tooltip": "Run motor backward at the given speed (0-255)"
95
+ },
96
+ {
97
+ "type": "tb6612_stop",
98
+ "message0": "%1 stop",
99
+ "args0": [
100
+ {
101
+ "type": "field_variable",
102
+ "name": "VAR",
103
+ "variable": "motor1",
104
+ "variableTypes": ["TB6612"],
105
+ "defaultType": "TB6612"
106
+ }
107
+ ],
108
+ "previousStatement": null,
109
+ "nextStatement": null,
110
+ "colour": "#FF6600",
111
+ "tooltip": "Stop the motor (free run to stop)"
112
+ },
113
+ {
114
+ "type": "tb6612_brake",
115
+ "message0": "%1 brake",
116
+ "args0": [
117
+ {
118
+ "type": "field_variable",
119
+ "name": "VAR",
120
+ "variable": "motor1",
121
+ "variableTypes": ["TB6612"],
122
+ "defaultType": "TB6612"
123
+ }
124
+ ],
125
+ "previousStatement": null,
126
+ "nextStatement": null,
127
+ "colour": "#FF6600",
128
+ "tooltip": "Brake the motor (short brake)"
129
+ }
130
+ ]
package/generator.js ADDED
@@ -0,0 +1,71 @@
1
+ Arduino.forBlock['tb6612_init'] = function(block, generator) {
2
+ // Variable rename listener
3
+ if (!block._tb6612VarMonitorAttached) {
4
+ block._tb6612VarMonitorAttached = true;
5
+ block._tb6612VarLastName = block.getFieldValue('VAR') || 'motor1';
6
+ registerVariableToBlockly(block._tb6612VarLastName, 'TB6612');
7
+ const varField = block.getField('VAR');
8
+ if (varField) {
9
+ const originalFinishEditing = varField.onFinishEditing_;
10
+ varField.onFinishEditing_ = function(newName) {
11
+ if (typeof originalFinishEditing === 'function') {
12
+ originalFinishEditing.call(this, newName);
13
+ }
14
+ const workspace = block.workspace || (typeof Blockly !== 'undefined' && Blockly.getMainWorkspace && Blockly.getMainWorkspace());
15
+ const oldName = block._tb6612VarLastName;
16
+ if (workspace && newName && newName !== oldName) {
17
+ renameVariableInBlockly(block, oldName, newName, 'TB6612');
18
+ block._tb6612VarLastName = newName;
19
+ }
20
+ };
21
+ }
22
+ }
23
+
24
+ const varName = block.getFieldValue('VAR') || 'motor1';
25
+ const in1 = block.getFieldValue('IN1');
26
+ const in2 = block.getFieldValue('IN2');
27
+ const pwm = block.getFieldValue('PWM');
28
+ const stby = block.getFieldValue('STBY');
29
+
30
+ generator.addLibrary('TB6612', '#include <TB6612.h>');
31
+ generator.addVariable('tb6612_stby_pin', 'int tb6612_stby_pin = ' + stby + ';');
32
+ generator.addObject(varName, 'TB6612 ' + varName + '(' + in1 + ', ' + in2 + ', ' + pwm + ', ' + stby + ');');
33
+
34
+ let setupCode = varName + '.begin();\n';
35
+ return setupCode;
36
+ };
37
+
38
+ Arduino.forBlock['tb6612_stby'] = function(block, generator) {
39
+ const level = block.getFieldValue('LEVEL') || 'HIGH';
40
+ return 'digitalWrite(tb6612_stby_pin, ' + level + ');\n';
41
+ };
42
+
43
+ Arduino.forBlock['tb6612_forward'] = function(block, generator) {
44
+ const varField = block.getField('VAR');
45
+ const varName = varField ? varField.getText() : 'motor1';
46
+ const speed = generator.valueToCode(block, 'SPEED', generator.ORDER_ATOMIC) || '255';
47
+
48
+ return varName + '.forward(' + speed + ');\n';
49
+ };
50
+
51
+ Arduino.forBlock['tb6612_backward'] = function(block, generator) {
52
+ const varField = block.getField('VAR');
53
+ const varName = varField ? varField.getText() : 'motor1';
54
+ const speed = generator.valueToCode(block, 'SPEED', generator.ORDER_ATOMIC) || '255';
55
+
56
+ return varName + '.backward(' + speed + ');\n';
57
+ };
58
+
59
+ Arduino.forBlock['tb6612_stop'] = function(block, generator) {
60
+ const varField = block.getField('VAR');
61
+ const varName = varField ? varField.getText() : 'motor1';
62
+
63
+ return varName + '.stop();\n';
64
+ };
65
+
66
+ Arduino.forBlock['tb6612_brake'] = function(block, generator) {
67
+ const varField = block.getField('VAR');
68
+ const varName = varField ? varField.getText() : 'motor1';
69
+
70
+ return varName + '.brake();\n';
71
+ };
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@zaowu357/lib-tb6612",
3
+ "nickname": "TB6612 电机驱动",
4
+ "nickname_zh_cn": "TB6612 电机驱动",
5
+ "nickname_en": "TB6612 Motor Driver",
6
+ "description": "TB6612FNG 双路直流电机驱动模块",
7
+ "version": "1.0.0",
8
+ "author": "ailyProject",
9
+ "compatibility": {
10
+ "core": [],
11
+ "voltage": [3.3, 5]
12
+ },
13
+ "keywords": ["aily", "blockly", "motor", "tb6612", "dc motor"],
14
+ "tested": false,
15
+ "url": "https://www.sparkfun.com/products/14450"
16
+ }
package/readme.md ADDED
@@ -0,0 +1,27 @@
1
+ # TB6612 电机驱动
2
+
3
+ TB6612FNG 双路直流电机驱动模块的 Blockly 库。
4
+
5
+ ## Library Info
6
+
7
+ | Field | Value |
8
+ |-------|-------|
9
+ | Package | @aily-project/lib-tb6612 |
10
+ | Version | 1.0.0 |
11
+ | Author | ailyProject |
12
+ | Source | [SparkFun TB6612FNG](https://www.sparkfun.com/products/14450) |
13
+ | License | MIT |
14
+
15
+ ## Supported Boards
16
+
17
+ 所有支持 Arduino API 的开发板(UNO, Nano, ESP32, ESP8266 等)
18
+
19
+ ## Description
20
+
21
+ TB6612FNG 是一款双路直流电机驱动芯片,最高支持 15V/1.2A。本库封装了电机初始化、正转、反转、停止和刹车功能,通过 PWM 控制速度(0-255)。
22
+
23
+ ## Quick Start
24
+
25
+ 1. 初始化电机:选择 IN1、IN2、PWM 引脚
26
+ 2. 调用 forward/backward 控制方向和速度
27
+ 3. 调用 stop 或 brake 停止电机
package/readme_ai.md ADDED
@@ -0,0 +1,61 @@
1
+ # TB6612 电机驱动
2
+
3
+ TB6612FNG 双路直流电机驱动模块。
4
+
5
+ ## Library Info
6
+ - **Name**: @aily-project/lib-tb6612
7
+ - **Version**: 1.0.0
8
+
9
+ ## Block Definitions
10
+
11
+ | Block Type | Connection | Parameters (args0 order) | ABS Format | Generated Code |
12
+ |------------|------------|--------------------------|------------|----------------|
13
+ | `tb6612_init` | Statement | VAR(field_input), IN1(dropdown), IN2(dropdown), PWM(dropdown) | `tb6612_init("motor1", 5, 6, 9)` | `TB6612 motor1(5, 6, 9);` + `motor1.begin();` |
14
+ | `tb6612_forward` | Statement | VAR(field_variable), SPEED(input_value) | `tb6612_forward($motor1, math_number(200))` | `motor1.forward(200);` |
15
+ | `tb6612_backward` | Statement | VAR(field_variable), SPEED(input_value) | `tb6612_backward($motor1, math_number(200))` | `motor1.backward(200);` |
16
+ | `tb6612_stop` | Statement | VAR(field_variable) | `tb6612_stop($motor1)` | `motor1.stop();` |
17
+ | `tb6612_brake` | Statement | VAR(field_variable) | `tb6612_brake($motor1)` | `motor1.brake();` |
18
+
19
+ ## Parameter Options
20
+
21
+ | Parameter | Values | Description |
22
+ |-----------|--------|-------------|
23
+ | IN1/IN2/PWM | ${board.digitalPins} | 数字引脚(由开发板配置自动填充) |
24
+ | SPEED | 0-255 | PWM 速度值 |
25
+
26
+ ## ABS Examples
27
+
28
+ ### Basic Usage
29
+ ```abs
30
+ arduino_setup()
31
+ tb6612_init("motor1", 5, 6, 9)
32
+
33
+ arduino_loop()
34
+ tb6612_forward($motor1, math_number(200))
35
+ time_delay(math_number(2000))
36
+ tb6612_backward($motor1, math_number(200))
37
+ time_delay(math_number(2000))
38
+ tb6612_stop($motor1)
39
+ time_delay(math_number(1000))
40
+ ```
41
+
42
+ ### Two Motors (Differential Drive)
43
+ ```abs
44
+ arduino_setup()
45
+ tb6612_init("leftMotor", 5, 6, 9)
46
+ tb6612_init("rightMotor", 7, 8, 10)
47
+
48
+ arduino_loop()
49
+ tb6612_forward($leftMotor, math_number(200))
50
+ tb6612_forward($rightMotor, math_number(200))
51
+ time_delay(math_number(1000))
52
+ tb6612_stop($leftMotor)
53
+ tb6612_stop($rightMotor)
54
+ ```
55
+
56
+ ## Notes
57
+
58
+ 1. **Initialization**: Call `tb6612_init` inside `arduino_setup()`
59
+ 2. **Variable reference**: Use `$varName` to reference motor variable in control blocks
60
+ 3. **PWM pins**: PWM pin must support analogWrite on your board
61
+ 4. **stop vs brake**: `stop` = free run (coast), `brake` = short brake (fast stop)
@@ -0,0 +1,50 @@
1
+ #include "TB6612.h"
2
+
3
+ TB6612::TB6612(int in1, int in2, int pwm, int stby) {
4
+ _in1 = in1;
5
+ _in2 = in2;
6
+ _pwm = pwm;
7
+ _stby = stby;
8
+ }
9
+
10
+ void TB6612::begin() {
11
+ pinMode(_in1, OUTPUT);
12
+ pinMode(_in2, OUTPUT);
13
+ pinMode(_pwm, OUTPUT);
14
+ if (_stby != -1) {
15
+ pinMode(_stby, OUTPUT);
16
+ digitalWrite(_stby, HIGH);
17
+ }
18
+ }
19
+
20
+ void TB6612::enable() {
21
+ if (_stby != -1) digitalWrite(_stby, HIGH);
22
+ }
23
+
24
+ void TB6612::disable() {
25
+ if (_stby != -1) digitalWrite(_stby, LOW);
26
+ }
27
+
28
+ void TB6612::forward(int speed) {
29
+ digitalWrite(_in1, HIGH);
30
+ digitalWrite(_in2, LOW);
31
+ analogWrite(_pwm, speed);
32
+ }
33
+
34
+ void TB6612::backward(int speed) {
35
+ digitalWrite(_in1, LOW);
36
+ digitalWrite(_in2, HIGH);
37
+ analogWrite(_pwm, speed);
38
+ }
39
+
40
+ void TB6612::stop() {
41
+ digitalWrite(_in1, LOW);
42
+ digitalWrite(_in2, LOW);
43
+ analogWrite(_pwm, 0);
44
+ }
45
+
46
+ void TB6612::brake() {
47
+ digitalWrite(_in1, HIGH);
48
+ digitalWrite(_in2, HIGH);
49
+ analogWrite(_pwm, 0);
50
+ }
@@ -0,0 +1,23 @@
1
+ #ifndef TB6612_h
2
+ #define TB6612_h
3
+
4
+ #include <Arduino.h>
5
+
6
+ class TB6612 {
7
+ public:
8
+ TB6612(int in1, int in2, int pwm, int stby = -1);
9
+ void begin();
10
+ void forward(int speed);
11
+ void backward(int speed);
12
+ void stop();
13
+ void brake();
14
+ void enable();
15
+ void disable();
16
+ private:
17
+ int _in1;
18
+ int _in2;
19
+ int _pwm;
20
+ int _stby;
21
+ };
22
+
23
+ #endif
package/toolbox.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "kind": "category",
3
+ "name": "TB6612",
4
+ "colour": "#FF6600",
5
+ "icon": "fa-solid fa-gear",
6
+ "contents": [
7
+ {"kind": "label", "text": "初始化"},
8
+ {
9
+ "kind": "block",
10
+ "type": "tb6612_init",
11
+ "fields": {"VAR": "motor1"}
12
+ },
13
+ {"kind": "label", "text": "运动控制"},
14
+ {
15
+ "kind": "block",
16
+ "type": "tb6612_forward",
17
+ "inputs": {
18
+ "SPEED": {"shadow": {"type": "math_number", "fields": {"NUM": "200"}}}
19
+ }
20
+ },
21
+ {
22
+ "kind": "block",
23
+ "type": "tb6612_backward",
24
+ "inputs": {
25
+ "SPEED": {"shadow": {"type": "math_number", "fields": {"NUM": "200"}}}
26
+ }
27
+ },
28
+ {"kind": "block", "type": "tb6612_stop"},
29
+ {"kind": "block", "type": "tb6612_brake"},
30
+ {"kind": "label", "text": "待机控制"},
31
+ {
32
+ "kind": "block",
33
+ "type": "tb6612_stby",
34
+ "fields": {"LEVEL": "HIGH"}
35
+ }
36
+ ]
37
+ }