IMU data in void Update()


I would like to receive data from Bluetooth IMU module, and rotate a gameObject with it!
I successfully connect and get first data from the sensor with void Start () method;

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using LpmsCSharpWrapper;

public class ImuReader_BB : MonoBehaviour 
	public string sensor1;
	public float rotationX;
	public float rotationY;
	public float rotationZ;

	void Start ()
		string sensor1 = "00:04:3E:9F:E0:DA";

		// Initalize sensor manager
		LpSensorManager.initSensorManager ();

		// Connect to sensor 1 
		LpSensorManager.connectToLpms (LpSensorManager.DEVICE_LPMS_B2, sensor1);
		//wait for estabilishment of sensor 1 connection
		Debug.Log ("Sensor connected");

	void Update()
		SensorData sd1;
			sd1 = *((SensorData*)LpSensorManager.getSensorData(sensor1));
		Debug.Log(String.Format("{0:0.000} {1:0.000} {2:0.000} {3:0.000} ", 
			sd1.timeStamp,, sd1.ay,;

To move the gameObject I would like to call per frame in void Update() but in this way, every data from the sensor is 0.

What I’m doing wrong?


Hi @nincs,

it seems like your code displays the acceleration data of your IMU, as you print the ax, ay, and az data fields. In order to have them filled in you may have to change the configuration of your LPMS-B2 IMU in LpmsControl. Also, in order to obtain the rotation data you will have to look at the quaternion data instead of the acceleration data. Make sure that the IMU is configured to transfer the quaternion data and look at that data instead, i.e., the qw, qx, qy, qz fields. There are a few conventions that are different: Unity uses a left-handed coordinate system while the LPMS sensor uses a right-handed system; the convention for world-to-object or object-to-world may be different (I can never remember); the LP library uses z for the vertical, Unity uses y. Therefore you will have to modify the quaternion before passing it to Unity, I think unity x = sd1.qx, unity y = sd1.qz, unity z = sd1.qy, unity w = sd.qw should do the trick.

Finally, getNextData actually returns the front of the data queue. In order to retrieve the latest data sample, you will have to loop over getNextData until you are at the latest sample. Untested example code follows:

double lastTimeStamp = -1;
bool found;
do {
   sd1 = *((SensorData *)LpSensorManager.getSensorData(sensor1));
   found = sd1.timeStamp == lastTimeStamp;  // stop if we found the same timestamp twice, i.e. the same sample
   lastTimeStamp = sd1.timeStamp;
} while (!found);
// example use of sd1 ... conventions may be different.
this.transform.localRotation = new Quaternion(sd1.qx, sd1.qz, sd1.qy, sd1.qw);

Hope this helps, please let me know here or follow up at

ps I just learned that an example Unity project can be found here: