Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Output processed data fast

  1. #1
    Junior Member
    Join Date
    Jun 2020

    Output processed data fast

    Im new to this forum so please excuse me if i have posted this in the wrong category.

    Im doing a project where im using the madgwick algorithm to obtain roll, yaw, pitch angles. And i output the angles, all 3 accelerations, gyro and magnetometer outputs. So a output vector [roll, pitch, yaw, ax, ay, az, gx, gy gz, mx, my, mz]
    im using the teensy 3.6 at normal speed 180Ghz.

    My question is: when outputting processed data what is the fastest way of doing this?
    I can't seem to find the AVR instructions used for Serial.print() to get the exact number of clock cycles. digitalfastread is for reading from a pin directly so i assume this can't be used.
    I know that it's of course faster to output the data by using AVR clib functions to convert number to a string and then print the string and not send it to ASCII representation as the serial.print normaly does. However it is easier when having to use the data for calculations.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    There is no simple answer to the USB speed. It depends on many complicated factors.

    But as a general rule of thumb, if your program just uses Serial.print() without waiting, and if you don't disable interrupts, usually you can transmit about 1 MByte/sec using Teensy 3.x (much higher speeds are possible with Teensy 4.x). The USB code uses buffers and DMA, so it's very efficient. Likewise, the Serial.print() code is very fast, even for converting numbers to ASCII, so that's rarely a factor.

    So if your motion sensors are producing data at a rate like 100 Hz, you can transmit a message up to about 10K size before you run into the USB bandwidth limitation. If you're just transmitting 12 numbers, you might as well send them as ASCII text. You'll still be nowhere near 10,000 bytes to just send 12 numbers as text.

    The place where we see people run into performance problems is when their program waits. A common approach is to wait until Serial.available() shows you've received data from the PC, and then parse the command, then read the sensors and transmit a message. Sometimes this works, but often the result is dismal performance. Especially on Windows, the latency can be up to 16 ms, due to how the operating system schedules programs to run. Your program can spend nearly all its time waiting rather than doing anything useful and communicating. On the PC side, doing this sort of send-command-wait-for-response programming method almost never gives good performance over USB. Instead you want to have the Teensy side just transmit data as it becomes available from the sensor.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    What are the most common factors that has the greatest influence on the USB speed?
    And is the 1MByte/sec a value fround from experiments. I have read that the USB0 can transmit 12 Mbit/sec?
    I assume it would be faster to use the USB1 with 480 Mbit/sec as a host for a USB Stick or is this a fallacy?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts