Skip to content
LegoFind

LegoFind

乐高机械组(Technic)、机器人资源分享。

  • Powered Up 教程
  • BOOST 教程
  • 乐高相关网站
  • 机械组套装
  • 教育及机器人套装
  • 机械组资源
  • 留言板
  • Toggle search form

没有手机等智能设备也能控制乐高Control+套装?PyBricks可以帮你。

Posted on 2021年8月14日2021年11月7日 By 黑猫 没有手机等智能设备也能控制乐高Control+套装?PyBricks可以帮你。无评论

关于 Powered Up 系统最常见的抱怨之一就是需要使用智能设备进行控制,并且您只能通过官方 Control+ 应用程序,通过屏幕进行触摸控制。即使利用Powered Up编程可以进行自定义控制,您仍然需要在智能设备上一直运行 Powered Up 应用程序。特别是对于小孩子而言,长期接触手机或平板等智能设备总归不是一件太好的事情。

现在,你可以利用Pybricks 来摆脱乐高Control+套装对智能设备的依赖。通过在集线器中运行PyBricks程序代码即可实现。听起来很复杂?其实很简单,无需编程基础也能搞定!以下是RacingBricks的视频教程。

下面是视频中用于带遥控器的 42124 越野车的修改代码:

from pybricks.pupdevices import Motor, Remote
from pybricks.parameters import Port, Direction, Stop, Button
from pybricks.tools import wait

# Initialize the motors.
steer = Motor(Port.B)
front = Motor(Port.A, Direction.COUNTERCLOCKWISE)

# Connect to the remote.
remote = Remote()

# Read the current settings
old_kp, old_ki, old_kd, _, _ = steer.control.pid()

# Set new values
steer.control.pid(kp=old_kp*4, kd=old_kd*0.4)

# Find the steering endpoint on the left and right.
# The middle is in between.
left_end = steer.run_until_stalled(-200, then=Stop.HOLD)
right_end = steer.run_until_stalled(200, then=Stop.HOLD)

# We are now at the right. Reset this angle to be half the difference.
# That puts zero in the middle.
steer.reset_angle((right_end - left_end)/2)
steer.run_target(speed=200, target_angle=0, wait=False)

# Set steering angle for the buggy
steer_angle = (((right_end - left_end)/2)-5)
print('steer angle:',steer_angle)

# Now we can start driving!
while True:
    # Check which buttons are pressed.
    pressed = remote.buttons.pressed()

    # Choose the steer angle based on the right controls.

    if Button.RIGHT_PLUS in pressed:
        steer.run_target(1400, -steer_angle, Stop.HOLD, False)
    elif Button.RIGHT_MINUS in pressed:
        steer.run_target(1400, steer_angle, Stop.HOLD, False)
    else:
        steer.track_target(0)

    # Choose the drive speed based on the left controls.
    drive_speed = 0
    if Button.LEFT_PLUS in pressed:
        drive_speed += 100
    if Button.LEFT_MINUS in pressed:
        drive_speed -= 100

    # Apply the selected speed.
    front.dc(drive_speed)

    # Wait.
    wait(10)

下面是带有遥控器的 42129 4×4 梅赛德斯-奔驰卡车的代码:

from pybricks.pupdevices import Motor, DCMotor, Remote
from pybricks.parameters import Port, Direction, Stop, Button, Color
from pybricks.hubs import TechnicHub
from pybricks.tools import wait

# Initialize the motors.
steer = Motor(Port.D)
drive1 = Motor(Port.B)
drive2 = Motor(Port.A)
diff_control = DCMotor(Port.C)

# Initialize the hub.
hub = TechnicHub()

# Connect to the remote.
remote = Remote()

# Read the current settings
old_kp, old_ki, old_kd, _, _ = steer.control.pid()

# Set new values
steer.control.pid(kp=old_kp*4, kd=old_kd*0.8)

#Set the differential lock in open position
diff_control.dc(100)
wait(400)
diff_control.brake()

hub.light.on(Color.GREEN)
hublight = 0

# Find the steering endpoint on the left and right.
# The middle is in between.
left_end = steer.run_until_stalled(-500, then=Stop.HOLD)
right_end = steer.run_until_stalled(500, then=Stop.HOLD)

# We are now at the right. Reset this angle to be half the difference.
# That puts zero in the middle.
steer.reset_angle((right_end - left_end)/2)
steer.run_target(speed=200, target_angle=0, wait=False)

