Beispiel #1
0
 public void ControlThreadStarter(Action MethodName, string name)
 {
     if (!isAlarm)
     {
         if (!isAbort)
         {
             if (ControlSemaphore.WaitOne(0))
             {
                 WriteMessageQueue(name + " started");
                 MachineControlThread = new Thread(new ThreadStart(MethodName));
                 MachineControlThread.Start();
             }
             else
             {
                 WriteMessageQueue("Thread already working");
             }
         }
         else
         {
             WriteMessageQueue("Turn off Abort");
         }
     }
     else
     {
         WriteMessageQueue("THE ALARM IS ON TURN OFF THE ALARM FIRST");
     }
 }
Beispiel #2
0
        internal void MysteryBoxConnect()
        {
            int    ByteCount;
            String RecieveData;
            string SendData;

            Byte[] BytesOut = new Byte[256];
            Byte[] BytesIn  = new Byte[256];

            try
            {
                SenClient = new TcpClient();
                SenClient.Connect(IPAddress.Parse("10.10.6.100"), 23);
                SenStream = SenClient.GetStream();
                SendData  = "A";
                SendData += "X";

                //Clear BytesOut
                //Translate the passed message
                BytesOut = Encoding.ASCII.GetBytes(SendData);
                //Send the message to the sensor client

                SenStream.Write(BytesOut, 0, BytesOut.Length);
                //Machine must cross over the reference mark on the vertical motor to start
                //the connection
                //Read the incoming data
                ByteCount   = SenStream.Read(BytesIn, 0, BytesIn.Length);
                RecieveData = Encoding.ASCII.GetString(BytesIn, 0, ByteCount);
                isSenCon    = true;
                DataController.Controller.MotorController.StopVer();
            }
            catch (Exception ex)
            {
                DataController.Controller.MotorController.StopVer();
                MessageBox.Show(ex.Message.ToString(), "Mystery Box Connect Error");
            }
            WriteMessageQueue("MysteryBoxConnect Completed");
            try { ControlSemaphore.Release(1); }
            catch (System.Threading.SemaphoreFullException ex)
            {
                MessageBox.Show("error releasing ControlSemaphore 4 " + ex.Message.ToString());
            }
        }
        internal double AutoZero(double returnHeight) //returnHeight represents the height above the found surface the machine returns to.
        {
            double height  = 0;
            double epsilon = 0.0006;


            MessageBox.Show("AutoAUTOzero:\nMake sure tool positioned over set piece\n");
            double autoautozerocurrent, autoautozeroprevious;
            bool   isAutoAutoZeroZeroed;

            //*********Zero the axial force reading************
            btnZzero_Click(new object(), new EventArgs());
            if (MaxZForce < 800)
            {
                MaxZForce = 800;
            }
            Thread.Sleep(1000);
            double azforce;

            azforce = (double)nmPinchForce.Value; //from the spot welding box added by chase on 4/9/12

            if (ZForce > 100)                     // This is a test for initial force
            {
                MessageBox.Show("Force Already Exceeds Turn-On Force");
            }
            else
            {
                WriteMessageQueue("The AutoAutoZero Process Will Now Begin\nNOTE:Currently using .0006 as tolerance");
                //Alert GUI of first upward motion
                //isAutoisfirstup = true;

                //initiate table raising
                VerPort.Write("MC H+ A10 V.3 G\r");

                while (ZForce < azforce) //changed from 100 to azforce by Chase 4/9/2012
                {
                    Thread.Sleep(0);     //changed to sleep 0 by Jay 6/10/2015
                }
                //Stop the motor
                StopVer();

                Thread.Sleep(500);

                ////Alert GUI of first downward motion
                //isAutoisfirstdown = true;

                ////initiate table lowering
                VerPort.Write("MN A10 V2 D-3000 G\r");

                ////Sleep for one second
                Thread.Sleep(1000);

                ////Alert GUI of final upward motion
                //isAutoissecondup = true;
                autoautozerocurrent  = -2;
                autoautozeroprevious = -1;
                bool LoopRepeat = true;
                isAutoAutoZeroZeroed = false;
                while (!isAutoAutoZeroZeroed)
                {
                    autoautozeroprevious = autoautozerocurrent;
                    ////Do final upward in a loop
                    LoopRepeat = true;

                    while (LoopRepeat)
                    {
                        //Wait for turn off signal
                        if (ZForce > azforce) //changed from ZForce changed by Chase on 11/21/11 //changed from 100 to azforce by Chase 4/9/2012
                        {
                            LoopRepeat = false;
                        }
                        else
                        {
                            VerPort.Write("MN A1 V1 D100 G\r");
                        }
                        Thread.Sleep(1000);
                    }

                    ////Sleep for stability
                    Thread.Sleep(500);
                    autoautozerocurrent = VerLoc;
                    if (Math.Abs(autoautozerocurrent - autoautozeroprevious) < .0006)
                    {
                        isAutoAutoZeroZeroed = true;
                    }
                    else
                    {
                        //move table down
                        VerPort.Write("MN A10 V2 D-3000 G\r");

                        ////Sleep for one second
                        Thread.Sleep(1000);
                    }
                    WriteMessageQueue(autoautozerocurrent.ToString("F4"));
                }
                ////Set weld height
                isSetVerWeld = true;
                VerWeld      = VerLoc;


                ////Sleep for assurance of GUI update
                Thread.Sleep(1000);
                //move table down
                VerPort.Write("MN A10 V2 D-3000 G\r");
                Thread.Sleep(1000);
                //Clear out port
                VerPort.ReadExisting(); //this was edited out

                //Query the motor and establish RS-232 control
                VerPort.Write("E ON 1R\r"); //this was edited out
                Thread.Sleep(300);          //this was edited out

                //Turn off limits
                VerPort.Write("1LD3\r"); //this was edited out
                Thread.Sleep(30);        //this was edited out

                //Clear out port and place in holding string
                VerPort.ReadExisting(); //this was edited out
                double tempver = VerLoc;
                //initiate table raising
                VerPort.Write("MC H- A10 V2 G\r");
                while (VerLoc > (tempver - .125))
                {
                    Thread.Sleep(30);
                }
                StopVer();
                UpdateLimitValues();
                WriteMessageQueue("AutoAutoZero Completed");
                //Always release the control semaphore before finishing a Machine Control Task
                try { ControlSemaphore.Release(1); }
                catch (System.Threading.SemaphoreFullException ex)
                {
                    MessageBox.Show("error releasing ControlSemaphore 6 " + ex.Message.ToString());
                }
            }

            //return height;
        }
        public void SimulinkReciveLoop()
        {
            //need to add code to set motor parameters to be snapper in simulink control but less jumpy normally
            bool isSimDone = false;

            isSimControl = true;
            Byte[] RecieveBytes = new Byte[256];
            double tempvar;

            StartUDP();
            WriteMessageQueue("waiting for communication from simulink..");
            Thread.Sleep(500);
            while (SimulinkReviceUDP.Available > 0)//clear udp buffer, want to recieve latest packet make sure simulink is running
            {
                RecieveBytes = SimulinkReviceUDP.Receive(ref IPRecivefromSimulink);
            }
            RecieveBytes = SimulinkReviceUDP.Receive(ref IPRecivefromSimulink);
            WriteMessageQueue("Giving control to simulink");
            RecieveBytes = SimulinkReviceUDP.Receive(ref IPRecivefromSimulink);
            VerSpeed[0]  = BitConverter.ToDouble(RecieveBytes, 0);
            SpiSpeed[0]  = BitConverter.ToDouble(RecieveBytes, 8);
            tempvar      = BitConverter.ToDouble(RecieveBytes, 16);

            #region Initialize Traverse Lateral Motor for Analog Control
            AnalogControl();
            #endregion
            #region Initialize Spindle Speed
            isSpiSpeedCW = trueifpositive(SpiSpeed[0]);
            if (isSpiSpeedCW)
            {
                StartSpiCW();
                isSpiCW = true;
            }
            else
            {
                StartSpiCCW();
                isSpiCW = false;
            }
            #endregion
            VerSpeed[0] = -99.9;
            SpiSpeed[0] = -99.9;
            //Why is analog control started twice? - adam
            AnalogControl();
            while (!isAbort && !isSimDone && !isAlarm) // checking to see that system is good move this up a few levels
            {
                //Try to recieve data
                if (SimulinkReviceUDP.Available > 0)
                {
                    RecieveBytes = SimulinkReviceUDP.Receive(ref IPRecivefromSimulink);
                }
                //if no data has been sent assume welding computer has crashed
                else
                {
                    WriteMessageQueue("Data Not recieved from the simulink real time computer");
                    //Break out of while loop then have rest of thread stop motors
                    break;
                }
                RecieveBytes = SimulinkReviceUDP.Receive(ref IPRecivefromSimulink);
                VerSpeed[1]  = VerSpeed[0];
                VerSpeed[0]  = BitConverter.ToDouble(RecieveBytes, 0);
                SpiSpeed[1]  = SpiSpeed[0];
                SpiSpeed[0]  = BitConverter.ToDouble(RecieveBytes, 8);
                tempvar      = BitConverter.ToDouble(RecieveBytes, 16);
                isSimDone    = System.Convert.ToBoolean(tempvar);
                #region Update Vertical Speed
                if (VerSpeed[0] != VerSpeed[1])
                {
                    isVerDown         = trueifpositive(VerSpeed[0]);
                    VerSpeedMagnitude = Math.Abs(VerSpeed[0]);
                    if (VerSpeedMagnitude > VerSpeedLimit)
                    {
                        VerSpeedMagnitude = VerSpeedLimit;
                    }
                    if (VerSpeedMagnitude < VerSpeedMinimum)
                    {
                        VerSpeedMagnitude = VerSpeedMinimum;
                    }
                    VerMessage = "H";
                    if (isVerDown)
                    {
                        VerMessage += "+";
                    }
                    else
                    {
                        VerMessage += "-";
                    }

                    VerMessage += "A" + VerAccel.ToString("F2") + " V" + VerSpeedMagnitude.ToString("F5") + " G\r";
                    VerPort.Write(VerMessage);
                }
                #endregion
                #region Update Spindle Speed
                if (SpiSpeed[0] != SpiSpeed[1])
                {
                    isSpiSpeedCW      = trueifpositive(SpiSpeed[0]);
                    SpiSpeedMagnitude = Math.Abs(SpiSpeed[0]);
                    if (SpiSpeedMagnitude > 100)
                    {
                        isLubWanted = true;
                    }
                    else
                    {
                        isLubWanted = false;
                    }
                    //Limit Spindle speed
                    if (SpiSpeedMagnitude > SpiSpeedLimit)
                    {
                        SpiSpeedMagnitude = SpiSpeedLimit;
                    }
                    if (isSpiSpeedCW)
                    {
                        if (!isSpiCW)
                        {
                            StartSpiCW();
                            isSpiCW = true;
                        }
                        SpiMessage = "CW";
                    }
                    else
                    {
                        if (isSpiCW)
                        {
                            StartSpiCCW();
                            isSpiCW = false;
                        }
                        SpiMessage = "CCW";
                    }
                    ChangeSpiRef(SpiSpeedMagnitude);
                    WriteMessageQueue("Spi set to:" + SpiSpeedMagnitude.ToString("F0") + SpiMessage);
                }
                #endregion
                //clear udp buffer, want to recieve latest packet
                while (SimulinkReviceUDP.Available > 0)
                {
                    RecieveBytes = SimulinkReviceUDP.Receive(ref IPRecivefromSimulink);
                }
                Thread.Sleep(25);
            }
            StopAllMotors();
            isLubWanted = false;
            btnZzero_Click(new object(), new EventArgs());
            Program.Safetyfrm.UpdateLimitsSafe();
            WriteModbusQueue(3, 0x0300, 04, false); //give lateral motor master frequency control to rs-485
            WriteModbusQueue(3, 0x0301, 03, false); //give lateral motor Source of operation command  to rs-485
            WriteModbusQueue(3, 0x0705, 0, false);  //set lateral speed to zero
            WriteModbusQueue(3, 0x010D, 0, false);  //set Lateral motor direcction (Fwd/Rev) to be controled by rs-485

            WriteModbusQueue(2, 0x0300, 04, false); //give Traverse motor master frequency control to rs-485
            WriteModbusQueue(2, 0x0301, 03, false); //give Traverse motor Source of operation command  to rs-485
            WriteModbusQueue(2, 0x0705, 0, false);  //set Traverse speed to zero
            WriteModbusQueue(2, 0x010D, 0, false);  //set Traverse motor direcction (Fwd/Rev) to be controled by rs-485
            SoftStop = 1;
            Thread.Sleep(25);
            isSimControl = false;
            MatlabExecute("SaveData");//run script to save data
            WriteMessageQueue("SimulinkRecieve Completed");
            try { ControlSemaphore.Release(1); }
            catch (System.Threading.SemaphoreFullException ex)
            {
                MessageBox.Show("error releasing ControlSemaphore 3 " + ex.Message.ToString());
            }
        }