Making motion controller (something like avata motion sensor) for UAV with MPU6050 (need help)

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?

It sounds like the delays you mention are due to communications failures on the Python end, and have nothing to do with Arduino.

It would help if the Arduino transmitted data using a more sensible serial communications protocol, with record start and end markers, as described in this tutorial: Serial Input Basics - updated - #3 by Robin2

By the way, the gyro data are angular rates of rotation, not angular acceleration.

thank u so much for your time!
As u said I changed my code and it is working (i mean transmission data protocol)!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.