# Set steering angle for the Zetros
steer_angle = (((right_end - left_end)/2)+5)

# Now we can start driving!
while True:
    # Check which buttons are pressed.
    pressed = remote.buttons.pressed()

    # Choose the steer angle based on the right controls.

    if Button.RIGHT_PLUS in pressed:
        steer.run_target(1400, -steer_angle, Stop.HOLD, False)
    elif Button.RIGHT_MINUS in pressed:
        steer.run_target(1400, steer_angle, Stop.HOLD, False)
    else:
        steer.track_target(0)

    # Choose the drive speed based on the left controls.
    drive_speed = 0
    if Button.LEFT_PLUS in pressed:
        drive_speed += 100
    if Button.LEFT_MINUS in pressed:
        drive_speed -= 100
    if Button.LEFT in pressed:
        print('Battery voltage:',(hub.battery.voltage())/1000,"V")
        wait(100)


    # Apply the selected speed.
    drive1.dc(drive_speed)
    drive2.dc(drive_speed)

    if (Button.RIGHT in pressed) and (hublight == 0):
        hublight = 1
        diff_control.dc(-100)
        wait(400)
        diff_control.brake()
        hub.light.on(Color.RED)
        
    
    elif (Button.RIGHT in pressed) and (hublight == 1):
        diff_control.dc(100)
        wait(400)
        diff_control.brake()
        hub.light.on(Color.GREEN)
        hublight = 0

    # Wait.
    wait(10)

参考来源:https://racingbrick.com/

关于PyBricks,后面我会抽时间进行更多介绍和编制教程。

机械组资源

文章导航

Previous Post: 《LEGO BOOST 机器人编程经典教程》中文翻译 第11章:四周看看
Next Post: LEGO BOOST 专用模块说明:多工用探测车MTR4

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

常用网址

Rebrickable
MOC分享网站。
LEGO官网图纸下载
官方图纸下载地址。
BrickLink
零件、人仔、套装买卖。
BrickSet
LEGO数据库网站。
Brothers-Brick
LEGO资讯第一门户。
EuroBricks
全球第一LEGO论坛。
LDraw零件
LDraw零件模型下载。
BrickOwl
零件、人仔、套装买卖。
BrickEconomy
LEGO价格及趋势分析。
Studio
LEGO虚拟拼搭软件。
BrickInsights
LEGO套装评分网站。
MecaBricks
3D在线LEGO拼搭工具。
LEGO官网设备指南
LEGO官方APP汇总。
LDD
LEGO虚拟拼搭软件。
BrickFanatics
LEGO资讯网站。
pyBricks
LEGO Python编程。
  • 乐高图纸 (76)
  • 套装评测 (13)
  • 机器人资源 (33)
  • 机器人资讯 (2)
  • 机械组资源 (28)
  • 机械组资讯 (26)
  1. 黑猫发表在乐高机械组官方套装Studio模型(IO文件)资源分享2022年12月22日

    找到一个lxf格式的,试了下Studio…

  2. 匿名发表在乐高电机性能参数(扭矩、转速)整理2022年12月19日

    棒

  3. 匿名发表在乐高机械组官方套装Studio模型(IO文件)资源分享2022年12月17日

    想要CREATOR系列10242MINI…

  4. 黑猫发表在乐高机械组官方套装Studio模型(IO文件)资源分享2022年11月17日

    没找到,只找到了71746的一些MOC的…

  5. 黑猫发表在留言板2022年11月17日

    方法有很多,看你具体的要求。可以看下下面…

Powered Up模块说明

Powered Up 教程一
流程模块
Powered Up 教程二
传感器模块
Powered Up 教程三
电机模块
Powered Up 教程四
自定义控制模块
Powered Up 教程五
声光模块
Powered Up 教程六
数据操作模块

LEGO Boost 模块说明

LEGO Boost教程一
流程模块
LEGO Boost教程二
传感器模块
LEGO Boost教程三
遥控模块模块
LEGO Boost教程四
电机模块
LEGO Boost教程五
声光模块
LEGO Boost教程六
数据操作模块
友情链接  |   DNF资料站   |   Brick4   |   孙老四   |   乐乐镇的故事

Copyright © 2023 LegoFind.

Powered by PressBook WordPress theme