pyb.USB_VCP() performance?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
bibile
Posts: 11
Joined: Sat Sep 24, 2016 9:37 am

pyb.USB_VCP() performance?

Post by bibile » Mon Nov 14, 2016 3:45 pm

Hi,

I run into a small performance issue.
I read an encoder and send a torque value (DAC) to a DC motor.

My control loop runs at 200Hz, using a Timer.
Everything works well, except when I try to send some data to the PC.

I use USB_VCP

Code: Select all

u = pyb.USB_VCP()
And write to the PC at 100Hz (another Timer) with:

Code: Select all

u.write(str(nbtop))
I tryed many things, using several timers, or other way, in the mainloop.
But as soon as I use the u.write(), it damages my control loop (encoder=>torque value), it still works, but less good.
I guess that u.write() uses to much time.

Is there a way to improve it?

bibile
Posts: 11
Joined: Sat Sep 24, 2016 9:37 am

Re: pyb.USB_VCP() performance?

Post by bibile » Tue Nov 15, 2016 10:19 am

Hi,

Would it be more efficient to use pyb.UART?
Actually, I will have to use a RS232/USB adapter, but why not!

Any idea?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: pyb.USB_VCP() performance?

Post by dhylands » Tue Nov 15, 2016 4:56 pm

The way USB works is that it's polled by the host.

So when you want to write data to the host, the pyboard has to wait for the host to ask it if it has any data available.

I haven't looked at the code to see how much data gets buffered and when exactly it gets buffered, but part of the performance equation depends on the host and how frequently it polls.

The data is actually transferred at 12 Mbits/sec. For UART the amount of time taken (on the pyboard) will be directly proportional to the baud rate. If you set the baud rate to 115200 bits/sec, then you will get about 11520 bytes per second sent thru the UART. Which means that each character will take approx 100 usec. So you multiply the number of characters you're sending by 100 usec and that''s how long each write call will take.

bibile
Posts: 11
Joined: Sat Sep 24, 2016 9:37 am

Re: pyb.USB_VCP() performance?

Post by bibile » Wed Nov 16, 2016 4:08 pm

Thanks!

I guess that with 9600, I am short.

On PC side, I simply run:

Code: Select all

import serial
ser = serial.Serial('COM5', 9600)
while True:
    nb = ser.in_waiting
    if nb > 0:
        buf = ser.read(nb)
        print buf
I will upgrade to 115200, that should help.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: pyb.USB_VCP() performance?

Post by dhylands » Wed Nov 16, 2016 4:44 pm

Note that the baud rate on the USB serial link is totally ignored and has no bearing on any performance (at least when talking via USB to say a pyboard).

Anytime pyb.USB_VCP is involved (on the MicroPython side) then the baud rate from the host is totally ignored. The baud calculations I did in my previous message apply to using a UART.

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: pyb.USB_VCP() performance?

Post by marfis » Wed Nov 16, 2016 8:27 pm

Measured USB VCP performance is about 8Mbit/sec (but this can be less if the host handles several devices).

The pyboard uses the USB SOF (start of frame) event sent by the host. This fires an interrupt every 1msec. In the irq handler it checks if something is pending to send. If so it sends the data in 256 Bytes chunks every 250usec.

As @dhylands pointed out, the specified baudrate on the PC is completly irrelevant for USB VCP.

The best way to proceed for you is to toggle a pin at the start/end of your control loop and write function, so you'll get an idea on how much processing power is left to submit the values. Since you are calling the control loop in 5msec interval (and I guess you'll calculate staff there) it seems plausible that your board is already close to 100% busy.

You'll need a small logic analyser for that, but it's well invested money.

Post Reply