Exemplo n.º 1
0
        private void PrepareSimulation()
        {
            RoboDK RDK = new RoboDK("127.0.0.1");

            RoboDK.Item frame_pallet = RDK.GetItem("PalletA", RoboDK.ITEM_TYPE_FRAME);

            // 获取参数
            string SizeBox    = RDK.GetParam("SizeBox");                                                // 150,200,150  箱子大小
            string SizePallet = RDK.GetParam("SizePallet");                                             // 5,3,3  横纵列

            float[] SizeBoxXyz    = SizeBox.Split(',').Select((m) => float.Parse(m.Trim())).ToArray();  // [150,200,150]
            int[]   SizePalletXyz = SizePallet.Split(',').Select((m) => int.Parse(m.Trim())).ToArray(); // [5,3,3]

            float SizeBoxZ = SizeBoxXyz[2];                                                             // 箱子的高度很重要,用来计算位置和碰撞检测的


            RDK.Render(false);

            CleanUp(RDK.GetItemList(RoboDK.ITEM_TYPE_OBJECT), "Part ");
            CleanUp(RDK.GetItemList(RoboDK.ITEM_TYPE_TOOL), "TCP ");

            RDK.GetItem("box100mm").Copy();
            Random random = new Random();

            for (int i = 0; i < SizePalletXyz[0]; i++)
            {
                for (int j = 0; j < SizePalletXyz[1]; j++)
                {
                    for (int k = 0; k < SizePalletXyz[2]; k++)
                    {
                        // 计算位置
                        float location_x = SizeBoxXyz[0] / 2 + i * SizeBoxXyz[0];
                        float location_y = SizeBoxXyz[1] / 2 + j * SizeBoxXyz[1];
                        float location_z = SizeBoxXyz[2] / 2 + k * SizeBoxXyz[2];

                        // 复制新的对象
                        RoboDK.Item newPart = frame_pallet.Paste();
                        newPart.Scale(new double[] { SizeBoxXyz[0] / 100, SizeBoxXyz[1] / 100, SizeBoxXyz[2] / 100 });
                        newPart.SetName("Part " + i.ToString() + j.ToString() + k.ToString());
                        newPart.SetPose(Matrix.Transl(location_x, location_y, location_z));
                        newPart.SetVisible(true, 0);
                        newPart.SetColor(new double[] { random.NextDouble(), random.NextDouble(), random.NextDouble(), 1 });
                    }
                }
            }


            RDK.SetParam("SENSOR", "0");
            RDK.Render(true);
            RDK.Disconnect();

            PartsClear();
        }
Exemplo n.º 2
0
        public void Test_ParallelRoboDKConnections()
        {
            var stopAsyncTask = false;
            var rdk           = new RoboDK
            {
                RoboDKServerStartPort = 10000,
                Logfile = Path.Combine(Directory.GetCurrentDirectory(), "RoboDk.log"),
                DefaultSocketTimeoutMilliseconds = 20 * 1000
            };

            rdk.Connect();
            rdk.Render(false);
            rdk.Command("AutoRenderDelay", 50);
            rdk.Command("AutoRenderDelayMax", 300);
            rdk.DefaultSocketTimeoutMilliseconds = 10 * 1000;

            List <IItem> AddStaticParts()
            {
                var parts = new List <IItem>();
                var cwd   = Directory.GetCurrentDirectory();

                parts.Add(rdk.AddFile(Path.Combine(cwd, "TableOut.sld")));
                parts.Add(rdk.AddFile(Path.Combine(cwd, "robot.robot")));
                return(parts);
            }

            List <IItem> AddDynamicParts()
            {
                var parts = new List <IItem>();
                var cwd   = Directory.GetCurrentDirectory();

                for (var n = 0; n < 10; n++)
                {
                    parts.Add(rdk.AddFile(Path.Combine(cwd, "Phone Case Box.sld")));
                    parts.Add(rdk.AddFile(Path.Combine(cwd, "Box.sld")));
                    parts.Add(rdk.AddFile(Path.Combine(cwd, "Phone Case Done.sld")));
                }

                return(parts);
            }

            // Task which opens a temporary new connection
            void DoTemporaryConnectionCalls(IReadOnlyCollection <IItem> staticParts)
            {
                // ReSharper disable once AccessToModifiedClosure
                while (!stopAsyncTask)
                {
                    foreach (var staticPart in staticParts)
                    {
                        Thread.Sleep(1);
                        using (var roboDkLink = new RoboDK.RoboDKLink(staticPart.RDK()))
                        {
                            var clonedItem = staticPart.Clone(roboDkLink.RoboDK);
                            clonedItem.Pose();
                        }
                    }
                }
            }

            var p    = AddStaticParts();
            var task = new Task(() => DoTemporaryConnectionCalls(p));

            task.Start();

            try
            {
                for (var i = 0; i < 20; i++)
                {
                    var dynamicParts = AddDynamicParts();
                    rdk.Command("CollisionMap", "Off");
                    rdk.SetCollisionActive(CollisionCheckOptions.CollisionCheckOff);
                    rdk.Delete(dynamicParts);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                stopAsyncTask = true;
            }

            try
            {
                task.Wait();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                rdk.CloseRoboDK();
            }
        }