Ejemplo n.º 1
0
        public void TestWrite()
        {
            PLCMemory memory = new PLCMemory();
            {
                Assert.Equal(0, memory.Count);

                Dictionary <string, object> values = new Dictionary <string, object>();
                values["test1"] = true;
                memory.Write(values);

                Assert.Equal(1, memory.Count);
                Assert.Equal(true, memory["test1"]);
            }

            {
                Assert.Equal(1, memory.Count);

                Dictionary <string, object> values = new Dictionary <string, object>();
                values["test1"] = "true";
                values["test2"] = null;
                memory.Write(values);

                Assert.Equal(2, memory.Count);
                Assert.Equal("true", memory["test1"]);
                Assert.Equal(null, memory["test2"]);
            }
        }
Ejemplo n.º 2
0
        public void TestRead()
        {
            PLCMemory memory = new PLCMemory();
            {
                memory["test1"] = 10;
                Assert.Equal(1, memory.Count);

                Dictionary <string, object> data = memory.Read(new string[] { "test1", "test2" });
                Assert.NotNull(data);
                Assert.Equal(1, data.Count);
                Assert.Equal(10, data["test1"]);
            }

            {
                memory["test1"] = "10";
                memory["test2"] = int.MinValue;
                Assert.Equal(2, memory.Count);

                Dictionary <string, object> data = memory.Read(new string[] { "test1", "test2" });
                Assert.NotNull(data);
                Assert.Equal(2, data.Count);
                Assert.Equal("10", data["test1"]);
                Assert.Equal(int.MinValue, data["test2"]);
            }
        }
Ejemplo n.º 3
0
        public void TestStopImmediately()
        {
            var timeout         = TimeSpan.FromMilliseconds(100);
            var memory          = new PLCMemory();
            var customerLogic   = new PLCLogic(new PLCController(memory));
            var mujinController = new PLCController(memory);

            customerLogic.ClearAllSignals();

            // indicate we are ready
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRunningOrderCycle", false },
                { "isRobotMoving", false },
                { "isModeAuto", true },
                { "isSystemReady", true },
                { "isCycleReady", true },
            });
            customerLogic.WaitUntilOrderCycleReady(timeout);

            // start order cycle
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRunningOrderCycle", true },
                { "numLeftInOrder", 1 },
                { "numLeftInLocation1", 1 },
            });
            var status = customerLogic.StartOrderCycle("orderId", "orderPartType", 1, timeout);

            Assert.Equal(true, status.isRunningOrderCycle);
            Assert.Equal(1, status.numLeftInOrder);
            Assert.Equal(1, status.numLeftInLocation1);

            // stop immediately
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRobotMoving", false },
                { "isRunningOrderCycle", false },
                { "numLeftInOrder", 1 },
                { "numLeftInLocation1", 1 },
                { "orderCycleFinishCode", (int)PLCLogic.PLCOrderCycleFinishCode.FinishedStoppedImmediately },
            });
            customerLogic.StopImmediately();

            // controller might reset variables
            // mujinController.Set(new Dictionary<string, object>()
            // {
            //     { "numLeftInOrder", 0 },
            //     { "numLeftInLocation1", 0 },
            //     { "orderCycleFinishCode", 0 },
            // });

            status = customerLogic.GetOrderCycleStatus();
            Assert.Equal(false, status.isRunningOrderCycle);
            Assert.Equal(false, status.isRobotMoving);
            Assert.Equal(1, status.numLeftInOrder);
            Assert.Equal(1, status.numLeftInLocation1);
            Assert.Equal(PLCLogic.PLCOrderCycleFinishCode.FinishedStoppedImmediately, status.orderCycleFinishCode);
        }
Ejemplo n.º 4
0
        public void TestEmpty()
        {
            PLCMemory memory = new PLCMemory();
            {
                Assert.Equal(0, memory.Count);

                Dictionary <string, object> data = memory.Read(new string[] { "test1", "test2" });
                Assert.NotNull(data);
                Assert.Equal(0, data.Count);
            }
        }
