Unity 使用G4做雷达互动开发

Unity 使用G4做雷达互动开发_第1张图片

下面是主要代码,基本上都是sdk中的代码,只不过自己做了雷达监测数据显示。

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
//using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public struct ST_Point
    {
        public float x;         /*	坐标, 雷达点云在设定识别区域中的坐标 */
        public float y;
    };

    //Mark 这里不需要加.dll
    [DllImport("ydlidar_driver.dll", EntryPoint = "EAIScreenLd_init", CharSet = CharSet.Ansi,
        ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]

    extern static IntPtr EAIScreenLd_init(char[] ulrSetting);

    [DllImport("ydlidar_driver.dll", EntryPoint = "EAIScreenLd_getLdMap", CharSet = CharSet.Ansi,
        ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]

    extern static int EAIScreenLd_getLdMap(IntPtr handle, ref ST_Point ptArr, ref int nPtArr);

    [DllImport("ydlidar_driver.dll", EntryPoint = "EAIScreenLd_getDeviceInfo", CharSet = CharSet.Ansi,
        ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]

    extern static int EAIScreenLd_getDeviceInfo(IntPtr hhandle, byte[] seviceInfo);

    [DllImport("ydlidar_driver.dll", EntryPoint = "EAIScreenLd_getDeviceCom", CharSet = CharSet.Ansi,
        ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]

    extern static int EAIScreenLd_getDeviceCom(IntPtr hhandle, byte[] seviceCom);

    [DllImport("ydlidar_driver.dll", EntryPoint = "EAIScreenLd_destory", CharSet = CharSet.Ansi,
        ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]

    extern static int EAIScreenLd_destory(ref IntPtr hhandle);

    ////////////////////////////////////////////////////////////////////////////////////////////默认
    //                     G4雷达零点
    //                      |
    //                      |______

    //|-------960----------|////                ------
    //                //雷达安装位置//               |
    //                                             | 260
    //|----------------1920----------------------| |
    ////////////////////////////////////////////----
    // (0,0)                                  //  |
    //                                        //  |
    //                                        //  |
    //                                        //  |
    //                                        // 1080
    //                屏幕                    //  |
    //                                        //  |
    //                                        //  |
    //                                        //  |
    //                                        //  |
    ////////////////////////////////////////////-----

    string ulrSetting;

    public Text text;
    public Transform content;
    Vector3 pos = Vector3.zero;
    IntPtr handle;

    public Slider xAdjust;
    public Slider yAdjust;
    private float xOffset = 0;
    private float yOffset = 0;

    public GameObject pointPref;

    public GamePanel_Test gamePanel;

    void Start()
    {
       // StartCoroutine(InitLider());
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Keypad1))
        {
            StartCoroutine(InitLider());
        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            StopLider();
        }
    }

    /// 
    /// 雷达初始化
    /// 
    WaitForSeconds ws = new WaitForSeconds(0.15f);
    private IEnumerator InitLider()
    {
        text.text = "开始记录\n";

        ulrSetting = "setting.ini";

        //设备初始化
        handle = EAIScreenLd_init(ulrSetting.ToCharArray());

        //Debug.Log("初始化雷达设备:" + handle);
        if (null == handle)
        {
            Debug.Log("lidar init error !!! \n");
            yield return null;
        }

        //Debug.Log(handle);

        ///获取设备序列号
        byte[] seviceInfo = new byte[16];

        if (1 == EAIScreenLd_getDeviceInfo(handle, seviceInfo))
        {
            Debug.Log("\n\n\nSerial1111 : ");
            for (int i = 0; i < seviceInfo.Length; i++)
            {
                //seviceInfo[i] = '2';
                Debug.Log(seviceInfo[i]);
            }
            Debug.Log("\n\n\n");
        }

        text.text += "设备序列号: "+ EAIScreenLd_getDeviceInfo(handle, seviceInfo) + "\n";

        ///获取设备端口号
        byte[] seviceCom = new byte[16];

        if (1 == EAIScreenLd_getDeviceCom(handle, seviceCom))
        {
            Debug.Log("\n\n\n");
        }

        text.text += "设备端口号: " + EAIScreenLd_getDeviceCom(handle, seviceCom) + "\n";

        ST_Point[] ptArr = new ST_Point[2048];
        int nPtArr = 0;
        int frame = 0;

        while (true)
        {
            ClearFrameData();

            frame++;

            ////获取雷达数据
            nPtArr = 2048;

            if (1 != EAIScreenLd_getLdMap(handle, ref ptArr[0], ref nPtArr))
            {
                Debug.Log("get lidar map error !!!+ +++" + EAIScreenLd_getLdMap(handle, ref ptArr[0], ref nPtArr));
                break;
            }
                      
            text.text += ("雷达1 frame:  " + frame + "              " + "nPtArr:" + nPtArr + "\n");

            for (int i = 0; i < nPtArr; i++)
            {
                pos.x = ptArr[i].y /** 0.05f*/ + xOffset;
                pos.y = ptArr[i].x /** 0.1f*/ + yOffset;

                text.text += ("  x: " + pos.x.ToString("f1") + "    y: " + pos.y.ToString("f1") + "\n");

                // //标记点位
                ObjectPool.GetInstance().GetObj(PrefabType.Point.ToString(), content, pos);

                if (gamePanel)
                {
                    gamePanel.JudgeArea(pos);
                }
               
            }

            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;

            Debug.Log("\n\n\n");
        }
       
    }

    void OnApplicationQuit()
    {
        StopLider();
    }

    /// 
    /// 雷达停止
    /// 
    private void StopLider()
    {
        EAIScreenLd_destory(ref handle);//退出
    }

    /// 
    /// 清除上一次的数据
    /// 
    private void ClearFrameData()
    {
        int childCount = content.childCount;
        if (childCount > 0)
        {
            for (int i = 0; i < content.childCount; i++)
            {
                ObjectPool.GetInstance().RecycleObj(content.GetChild(i).gameObject);
              
            }
        }

        text.text = "开始记录\n";
    }

    public void OnXSliderValueChanged()
    {
        xOffset = xAdjust.value * 1080;
    }

    public void OnYSliderValueChanged()
    {
        yOffset = yAdjust.value * 1920;
    }
}

 

 

你可能感兴趣的:(Unity,G4雷达,雷达互动)