3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Realsense D435i のIMUの値をUnityで取得する。

Last updated at Posted at 2020-02-20

UnityでIMUのデータを取得するためのスクリプト

データが取得できることを確認。。詳細はまだ、、、


Realsens SDK/Prefabs/RsDeveice を Assestにおいて、
RdDeviceの 
ストリームを"accel"または"gyro"
形式を"Motion Xyz 32f"
Framerate,StreamIndex,幅,高さを"0"
RsDevice.png

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と一緒にする。

image.png

まだ、うまくデータが取得出来たら、printは消す。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?