Ejemplo n.º 5
0
        public void TestWaitTimeout(int maxHeartbeatIntervalMilliseconds, int timeoutMilliseconds)
        {
            var      memory = new PLCMemory();
            TimeSpan?maxHeartbeatInterval = null;
            TimeSpan?timeout = null;

            if (maxHeartbeatIntervalMilliseconds > 0)
            {
                maxHeartbeatInterval = TimeSpan.FromMilliseconds(maxHeartbeatIntervalMilliseconds);
            }
            if (timeoutMilliseconds > 0)
            {
                timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
            }
            var controller = new PLCController(memory, maxHeartbeatInterval);

            if (maxHeartbeatInterval.HasValue)
            {
                Assert.Equal(false, controller.IsConnected);
                if (timeout.HasValue)
                {
                    Assert.Throws <TimeoutException>(() =>
                    {
                        controller.WaitUntilConnected(timeout);
                    });
                }
            }
            else
            {
                Assert.Equal(true, controller.IsConnected);
                controller.WaitUntilConnected(timeout);
            }

            Assert.Throws <TimeoutException>(() =>
            {
                controller.WaitFor("someSignal", true, timeout);
            });

            Assert.Throws <TimeoutException>(() =>
            {
                controller.WaitUntil("someSignal", true, timeout);
            });
        }
Ejemplo n.º 6
0
        public void TestResetError()
        {
            var timeout         = TimeSpan.FromMilliseconds(100);
            var memory          = new PLCMemory();
            var customerLogic   = new PLCLogic(new PLCController(memory));
            var mujinController = new PLCController(memory);

            customerLogic.ClearAllSignals();

            // indicate we are ready
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRunningOrderCycle", false },
                { "isRobotMoving", false },
                { "isModeAuto", true },
                { "isSystemReady", true },
                { "isCycleReady", true },
            });
            customerLogic.WaitUntilOrderCycleReady(timeout);

            // start order cycle
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRunningOrderCycle", true },
                { "numLeftInOrder", 1 },
                { "numLeftInLocation1", 1 },
            });
            var status = customerLogic.StartOrderCycle("orderId", "orderPartType", 1, timeout);

            Assert.Equal(true, status.isRunningOrderCycle);
            Assert.Equal(1, status.numLeftInOrder);
            Assert.Equal(1, status.numLeftInLocation1);

            // set error
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRobotMoving", false },
                { "isRunningOrderCycle", false },
                { "numLeftInOrder", 1 },
                { "numLeftInLocation1", 1 },
                { "orderCycleFinishCode", (int)PLCLogic.PLCOrderCycleFinishCode.FinishedPlanningFailure },
                { "isError", true },
                { "errorcode", (int)PLCLogic.PLCErrorCode.PlanningError },
            });
            var e = Assert.Throws <PLCLogic.PLCError>(() =>
            {
                customerLogic.WaitUntilOrderCycleFinish();
            });

            Assert.Equal(PLCLogic.PLCErrorCode.PlanningError, e.errorcode);
            //Assert.Equal(0, e.detailedErrorCode);

            status = customerLogic.GetOrderCycleStatus();
            Assert.Equal(false, status.isRunningOrderCycle);
            Assert.Equal(false, status.isRobotMoving);
            Assert.Equal(1, status.numLeftInOrder);
            Assert.Equal(1, status.numLeftInLocation1);
            Assert.Equal(PLCLogic.PLCOrderCycleFinishCode.FinishedPlanningFailure, status.orderCycleFinishCode);

            // reset error
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isError", false },
                { "errorcode", 0 },
                { "orderCycleFinishCode", 0 },
            });
            customerLogic.ResetError();

            status = customerLogic.GetOrderCycleStatus();
            Assert.Equal(false, status.isRunningOrderCycle);
            Assert.Equal(false, status.isRobotMoving);
            Assert.Equal(1, status.numLeftInOrder);
            Assert.Equal(1, status.numLeftInLocation1);
            Assert.Equal(PLCLogic.PLCOrderCycleFinishCode.FinishedNotAvailable, status.orderCycleFinishCode);
        }
