dummy机械臂代码学习(八)

        今天,让我们来看一下shell.py文件吧。

        这段代码呢是一个Python脚本,用于与REF-Unit设备进行交互。具体内容如下

import fibre
import ref_tool
from ref_tool.utils import start_liveplotter

def print_banner():#打印欢迎信息
    print('Please connect your Dummy-Robot.')
    print('You can also type help() or quit().')

def print_help(args, have_devices):
    print('')
    if have_devices:
        print('Connect your REF-Unit to {} and power it up.'.format(args.path))
        print('After that, the following message should appear:')
        print('  "Connected to REF-Unit [serial number] as odrv0"')
        print('')
        print('Once the REF-Unit is connected, type "ref0." and press <tab>')
    else:
        print('Type "ref0." and press <tab>')
    print('This will present you with all the properties that you can reference')
    print('')
    print('For example: "odrv0.motor0.encoder.pos_estimate"')
    print('will print the current encoder position on motor 0')
    print('and "odrv0.motor0.pos_setpoint = 10000"')
    print('will send motor0 to 10000')
    print('')


interactive_variables = {}

discovered_devices = []

def did_discover_device(odrive, logger, app_shutdown_token):
    """ 连接和断开连接事件
    message and making the device available to the interactive
    console
    """
    serial_number = odrive.serial_number if hasattr(odrive, 'serial_number') else "[unknown serial number]"
    if serial_number in discovered_devices:
        verb = "Reconnected"
        index = discovered_devices.index(serial_number)
    else:
        verb = "Connected"
        discovered_devices.append(serial_number)
        index = len(discovered_devices) - 1
    interactive_name = "odrv" + str(index)

    # Publish new ODrive to interactive console
    interactive_variables[interactive_name] = odrive
    globals()[interactive_name] = odrive # Add to globals so tab complete works
    logger.notify("{} to ODrive {:012X} as {}".format(verb, serial_number, interactive_name))

    # Subscribe to disappearance of the device
    odrive.__channel__._channel_broken.subscribe(lambda: did_lose_device(interactive_name, logger, app_shutdown_token))

def did_lose_device(interactive_name, logger, app_shutdown_token):
    """
    Handles the disappearance of a device by displaying
    a message.
    """
    if not app_shutdown_token.is_set():
        logger.warn("Oh no {} disappeared".format(interactive_name))

def launch_shell(args, logger, app_shutdown_token):
    """
    Launches an interactive python or IPython command line
    interface.
    As ODrives are connected they are made available as
    "odrv0", "odrv1", ...
    """

    interactive_variables = {
        'start_liveplotter': start_liveplotter,
    }

    fibre.launch_shell(args,
                       interactive_variables,
                       print_banner, print_help,
                       logger, app_shutdown_token,
                       branding_short="dummy", branding_long="Dummy-Robot")

        首先呢引入了一些模块,liveplotter是实时绘图模块

import fibre
import ref_tool
from ref_tool.utils import start_liveplotter

        然后打印了欢迎信息和帮助信息,提供有关如何连接和使用REF-Unit设备的详细说明。其中,print_help(args, have_devices)函数里有两个参数

  • args是命令行参数,用于确定是否已经连接到设备。
  • have_devices是一个布尔值,指示是否已经连接到设备。

帮助信息内容如下

  1. 设备连接说明

    • 根据have_devices参数判断是否已经连接到设备,如果已连接则打印相应的连接状态信息,否则打印提示信息。
    • 提供连接设备的命令行参数示例,包括设备的串口号和波特率,以便用户可以按照示例来连接设备。
  2. 设备使用说明

    • 提供与设备交互的命令行操作示例,包括读取和设置设备参数,执行设备动作等。这些示例命令将向用户展示如何使用脚本与设备进行通信。
    • 提供连接到设备后的命令行操作示例,这些示例命令将展示一些在连接设备后可以执行的操作,如读取设备信息、执行设备动作等。
def print_banner():#打印欢迎信息
    print('Please connect your Dummy-Robot.')
    print('You can also type help() or quit().')

