MainMachine.cs 20.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceLibrary
{
    public partial class MainMachine : IRobot
    {
        public string Name { get; set; } = "XLC";
        private bool _canRunning = true;
        public bool canRunning
        {
            get { return _canRunning; }
            set
            {
                if (_canRunning != value) {
                    Msg.setlogones();
                }
                _canRunning = value;
            }
        }
        public bool isBusy { get; set; } = false;
        public bool isAlarm { get; set; } = false;
        public RunStatus runStatus { get; set; } = RunStatus.Stop;
        public Robot_Config Config { get; set; }
        public bool UserPause { get; set; } = false;


        MoveInfo ResetMoveInfo;
        /// <summary>
        /// 右侧移动信息
        /// </summary>
        public MoveInfo InMoveInfo;
        public MoveInfo OutMoveInfo;
        MoveInfo StoreMoveInfo;
        MoveInfo AIOTMoveInfo;

        public delegate void ProcessMsg(List<Msg> msg);
        public event ProcessMsg ProcessMsgEvent;

        //public delegate void ProcessMoveinfo(List<MoveInfo> moveinfoList);
        //public event ProcessMoveinfo ProcessMoveinfoEvent;

        internal AxisBean XAxis;
        internal AxisBean YAxis;
        internal AxisBean ZAxis;
        AxisBean InLift_Moto;
        AxisBean OutLift_Moto;

        LineRunMonitor LineIn;
        LineRunMonitor LineOut;

        LiftMonitor InSideLift;
        LiftMonitor OutSideLift;

        BoxTransport boxTransport;
        public bool boxTransportIsFree { get => boxTransport.IsComplateOrFree; }
        public event Action<string, StoreMoveType, bool> InOutEndProcessEvent;

        ServerCommunication ServerCM = new ServerCommunication();
        /// <summary>
        /// 开始运行的时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 是否在急停中
        /// </summary>
        public bool isInSuddenDown = false;



        public MainMachine(Robot_Config _config) {
            Config = _config;            
            
            InMoveInfo = new MoveInfo("A侧取料");
            InMoveInfo.SetStateDelegate(InState);            
            StoreMoveInfo = new MoveInfo("出入库");
            StoreMoveInfo.SetStateDelegate(StoreState);
            OutMoveInfo = new MoveInfo("B侧出料");
            OutMoveInfo.SetStateDelegate(OutState);
            ResetMoveInfo = new MoveInfo("重置");
            AIOTMoveInfo = new MoveInfo("出入库测试");
            //MoveInfo.List.Remove(AIOTMoveInfo);

            #region 初始化led
            AlarmLed = new Led(Config.DOList[IO_Type.Alarm_Led].GetIOAddr());
            StandbyLed = new Led(Config.DOList[IO_Type.Standby_Led].GetIOAddr());
            RunningLed = new Led(Config.DOList[IO_Type.Run_Led].GetIOAddr());
            NG_Led = new Led(Config.DOList[IO_Type.MaterialNG_Led].GetIOAddr());
            #endregion
            #region 初始化伺服轴
            XAxis = new AxisBean(Config.XAxis, Name);
            XAxis.interference += XAxis_interference;
            YAxis = new AxisBean(Config.YAxis, Name);
            YAxis.interference += YAxis_interference;
            ZAxis = new AxisBean(Config.ZAxis, Name);
            ZAxis.interference += ZAxis_interference;
            InLift_Moto = new AxisBean(Config.InLift_Moto, Name);
            OutLift_Moto = new AxisBean(Config.OutLift_Moto, Name);
            #endregion
            LineIn = new LineRunMonitor("入料滚筒", Config.DOList[IO_Type.InMoto_Run].GetIOAddr());
            LineOut = new LineRunMonitor("出料滚筒", Config.DOList[IO_Type.OutMoto_Run].GetIOAddr());

            InSideLift = new LiftMonitor(IO_Type.InLift_Up, IO_Type.InLift_Down, IO_Type.InLift_Break, InLift_Moto, Config.InLift_Moto_Speed);
            OutSideLift = new LiftMonitor(IO_Type.OutLift_Up, IO_Type.OutLift_Down, IO_Type.OutLift_Break, OutLift_Moto, Config.OutLift_Moto_Speed);

            boxTransport = new BoxTransport(Config, this);

            boxTransport.InOutEndProcessEvent += delegate (string posid, StoreMoveType storeMoveType, bool arg4)
            {
                InOutEndProcessEvent?.Invoke(posid, storeMoveType, arg4);
            };
            //BoxTransport_InOutEndProcessEvent;
            //ProcessMsgEvent += MainMachine_ProcessMsgEvent;

            AlarmBuzzer.SetOnOffAction(() =>{ IOMove(IO_Type.Alarm_Buzzer, IO_VALUE.HIGH); }, () => { IOMove(IO_Type.Alarm_Buzzer, IO_VALUE.LOW); });

            LedProcessInit();
        }
        private (bool, string) ZAxis_interference(int from, int to)
        {
            if (to==Config.Zaxis_P1)
                return (false, "");

            if (XAxis.GetAclPosition() > Config.Xaxis_P1 + Config.XAxis.CanErrorCountMax)
            {
                //if (to=Config.y)
            }
            else
            {
                if (GetYAxisLevel() == 0)
                {
                    return (true, "升降机构不在托盘进出位置");
                }
            }
            return (false, "");
        }

        private (bool, string) YAxis_interference(int from, int to)
        {
            if (XAxis.GetAclPosition() > Config.Xaxis_P1 + Config.XAxis.CanErrorCountMax)
            {
                //在出入库口

                //if (to=Config.y)
            }
            else { ///在料仓        
                if (!ZAxis.IsInPosition(Config.Zaxis_P1) && Math.Abs(from - to) > 1000 * 10) {
                    return (true, "进出轴伸出时升降轴不可以大幅运动");
                }

            }
            return (false, "");
        }

        private (bool, string) XAxis_interference(int from, int to)
        {
            //行走机构目的地小于P1待机点
            if (to > Config.Xaxis_P1+ Config.XAxis.CanErrorCountMax) {
                //上下周小于上下周待机点
                if (YAxis.GetAclPosition() < Config.Yaxis_P1 + Config.YAxis.CanErrorCountMax) {
                    return (true, "行走机构托盘与进出库线体干涉,请升高升降轴");
                }
            }
            if (!ZAxis.IsInPosition(Config.Zaxis_P1)) {
                return (true, "周转箱托盘没有收回,无法移动行走机构");
            }
            return (false, "");
        }

        /// <summary>
        /// 整机启动变量,设置为false后将退出线程,只在停止时调用
        /// </summary>
        bool mstart=true;
        public void Run() {
            mstart = true;
            while (mstart) {
                try
                {
                    canRunning = DeviceCheck();
                    if (canRunning)
                    {
                        BtnProcess();
                        canRunning = SafeCheck();
                        if (canRunning && !lastSafeCheckStatus)
                        { 
                        
                        }
                        lastSafeCheckStatus = canRunning;
                    }
                    Thread.Sleep(200);
                    if (!canRunning || !mstart)
                        continue;
                    if (runStatus == RunStatus.Running)
                    {
                        ioMonitor();
                        InProcess();
                        boxTransport.Process();
                        if (RobotManage.mainMachine.AutoInOutTest)
                            AutoInOutTestProcess();
                        else
                            StoreProcess();
                        OutProcess();
                    }
                    else if (runStatus == RunStatus.HomeReset)
                    {
                        HomeReset();
                    }
                }
                catch (Exception ex)
                {
                    Msg.add(ex.ToString(), MsgLevel.warning);
                    Msg.setlogones();
                }
                finally {
                    var m = Msg.get();
                    ProcessMsgEvent?.Invoke(m);
                    ServerCM.ProcessMsg(m);
                    //ProcessMoveinfoEvent?.Invoke(MoveInfo.List);
                    if (!UserPause)
                        Msg.clear();
                }
            }
            LogUtil.info("主线程已退出.");
        }
        public void Stop() {
            mstart = false;
            Thread.Sleep(300);
            Alarm(AlarmType.None);
            StopMove(true);
            IOMove(IO_Type.InMoto_Run, IO_VALUE.LOW);
            IOMove(IO_Type.OutMoto_Run, IO_VALUE.LOW);
        }
        public void BeginHomeReset(bool firstRun=false) {
            if (!firstRun)
            {
                StopMove();
                Thread.Sleep(500);
            }
            OpenAllServo();
            Alarm(AlarmType.None);
            runStatus = RunStatus.HomeReset;
            ResetMoveInfo.NewMove(MoveStep.H01_HomeReset);
            ResetMoveInfo.log("开始回原");
            ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
        }
        //强制回原
        bool forceHome=true;
        void HomeReset()
        {
            if (CheckWait(ResetMoveInfo))
                return;

            switch (ResetMoveInfo.MoveStep)
            {
                case MoveStep.H01_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H02_HomeReset);
                    ResetMoveInfo.log("开始回原,进出轴回原");

                    ZAxis.HomeMove(ResetMoveInfo, forceHome);
                    break;
                case MoveStep.H02_HomeReset:                    
                    if (IOValue(IO_Type.SideA_ForkMaterial_Check).Equals(IO_VALUE.HIGH) ||
                        IOValue(IO_Type.SideB_ForkMaterial_Check).Equals(IO_VALUE.HIGH))
                    {
                        Msg.add("料叉上有没有放正的物料", MsgLevel.warning);
                        RobotManage.UserPause("回源时料叉上有物料");
                        return;
                    }
                    ResetMoveInfo.NextMoveStep(MoveStep.H03_HomeReset);
                    ResetMoveInfo.log("开始回原,行走机构,回原");
                    XAxis.HomeMove(ResetMoveInfo, forceHome);                    
                    ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(500));
                    break;
                case MoveStep.H03_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H04_HomeReset);
                    YAxis.HomeMove(ResetMoveInfo, forceHome);
                    ResetMoveInfo.log("正在回原 ,升降轴,回原");
                    ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                    break;
                case MoveStep.H04_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H05_HomeReset);
                    ResetMoveInfo.log("正在回原,料叉P1待机点");
                    XAxis.AbsMove(ResetMoveInfo, Config.Zaxis_P1, Config.Zaxis_P1_speed);
                    break;
                case MoveStep.H05_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H06_HomeReset);
                    ResetMoveInfo.log("正在回原,行走机构,升降轴,到P1待机点,入口顶升下降,出口顶升上升");
                    YAxis.AbsMove(ResetMoveInfo, Config.Yaxis_P1,Config.Yaxis_P1_speed);
                    XAxis.AbsMove(ResetMoveInfo, Config.Xaxis_P1,Config.Xaxis_P1_speed);
                    OutSideLift.LiftUp(ResetMoveInfo);
                    InSideLift.LiftUp(ResetMoveInfo);
                    break;
                case MoveStep.H06_HomeReset:
                    if (IOValue(IO_Type.ForkMaterial_Check).Equals(IO_VALUE.HIGH) &&
                        IOValue(IO_Type.SideA_ForkMaterial_Check).Equals(IO_VALUE.LOW) &&
                        IOValue(IO_Type.SideB_ForkMaterial_Check).Equals(IO_VALUE.LOW))
                    {
                        ResetMoveInfo.NextMoveStep(MoveStep.H07_HomeReset);
                        ResetMoveInfo.log("进出轴上有周转箱");

                    }
                    else if (IOValue(IO_Type.SideA_ForkMaterial_Check).Equals(IO_VALUE.HIGH) ||
                             IOValue(IO_Type.SideB_ForkMaterial_Check).Equals(IO_VALUE.HIGH))
                    {
                        Msg.add("料叉上有没有放正的物料", MsgLevel.warning);
                        RobotManage.UserPause("回源时料叉上有物料");
                        return;
                    }
                    else {
                        ResetMoveInfo.NextMoveStep(MoveStep.HEND_HomeReset);
                        ResetMoveInfo.log("进出轴上没有周转箱");
                    }
                    break;
                case MoveStep.H07_HomeReset:
                    if (OutMoveInfo.MoveStep == MoveStep.Wait && IsOutLiftEmpty) {
                        ResetMoveInfo.NextMoveStep(MoveStep.H08_HomeReset);
                        boxTransport.Start(null, new BoxStorePosition(Config, StoreSide.B), StoreMoveType.ReturnHome);
                        ResetMoveInfo.log("进出轴上有周转箱,开始送到出口");
                        ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                    }
                    break;
                case MoveStep.H08_HomeReset:
                    boxTransport.Process();
                    if (boxTransport.IsComplateOrFree)
                    {
                        ResetMoveInfo.NextMoveStep(MoveStep.HEND_HomeReset);
                    }
                    break;
                case MoveStep.HEND_HomeReset:
                    forceHome = false;
                    ResetMoveInfo.log("回原完成");
                    ResetMoveInfo.EndMove();
                    runStatus = RunStatus.Running;
                    break;
            }
        }
        public bool IgnoreSafecheck=false;
        public bool IgnoreGratingSignal = false;

        bool lastSafeCheckStatus = true;
        bool SafeCheck() {
            bool ok = true;
            if (IOValue(IO_Type.InEntry_SafetyGrating).Equals(IO_VALUE.LOW))
            {
                if (!IgnoreSafecheck && !IgnoreGratingSignal
                    && IOValue(IO_Type.InFrontCar_Check).Equals(IO_VALUE.LOW))
                {
                    ok = false;
                    DeviceSuddenStop();
                }
                Msg.add("安全光栅被遮挡" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }else if (IOValue(IO_Type.OutExit_SafetyGrating).Equals(IO_VALUE.LOW))
            {
                if (!IgnoreSafecheck && !IgnoreGratingSignal
                    && IOValue(IO_Type.InFrontCar_Check).Equals(IO_VALUE.LOW))
                {
                    ok = false;
                    DeviceSuddenStop();
                }
                Msg.add("安全光栅被遮挡" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }

            if (IOValue(IO_Type.InSide_SafetyGrating).Equals(IO_VALUE.LOW))
            {
                if (!IgnoreSafecheck && !IgnoreGratingSignal
                    && IOValue(IO_Type.InFrontCar_Check).Equals(IO_VALUE.LOW))
                {
                    ok = false;
                    DeviceSuddenStop();
                }
                Msg.add("安全光栅被遮挡" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }
            else if (IOValue(IO_Type.OutSide_SafetyGrating).Equals(IO_VALUE.LOW))
            {
                if (!IgnoreSafecheck && !IgnoreGratingSignal
                    && IOValue(IO_Type.OutExitCar_Check).Equals(IO_VALUE.LOW))
                {
                    ok = false;
                    DeviceSuddenStop();
                }
                Msg.add("安全光栅被遮挡" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }

            if (IOValue(IO_Type.FrontDoor_Check).Equals(IO_VALUE.LOW))
            {
                if (!IgnoreSafecheck)
                {
                    ok = false;
                    DeviceSuddenStop();
                }
                Msg.add("前门没有关闭" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }
            if (IOValue(IO_Type.BackDoor_Check).Equals(IO_VALUE.LOW))
            {
                if (!IgnoreSafecheck)
                {
                    ok = false;
                    DeviceSuddenStop();
                }
                Msg.add("后门没有关闭" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }
            
            return ok;
        }

        void DeviceSuddenStop() {
            if (lastSafeCheckStatus)
            {
                //lastSafeCheckStatus = false;
                //AxisBean.StopMultiAxis(new List<AxisBean>() { Take_Middle_Axis, Take_UpDown_Axis, Label_X_Axis,Label_Y_Axis, Label_Z_Axis, Label_R_Axis });
                AxisBean.StopMultiAxis(AxisBean.List);
                MoveInfo.List.ForEach((m) => { m.CanWhileCount = 5; });
                LineIn.Pause();
                LineOut.Pause();
                if (runStatus == RunStatus.HomeReset)
                {
                    ResetMoveInfo.NewMove(MoveStep.H01_HomeReset);
                }
            }
        }
        void DeviceResume() {
            LineIn.Resume();
            LineOut.Resume();
        }

        /// <summary>
        /// 最后一次气压检测变为0的时间
        /// </summary>
        DateTime lastAirCloseTime = DateTime.MinValue;

        internal DateTime checkAlarmTime = DateTime.Now;
        public bool DeviceCheck() {
            bool ok = true;
            if (UserPause)
            {
                Msg.add("用户暂停", MsgLevel.warning);
                DeviceSuddenStop();
                ok = false;
                return ok;
            }
            else if (IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
            {
                Alarm(AlarmType.SuddenStop);
                Msg.add("急停中", MsgLevel.warning);
                ok = false;
            }
            else if (alarmType == AlarmType.SuddenStop) {
                //if (IOValue(IO_Type.Right_BTN).Equals(IO_VALUE.HIGH) || IOValue(IO_Type.Left_BTN).Equals(IO_VALUE.HIGH))
                //{
                //    Alarm(AlarmType.None);
                //}
                //else
                {
                    Msg.add("系统需要重置", MsgLevel.warning);
                    ok = false;
                }
            }
            if (RobotManage.InoutDebugMode)
            {
                Msg.add("进出库调试模式", MsgLevel.warning);
                ok = false;
            }
            if (IOValue(IO_Type.Airpressure_Check).Equals(IO_VALUE.LOW))
            {
                if (lastAirCloseTime == DateTime.MinValue)
                    lastAirCloseTime = DateTime.Now;

                TimeSpan span = DateTime.Now - lastAirCloseTime;
                if (span.TotalSeconds > RobotManage.Config.AirCheckSeconds)
                {
                    ok = false;
                    Msg.add("气压不足", MsgLevel.warning);
                }
            }
            else {
                lastAirCloseTime = DateTime.MinValue;
            }
            if (alarmType!=AlarmType.SuddenStop)
            {
                TimeSpan span = DateTime.Now - checkAlarmTime;
                if (span.TotalSeconds > 2)
                {
                    checkAlarmTime = DateTime.Now;
                    foreach (ConfigMoveAxis configMoveAxis in Config.moveAxisList)
                    {
                        if (AxisManager.GetAlarmStatus(configMoveAxis.DeviceName, configMoveAxis.GetAxisValue()) == 1)
                        {
                            Msg.add($"{configMoveAxis.Explain}[{configMoveAxis.GetAxisValue()}]:运动报警", MsgLevel.warning);
                            ok = false;
                        }
                    }
                }
            }
            return ok;
        }

        int GetYAxisLevel() {
            if (IOValue(IO_Type.Lift_1Level_Check).Equals(IO_VALUE.HIGH))
                return 1;
            if (IOValue(IO_Type.Lift_2Level_Check).Equals(IO_VALUE.HIGH))
                return 2;
            if (IOValue(IO_Type.Lift_3Level_Check).Equals(IO_VALUE.HIGH))
                return 3;
            if (IOValue(IO_Type.Lift_4Level_Check).Equals(IO_VALUE.HIGH))
                return 4;
            if (IOValue(IO_Type.Lift_5Level_Check).Equals(IO_VALUE.HIGH))
                return 5;
            if (IOValue(IO_Type.Lift_6Level_Check).Equals(IO_VALUE.HIGH))
                return 6;
            return 0;
        }

    }
}