Slow Serial Write In Python Compared To Arduino IDE Serial Monitor

Hi all,

This is my first post and the title/subject says it all.

For some background: I'm writing my Msc thesis and will be tracking and navigating a robot using a ceiling mounted camera. The image processing and navigation is done on my pc and through a USB serial connection (later I want to turn this into wireless) the robot will receive it's commands.

Robot is a mBot from makeblock which is based/uses a arduino uno and can be communicated with as such. I wrote a simple program to drive the robot respectively forward, backward left turn, right turn. The serial commands the sketch expects are: w,a,s or d. So far no problem and when I use the serial monitor from Arduino IDE it works fine. Meaning motors/robot reacts "immediatly" (I don't know if there is a way to easily measure response time?).

But my main program will be written in Python so I want to write serial commands through Python. So I wrote a simple program for this but here arrises the problem. Whenever I write a serial comman using python the robot responds/executes the command with 2 sec delay. I tried adjusting/removing the time.sleep in my code without luck. I'm using Spyder (Python 3.7) through anaconda as my Python IDE

So my question is: Is there a way to speed this up? Am i doing something wrong? I already spent some time on the forum looking for solutions but so far none have worked for me.

Here is my sketch

//For communicating with the mBot arduino board #include "MeMCore.h" // initializing the motors MeDCMotor motor1(M1); MeDCMotor motor2(M2); //initialize speed int leftspeed = 0; /* value: between -255 and 255. */ int rightspeed = 0; /* value: between -255 and 255. */ int speed = 100; //Has to be "defined" for the serial communication input String command; void setup() {   // initialize serial communication:   Serial.begin(9600); } void loop() {   if (Serial.available()) {     command = Serial.readStringUntil('\n');     if (command.equals("w")) { //forward       leftspeed = speed;       rightspeed = speed;     }     else if (command.equals("a")) { //left       leftspeed = 0;       rightspeed = speed;     }     else if (command.equals("s")) { //backward       leftspeed = -speed;       rightspeed = -speed;     }     else if (command.equals("d")) { //right       leftspeed = speed;       rightspeed = 0;     }   }   else {     Serial.println("Invalid command");     Serial.println(leftspeed);     Serial.println(rightspeed);   }   motor1.run((M1) == M1 ? -(leftspeed) : (leftspeed));   motor2.run((M2) == M1 ? -(rightspeed) : (rightspeed)); }

Here is my python code

import serial import time # Define the serial port and baud rate. # Default baud rate for mBot USB is 9600 and 115200 for Bluetooth # Ensure the 'COM#' is correctly selected for the mBot ser = serial.Serial('COM5', 9600) def drivepy():     user_input = input("\n Type w / a / s / d / quit: ")     if user_input =="w":         print("Driving Forward")         time.sleep(0.1)         ser.write(b'w')         drivepy()     elif user_input =="a":         print("Turn Left")         time.sleep(0.1)         ser.write(b'a')         drivepy()     elif user_input =="s":         print("Drive Backwards")         time.sleep(0.1)         ser.write(b's')         drivepy()     elif user_input =="d":         print("Turn Right")         time.sleep(0.1)         ser.write(b'd')         drivepy()     elif user_input =="quit" or user_input == "q":         print("Program Exiting")         time.sleep(0.1)         ser.write(b'L')         ser.close()     else:         print("Invalid input. Type on / off / quit.")         drivepy() time.sleep(2) # wait for the serial connection to initialize drivepy()

Probably I'm missing some basic knowledge here about serial communication in combination with Python. I hope someone can help me

Tag » Arduino.write(byte(x 'utf-8'))