Ejemplo n.º 7
0
        public void TestOrderCycle(string orderId, string orderPartType, int orderNumber, int supplyNumber)
        {
            var timeout         = TimeSpan.FromMilliseconds(100);
            var memory          = new PLCMemory();
            var customerLogic   = new PLCLogic(new PLCController(memory));
            var mujinController = new PLCController(memory);

            customerLogic.ClearAllSignals();

            // indicate we are ready
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRunningOrderCycle", false },
                { "isRobotMoving", false },
                { "isModeAuto", true },
                { "isSystemReady", true },
                { "isCycleReady", true },
            });
            customerLogic.WaitUntilOrderCycleReady(timeout);

            // start order cycle
            mujinController.Set(new Dictionary <string, object>()
            {
                { "isRunningOrderCycle", true },
                { "numLeftInOrder", orderNumber },
                { "numLeftInLocation1", supplyNumber },
            });
            var status = customerLogic.StartOrderCycle(orderId, orderPartType, orderNumber, timeout);

            Assert.Equal(true, status.isRunningOrderCycle);
            Assert.Equal(orderNumber, status.numLeftInOrder);
            Assert.Equal(supplyNumber, status.numLeftInLocation1);

            // check order status
            while (true)
            {
                mujinController.Set(new Dictionary <string, object>()
                {
                    { "isRobotMoving", true },
                    { "numLeftInOrder", orderNumber },
                    { "numLeftInLocation1", supplyNumber },
                });
                status = customerLogic.GetOrderCycleStatus(timeout);
                Assert.Equal(true, status.isRobotMoving);
                Assert.Equal(orderNumber, status.numLeftInOrder);
                Assert.Equal(supplyNumber, status.numLeftInLocation1);

                if (orderNumber <= 0 || supplyNumber <= 0)
                {
                    break;
                }

                orderNumber--;
                supplyNumber--;
            }

            if (orderNumber == 0)
            {
                mujinController.Set(new Dictionary <string, object>()
                {
                    { "isRobotMoving", false },
                    { "isRunningOrderCycle", false },
                    { "numLeftInOrder", orderNumber },
                    { "numLeftInLocation1", supplyNumber },
                    { "orderCycleFinishCode", (int)PLCLogic.PLCOrderCycleFinishCode.FinishedOrderComplete },
                });
                status = customerLogic.WaitUntilOrderCycleFinish(timeout);
                Assert.Equal(false, status.isRunningOrderCycle);
                Assert.Equal(false, status.isRobotMoving);
                Assert.Equal(0, status.numLeftInOrder);
                Assert.Equal(supplyNumber, status.numLeftInLocation1);
                Assert.Equal(PLCLogic.PLCOrderCycleFinishCode.FinishedOrderComplete, status.orderCycleFinishCode);
            }
            else
            {
                mujinController.Set(new Dictionary <string, object>()
                {
                    { "isRobotMoving", false },
                    { "isRunningOrderCycle", false },
                    { "numLeftInOrder", orderNumber },
                    { "numLeftInLocation1", supplyNumber },
                    { "orderCycleFinishCode", (int)PLCLogic.PLCOrderCycleFinishCode.FinishedNoMoreTargets },
                });
                status = customerLogic.WaitUntilOrderCycleFinish(timeout);
                Assert.Equal(false, status.isRunningOrderCycle);
                Assert.Equal(false, status.isRobotMoving);
                Assert.Equal(orderNumber, status.numLeftInOrder);
                Assert.Equal(0, status.numLeftInLocation1);
                Assert.Equal(PLCLogic.PLCOrderCycleFinishCode.FinishedNoMoreTargets, status.orderCycleFinishCode);
            }
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            var           memory     = new PLCMemory();
            PLCController controller = new PLCController(memory, TimeSpan.FromSeconds(1.0));
            var           logic      = new PLCLogic(controller);
            var           server     = new PLCServer(memory, "tcp://*:5555");

            Console.WriteLine("Starting server to listen on {0} ...", server.Address);
            server.Start();

            Console.WriteLine("Waiting for controller connection ...");
            logic.WaitUntilConnected();
            Console.WriteLine("Controller connected.");

            try
            {
                if (controller.GetBoolean("isError"))
                {
                    Console.WriteLine("controller is in error 0x{0:X}, resetting", controller.Get("errorcode"));
                    logic.ResetError();
                }

                if (controller.GetBoolean("isRunningOrderCycle"))
                {
                    Console.WriteLine("previous cycle already running, so stop and wait");
                    logic.StopOrderCycle();
                }

                Console.WriteLine("Waiting for cycle ready...");
                logic.WaitUntilOrderCycleReady();

                Console.WriteLine("Starting order cycle ...");
                PLCLogic.PLCOrderCycleStatus status;
                if (true)
                {
                    // first work piece
                    controller.Set("orderRobotId", 1);
                    status = logic.StartOrderCycle("123", "work1", 1);
                }
                else
                {
                    // for the second work piece do
                    controller.Set("orderRobotId", 2);
                    status = logic.StartOrderCycle("123", "work2_b", 1);
                }
                Console.WriteLine("Order cycle started. numLeftInOrder = {0}, numLeftInLocation1 = {1}.", status.numLeftInOrder, status.numLeftInLocation1);

                while (true)
                {
                    status = logic.WaitForOrderCycleStatusChange();
                    if (!status.isRunningOrderCycle)
                    {
                        Console.WriteLine("Cycle finished. {0}", status.orderCycleFinishCode);
                        break;
                    }
                    Console.WriteLine("Cycle running. numLeftInOrder = {0}, numLeftInLocation1 = {1}.", status.numLeftInOrder, status.numLeftInLocation1);
                }
            }
            catch (PLCLogic.PLCError e)
            {
                Console.WriteLine("PLC Error. {0}", e.Message);
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey(true);

            server.Stop();
        }