Skip to content

Commit d429294

Browse files
hlym123lbuque
authored andcommitted
libs/module: Add support for Bala2 Module.
Signed-off-by: hlym123 <[email protected]>
1 parent 7f8c8a0 commit d429294

17 files changed

+1395
-0
lines changed

docs/en/module/bala2.rst

Lines changed: 444 additions & 0 deletions
Large diffs are not rendered by default.

docs/en/module/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Module
66

77
4in8out.rst
88
ain4.rst
9+
bala2.rst
910
display.rst
1011
dmx.rst
1112
dualkmeter.rst

docs/en/refs/module.bala2.ref

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
.. |Bala2-Fire| image:: https://static-cdn.m5stack.com/resource/docs/products/app/bala2fire/bala2_01.webp
3+
:target: https://docs.m5stack.com/en/app/bala2fire
4+
:height: 200px
5+
:width: 200px
6+
7+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/init.png
8+
.. |get_encoder_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/get_encoder_value.png
9+
.. |get_angle_pid.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/get_angle_pid.png
10+
.. |get_angle.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/get_angle.png
11+
.. |get_angle_pid_target.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/get_angle_pid_target.png
12+
.. |get_speed_pid.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/get_speed_pid.png",
13+
.. |get_speed_pid_target.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/get_speed_pid_target.png
14+
.. |start.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/start.png
15+
.. |stop.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/stop.png
16+
.. |calibrate.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/calibrate.png
17+
.. |set_motor_speed.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_motor_speed.png
18+
.. |set_encoder_value.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_encoder_value.png
19+
.. |set_servo_angle.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_servo_angle.png
20+
.. |set_angle_pid.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_angle_pid.png
21+
.. |set_angle_pid_target.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_angle_pid_target.png
22+
.. |set_speed_pid.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_speed_pid.png
23+
.. |set_speed_pid_target.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_speed_pid_target.png
24+
.. |set_turn_speed.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/set_turn_speed.png
25+
26+
.. |motor_control_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/motor_control_example.png
27+
.. |read_encoder_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/read_encoder_example.png
28+
.. |servo_control_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/servo_control_example.png
29+
.. |car_control_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/bala2/car_control_example.png
30+
31+
.. |motor_control_example.m5f2| raw:: html
32+
33+
<a
34+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/bala2/motor_control_example.m5f2"
35+
target="_blank"
36+
>
37+
motor_control_example.m5f2
38+
</a>
39+
40+
.. |read_encoder_example.m5f2| raw:: html
41+
42+
<a
43+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/bala2/read_encoder_example.m5f2"
44+
target="_blank"
45+
>
46+
read_encoder_example.m5f2
47+
</a>
48+
49+
.. |servo_control_example.m5f2| raw:: html
50+
51+
<a
52+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/bala2/servo_control_example.m5f2"
53+
target="_blank"
54+
>
55+
servo_control_example.m5f2
56+
</a>
57+
58+
.. |car_control_example.m5f2| raw:: html
59+
60+
<a
61+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/bala2/car_control_example.m5f2"
62+
target="_blank"
63+
>
64+
car_control_example.m5f2
65+
</a>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"V2.0","versionNumber":"V2.2.1","type":"fire","components":[{"name":"screen","type":"screen","layer":0,"screenId":"builtin","screenName":"","id":"__fire_screen","createTime":1739843623137,"x":0,"y":0,"width":320,"height":240,"backgroundColor":"#222222","size":0,"isSelected":true},{"name":"title0","type":"title","layer":1,"screenId":"builtin","screenName":"","id":"hjKc%ViHl9qe9gC=","createTime":1739843645399,"x":0,"y":0,"color":"#ffffff","backgroundColor":"#0000FF","text":"Self-Balancing Robot ","textOffset":3,"font":"Widgets.FONTS.DejaVu24","isSelected":false}],"resources":[{"hardware":["hardware_button","hardware_pin_button","imu","rgb","speaker"]},{"module":["module_bala2"]}],"units":[],"hats":[],"bases":[],"i2cs":[],"blockly":"<variables><variable id=\"/NqprTXhI?(J6NhG-(Dd\">i</variable></variables><block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"370\" y=\"50\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"system_m5_begin\"><next><block type=\"module_bala2_init\" id=\"E?b0{i6P{-eb7T!Fh-R-\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"kTB/,2,b9@bm7aA4`Sa~\"><mutation max=\"3\" min=\"0\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">0</field></shadow></value><next><block type=\"module_bala2_calibrate\" id=\"Rz))Lx,^lU6wQyUkQ@(9\"><field name=\"NAME\">module_bala2_0</field><next><block type=\"module_bala2_start\" id=\"qP8L.{Hgl5~}=lZ7PS`W\"><field name=\"NAME\">module_bala2_0</field><next><block type=\"time_sleep_millisecond\" id=\"h2Qlg|^[k(Dk;#qF/mm7\"><value name=\"MS\"><shadow type=\"math_number\" id=\"^,e?SYR)2L2]sa3YZpLy\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">2000</field></shadow></value><next><block type=\"module_bala2_set_turn_speed\" id=\"Oj/pG1YqM5~4X0e(y;1p\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"br@(kCpUl$%IfN?d`Wk@\"><mutation max=\"1023\" min=\"-1023\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">-100</field></shadow></value><next><block type=\"time_sleep_millisecond\" id=\"q.hT-*!,b{2}xowwt!,-\"><value name=\"MS\"><shadow type=\"math_number\" id=\"s|)k60X~N8a+WT3A;![v\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">1000</field></shadow></value><next><block type=\"module_bala2_set_turn_speed\" id=\"mo@c:S(VC0#FO+Vd8?l!\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_slider\" id=\",Ucl:EMGEZ$4ho.Bnt8$\"><mutation max=\"1023\" min=\"-1023\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">100</field></shadow></value><next><block type=\"time_sleep_millisecond\" id=\"[SRYwoNfb,zMS}z)d/7J\"><value name=\"MS\"><shadow type=\"math_number\" id=\"qm!=A49$Sdx{Njs%HC0@\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">1000</field></shadow></value><next><block type=\"module_bala2_set_turn_speed\" id=\"A.YiZW-50~NVqXpq7tQz\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"1k~.oMsEVQar;k2ZsL8*\"><mutation max=\"1023\" min=\"-1023\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">0</field></shadow></value><next><block type=\"time_sleep_millisecond\" id=\"6-0Al|)Ym@2n~0^UP%P2\"><value name=\"MS\"><shadow type=\"math_number\" id=\"TH]{E01gx31W28H$72eb\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">2000</field></shadow></value><next><block type=\"controls_for_range\" id=\"BJuHsqn(me2JN/KX$bGi\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field><value name=\"VALUE\"><block type=\"math_number\" id=\"CsgLPpL/+f~0D9L+}s$f\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">20</field></block></value><statement name=\"DO\"><block type=\"module_bala2_set_angle_pid_target\" id=\"xlx~)e+.dFbCChN1zB)m\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_number\" id=\"lMEr[j6#Yb{6E*OzUJvF\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\" id=\"1%=K/%9ubC$EU1M]LLn)\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\" id=\"wFwk)-T/|dy.WdO}{kdY\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></shadow></value><value name=\"B\"><shadow type=\"math_number\" id=\"z:{0sa}+Dd}w;//-|0sx\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow><block type=\"variables_get\" id=\"8:)HwYzRi1ei88qr29~i\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field></block></value></block></value><next><block type=\"time_sleep_millisecond\" id=\"o~hQ?;O/m]J6_i1bLH^X\"><value name=\"MS\"><shadow type=\"math_number\" id=\"%C*OUZqGwQlK{ke@^@Hf\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">100</field></shadow></value></block></next></block></statement><next><block type=\"time_sleep_millisecond\" id=\"Cx#_bA}``xco80/==Kk$\"><value name=\"MS\"><shadow type=\"math_number\" id=\"fpPd:*A:rk*oNmH_uX6,\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">2000</field></shadow></value><next><block type=\"controls_for_range\" id=\"nI.z7rCe~$Xs@aeaK@ng\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field><value name=\"VALUE\"><block type=\"math_number\" id=\"gng|}yNj_YhnJNH1@17k\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">20</field></block></value><statement name=\"DO\"><block type=\"module_bala2_set_angle_pid_target\" id=\"Xyh~Srl`7yX{XNSm%MkK\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_number\" id=\"lMEr[j6#Yb{6E*OzUJvF\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\" id=\"})9(+,~Ut7Cp/o{;F#mf\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\" id=\"scoe?La{=y|Ni:fwV^uG\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></shadow><block type=\"variables_get\" id=\"AJ56|v{(U6`Pd1YEk6|d\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field></block></value><value name=\"B\"><shadow type=\"math_number\" id=\"z:{0sa}+Dd}w;//-|0sx\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">20</field></shadow></value></block></value><next><block type=\"time_sleep_millisecond\" id=\"X,MP67ryD5cTQ6GF)S`*\"><value name=\"MS\"><shadow type=\"math_number\" id=\"t;f^F%U%!P{L~xcMO?RI\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">100</field></shadow></value></block></next></block></statement><next><block type=\"controls_for_range\" id=\"?qI++%3aw2sX_v8n7?Vf\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field><value name=\"VALUE\"><block type=\"math_number\" id=\"9Yl~?gitO`H-6wB-{BbV\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">20</field></block></value><statement name=\"DO\"><block type=\"module_bala2_set_angle_pid_target\" id=\"5^NM^vB=|9DW)~yfjX#y\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_number\" id=\"lMEr[j6#Yb{6E*OzUJvF\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></shadow><block type=\"variables_get\" id=\"YR]L~e)g2$$j+RM_BS50\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field></block></value><next><block type=\"time_sleep_millisecond\" id=\"te=?tVv`Se[{jC|f:.TF\"><value name=\"MS\"><shadow type=\"math_number\" id=\"zeafBV5[eAa.xXZAGz5E\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">100</field></shadow></value></block></next></block></statement><next><block type=\"time_sleep_millisecond\" id=\"D+Zr1k5nVH:r_t5eIRi8\"><value name=\"MS\"><shadow type=\"math_number\" id=\"*[v{KVsE;`yE!,t{v%tY\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">2000</field></shadow></value><next><block type=\"controls_for_range\" id=\"AUbom,oCDa9hClMIQs3}\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field><value name=\"VALUE\"><block type=\"math_number\" id=\"[dGM),?8n!ay/hs[,uh[\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">20</field></block></value><statement name=\"DO\"><block type=\"module_bala2_set_angle_pid_target\" id=\"f3AcJ%W;XVO@9C_u?Pqu\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_number\" id=\"lMEr[j6#Yb{6E*OzUJvF\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\" id=\"mBmJL%DKV=FJ|J?,RYXV\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\" id=\")(oUlos$^`/ap067KX+*\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">20</field></shadow></value><value name=\"B\"><shadow type=\"math_number\" id=\"I?72cxGcD]MRcy8xN{Xl\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow><block type=\"variables_get\" id=\";St61$psbFcZWM$`$|SX\"><field name=\"VAR\" id=\"/NqprTXhI?(J6NhG-(Dd\">i</field></block></value></block></value><next><block type=\"time_sleep_millisecond\" id=\".QI.R{_V2rj6yt-CstU2\"><value name=\"MS\"><shadow type=\"math_number\" id=\"x(D${KOBV:n1LW($fn:M\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">100</field></shadow></value></block></next></block></statement><next><block type=\"module_bala2_set_angle_pid_target\" id=\"%QHnzvY!y4X|*lsqz6#T\"><field name=\"NAME\">module_bala2_0</field><value name=\"VALUE\"><shadow type=\"math_number\" id=\"lMEr[j6#Yb{6E*OzUJvF\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"890\" y=\"50\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_update\" id=\"system_m5_update\"></block></statement></block>","screen":[{"simulationName":"Built-in","type":"builtin","width":320,"height":240,"scale":0.78,"screenName":"","blockId":"","screenColorType":0,"id":"builtin","createTime":1739843623136}],"logicWhenNum":0,"customList":[]}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os, sys, io
6+
import M5
7+
from M5 import *
8+
from module import Bala2Module
9+
import time
10+
11+
12+
title0 = None
13+
module_bala2_0 = None
14+
i = None
15+
16+
17+
def setup():
18+
global title0, module_bala2_0, i
19+
M5.begin()
20+
Widgets.fillScreen(0x222222)
21+
title0 = Widgets.Title("Self-Balancing Robot", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
22+
module_bala2_0 = Bala2Module(0)
23+
module_bala2_0.calibrate()
24+
module_bala2_0.start()
25+
time.sleep_ms(2000)
26+
module_bala2_0.set_turn_speed(-100)
27+
time.sleep_ms(1000)
28+
module_bala2_0.set_turn_speed(100)
29+
time.sleep_ms(1000)
30+
module_bala2_0.set_turn_speed(0)
31+
time.sleep_ms(2000)
32+
for i in range(20):
33+
module_bala2_0.set_angle_pid_target(0 - i)
34+
time.sleep_ms(100)
35+
time.sleep_ms(2000)
36+
for i in range(20):
37+
module_bala2_0.set_angle_pid_target(i - 20)
38+
time.sleep_ms(100)
39+
for i in range(20):
40+
module_bala2_0.set_angle_pid_target(i)
41+
time.sleep_ms(100)
42+
time.sleep_ms(2000)
43+
for i in range(20):
44+
module_bala2_0.set_angle_pid_target(20 - i)
45+
time.sleep_ms(100)
46+
module_bala2_0.set_angle_pid_target(0)
47+
48+
49+
def loop():
50+
global title0, module_bala2_0, i
51+
M5.update()
52+
53+
54+
if __name__ == "__main__":
55+
try:
56+
setup()
57+
while True:
58+
loop()
59+
except (Exception, KeyboardInterrupt) as e:
60+
try:
61+
from utility import print_error_msg
62+
63+
print_error_msg(e)
64+
except ImportError:
65+
print("please update to latest firmware")

0 commit comments

Comments
 (0)