Esempio n. 1
0
        private async void idButton_Click(object sender, EventArgs e)
        {
            try
            {
                Task t = Task.Factory.StartNew(() => TcpserverEx.net_port_initial());

                await t;

                if (t.IsFaulted)
                {
                    debugBox.AppendText("No net port initial\n");
                }
                if (t.IsCompleted)
                {
                    debugBox.AppendText("Net port initial completed\n");
                }
                if (t.IsCanceled)
                {
                    debugBox.AppendText("Cancelled");
                }
                if (t.Exception != null)
                {
                    throw t.Exception;
                }
            }
            catch (Exception ex)
            {
                debugBox.AppendText(ex.Message);
            }

            Thread.Sleep(1000);

            bool found = false;

            try
            {
                for (int i = 0; i < 256; i++)
                {
                    if (TcpserverEx.card_number_connect(i) == 1)
                    {
                        zArmId = i;
                        found  = true;
                        debugBox.AppendText(string.Format("Zarm id: {0}\n", i));
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                debugBox.AppendText(ex.Message);
            }

            if (!found)
            {
                debugBox.AppendText("No arm found\n");
            }
        }
Esempio n. 2
0
        public IReadOnlyList <int> GetConnectedZArms()
        {
            List <int> ids = new List <int>();

            for (int i = 0; i < 256; i++)
            {
                if (TcpserverEx.card_number_connect(i) == 1)
                {
                    ids.Add(i);
                }
            }

            return(ids);
        }
Esempio n. 3
0
        public static async Task <int> FindZArmId()
        {
            CancellationTokenSource tcs = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            await Task.Factory.StartNew(() => TcpserverEx.net_port_initial(), tcs.Token);

            await Task.Delay(1200);

            return(await Task.Factory.StartNew <int>(() =>
            {
                for (int i = 0; i < 256; i++)
                {
                    if (TcpserverEx.card_number_connect(i) == 1)
                    {
                        return i;
                    }
                }
                return -1;
            },
                                                     tcs.Token));
        }
Esempio n. 4
0
        private async void connectButton_Click(object sender, EventArgs e)
        {
            try
            {
                Task task = Task.Factory.StartNew(() => TcpserverEx.net_port_initial());

                await task;

                if (task.IsFaulted)
                {
                    debugTextbox.AppendText("No net port initial\r\n");
                }
                if (task.IsCanceled)
                {
                    debugTextbox.AppendText("Cancelled\r\n");
                }
                if (task.IsCompleted)
                {
                    debugTextbox.AppendText("Net port initial completed\r\n");
                }
                if (task.Exception != null)
                {
                    throw task.Exception;
                }
            }
            catch (Exception ex)
            {
                debugTextbox.AppendText(ex.Message);
                return;
            }

            // Wait for net_port_initial to complete
            await Task.Delay(1200);

            debugTextbox.AppendText("Finding Z-Arm. \r\n");

            bool found = false;

            try
            {
                for (int i = 0; i < 256; i++)
                {
                    if (TcpserverEx.card_number_connect(i) == 1)
                    {
                        zArmId = i;
                        found  = true;
                        debugTextbox.AppendText($"Id: {i}\r\n");
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                debugTextbox.AppendText(ex.Message);
                return;
            }

            if (!found)
            {
                debugTextbox.AppendText("No arm found. Retry.\r\n");
                return;
            }

            // Connect to the Z-Arm
            robot = TcpserverEx.get_robot(zArmId);

            int  retryCount  = 0;
            bool isConnected = false;

            while (!isConnected)
            {
                isConnected = await Task.Factory.StartNew(() => robot.is_connected());

                debugTextbox.AppendText($"Attempting to connect to Z-Arm {zArmId}: {isConnected}\r\n");
                if (++retryCount > 3)
                {
                    break;
                }
                await Task.Delay(1000);
            }

            if (retryCount >= 3)
            {
                debugTextbox.AppendText($"Could not connect. Retry.\r\n");
                return;
            }

            int ret = -1;

            retryCount = 0;
            while (ret != 1)
            {
                ret = await Task.Factory.StartNew(() => robot.initial(generation, verticalReach));

                debugTextbox.AppendText($"Initializing: {ret} = {initialDebugMap[ret]}\r\n");
                if (++retryCount > 3)
                {
                    break;
                }
                await Task.Delay(1000);
            }

            if (ret != 1)
            {
                debugTextbox.AppendText($"Could not initialize Z-Arm Id {zArmId}, return value: {ret}\r\n");
                return;
            }
            else
            {
                robot.unlock_position();
                robot.set_arm_length(armLength, armLength);
                robot.set_catch_or_release_accuracy(0.5f);
                debugTextbox.AppendText($"Z Arm Id {zArmId} Initialized!\r\n");

                SetManualControls(true);
            }
        }