Hello. As I wrote in the subject, I would like to create a motion controller project for a drone using the IMU MPU6050. I want to read data from the gyroscope (angular acceleration) using Python, and then move the drone (in my case, I operate on the AirSim simulator).
The problem is that although I wrote a program in Arduino IDE, which I uploaded to the board and which reads the information from the IMU almost immediately(from moving IMU), there are some delays in Python that I have no idea where they came from. Can someone help me? Here is the code for Arduino and Python:
Python:
import airsim
import serial
from PySide2.QtCore import QThread
import time
# connect to the AirSim simulator
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)
client.takeoffAsync().join()
client.moveToPositionAsync(-10, 10, -50, 5).join()
# Ustawienie połączenia z portem szeregowym
se = serial.Serial('COM6', 115200)
while True:
if se.in_waiting:
# Odczyt wszystkich dostępnych danych z bufora
buffer_data = se.read(se.in_waiting).decode('utf-8', 'ignore').strip().splitlines()
for data in buffer_data:
try:
# Sprawdzenie, czy linia zawiera dane prędkości kątowych
if "Xnorm = " in data and "Ynorm = " in data and "Znorm = " in data:
# Ekstrakcja wartości prędkości kątowych
parts = data.split()
Xnorm = float(parts[2])*3.14/180
Ynorm = float(parts[5])*3.14/180
Znorm = float(parts[8])*3.14/180
print(f"Xnorm: {Xnorm}, Ynorm: {Ynorm}, Znorm: {Znorm}")
# simulatorshit
client.moveByAngleRatesZAsync(roll_rate=Xnorm, pitch_rate=Ynorm, yaw_rate=Znorm, z=-50, duration=0.000001).join()
except (UnicodeDecodeError, IndexError, ValueError) as e:
print("Błąd w przetwarzaniu danych:", e)
Arduino:
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup()
{
Serial.begin(115200);
Serial.println("Inicjalizacja MPU6050");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Nie mozna znalezc MPU6050 - sprawdz polaczenie!");
delay(500);
}
// Kalibracja żyroskopu
mpu.calibrateGyro();
// Ustawienie czułości
mpu.setThreshold(3);
}
void loop()
{
Vector rawGyro = mpu.readRawGyro();
Vector normGyro = mpu.readNormalizeGyro();
Serial.print(" Xnorm = ");
Serial.print(normGyro.XAxis);
Serial.print(" Ynorm = ");
Serial.print(normGyro.YAxis);
Serial.print(" Znorm = ");
Serial.println(normGyro.ZAxis);
}
Does anyone have ane advice how to deal with delays?