public static string GetRadarMessage(RadarMessage meg)
        {
            string ans = "";

            ans = JsonConvert.SerializeObject(meg, Formatting.Indented, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            });


            ////去除字符中间的空隙
            ans = ans.Replace("\n", "");
            ans = ans.Replace("\r", "");
            ans = ans.Replace("\t", "");
            return(ans);
        }
Exemplo n.º 2
0
        private IEnumerator UpdateRadar()
        {
            // Set Global Buffers
            computeShader.SetMatrix("inv_CameraMatrix", radarCameras[0].projectionMatrix.inverse);
            computeShader.SetInt("ImageWidthRes", WidthRes);
            computeShader.SetInt("ImageHeightRes", HeightRes);
            computeShader.SetInt("NrOfImages", NrOfCameras);
            computeShader.SetInt("radarResolution", radarSpokeResolution);
            computeShader.SetInt("radarSweepResolution", radarSweepResolution);
            computeShader.SetFloat("MaxDist", MaxDistance);
            computeShader.SetFloat("MinDist", MinDistance);
            computeShader.SetFloat("AntennaGainDbi", AntennaGainDbi);
            computeShader.SetFloat("PowerW", PowerW);
            computeShader.SetFloat("RadarFrequencyGhz", RadarFrequencyGhz);
            computeShader.SetFloat("SpokeAngle", SpokeAngle);

            // Radar Spokes buffers
            int RadarSpokesHandle = computeShader.FindKernel("RadarSpokes");

            computeShader.SetTexture(RadarSpokesHandle, "RadarCharacteristics", RadiationPattern);
            int           dataSize             = sizeof(int);
            ComputeBuffer RadarSpokesIntBuffer = new ComputeBuffer(radarSpokeResolution * radarSweepResolution, dataSize);

            RadarSpokesInt = new NativeArray <int>(radarSpokeResolution * radarSweepResolution, Allocator.Temp, NativeArrayOptions.ClearMemory);
            RadarSpokesIntBuffer.SetData(RadarSpokesInt);
            computeShader.SetBuffer(RadarSpokesHandle, "RadarSpokesInt", RadarSpokesIntBuffer);
            for (int i = 0; i < radarCameras.Length; i++)
            {
                Quaternion angle = Quaternion.Euler(0, i * 360.0f / NrOfCameras, 0);
                Matrix4x4  m     = Matrix4x4.Rotate(angle);

                computeShader.SetTexture(RadarSpokesHandle, "depthImage" + i.ToString(), radarCameras[i].targetTexture);
                computeShader.SetMatrix("CameraRotationMatrix" + i.ToString(), m);
            }

            // Radar range Convolution buffers
            int RadarRangeCovolutionHandle = computeShader.FindKernel("RadarRangeCovolution");

            computeShader.SetBuffer(RadarRangeCovolutionHandle, "RadarSpokesInt", RadarSpokesIntBuffer);
            dataSize = sizeof(float);
            ComputeBuffer RadarSpokesFloatBuffer = new ComputeBuffer(radarSpokeResolution * radarSweepResolution, dataSize);

            RadarSpokesFloat = new NativeArray <float>(radarSpokeResolution * radarSweepResolution, Allocator.Temp, NativeArrayOptions.ClearMemory);
            RadarSpokesFloatBuffer.SetData(RadarSpokesFloat);
            computeShader.SetBuffer(RadarRangeCovolutionHandle, "RadarSpokesFloat", RadarSpokesFloatBuffer);

            // Radar spoke max buffers
            int                 RadarSpokeMax   = computeShader.FindKernel("FindRadarSpokeMax");
            ComputeBuffer       MaxSpokesBuffer = new ComputeBuffer(radarSweepResolution, sizeof(float));
            NativeArray <float> MaxSpokes       = new NativeArray <float>(radarSweepResolution, Allocator.Temp, NativeArrayOptions.ClearMemory);

            MaxSpokesBuffer.SetData(MaxSpokes);
            computeShader.SetBuffer(RadarSpokeMax, "MaxSpokes", MaxSpokesBuffer);
            computeShader.SetBuffer(RadarSpokeMax, "RadarSpokesFloat", RadarSpokesFloatBuffer);

            // Radar plot image buffers
            int RadarPlotImageHandle = computeShader.FindKernel("CreateRadarPlotImage");

            if (RadarPlotExternalImage != null)
            {
                dataSize       = 24;
                RadarPlotImage = new RenderTexture(RadarPlotExternalImage.width, RadarPlotExternalImage.height, dataSize);
                RadarPlotImage.enableRandomWrite = true;
                RadarPlotImage.Create();
                computeShader.SetTexture(RadarPlotImageHandle, "plotImage", RadarPlotImage);
                computeShader.SetBuffer(RadarPlotImageHandle, "RadarSpokesFloat", RadarSpokesFloatBuffer);
                computeShader.SetBuffer(RadarPlotImageHandle, "MaxSpokes", MaxSpokesBuffer);
            }

            // Radar spoke image buffers
            int RadarSpokeImageHandle = computeShader.FindKernel("CreateRadarSpokeImage");

            if (RadarSpokeExternalImage != null)
            {
                dataSize        = 24;
                RadarSpokeImage = new RenderTexture(RadarSpokeExternalImage.width, RadarSpokeExternalImage.height, dataSize);
                RadarSpokeImage.enableRandomWrite = true;
                RadarSpokeImage.Create();
                computeShader.SetTexture(RadarSpokeImageHandle, "spokeImage", RadarSpokeImage);
                computeShader.SetBuffer(RadarSpokeImageHandle, "RadarSpokesFloat", RadarSpokesFloatBuffer);
                computeShader.SetBuffer(RadarSpokeImageHandle, "MaxSpokes", MaxSpokesBuffer);
            }

            // Radar Data
            string radarDataKernel     = "RadarDataKernel";
            string timeInSecondsBuffer = "timeInSeconds";
            string azimuthBuffer       = "azimuth";
            string radarDataBuffer     = "spokeData";

            UnifiedArray <byte> radarData = new UnifiedArray <byte>(radarSweepResolution * radarSpokeResolution / 4, sizeof(uint), radarDataBuffer);

            radarData.SetBuffer(computeShader, radarDataKernel);

            UnifiedArray <float> azimuth = new UnifiedArray <float>(radarSweepResolution, sizeof(float), azimuthBuffer);;

            azimuth.SetBuffer(computeShader, radarDataKernel);

            UnifiedArray <double> timeInSeconds = new UnifiedArray <double>(radarSweepResolution, sizeof(double), timeInSecondsBuffer);;

            timeInSeconds.SetBuffer(computeShader, radarDataKernel);

            int radarDataKernelHandle = computeShader.FindKernel(radarDataKernel);

            computeShader.SetBuffer(radarDataKernelHandle, "RadarSpokesFloat", RadarSpokesFloatBuffer);
            computeShader.SetBuffer(radarDataKernelHandle, "MaxSpokes", MaxSpokesBuffer);

            ComputeBuffer timeArray = new ComputeBuffer(2, sizeof(double));

            double[] timeArr = new double[2] {
                OSPtime, nextOSPtime
            };
            timeArray.SetData(timeArr);
            computeShader.SetBuffer(radarDataKernelHandle, "timeArray", timeArray);

            // Radar clear buffers
            int ClearKernelHandle = computeShader.FindKernel("ClearRadar");

            computeShader.SetBuffer(ClearKernelHandle, "RadarSpokesInt", RadarSpokesIntBuffer);

            // Fetch data from GPU
            float tempSens       = -1;
            float tempSpokeAngle = -1;

            while (true)
            {
                // Check change in Inspector Variables
                if (Sensitivity != tempSens)
                {
                    computeShader.SetFloat("Sensitivity", Sensitivity);
                    tempSens = Sensitivity;
                }
                if (SpokeAngle != tempSpokeAngle)
                {
                    computeShader.SetFloat("SpokeAngle", SpokeAngle);
                    tempSpokeAngle = SpokeAngle;
                }

                timeArr[0] = OSPtime;
                timeArr[1] = nextOSPtime;
                timeArray.SetData(timeArr);
                computeShader.SetBuffer(radarDataKernelHandle, "timeArray", timeArray);

                // Create and Radar Data
                computeShader.Dispatch(RadarSpokesHandle, (int)Mathf.Ceil(NrOfCameras * WidthRes / 32), (int)Mathf.Ceil(HeightRes / 32), 1);
                computeShader.Dispatch(RadarRangeCovolutionHandle, (int)Mathf.Ceil(radarSweepResolution / 512), 1, 1);
                computeShader.Dispatch(RadarSpokeMax, (int)Mathf.Ceil(radarSweepResolution / 512), 1, 1);
                computeShader.Dispatch(radarDataKernelHandle, (int)Mathf.Ceil(radarSweepResolution / 512), 1, 1);

                // Recieve and send radar data
                var request2 = AsyncGPUReadback.Request(radarData.buffer);
                yield return(new WaitUntil(() => request2.done));

                radarData.AsynchUpdate(request2);

                var request3 = AsyncGPUReadback.Request(azimuth.buffer);
                yield return(new WaitUntil(() => request3.done));

                azimuth.AsynchUpdate(request3);

                var request4 = AsyncGPUReadback.Request(timeInSeconds.buffer);
                yield return(new WaitUntil(() => request4.done));

                timeInSeconds.AsynchUpdate(request4);

                message = new RadarMessage(radarData.array, timeInSeconds.array, azimuth.array, (uint)MinDistance, (uint)MaxDistance, (uint)radarSpokeResolution);
                gate    = true;

                // Debugging Images
                if (RadarPlotExternalImage != null)
                {
                    computeShader.Dispatch(RadarPlotImageHandle, (int)Mathf.Ceil(RadarPlotImage.width / 32), (int)Mathf.Ceil(RadarPlotImage.height / 32), 1);
                }

                if (RadarSpokeExternalImage != null)
                {
                    computeShader.Dispatch(RadarSpokeImageHandle, (int)Mathf.Ceil(RadarSpokeImage.width / 32), (int)Mathf.Ceil(RadarSpokeImage.height / 32), 1);
                }

                // Clear GPU memory
                computeShader.Dispatch(ClearKernelHandle, (int)Mathf.Ceil((radarSpokeResolution * radarSweepResolution) / 1024), 1, 1);
            }
        }