0

I need to work with 5x MPU9250 sensors, i2c 400kHz, Arduino nano 8MHz also, I have HC-05 Bluetooth device 115200 baud rate serial.

is it possible to pre-calculate loop time to gather data from sensors (3x accelerometer,3x gyro, 3x magnetometer) and send through HC-05 serial Bluetooth module without coding and setting up the above setup?

Voltage Spike
  • 75,799
  • 36
  • 80
  • 208
vassidefuk
  • 89
  • 1
  • 8
  • You can use a timer to get accurate timings or if you code directly in assembly number of cycles for each instruction is known. – Warren Hill Apr 10 '20 at 07:01
  • 1
    Before you can estimate, you need info how much data thare will be transferred on each bus (UART and I2C). UART can transfer 11520 bytes per second and I2C can transfer over 44000 bytes per second. So whatever data you gather, it takes roughly 3x-4x more time to transmit it out from UART, taking I2C overhead into account. Even if you use interrupts to handle two interfaces simultaneously. – Justme Apr 10 '20 at 08:42
  • 1
    UART baud rate is "bits", not "bytes" per second and you have to take into account start, stop, and possibly parity bits. – Warren Hill Apr 10 '20 at 09:32

2 Answers2

2

Yes, it is possible, but you need more information than you provided.

You mention 9x sensors, i2c rate is 400 kHz; for a 16b register you need approximately 30 clock pulses, that means 75 µs; assuming you mean 9x axis and not 9x, 3-axis sensors, and assuming each axis is a 16 bit value, this means you will need approximately 675 µs to read all the sensors.

Then you want to send your stuff through serial, again the payload is assumed to be 16b x 9 = 144b, at 115200 baud (=bits per second) this will take 1.56 ms, assuming 8N1.

The total is a little more of 2 ms, that would be 500 Hz of loop frequency.

This assumes that the micro can move the data around as soon as data is ready, but with an 8 MHz clock I am not sure you're gonna be able to do that.

I would say that, given my assumptions, a safe bet is a 100 Hz loop; you probably can go faster, but 100 Hz should be easy to achieve.

If the sensor data is more (or less) than 16b then of course the calculation needs to be redone.

Vladimir Cravero
  • 16,007
  • 2
  • 38
  • 71
  • What about 'stop', 'start' and 'parity' bits these will increase the payload slightly. You don't have to have 'parity' but you need the other two. – Warren Hill Apr 10 '20 at 07:46
  • Start and stop bits are already calculated in. 115200 bits per second at 8N1 is simply 11520 bytes per second. – Justme Apr 10 '20 at 08:45
  • 2
    Assuming 8N1 then 10bits are transmitted for 8 bits of data so 16bit data requires 20 bits to send. 20 bits x 9 = 180 bits transmitted making the minimum transmission time 1.5625ms. A 2ms loop (500Hz) may still be possible if the other peripherals are fast enough, 100Hz should easily be possible. The baud rate is transmitted bits not data bits, look on a scope to verify this for yourself. – Warren Hill Apr 10 '20 at 09:19
  • Thanks @WarrenHill, refined the estimate as per your comment. – Vladimir Cravero Apr 10 '20 at 10:18
2

No, it is not possible to calculate it in advance of writing the software.

Just to be sure of your terminology: 'calculate' means to reach a precise and exact answer mathematically.

If you'd said 'estimate', you'd be able to take your communications time and add a margin for data processing. That margin would be a combination of (a) your CPU's execution time and (b) a high-guess for the number of instructions needed from your experience or from example software.

So, as ever, it depends on what you are trying to do. But if it's a calculation you're looking for then you have to add all the elements. That includes your software execution time between comms, which means you need the software.

Your CPU's clock speed is low enough for its execution times to represent a percentage of the communications times, so that needs to be accounted for. That means knowing what the software will do inbetween receiving data and it being transmitted on another interface, which will be seen as communications latency if you're reading data and immediately transmitting it or something.

TonyM
  • 21,742
  • 4
  • 39
  • 62