Unity 使用G4做雷达互动开发

下面是主要代码,基本上都是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();
        }
    }

    /// <summary>
    /// 雷达初始化
    /// </summary>
    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();
    }

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

    /// <summary>
    /// 清除上一次的数据
    /// </summary>
    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;
    }
}

 

 

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 24
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值