【问题标题】:Real-time data transfer from Python to MATLAB从 Python 到 MATLAB 的实时数据传输
【发布时间】:2014-07-30 00:30:40
【问题描述】:

我正在使用 python 从 USB 输入设备读取数据。我想知道是否有一种方法可以与 MATLAB 中的模型实时交换。我现在的做法是将读取的数据保存在.mat文件中,然后让模型从那里读取,这不是很直观。我使用的代码如下:

    #Import the needed libraries
    import usb.core,usb.util,sys,time
    import sys
    from array import *
    import scipy.io

    #find our device
    dev = usb.core.find(idVendor=0x046d, idProduct=0xc29a)

    # was it found?
    if dev is None:
       raise ValueError('Device not found')

   # set the active configuration. With no arguments, the first
   # configuration will be the active one
   try:
     dev.set_configuration()
     #In the event of an error
   except usb.core.USBError as e:
     print('Cannot set configuration the device: %s' %str(e))
     sys.exit()

   # get an endpoint instance
   cfg = dev.get_active_configuration()
   intf = cfg[(0,0)]
   ep = usb.util.find_descriptor(
               intf,
     # match the first IN endpoint
     custom_match = \
     lambda e: \
     usb.util.endpoint_direction(e.bEndpointAddress) == \
     usb.util.ENDPOINT_IN)

  #Initialising variables
  #Databases for access in MATLAB
  gas_pedal_data={}
  brake_pedal_data={}
  steering_wheel_bit5_data={}
  steering_wheel_bit6_data={}
  gas_pedal=[]
  brake_pedal=[]
  steering_wheel_bit5=[]
  steering_wheel_bit6=[]
  i=0
  #Read data from the device as long as it is connected
  while(dev!= None):
       try:
          #Read data
          data = dev.read(ep.bEndpointAddress, ep.wMaxPacketSize,
                          timeout=1000)
          gas_pedal.append(data[6])
          gas_pedal_data['gas_pedal']=gas_pedal
          scipy.io.savemat('test.mat',gas_pedal_data)
          brake_pedal.append(data[7])
          brake_pedal_data['brake_pedal']=brake_pedal
          scipy.io.savemat('test.mat',brake_pedal_data)
          steering_wheel_bit5.append(data[4])
          steering_wheel_bit5_data['steering_wheel_bit5']=steering_wheel_bit5
          scipy.io.savemat('test.mat',steering_wheel_bit5_data)
          steering_wheel_bit6.append(data[5])
          steering_wheel_bit6_data['steering_wheel_bit6']=steering_wheel_bit6
          scipy.io.savemat('test.mat',steering_wheel_bit6_data)
       except usb.core.USBError as e:
          print("Error readin data: %s" %str(e))
          time.sleep(5)
          continue

【问题讨论】:

  • 那么这行得通吗,您正在寻找更好的东西,还是有问题?运行此代码或在 MATLAB 中读取 .mat 时出现问题?如果是后者,您是否尝试过使用另一个 Python 进程读取 .mat?
  • 这行得通。我现在要做的是在读取数据的同时使数据可用于 MATLAB 模型(与 MATLAB 读取数据一样好)。而不是将其存储在 *.mat 文件中,然后从那里读取它。
  • 有没有想过直接用matlab读入数据?你的“模型”是什么?是否在 Simulink 中实现?你有Simulink Real-Time 工具箱吗?
  • 我的USB设备是罗技驱动GT。我曾尝试使用仪器控制工具箱读取数据,但似乎不支持此设备。我还读到如果我可以设置一个虚拟 COM 端口,就可以在 MATLAB 中读取数据。这需要一个 USB-COM 适配器。是的,我有一个实时工具箱可用。

标签: python matlab scipy


【解决方案1】:

你有几个选择。

  • 您可以在 Matlab 中轮询文件是否存在,然后在可用时读入新数据
  • 可以打开管道进行python和matlab之间的进程间通信(也需要从matlab端轮询)。代码见here
  • 您可以使用本地 UDP 或 TCP 套接字进行通信。使用PNET(仍需要轮询)或matlab Instrument Control Toolbox(允许您配置回调函数)。

由于 matlab 是单线程的,因此您的模型在设计时必须考虑到提供新数据。当提供新数据时,您需要显式触发模型以重新评估。

【讨论】:

猜你喜欢
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 2021-03-01
相关资源
最近更新 更多