| | |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.core.common.Cools; |
| | | import com.core.common.DateUtils; |
| | | import com.core.common.SpringUtils; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.asrs.entity.BasDevp; |
| | | import com.zy.asrs.entity.BasRgv; |
| | | import com.zy.asrs.entity.BasRgvMap; |
| | | import com.zy.asrs.entity.BasRgvOpt; |
| | | import com.zy.asrs.service.BasDevpService; |
| | | import com.zy.asrs.service.BasRgvMapService; |
| | | import com.zy.asrs.service.BasRgvOptService; |
| | | import com.zy.asrs.service.BasRgvService; |
| | | import com.zy.common.utils.News; |
| | |
| | | import com.zy.core.model.command.CrnCommand; |
| | | import com.zy.core.model.command.RgvCommand; |
| | | import com.zy.core.model.protocol.RgvProtocol; |
| | | import com.zy.core.model.protocol.StaProtocol; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.Date; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * RGV线程 |
| | |
| | | private SiemensS7Net siemensNet; |
| | | private RgvSlave slave; |
| | | private RgvProtocol rgvProtocol; |
| | | private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * 工位1复位信号 |
| | | */ |
| | |
| | | private boolean PakRgv = true; |
| | | //接驳标记 |
| | | private boolean PakToCrn = true; |
| | | // 任务锁定 |
| | | private boolean Paking = true; |
| | | |
| | | public RgvThread(RgvSlave slave) { |
| | | this.slave = slave; |
| | |
| | | command.setTaskStatus(RgvTaskStatusType.NONE); // 任务模式 |
| | | command.setTargetPosition(0); // 源站 |
| | | command.setWrkTaskPri(0); // 目标站 |
| | | write(command); |
| | | command.setCommand(false); |
| | | write2(command); |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | } |
| | | |
| | | } |
| | | } |
| | | private static final Set<Integer> specialSites = new HashSet<>( |
| | | Arrays.asList(1004,1014,1018,1028,1035)); |
| | | public static final Map<Integer, Integer> StaPosition = new HashMap<Integer, Integer>() {{ |
| | | put(1004, 1001);put(1014, 1011);put(1018, 1015);put(1028, 1025);put(1035, 1032); |
| | | |
| | | }}; |
| | | public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{ |
| | | add(1);add(2); |
| | | |
| | | }}; |
| | | |
| | | public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{ |
| | | add(3);add(4); |
| | | |
| | | }}; |
| | | /** |
| | | * 初始化站点状态 |
| | | */ |
| | | private void initSite() { |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | |
| | | // 站点编号 |
| | | for (Integer siteId : staNos) { |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null == staProtocol) { |
| | | staProtocol = new StaProtocol(); |
| | | staProtocol.setSiteId(siteId); |
| | | station.put(siteId, staProtocol); |
| | | } |
| | | staProtocol.setWorkNo(0); // ID |
| | | staProtocol.setAutoing(false); // 自动 |
| | | staProtocol.setLoading(false); // 有物 |
| | | staProtocol.setInEnable(false); // 可入 |
| | | staProtocol.setOutEnable(false); // 可出 |
| | | staProtocol.setEmptyMk(false); // 空板信号 |
| | | staProtocol.setStaNo((short) 0); // 目标站 |
| | | |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | // rgvProtocol.setxDistance((short) 0); |
| | | // rgvProtocol.setxDuration((short) 0); |
| | | } |
| | | |
| | | private ArrayList<Integer> getStaNo() { |
| | | switch (slave.getId()) { |
| | | case 1: |
| | | return staNos1; |
| | | case 2: |
| | | return staNos2; |
| | | default: |
| | | throw new CoolException("服务器异常"); |
| | | } |
| | | } |
| | | @Override |
| | | public boolean connect() { |
| | | boolean result = false; |
| | |
| | | */ |
| | | private void readStatus(){ |
| | | try { |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 27); |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 38); |
| | | if (result.IsSuccess) { |
| | | if (null == rgvProtocol) { |
| | | rgvProtocol = new RgvProtocol(); |
| | |
| | | rgvProtocol.setStatus1(siemensNet.getByteTransform().TransInt16(result.Content, 12)); |
| | | rgvProtocol.setTaskNo1(siemensNet.getByteTransform().TransInt32(result.Content, 16)); |
| | | rgvProtocol.setTaskNo2(siemensNet.getByteTransform().TransInt32(result.Content, 20)); |
| | | boolean[] status1 = siemensNet.getByteTransform().TransBool(result.Content, 24, 2); |
| | | rgvProtocol.setStaNo1(siemensNet.getByteTransform().TransInt32(result.Content, 28)); |
| | | rgvProtocol.setStaNo2(siemensNet.getByteTransform().TransInt32(result.Content, 32)); |
| | | boolean[] status1 = siemensNet.getByteTransform().TransBool(result.Content, 24, 1); |
| | | rgvProtocol.setLoaded1(status1[0]); |
| | | rgvProtocol.setLoaded2(status1[1]); |
| | | boolean[] status2 = siemensNet.getByteTransform().TransBool(result.Content, 26, 1); |
| | | boolean[] status2 = siemensNet.getByteTransform().TransBool(result.Content, 36,1); |
| | | rgvProtocol.setErr1(status2[0]); |
| | | rgvProtocol.setErr2(status2[1]); |
| | | rgvProtocol.setErr3(status2[2]); |
| | |
| | | rgvProtocol.setErr5(status2[4]); |
| | | rgvProtocol.setErr6(status2[5]); |
| | | rgvProtocol.setErr7(status2[6]); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId())); |
| | | rgvProtocol.setErr8(status2[7]); |
| | | boolean[] status3 = siemensNet.getByteTransform().TransBool(result.Content, 37, 1); |
| | | rgvProtocol.setErr9(status3[0]); |
| | | rgvProtocol.setErr10(status3[1]); |
| | | rgvProtocol.setErr11(status3[2]); |
| | | rgvProtocol.setErr12(status3[3]); |
| | | BasRgvMapService basRgvMapService = SpringUtils.getBean(BasRgvMapService.class); |
| | | |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId())); |
| | | try { |
| | | // 根据实时信息更新数据库 |
| | | BasRgvService basRgvService = SpringUtils.getBean(BasRgvService.class); |
| | |
| | | } |
| | | byte[] writeBytes = new byte[14]; |
| | | command.setRgvNo(slave.getId()); |
| | | if(specialSites.contains(command.getTargetPosition())){ |
| | | command.setTargetPosition(StaPosition.get(command.getTargetPosition())); |
| | | } |
| | | writeInt32(writeBytes, 0, command.getWrkTaskPri()); |
| | | writeInt32(writeBytes, 4, command.getTargetPosition()); |
| | | writeInt16(writeBytes, 8, (short)command.getTaskStatus()); |
| | |
| | | int writeCount2 = 0; |
| | | do { |
| | | writeCount2++; |
| | | boolean commandFinish = true; |
| | | Short commandFinish = 1; |
| | | result = siemensNet.Write("DB100.34.0", commandFinish); |
| | | if(result.IsSuccess){ |
| | | //RGV任务写入后,回读一次,看是否成功 |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.34", (short) 2); |
| | | if (resultRead.IsSuccess) { |
| | | commandFinish=siemensNet.getByteTransform().TransBool(resultRead.Content, 0); |
| | | if (!commandFinish){ |
| | | commandFinish=siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); |
| | | if (commandFinish !=1){ |
| | | News.error("RGV任务确认位"+commandFinish+"写入数据与回读数据不一致!"+"循环执行次数:"+writeCount2+"次"); |
| | | }else{ |
| | | //任务命令写入成功 |
| | | News.info("RGV任务确认位"+commandFinish+"回读成功!"+"循环执行次数:"+writeCount2+"次"); |
| | | break; |
| | | } |
| | | }else { |
| | | News.error("RGV任务确认位"+commandFinish+"回读失败!"+"循环执行次数:"+writeCount2+"次"); |
| | | } |
| | | } else { |
| | | News.error("RGV任务确认位"+commandFinish+"写入失败!"+"循环执行次数:"+writeCount2+"次"); |
| | | } |
| | | }while (writeCount2<5); |
| | | } |
| | | try { |
| | | // 日志记录 |
| | | BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); |
| | | BasRgvOpt basRgvOpt = new BasRgvOpt( |
| | | command.getTaskNo(), |
| | | command.getRgvNo(), |
| | | new Date(), |
| | | command.getTaskStatus().toString(), |
| | | command.getTargetPosition(), |
| | | command.getWrkTaskPri(), |
| | | new Date() |
| | | ); |
| | | bean.insert(basRgvOpt); |
| | | } catch (Exception ignore) { |
| | | log.error(ignore.getMessage()); |
| | | } |
| | | |
| | | if (result != null && result.IsSuccess) { |
| | | Thread.sleep(200); |
| | | this.readStatus(); |
| | | log.info("RGV 工位命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 工位命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); |
| | | return true; |
| | | } else { |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc工位数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | log.error("写入RGV plc工位数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | return false; |
| | | } |
| | | } |
| | | private boolean write2(RgvCommand command) throws InterruptedException { |
| | | if (null == command) { |
| | | log.error("RGV写入命令为空"); |
| | | return false; |
| | | } |
| | | OperateResultExOne<byte[]> result1 = siemensNet.Read("DB100.0", (short) 39); |
| | | if (result1.IsSuccess){ |
| | | RgvCommand one = new RgvCommand(); |
| | | one.setWrkTaskPri(siemensNet.getByteTransform().TransInt32(result1.Content, 0));//执行工位 |
| | | one.setTargetPosition(siemensNet.getByteTransform().TransInt32(result1.Content, 4));//行走目标站 |
| | | one.setTaskStatus(siemensNet.getByteTransform().TransInt16(result1.Content, 8));//小车状态 |
| | | one.setTaskNo(siemensNet.getByteTransform().TransInt32(result1.Content, 10));//下发工作号 |
| | | News.info("RGV命令下发前读取状态[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one)); |
| | | } |
| | | byte[] writeBytes = new byte[36]; |
| | | command.setRgvNo(slave.getId()); |
| | | if(specialSites.contains(command.getTargetPosition())){ |
| | | command.setTargetPosition(StaPosition.get(command.getTargetPosition())); |
| | | } |
| | | writeInt32(writeBytes, 0, command.getWrkTaskPri()); |
| | | writeInt32(writeBytes, 4, command.getTargetPosition()); |
| | | writeInt16(writeBytes, 8, (short)command.getTaskStatus()); |
| | | writeInt32(writeBytes, 10, command.getTaskNo()); |
| | | writeBool(writeBytes, 34, 0, command.getCommand()); |
| | | OperateResult result = siemensNet.Write("DB100.0", writeBytes); |
| | | |
| | | if (!result.IsSuccess){ |
| | | News.error("写入RGVplc数据失败,重新添加任务到队列 ===> [id:{}],{}",slave.getId(),JSON.toJSON(command)); |
| | | MessageQueue.offer(SlaveType.Rgv,slave.getId(),new Task(2,command)); |
| | | Thread.sleep(100); |
| | | readStatus(); |
| | | return false; |
| | | } |
| | | |
| | | //RGV任务写入后,回读一次,看是否成功 |
| | | Thread.sleep(400); |
| | | try { |
| | | OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 14); |
| | | if (resultRead.IsSuccess){ |
| | | RgvCommand one = new RgvCommand(); |
| | | one.setWrkTaskPri(siemensNet.getByteTransform().TransInt32(resultRead.Content, 0));//执行工位 |
| | | one.setTargetPosition(siemensNet.getByteTransform().TransInt32(resultRead.Content, 4));//行走目标站 |
| | | one.setTaskStatus(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8));//小车状态 |
| | | one.setTaskNo(siemensNet.getByteTransform().TransInt32(resultRead.Content, 10));//下发工作号 |
| | | if ( |
| | | !command.getTaskNo().equals(one.getTaskNo()) || |
| | | !command.getTaskStatus().equals(one.getTaskStatus()) || |
| | | !command.getWrkTaskPri().equals(one.getWrkTaskPri()) || |
| | | !command.getTargetPosition().equals(one.getTargetPosition()) |
| | | ) { |
| | | try{ |
| | | News.error("RGV命令地址写入后回读失败[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one)); |
| | | }catch (Exception e){ |
| | | try{ |
| | | News.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSON(command),JSON.toJSON(resultRead)); |
| | | }catch (Exception e1){ |
| | | News.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSON(command)); |
| | | } |
| | | } |
| | | News.error("Rgv命令回读失败后,重新添加任务到队列 ===>> [id:{}],{}", slave.getId(), JSON.toJSON(command)); |
| | | MessageQueue.offer(SlaveType.Rgv, slave.getId(), new Task(2, command)); |
| | | Thread.sleep(100); |
| | | readStatus(); |
| | | return false; |
| | | }else { |
| | | News.info("RGV命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one)); |
| | | } |
| | | } |
| | | }catch (Exception e){ |
| | | News.error("RGV命令地址写入后回读出错"); |
| | | } |
| | | |
| | | if (result.IsSuccess) { //任务下发确认 |
| | | Thread.sleep(300); |
| | | //任务下发次数 |
| | | int writeCount2 = 0; |
| | | do { |
| | | writeCount2++; |
| | | Short commandFinish = 1; |
| | | result = siemensNet.Write("DB100.34.0", commandFinish); |
| | | if(result.IsSuccess){ |
| | | //RGV任务写入后,回读一次,看是否成功 |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.34", (short) 2); |
| | | if (resultRead.IsSuccess) { |
| | | commandFinish=siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); |
| | | if (commandFinish !=1){ |
| | | News.error("RGV任务确认位"+commandFinish+"写入数据与回读数据不一致!"+"循环执行次数:"+writeCount2+"次"); |
| | | }else{ |
| | | //任务命令写入成功 |
| | |
| | | } |
| | | } |
| | | |
| | | public Integer getRgvPosI(Integer pos) { |
| | | if (pos == null) return 0; |
| | | |
| | | // key: 站点号 value: 基准物理位置 |
| | | Map<Integer, Integer> posMap = new HashMap<>(); |
| | | posMap.put(1004, 6534); |
| | | posMap.put(1007, 33634); |
| | | posMap.put(1010, 75174); |
| | | posMap.put(1014, 102124); |
| | | posMap.put(1018, 138224); |
| | | posMap.put(1021, 178034); |
| | | posMap.put(1024, 219684); |
| | | posMap.put(1028, 246724); |
| | | posMap.put(1031, 288194); |
| | | posMap.put(1035, 315204); |
| | | int tolerance = 50; // 允许误差范围 |
| | | |
| | | for (Map.Entry<Integer, Integer> entry : posMap.entrySet()) { |
| | | int site = entry.getKey(); |
| | | int basePos = entry.getValue(); |
| | | if (Math.abs(pos - basePos) <= tolerance) { |
| | | return site; |
| | | } |
| | | } |
| | | |
| | | return 0; // 没匹配到站点 |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |