I'm making a car-type-thingy where I control motors connected to an arduino with a raspberry pi via serial communication. I'm using the adafruit motor shield to control the motors. I want to control the car with arrow keys and I've used pygame, turtle and I've even tried pynput for that. They all work fine at first but I after an amount of time the commands being sent start lagging. I have print statements for whenever a key is pressed.
So one of the examples of what happens when the delays start is when I hold the up arrow key, the print statement may come up once or twice but then it would get stuck and after maybe 2 or 3 seconds (sometimes more), a bunch of print statements would appear and the motors would start moving for the amount of time it wouldve moved if there wasnt any lag.
In the beginning I thought this was an issue for the pi, that it was slow and couldnt process the code or something of that sorts but when I tried executing the pynput version of the code in the raspberry pi terminal, I would see "[[A" whenever i pressed the up key or something indicating that the raspberry pi was in fact detecting the key presses and it had nothing to do with the speed of the rpi.
Python code running on the RPi.
from pynput.keyboard import Listener
import serial
import time
uno = serial.Serial("/dev/ttyACM0", 115200, timeout=5)
print("Awaiting serial communication bootup...")
time.sleep(5)
print("Serial communication established")
def write_to_file(key):
if key == key.up:
uno.write(b'w')
print(b'w')
print("Up pressed")
elif key == key.down:
uno.write(b's')
print(b's')
print("Down pressed")
elif key == key.left:
uno.write(b'a')
print(b'a')
print("Left pressed")
elif key == key.right:
uno.write(b'd')
print(b'd')
print("Right pressed")
else:
uno.write(b"")
with Listener(on_press=write_to_file) as l:
l.join()
Arduino code.
#include <AFMotor.h>
AF_DCMotor motorA(3, MOTOR12_64KHZ);
AF_DCMotor motorB(4, MOTOR12_64KHZ);
int sped = 127;
int del = 90;
void setup() {
Serial.begin(115200);
Serial.println("Default speed set to 255");
motorA.setSpeed(sped);
motorB.setSpeed(sped);
}
void loop() {
if (Serial.available() > 0)
{
int directionz = Serial.read();
if (directionz == 119){
motorA.run(FORWARD);
motorB.run(FORWARD);
delay(del);
Serial.println(directionz);
motorA.run(RELEASE);
motorB.run(RELEASE);
}
if (directionz == 115){
motorA.run(BACKWARD);
motorB.run(BACKWARD);
delay(del);
Serial.println(directionz);
motorA.run(RELEASE);
motorB.run(RELEASE);
}
if (directionz == 100){
motorA.run(FORWARD);
motorB.run(BACKWARD);
delay(del);
Serial.println(directionz);
motorA.run(RELEASE);
motorB.run(RELEASE);
}
if (directionz == 97){
motorA.run(BACKWARD);
motorB.run(FORWARD);
delay(del);
Serial.println(directionz);
motorA.run(RELEASE);
motorB.run(RELEASE);
}
if (directionz == 'c'){
Serial.println(directionz);
Serial.println("Change Speed to? ");
if (Serial.available() > 0){
sped = Serial.read() - '0';
Serial.println("speed is now set to ");
Serial.println(sped);}
Serial.println("You may continue");
}
else{
motorA.run(RELEASE);
motorB.run(RELEASE);
}
}
}
One of the things I saw that worked that I didnt put in here is that I made a reset function on the raspberry pi code. In the function, I closed off the serial communication and then reopened it using the same procedure and all. That seemed to have worked but once again, after an amount of time, the whole thing started delaying again and I dont want to have reset every time things start to delay so a solution would be very helpful. Thank you.