0

I am using python to read data from a USB input device. I would like to know if there is a way this could be exchanged with the model in MATLAB real-time. How I do it now is to save the data read in a .mat file and then let the model read it from there, which is not very intuitive.The code I use for this is as below:

    #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
user3856486
  • 515
  • 1
  • 4
  • 16
  • So does this work, and you are looking for something better, or are there problems? Problems with running this code, or with reading the .mat in MATLAB? If the latter, have you tried reading the .mat with another Python process? – hpaulj Jul 30 '14 at 02:10
  • This works. What I am now trying to do is make the data available to the MATLAB model simultaneously as it is being read (as good as MATLAB reading it). Instead of storing it in in a *.mat file and then reading it from there on. – user3856486 Jul 30 '14 at 02:39
  • have you thought about reading in the data with matlab directly? And what is your "model"? Is it implemented in Simulink? Do you have the [**Simulink Real-Time**](http://www.mathworks.com/products/simulink-real-time/) Toolbox available? – Robert Seifert Jul 30 '14 at 07:12
  • My USB device is a Logitech driving force gt. I have tried using the instrument control toolbox to read data but, it seems like this device is not supported. I also have read that if I can set up a virtual COM port, data can be read in MATLAB. This would need a USB-COM adapter. And yes I have a real-time toolbox available. – user3856486 Jul 30 '14 at 16:10

1 Answers1

0

You have a few options.

  • You can poll from within Matlab for the presence of a file, then read in the new data when available
  • You can open a pipe to perform inter-process communication between python and matlab (also requires polling from the matlab side). See here for code.
  • You can use a local UDP or TCP socket for communication. Either by using PNET (which will still require polling), or the matlab Instrument Control Toolbox (which allows you to configure a callback function).

Since matlab is single-threaded, your model will have to be designed with the provision of new data in mind. You will need to explicitly trigger the model to re-evaluate when new data is provided.