n-wach / camino

A library for controlling an Arduino from Python over Serial
MIT License
16 stars 3 forks source link

Execution rate #8

Closed jpagliaccio closed 1 year ago

jpagliaccio commented 1 year ago

It looks like each communication takes 50 ms. Is that what you see? Any way to speed that up? Here is the Python code. I am on an Arduino MEGA 2560 at 115200 baud with your default ino file and just the baud rate changed.

while(1):
    n = n + 1       
    tms = time.time() * 1000
    arduino.digital_read(9) # 50 ms 
    arduino.say_hi(16, out=str) # 50 ms 
    a0 = arduino.analog_read(0, out=int) # 50 ms
    arduino.pin_mode(16, 1) # 50 ms  
    time.sleep(.005) # add 5 ms  
    tms = time.time() * 1000 - tms 
    print('n =', n, 'mills =', int(tms))

Thanks for sharing the code and your help.

n-wach commented 1 year ago

That seems unusual. Unfortunately I don't have access to an Arduino so am unable to confirm. It's also been a while since I've done serious performance testing. Totally possible something is messed up in the code.

But I'm 90% sure that I've gotten sub 50ms at that baud rate. I want to say around 2-3ms... That would've been using the GPIO UART port on a Raspberry Pi 3b and an Arduino Mega 2560.

What is your set up?

n-wach commented 1 year ago

I just noticed this line:

https://github.com/n-wach/camino/blob/main/camino/__init__.py#L94

time.sleep(0.05)

That must be the cause, and I’m not sure why it’s there. Any chance you could try with that line removed?

jpagliaccio commented 1 year ago

Thanks. The response was always a consistent 50 ms. Very suspicious. I was grepping 5's this morning and found the same.

Removing line 94 from init.py gives a healthy 4 ms response time. It looks the same (4 ms) for each of the functions (eg: analog_read or pin_mode). I did not find any other delays or sleeps.

So the results now:

at 19,200 baud I get 10 ms per function call
at 115,200 is 4 ms over 115,200 (eg: 150,000) same 4 ms over 150,000 returns " nothing sent"

I will test some more today to make sure everything is working.

Thanks again.

Ps: I may try to read more than one pin at a time, like Modbus to see how that behaves.

jpagliaccio commented 1 year ago

For multiple reads, this callable is working w/o much of a performance hit

// -----------------------------------------------------------
// Return 6 analog values in a comma separted string
// Py read with atup = arduino.multiAnalog(out=str).split(',') 
// -----------------------------------------------------------
void multiAnalog(byte dataLength, byte *dataArray) {

  char pubString[256] = "";

  a0 = analogRead(A0);
  sprintf(pubString, "%d,%d,%d,%d,%d,%d", a0, a0, a0, a0, a0, a0);
  returns(pubString);
}
n-wach commented 1 year ago

Awesome! I'll push an update.

Thanks for the help, and let me know if you find any other issues