MPU9250(IMU) Rotation - Yaw,Pitch,Roll Testing with Unity Application

MPU9250 Sensor

The MPU9250 is a 9-axis sensor consisting of an accelerometer, a gyrometer, and a magnetometer. MPU9250 is commonly used in smartphones, tablets, wearable sensors, and other electronic products.

The MPU9250 consists of 2 sensor units. 1) MPU-6500, which includes a three-axis accelerometer and a three-axis gyrometer. 2) The AK8963 sensor is a three-axis digital compass.

First of all, connect MPU9250 with any one of the microcontrollers (STM32, Arduino, NRF52, Raspberry Pi, Jetson, etc.)

From the micro controller, send the data to MongoDB. From MongoDB, the Unity application will take the data and show the visualization.


Unity Application

  • Open the Unity hub and create a new project.
    • Then add assets to the scene, i.e. a 3D cube. Then scale it to look like an imu sensor.
    • Create a C# script and open it with Visual Studio.

  • Add the following code to the c# code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO.Ports;
using System;
using System.Threading;
using MongoDB.Driver;
using MongoDB.Bson;


public class cube2 : MonoBehaviour
{
	double pitch, yaw, roll;
	MongoClient dbClient = new MongoClient("mongodb://admin:myadminpassword@92.205.18.120:27017/?authSource=admin&authMechanism=SCRAM-SHA-256&readPreference=primary&appname=MongoDB%20Compass&ssl=false");
	private Thread _t1;
	public int threadsToUse = 2;
	int threadsStarted, threadsCompleted;
	int pixelsPerThread, lostPixels;

	void Start()
	{
		_t1 = new Thread(_func1);
	}

	void Update()
	{

		var database = dbClient.GetDatabase("ASSET_TRACKING");
		var collection = database.GetCollection<BsonDocument>("Tag2");
		var filter = Builders<BsonDocument>.Filter.Eq("bool", 1);

		var doc = collection.Find(filter).FirstOrDefault();
		//String RoleName = doc.GetValue("bool").AsString;
		Debug.Log(doc.GetValue("Roll"));
		pitch = doc.GetValue("Pitch").ToDouble(); 
		roll = doc.GetValue("Roll").ToDouble(); 
		yaw = doc.GetValue("Yaw").ToDouble();
		transform.rotation = Quaternion.Euler(new Vector3((float)pitch, (float)yaw, (float)roll));
		if (Input.GetKey("escape"))
		{
			Application.Quit();
		}
	}
  • Then save the code in Visual Studio and move to the Unity editor.
    • Add the c# script to the cube object.
    • Press the play button. Then you can see the cube object move.
    • Then build the application.
    Final application


    Post a Comment

    Previous Post Next Post