UnityでIMUのデータを取得するためのスクリプト
データが取得できることを確認。。詳細はまだ、、、
Realsens SDK/Prefabs/RsDeveice を Assestにおいて、
RdDeviceの
ストリームを"accel"または"gyro"
形式を"Motion Xyz 32f"
Framerate,StreamIndex,幅,高さを"0"

Scriptを作成する。名前はIMUにする。
IMU.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Intel.RealSense;
using System;
using System.Threading;
using UnityEngine.Events;
using System.Runtime.InteropServices;
public class IMU : MonoBehaviour
{
private static TextureFormat Convert(Format lrsFormat)
{
switch (lrsFormat)
{
case Format.Z16: return TextureFormat.R16;
case Format.Disparity16: return TextureFormat.R16;
case Format.Rgb8: return TextureFormat.RGB24;
case Format.Rgba8: return TextureFormat.RGBA32;
case Format.Bgra8: return TextureFormat.BGRA32;
case Format.Y8: return TextureFormat.Alpha8;
case Format.Y16: return TextureFormat.R16;
case Format.Raw16: return TextureFormat.R16;
case Format.Raw8: return TextureFormat.Alpha8;
case Format.Disparity32: return TextureFormat.RFloat;
case Format.Yuyv:
case Format.Bgr8:
case Format.Raw10:
case Format.Xyz32f:
case Format.Uyvy:
case Format.MotionRaw:
case Format.MotionXyz32f:
case Format.GpioRaw:
case Format.Any:
default:
throw new ArgumentException(string.Format("librealsense format: {0}, is not supported by Unity", lrsFormat));
}
}
public RsFrameProvider Source;
public Stream _stream;
public Format _format;
public int _streamIndex;
Predicate<Frame> matcher;
public struct Vector3
{
public float x, y, z;
}
public Vector3 ImuData;
void Start()
{
Source.OnStart += OnStartStreaming;
Source.OnStop += OnStopStreaming;
}
void Update()
{
}
void OnDestroy()
{
}
protected void OnStopStreaming()
{
Source.OnNewSample -= OnNewSample;
}
void OnNewSample(Frame frame)
{
try
{
if (frame.IsComposite)
{
using (var fs = frame.As<FrameSet>())
using (var f = fs.FirstOrDefault(matcher))
{
if (f != null)
{
ImuData=Marshal.PtrToStructure<Vector3>(f.Data);
print("x=" + ImuData.x + " , y=" + ImuData.y + " , y=" + ImuData.z);
}
return;
}
}
if (!matcher(frame))
return;
}
catch (Exception e)
{
Debug.LogException(e);
// throw;
}
}
public void OnStartStreaming(PipelineProfile activeProfile)
{
matcher = new Predicate<Frame>(Matches);
Source.OnNewSample += OnNewSample;
}
private bool Matches(Frame f)
{
using (var p = f.Profile)
return p.Stream == _stream && p.Format == _format && p.Index == _streamIndex;
}
}
RsDeviceの下に空のGameObjectを置く。(名前を例えばIMUにする)
上記スクリプトを組み込む。設定をRsDeviceと一緒にする。
まだ、うまくデータが取得出来たら、printは消す。