def print_help(args, have_devices):
    print('')
    if have_devices:
        print('Connect your REF-Unit to {} and power it up.'.format(args.path))
        print('After that, the following message should appear:')
        print('  "Connected to REF-Unit [serial number] as odrv0"')
        print('')
        print('Once the REF-Unit is connected, type "ref0." and press <tab>')
    else:
        print('Type "ref0." and press <tab>')
    print('This will present you with all the properties that you can reference')
    print('')
    print('For example: "odrv0.motor0.encoder.pos_estimate"')
    print('will print the current encoder position on motor 0')
    print('and "odrv0.motor0.pos_setpoint = 10000"')
    print('will send motor0 to 10000')
    print('')

        接下来,代码定义了一个字典interactive_variables,用于存储交互式命令行界面中可用的变量和函数。这些变量和函数可以在命令行中直接使用。然后定义了两个函数did_discover_device()did_lose_device(),用于处理设备连接和断开连接的事件。具体解释见代码

interactive_variables = {}

discovered_devices = []

def did_discover_device(odrive, logger, app_shutdown_token):
    """ 连接和断开连接事件
    message and making the device available to the interactive
    console
    """
    serial_number = odrive.serial_number if hasattr(odrive, 'serial_number') else "[unknown serial number]"#获取设备的序列号如果设备对象具有serial_number属性,则获取该属性的值,
                                                                                                           #否则将序列号设置为"[unknown serial number]"。
    if serial_number in discovered_devices:#检查设备是否已经在discovered_devices列表中
        verb = "Reconnected" #如果是,将verb设置为重连
        index = discovered_devices.index(serial_number)#获取设备在discovered_devices列表中的索引
    else: #否则
        verb = "Connected"#将动词设置为"Connected"
        discovered_devices.append(serial_number) #将设备的序列号添加到discovered_devices列表中
        index = len(discovered_devices) - 1  #根据列表的长度计算出设备在列表中的索引
    interactive_name = "odrv" + str(index)#根据索引生成一个交互式名称,格式为"odrv" + 索引

    # Publish new ODrive to interactive console
    interactive_variables[interactive_name] = odrive#将设备添加到交互式命令行界面中可用的变量中,变量名为交互式名称
    globals()[interactive_name] = odrive # Add to globals so tab complete works将设备添加到全局变量中,以便在命令行中能够通过tab键自动补全变量名
    logger.notify("{} to ODrive {:012X} as {}".format(verb, serial_number, interactive_name))#使用日志记录器记录设备连接的消息

    # Subscribe to disappearance of the device
    odrive.__channel__._channel_broken.subscribe(lambda: did_lose_device(interactive_name, logger, app_shutdown_token))#在设备连接断开时,注册一个回调函数来处理断开设备的情况

def did_lose_device(interactive_name, logger, app_shutdown_token):
    """
    Handles the disappearance of a device by displaying
    a message.
    """
    if not app_shutdown_token.is_set():
        logger.warn("Oh no {} disappeared".format(interactive_name))

        

        最后,代码调用了fibre.launch_shell()函数,启动一个交互式的命令行界面。fibre.launch_shell()函数的参数包括:

  • args:命令行参数,包含了与设备连接相关的信息。用于在启动交互式界面时进行设备连接。
  • interactive_variables:交互式命令行界面中可用的变量和函数。这些变量和函数可以在命令行中直接使用。
  • print_bannerprint_help:用于打印欢迎信息和帮助信息的函数。在启动交互式界面时,这些函数会被调用来打印相关信息。
  • logger:日志记录器,用于记录程序运行过程中的消息和错误。它可以将日志消息记录到控制台、文件或其他目标。
  • app_shutdown_token:应用程序关闭令牌,用于标记应用程序是否正在关闭。当应用程序关闭时,交互式界面也应该相应地进行关闭。
  • branding_shortbranding_long:用于定制命令行界面的品牌信息。这些信息会被显示在命令行界面的欢迎信息中。

在这个交互式界面中,用户可以通过输入命令来操作设备,还可以使用一些预定义的变量和函数进行一些特定的操作,如启动实时绘图器等。具体内容如下

def launch_shell(args, logger, app_shutdown_token):
    """
    Launches an interactive python or IPython command line
    interface.
    As ODrives are connected they are made available as
    "odrv0", "odrv1", ...
    """

    interactive_variables = {
        'start_liveplotter': start_liveplotter,
    }

    fibre.launch_shell(args,
                       interactive_variables,
                       print_banner, print_help,
                       logger, app_shutdown_token,
                       branding_short="dummy", branding_long="Dummy-Robot")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值