controller.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class controller : MonoBehaviour
  5. {
  6. public GameObject machineArm;
  7. public GameObject scanBoard;
  8. public GameObject elevator_left;
  9. public GameObject elevator_right;
  10. public GameObject carModel;
  11. public GameObject eventGenerator;
  12. public GameObject[] transferVehicles;
  13. /// <summary>
  14. /// 0 idle, 1 停车机械手抓车, 2 停车右电梯送出中跑车接车, 3 停车左电梯送中跑车上楼并送车,cmd=2, 4 停车
  15. /// 4 fetch trans get car cmd=3, 5 fetch trans go downstairs, 6 fetch arm get car cmd=4, 7 fetch trans go back
  16. /// </summary>
  17. public int state;
  18. public bool ready;
  19. // 0等待锁,1设备锁
  20. public int[] doorLock = { 0, 0, 0, 0, 0, 0 };
  21. public GameObject[] doors;
  22. /// <summary>
  23. /// 停车终端个数,从生成器获取
  24. /// </summary>
  25. public int numOfParkTerm;
  26. public int[] termState = { 0, 0, 0, 0, 0, 0 };
  27. public int[] currTermVar = { 0, 0, 0, 0, 0, 0 };
  28. private int[] scanned = { 0, 0, 0, 0, 0, 0 };
  29. private static int[] termIndex = { 4, 6, 7, 9, 10, 12 };
  30. private List<CarInfo> movingCars;
  31. public int[] parkSpace;
  32. private bool initialized;
  33. private float prevTime;
  34. // 当前处理指令0-5
  35. public int currIndex;
  36. public GameObject parkUsingTrans;
  37. public GameObject fetchUsingTrans;
  38. private const int cmdInterval = 3;
  39. // 准备停入编号
  40. public int parkSpaceIndex;
  41. // 准备取出编号
  42. public int fetchSpaceIndex;
  43. //// 停车指令终端号
  44. //private int parkTermIndex;
  45. // 取车指令终端号
  46. public int fetchTermIndex;
  47. private float[] termX = { 1.2f, 6.2f, 8.7f, 13.7f, 16.2f, 21.2f };
  48. /// <summary>
  49. /// 中跑车根据左右上排序,便于控制
  50. /// </summary>
  51. public GameObject[] orderedTrans;
  52. /// <summary>
  53. /// 中跑车状态,0空闲状态;1上中跑车到左暂存位;2右中跑车上行到右边暂存位,可取车;3左中跑车向右到右暂存位,停车接车;4上中跑车到二楼左暂存位
  54. /// </summary>
  55. public int transState;
  56. public GameObject CreateCar(Vector3 position)
  57. {
  58. if (carModel != null)
  59. {
  60. GameObject aCar = Instantiate(carModel);
  61. aCar.GetComponent<Transform>().position = position;
  62. //movingCars.Add(new CarInfo(aCar,position));
  63. return aCar;
  64. }
  65. return null;
  66. }
  67. public void DestroyCar(GameObject car)
  68. {
  69. //Debug.Log("-----");
  70. CarInfo temp = new CarInfo(car, Vector3.zero);
  71. //temp.rndValue = value;
  72. //Debug.Log(movingCars.Contains(temp));
  73. if (movingCars.Contains(temp))
  74. {
  75. temp = movingCars.Find((CarInfo ci) => { return ci.Equals(temp); });
  76. int space = (temp.floor - 2) * 15 + temp.space;
  77. if (parkSpace[space] != 0)
  78. {
  79. parkSpace[space] = 0;
  80. }
  81. movingCars.Remove(temp);
  82. Destroy(car);
  83. }
  84. }
  85. public void AddToGarage(GameObject car, int floor, int space)
  86. {
  87. if (movingCars != null)
  88. {
  89. CarInfo temp = new CarInfo(car, floor, space);
  90. temp.state = 2;
  91. movingCars.Add(temp);
  92. int spaceNo = (floor - 2) * 15 + space;
  93. parkSpace[spaceNo] = 1;
  94. }
  95. }
  96. public GameObject GetCar(int floor, int space)
  97. {
  98. if (movingCars != null)
  99. {
  100. for (int i = 0; i < movingCars.Count; i++)
  101. {
  102. if (movingCars[i].floor == floor && movingCars[i].space == space && movingCars[i].handle != null)
  103. {
  104. return movingCars[i].handle;
  105. }
  106. }
  107. }
  108. return null;
  109. }
  110. // Start is called before the first frame update
  111. void Start()
  112. {
  113. for (int i = 0; i < doorLock.Length; i++)
  114. {
  115. doorLock[i] = 1;
  116. }
  117. Random.InitState((int)(Time.time * 1000.0f));
  118. transferVehicles = GameObject.FindGameObjectsWithTag("transfer");
  119. if (transferVehicles.Length != 3 || machineArm == null || elevator_left == null || elevator_right == null || carModel == null)
  120. {
  121. initialized = false;
  122. Debug.Log("初始化失败");
  123. return;
  124. }
  125. else
  126. {
  127. //termState = new int[6];
  128. //currTermVar = new int[6];
  129. fetchTermIndex = -1;
  130. transState = 0;
  131. orderedTrans = new GameObject[3];
  132. int count = 0;
  133. for (int i = 0; i < transferVehicles.Length; i++)
  134. {
  135. //Debug.Log(transferVehicles[i].GetComponent<transferBehavior>().GetFloor()+","+transferVehicles[i].GetComponent<transferBehavior>().GetNearestTerm());
  136. // 左中跑车
  137. if (transferVehicles[i].GetComponent<transferBehavior>().GetFloor() == 2 && transferVehicles[i].GetComponent<transferBehavior>().GetSpace() >= 12)
  138. {
  139. orderedTrans[0] = transferVehicles[i]; count++;
  140. }
  141. // 右中跑车
  142. else if (transferVehicles[i].GetComponent<transferBehavior>().GetFloor() == 2 && transferVehicles[i].GetComponent<transferBehavior>().GetSpace() <= 4)
  143. {
  144. orderedTrans[1] = transferVehicles[i]; count++;
  145. }
  146. // 上中跑车
  147. else if (transferVehicles[i].GetComponent<transferBehavior>().GetFloor() != 2)
  148. {
  149. orderedTrans[2] = transferVehicles[i]; count++;
  150. }
  151. }
  152. if (count != 3)
  153. {
  154. initialized = false;
  155. Debug.Log("初始化失败");
  156. return;
  157. }
  158. ready = false;
  159. parkUsingTrans = null;
  160. state = 0;
  161. prevTime = Time.time;
  162. movingCars = new List<CarInfo>();
  163. initialized = true;
  164. currIndex = 0;
  165. parkSpace = new int[165];
  166. //// 中跑车排序
  167. //int floor = 13;
  168. //int index = -1;
  169. //for (int i = 0; i < transferVehicles.Length; i++)
  170. //{
  171. // for (int j = i; j < transferVehicles.Length; j++)
  172. // {
  173. // if (transferVehicles[j].GetComponent<transferBehavior>().GetFloor() < floor)
  174. // {
  175. // floor = transferVehicles[j].GetComponent<transferBehavior>().GetFloor();
  176. // index = j;
  177. // }
  178. // }
  179. // GameObject temp = transferVehicles[i];
  180. // transferVehicles[i] = transferVehicles[index];
  181. // transferVehicles[index] = temp;
  182. // floor = 13;
  183. // index = -1;
  184. //}
  185. // object reference test
  186. //GameObject temp = CreateCar(new Vector3(5, 5, 5));
  187. //AddToGarage(temp, 2, 2);
  188. //test = temp;
  189. //temp = null;
  190. //DestroyCar(test);
  191. //Debug.Log("完成");
  192. }
  193. }
  194. // 根据停取车选择中跑车编号
  195. public int GetSpecificTrans(bool park)
  196. {
  197. for (int i = 0; i < transferVehicles.Length; i++)
  198. {
  199. int temp = transferVehicles[i].GetComponent<transferBehavior>().GetFloor();
  200. }
  201. return -1;
  202. }
  203. // 模拟PLC读取终端指令
  204. void UpdateCmd()
  205. {
  206. //Debug.Log("00000");
  207. if (parkSpace == null)
  208. {
  209. parkSpace = new int[165];
  210. for (int i = 1; i < 166; i++)
  211. {
  212. parkSpace[i] = 0;
  213. }
  214. }
  215. //Debug.Log("currIndex: "+currIndex);
  216. //Debug.Log("currIndex: "+currIndex +", state: "+currTermVar[currIndex]);
  217. if (currIndex > 5)
  218. {
  219. currIndex = 0;
  220. }
  221. if (currTermVar[currIndex] == 0)
  222. {
  223. currIndex++;
  224. // 遍历完6个终端
  225. if (currIndex > 5)
  226. {
  227. currIndex = 0;
  228. // 拷贝指令
  229. for (int i = 0; i < 6; i++)
  230. {
  231. currTermVar[i] = termState[i];
  232. }
  233. }
  234. }
  235. //Debug.Log("22222");
  236. if (fetchTermIndex <= 0)
  237. {
  238. for (int i = 0; i < 6; i++)
  239. {
  240. if (termState[i] == 2)
  241. {
  242. fetchTermIndex = i;
  243. break;
  244. }
  245. }
  246. }
  247. //Debug.Log("curIndex: " + currIndex + ", " + currTermVar[currIndex] + ", " + termState[currIndex]);
  248. // 更新停取车位编号
  249. //Debug.Log("33333");
  250. if (currTermVar[currIndex] == 1)
  251. {
  252. //Debug.Log("44444");
  253. //parkTermIndex = currIndex;
  254. if (parkSpaceIndex <= 0)
  255. {
  256. //parkSpaceIndex = Mathf.FloorToInt(Random.value*(167-46))+46;//暂时测试5楼以上功能
  257. //Debug.Log("找空位");
  258. for (int i = 46; i < 166; i++)
  259. {
  260. //Debug.Log("检测到停车指令,车位" + i + ": " + parkSpace[i]);
  261. if (parkSpace[i] == 0)
  262. {
  263. parkSpaceIndex = i; break;
  264. }
  265. //for (int j = 0; j < movingCars.Count; j++)
  266. //{
  267. // if ((movingCars[j].floor - 2) * 15 + movingCars[j].space == parkSpaceIndex)
  268. // {
  269. // parkSpaceIndex = 0;
  270. // break;
  271. // }
  272. //}
  273. //if (parkSpaceIndex != 0) break;
  274. }
  275. }
  276. }
  277. // 取车位指令不断遍历
  278. else if (currTermVar[currIndex] == 2)
  279. {
  280. Debug.Log(movingCars.Count);
  281. if (movingCars.Count <= 0)
  282. {
  283. currIndex++;
  284. if (currIndex > 5)
  285. {
  286. currIndex = 0;
  287. }
  288. }
  289. // 取车指令已就位
  290. else if (fetchTermIndex != -1)
  291. {
  292. Debug.Log("55555");
  293. //不是遍历到的编号,则跳过直到找到正确编号为止
  294. if (fetchTermIndex != currIndex)
  295. {
  296. currIndex++;
  297. return;
  298. }
  299. else
  300. {
  301. while (fetchSpaceIndex <= 0 && movingCars.Count > 0)
  302. {
  303. int index = Mathf.FloorToInt(Random.value * movingCars.Count);
  304. fetchSpaceIndex = (movingCars[index].floor - 2) * 15 + movingCars[index].space;
  305. }
  306. if (parkSpace[fetchSpaceIndex] > 0)
  307. {
  308. parkSpace[fetchSpaceIndex] = 0;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. /// <summary>
  315. /// 中跑车运动控制,上方5、6层为缓冲
  316. /// </summary>
  317. void TransferMove()
  318. {
  319. //if (currTermVar[currIndex] == 0) return;
  320. if (orderedTrans.Length != 3) return;
  321. int fetchFloor = (fetchSpaceIndex - 1) / 15 + 2;
  322. int fetchX = fetchSpaceIndex - (fetchFloor - 2) * 15;
  323. int parkFloor = (parkSpaceIndex - 1) / 15 + 2;
  324. int parkX = parkSpaceIndex - (parkFloor - 2) * 15;
  325. // 中跑车循环,无论停取,接到车给中跑车变量赋值
  326. switch (transState)
  327. {
  328. case 0:
  329. if (parkSpaceIndex > 0 || fetchSpaceIndex > 0)
  330. {
  331. transState = 1;
  332. }
  333. break;
  334. case 1://上左
  335. Debug.Log("1、上左");
  336. int floor = orderedTrans[2].GetComponent<transferBehavior>().GetFloor();
  337. if (floor == 2)
  338. {
  339. Debug.Log("上左异常");
  340. return;
  341. }
  342. // 停在楼上左边缓冲位则进下一步
  343. if (orderedTrans[2].GetComponent<transferBehavior>().state == 0 && orderedTrans[2].GetComponent<transferBehavior>().GetSpace() == 13)
  344. {
  345. transState = 2;
  346. return;
  347. }
  348. if (orderedTrans[2].GetComponent<transferBehavior>().state == 0)
  349. {
  350. //Debug.Log("上左 车状态0");
  351. //Debug.Log(parkX +", "+ orderedTrans[2].GetComponent<transferBehavior>().GetSpace());
  352. // 空中跑车不同层不取车
  353. if (fetchFloor != floor && orderedTrans[2].GetComponent<transferBehavior>().load == null)
  354. {
  355. orderedTrans[2].GetComponent<transferBehavior>().SetTarget(floor, 13);
  356. }
  357. // 空中跑车同层则取车
  358. else if (orderedTrans[2].GetComponent<transferBehavior>().load == null && fetchUsingTrans == null)
  359. {
  360. // 取车调用电梯便于移动
  361. fetchUsingTrans = orderedTrans[2];
  362. orderedTrans[2].GetComponent<transferBehavior>().SetTarget(floor, fetchX);
  363. if (fetchX <= 2 && elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load == null)
  364. {
  365. elevator_right.GetComponent<elevatorBehavior>().targetFloor = floor;
  366. }
  367. else if (fetchX >= 14 && elevator_left.GetComponent<elevatorBehavior>().state == 0 && elevator_left.GetComponent<elevatorBehavior>().load == null)
  368. {
  369. elevator_left.GetComponent<elevatorBehavior>().targetFloor = floor;
  370. }
  371. }
  372. // 取车中跑车接到车,送到13位
  373. else if (fetchUsingTrans == orderedTrans[2] && orderedTrans[2].GetComponent<transferBehavior>().load != null && orderedTrans[2].GetComponent<transferBehavior>().GetSpace() != 13)
  374. {
  375. orderedTrans[2].GetComponent<transferBehavior>().SetTarget(floor, 13);
  376. }
  377. // 停车中跑车有车,送入车位
  378. else if (parkUsingTrans == orderedTrans[2] && orderedTrans[2].GetComponent<transferBehavior>().load != null && parkFloor == floor /*&& parkX != orderedTrans[2].GetComponent<transferBehavior>().GetSpace()*/)
  379. {
  380. orderedTrans[2].GetComponent<transferBehavior>().SetTarget(floor, parkX);
  381. if (parkX <= 2 && elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load == null)
  382. {
  383. elevator_right.GetComponent<elevatorBehavior>().targetFloor = floor;
  384. }
  385. else if (parkX >= 14 && elevator_left.GetComponent<elevatorBehavior>().state == 0 && elevator_left.GetComponent<elevatorBehavior>().load == null)
  386. {
  387. elevator_left.GetComponent<elevatorBehavior>().targetFloor = floor;
  388. }
  389. }
  390. // 与上部条件隔离,停车中跑车已送完车,清理
  391. if (parkUsingTrans == orderedTrans[2] && orderedTrans[2].GetComponent<transferBehavior>().load == null && parkFloor == floor && parkX == orderedTrans[2].GetComponent<transferBehavior>().GetSpace())
  392. {
  393. currTermVar[currIndex] = 0;
  394. termState[currIndex] = 0;
  395. parkSpaceIndex = -1;
  396. parkUsingTrans = null;
  397. }
  398. }
  399. if (orderedTrans[2].GetComponent<transferBehavior>().state == 4)
  400. {
  401. // 有取车指令空载取车
  402. if (fetchX > 0 && orderedTrans[2].GetComponent<transferBehavior>().GetSpace() == fetchX && orderedTrans[2].GetComponent<transferBehavior>().load == null)
  403. {
  404. orderedTrans[2].GetComponent<transferBehavior>().SendCmd(3);
  405. }
  406. // 有停车指令送车,清除停车指令,复位变量
  407. else if (parkX > 0 && orderedTrans[2].GetComponent<transferBehavior>().GetSpace() == parkX && orderedTrans[2].GetComponent<transferBehavior>().load != null)
  408. {
  409. Debug.Log("停车入库,清除指令");
  410. orderedTrans[2].GetComponent<transferBehavior>().SendCmd(2);
  411. //state = 0;
  412. }
  413. // 无取车指令或已在13位
  414. else if (fetchX <= 0 || orderedTrans[2].GetComponent<transferBehavior>().GetSpace() == 13)
  415. {
  416. orderedTrans[2].GetComponent<transferBehavior>().SendCmd(-1);
  417. }
  418. }
  419. break;
  420. case 2://右上
  421. Debug.Log("2、右上");
  422. floor = orderedTrans[1].GetComponent<transferBehavior>().GetFloor();
  423. int targetFloor = 5;
  424. // 修改目标楼层
  425. if (parkFloor <= 2 && orderedTrans[2].GetComponent<transferBehavior>().GetFloor() == 5)
  426. {
  427. targetFloor = 6;
  428. }
  429. // 停车中跑车上行,修改目标楼层
  430. if (parkFloor > 2 && parkUsingTrans == orderedTrans[1] && orderedTrans[1].GetComponent<transferBehavior>().load != null)
  431. {
  432. targetFloor = parkFloor;
  433. }
  434. // 移动中跑车
  435. if (orderedTrans[1].GetComponent<transferBehavior>().state == 0)
  436. {
  437. if (floor != targetFloor || orderedTrans[1].GetComponent<transferBehavior>().GetSpace() != 3)
  438. {
  439. orderedTrans[1].GetComponent<transferBehavior>().SetTarget(targetFloor, 3);
  440. }
  441. else
  442. {
  443. transState = 3;
  444. return;
  445. }
  446. }
  447. else if (orderedTrans[1].GetComponent<transferBehavior>().state == 4)
  448. {
  449. orderedTrans[1].GetComponent<transferBehavior>().SendCmd(-1);
  450. }
  451. if (elevator_right.GetComponent<elevatorBehavior>().state == 0)
  452. {
  453. if (orderedTrans[1].GetComponent<transferBehavior>().waitForElevator && elevator_right.GetComponent<elevatorBehavior>().load == null)
  454. {
  455. elevator_right.GetComponent<elevatorBehavior>().targetFloor = 2;
  456. }
  457. else if (orderedTrans[1].GetComponent<transferBehavior>().state == 2 && elevator_right.GetComponent<elevatorBehavior>().load != null)
  458. {
  459. elevator_right.GetComponent<elevatorBehavior>().targetFloor = targetFloor;
  460. }
  461. }
  462. break;
  463. case 3://左右,机械手到位则停车接车或取车送车
  464. Debug.Log("3、左右");
  465. floor = orderedTrans[0].GetComponent<transferBehavior>().GetFloor();
  466. int space = orderedTrans[0].GetComponent<transferBehavior>().GetSpace();
  467. if (orderedTrans[0].GetComponent<transferBehavior>().state == 4)
  468. {
  469. // 中跑车送车交接
  470. if (machineArm.GetComponent<machineArmBehavior>().state == 160 && orderedTrans[0].GetComponent<transferBehavior>().load != null && fetchUsingTrans == orderedTrans[0])
  471. {
  472. orderedTrans[0].GetComponent<transferBehavior>().SendCmd(4);
  473. Debug.Log("取车送车,待机械手抓");
  474. }
  475. // 中跑车停车接车或空载
  476. else if (fetchUsingTrans != orderedTrans[0])
  477. {
  478. if (floor == 2 && space == 3)
  479. {
  480. orderedTrans[0].GetComponent<transferBehavior>().SendCmd(-1);
  481. }
  482. }
  483. }
  484. if (orderedTrans[0].GetComponent<transferBehavior>().state == 0)
  485. {
  486. // 与上部条件隔离,取车中跑车已送完车,清理
  487. if (fetchUsingTrans == orderedTrans[0] && orderedTrans[0].GetComponent<transferBehavior>().load == null)
  488. {
  489. Debug.Log("取车完成,清理");
  490. currTermVar[currIndex] = 0;
  491. termState[currIndex] = 0;
  492. fetchSpaceIndex = -1;
  493. fetchTermIndex = -1;
  494. fetchUsingTrans = null;
  495. }
  496. // 左中跑车负载,先启动机械手取车
  497. if (floor == 2 && orderedTrans[0].GetComponent<transferBehavior>().load != null && fetchUsingTrans == orderedTrans[0])
  498. {
  499. // 机械手等待取车
  500. if (machineArm.GetComponent<machineArmBehavior>().state == 0 && machineArm.GetComponent<machineArmBehavior>().terminalIndex == -1)
  501. {
  502. machineArm.GetComponent<machineArmBehavior>().terminalIndex = fetchTermIndex + 7;
  503. }
  504. orderedTrans[0].GetComponent<transferBehavior>().SetTarget(2, termIndex[fetchTermIndex]);
  505. }
  506. // 停车机械手等中跑车,中跑车移动
  507. else if (parkUsingTrans == null && parkSpaceIndex > 0 && floor == 2 && space != termIndex[currIndex] && orderedTrans[0].GetComponent<transferBehavior>().load == null)
  508. {
  509. // 机械手等待停车
  510. if (machineArm.GetComponent<machineArmBehavior>().state == 0 && machineArm.GetComponent<machineArmBehavior>().terminalIndex == -1)
  511. {
  512. machineArm.GetComponent<machineArmBehavior>().terminalIndex = currIndex + 1;
  513. }
  514. orderedTrans[0].GetComponent<transferBehavior>().SetTarget(2, termIndex[currIndex]);
  515. }
  516. // 中跑车接到车,且不是取车中跑车
  517. else if (parkSpaceIndex > 0 && floor == 2 && space == termIndex[currIndex] && orderedTrans[0].GetComponent<transferBehavior>().load != null && fetchUsingTrans != orderedTrans[0])
  518. {
  519. if (parkUsingTrans == null)
  520. {
  521. parkUsingTrans = orderedTrans[0];
  522. }
  523. orderedTrans[0].GetComponent<transferBehavior>().SetTarget(2, 3);
  524. }
  525. else if (floor == 2 && space == 3)
  526. {
  527. transState = 4;
  528. }
  529. else
  530. {
  531. orderedTrans[0].GetComponent<transferBehavior>().SetTarget(2, 3);
  532. }
  533. }
  534. break;
  535. case 4://上下
  536. Debug.Log("4、上下");
  537. floor = orderedTrans[2].GetComponent<transferBehavior>().GetFloor();
  538. space = orderedTrans[2].GetComponent<transferBehavior>().GetSpace();
  539. if (orderedTrans[2].GetComponent<transferBehavior>().state == 0)
  540. {
  541. if (floor != 2 || space != 13)
  542. {
  543. orderedTrans[2].GetComponent<transferBehavior>().SetTarget(2, 13);
  544. }
  545. else
  546. {
  547. // 更新中跑车顺序
  548. GameObject temp = orderedTrans[0];
  549. orderedTrans[0] = orderedTrans[2];
  550. orderedTrans[2] = orderedTrans[1];
  551. orderedTrans[1] = temp;
  552. transState = 0;
  553. }
  554. }
  555. else if (orderedTrans[2].GetComponent<transferBehavior>().state == 4)
  556. {
  557. orderedTrans[2].GetComponent<transferBehavior>().SendCmd(-1);
  558. }
  559. if (elevator_left.GetComponent<elevatorBehavior>().state == 0)
  560. {
  561. if (orderedTrans[2].GetComponent<transferBehavior>().waitForElevator && elevator_left.GetComponent<elevatorBehavior>().load == null)
  562. {
  563. elevator_left.GetComponent<elevatorBehavior>().targetFloor = floor;
  564. }
  565. else if (orderedTrans[2].GetComponent<transferBehavior>().state == 2 && elevator_left.GetComponent<elevatorBehavior>().load != null)
  566. {
  567. elevator_left.GetComponent<elevatorBehavior>().targetFloor = 2;
  568. }
  569. }
  570. break;
  571. default: break;
  572. }
  573. //// 处理停车中
  574. //if (currIndex <= numOfParkTerm - 1 && currTermVar[currIndex] == 1)
  575. //{
  576. //}
  577. //// 处理取车中
  578. //else if(currIndex > numOfParkTerm - 1 && currTermVar[currIndex] == 2)
  579. //{
  580. //}
  581. }
  582. void BasicStateMachine()
  583. {
  584. if (parkSpace == null)
  585. {
  586. parkSpace = new int[165];
  587. for (int i = 1; i < 166; i++)
  588. {
  589. parkSpace[i] = 0;
  590. }
  591. }
  592. switch (state)
  593. {
  594. case 0:
  595. UpdateCmd();
  596. //Debug.Log("更新cmd完成");
  597. if (currTermVar[currIndex] == 1 && parkSpaceIndex > 0)
  598. {
  599. state = 1;
  600. }
  601. else if (currTermVar[currIndex] == 2 && fetchSpaceIndex > 0)
  602. {
  603. state = 4;
  604. }
  605. break;
  606. // 停车控制机械手抓车
  607. case 1:
  608. if (machineArm.GetComponent<machineArmBehavior>().state == 23)
  609. {
  610. state = 2;
  611. }
  612. if (machineArm.GetComponent<machineArmBehavior>().state == 0 && machineArm.GetComponent<machineArmBehavior>().terminalIndex == -1)
  613. {
  614. machineArm.GetComponent<machineArmBehavior>().terminalIndex = currIndex + 1;
  615. }
  616. break;
  617. case 2:
  618. if (machineArm.GetComponent<machineArmBehavior>().state == 14)
  619. {
  620. machineArm.GetComponent<machineArmBehavior>().state = 24;
  621. }
  622. //中跑车运动移至transMove
  623. if (parkUsingTrans != null)
  624. {
  625. int transState = parkUsingTrans.GetComponent<transferBehavior>().state;
  626. int transFloor = parkUsingTrans.GetComponent<transferBehavior>().GetFloor();
  627. int transSpace = parkUsingTrans.GetComponent<transferBehavior>().GetSpace();
  628. ////Debug.Log("curr trans: " + transFloor + ", " + transSpace);
  629. //// 未抵达目标点
  630. //if (transState == 0 && (transFloor != 2 || transSpace != termIndex[currIndex]))
  631. //{
  632. // parkUsingTrans.GetComponent<transferBehavior>().SetTarget(2, termIndex[currIndex]);
  633. //}
  634. //// 等电梯
  635. //else if (parkUsingTrans.GetComponent<transferBehavior>().waitForElevator)
  636. //{
  637. // //Debug.Log(elevator_right.GetComponent<elevatorBehavior>().state);
  638. // //Debug.Log(elevator_right.GetComponent<elevatorBehavior>().load == null);
  639. // if (transSpace < 6 && elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load == null)
  640. // {
  641. // elevator_right.GetComponent<elevatorBehavior>().targetFloor = transFloor;
  642. // }
  643. // else if (transSpace > 10 && elevator_left.GetComponent<elevatorBehavior>().state == 0 && elevator_left.GetComponent<elevatorBehavior>().load == null)
  644. // {
  645. // elevator_left.GetComponent<elevatorBehavior>().targetFloor = transFloor;
  646. // }
  647. //}
  648. //else if (transState == 2)
  649. //{
  650. // if (transSpace < 6 && elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load != null)
  651. // {
  652. // elevator_right.GetComponent<elevatorBehavior>().targetFloor = 2;
  653. // }
  654. // else if (transSpace > 10 && elevator_left.GetComponent<elevatorBehavior>().state == 0 && elevator_left.GetComponent<elevatorBehavior>().load != null)
  655. // {
  656. // elevator_left.GetComponent<elevatorBehavior>().targetFloor = 2;
  657. // }
  658. //}
  659. /*else */
  660. if (transFloor == 2 && transSpace == termIndex[currIndex] && parkUsingTrans.GetComponent<transferBehavior>().load != null)
  661. {
  662. // 中跑车到停车接车位置
  663. state = 3;
  664. }
  665. }
  666. // 寻找空闲中跑车,赋值由transMove完成
  667. else if (parkUsingTrans == null)
  668. {
  669. //int transIndex = GetSpecificTrans(Mathf.FloorToInt((parkSpaceIndex - 1) / 15.0f) + 2);
  670. //Debug.Log("choose trans: " + transIndex);
  671. //if (transIndex != -1)
  672. // usingTrans = transferVehicles[transIndex];
  673. //for (int i = 0; i < transferVehicles.Length; i++)
  674. //{
  675. // if (transferVehicles[i].GetComponent<transferBehavior>().GetFloor() < manDist)
  676. // {
  677. // manDist = transferVehicles[i].GetComponent<transferBehavior>().GetFloor();
  678. // usingTrans = transferVehicles[i];
  679. // }
  680. //}
  681. }
  682. break;
  683. case 3:
  684. if (parkUsingTrans != null)
  685. {
  686. GameObject transLoad = parkUsingTrans.GetComponent<transferBehavior>().load;
  687. int transState = parkUsingTrans.GetComponent<transferBehavior>().state;
  688. int transFloor = parkUsingTrans.GetComponent<transferBehavior>().GetFloor();
  689. int transSpace = parkUsingTrans.GetComponent<transferBehavior>().GetSpace();
  690. int targetFloor = Mathf.FloorToInt((parkSpaceIndex - 1) / 15.0f) + 2;
  691. int targetSpace = parkSpaceIndex - (targetFloor - 2) * 15;
  692. if (transLoad == null && transState == 0)
  693. {
  694. if (machineArm.GetComponent<machineArmBehavior>().state == 14)
  695. {
  696. machineArm.GetComponent<machineArmBehavior>().state = 24;
  697. }
  698. // 停车中跑车空载,空闲且在目标层,对应中跑车1状态
  699. if (transFloor == targetFloor)
  700. {
  701. currTermVar[currIndex] = 0;
  702. termState[currIndex] = 0;
  703. parkSpaceIndex = -1;
  704. parkUsingTrans = null;
  705. state = 0;
  706. //eventGenerator.GetComponent<eventGenerateBehavior>().myState[currIndex]=0;
  707. }
  708. }
  709. //// 中跑车在下方接车点
  710. //if (transLoad != null && transState == 0 && transFloor == 2 && transSpace == termIndex[currIndex])
  711. //{
  712. // if (parkSpaceIndex > 0 && parkSpaceIndex < 166)
  713. // {
  714. // parkUsingTrans.GetComponent<transferBehavior>().SetTarget(targetFloor, targetSpace);
  715. // }
  716. //}
  717. // 电梯由transMove控制
  718. //// 电梯去二楼接
  719. //else if (parkUsingTrans.GetComponent<transferBehavior>().waitForElevator)
  720. //{
  721. // if (/*transSpace < 6 && */elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load == null)
  722. // {
  723. // elevator_right.GetComponent<elevatorBehavior>().targetFloor = 2;
  724. // }
  725. // //else if (transSpace > 10 && elevator_left.GetComponent<elevatorBehavior>().state == 0 && elevator_left.GetComponent<elevatorBehavior>().load == null)
  726. // //{
  727. // // elevator_left.GetComponent<elevatorBehavior>().targetFloor = 2;
  728. // //}
  729. //}
  730. //// 电梯去目标层
  731. //else if (transState == 2)
  732. //{
  733. // if (/*transSpace < 6 &&*/ elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load != null)
  734. // {
  735. // elevator_right.GetComponent<elevatorBehavior>().targetFloor = targetFloor;
  736. // }
  737. // //else if (transSpace > 10 && elevator_left.GetComponent<elevatorBehavior>().state == 0 && elevator_left.GetComponent<elevatorBehavior>().load != null)
  738. // //{
  739. // // elevator_left.GetComponent<elevatorBehavior>().targetFloor = targetFloor;
  740. // //}
  741. //}
  742. //// 中跑车在上方车位,等待送车进车位
  743. //if (transFloor == targetFloor)
  744. //{
  745. // if (transState == 4)
  746. // {
  747. // // 送车
  748. // if (transSpace == targetSpace)
  749. // {
  750. // parkUsingTrans.GetComponent<transferBehavior>().SendCmd(2);
  751. // }
  752. // // 移位状态复位
  753. // else if (transLoad == null)
  754. // {
  755. // parkUsingTrans.GetComponent<transferBehavior>().SendCmd(-1);
  756. // }
  757. // }
  758. // else if (transLoad == null && transState == 0)
  759. // {
  760. // //if (transSpace == targetSpace)
  761. // //{
  762. // if (machineArm.GetComponent<machineArmBehavior>().state == 14)
  763. // {
  764. // machineArm.GetComponent<machineArmBehavior>().state = 24;
  765. // }
  766. // if (transSpace > 2 && transSpace < 14)
  767. // {
  768. // currTermVar[currIndex] = 0;
  769. // termState[currIndex] = 0;
  770. // parkSpaceIndex = -1;
  771. // parkUsingTrans = null;
  772. // state = 0;
  773. // //eventGenerator.GetComponent<eventGenerateBehavior>().myState[currIndex]=0;
  774. // }
  775. // else
  776. // {
  777. // parkUsingTrans.GetComponent<transferBehavior>().SetTarget(transFloor, transSpace <= 3 ? 3 : 13);
  778. // }
  779. // //}
  780. // }
  781. // else if (transState == 3)
  782. // {
  783. // //Debug.Log(targetSpace);
  784. // if (targetSpace < 4 && elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load == null)
  785. // {
  786. // elevator_right.GetComponent<elevatorBehavior>().targetFloor = targetFloor;
  787. // }
  788. // else if (targetSpace > 12 && elevator_left.GetComponent<elevatorBehavior>().state == 0 && elevator_left.GetComponent<elevatorBehavior>().load == null)
  789. // {
  790. // elevator_left.GetComponent<elevatorBehavior>().targetFloor = targetFloor;
  791. // }
  792. // }
  793. //}
  794. }
  795. break;
  796. // 取车
  797. case 4:
  798. break;
  799. case 5:
  800. break;
  801. case 6:
  802. break;
  803. case 7:
  804. break;
  805. default: break;
  806. }
  807. }
  808. /// <summary>
  809. /// 检查停取终端状态,停车终端是否有车等待,取车终端是否有人等待
  810. /// </summary>
  811. void CheckState()
  812. {
  813. int[] waitingForDoors = eventGenerator.GetComponent<eventGenerateBehavior>().waitingForDoors;
  814. GameObject[] objs = eventGenerator.GetComponent<eventGenerateBehavior>().myObject;
  815. doors = eventGenerator.GetComponent<eventGenerateBehavior>().doors;
  816. // 检查门状态并长期上锁
  817. for (int i = 0; i < doors.Length; i++)
  818. {
  819. if (doors[i].GetComponent<doorBehavior>().close && doorLock[i] != 1 && !doors[i].GetComponent<doorBehavior>().moving)
  820. {
  821. doorLock[i] = 1;
  822. }
  823. else if (!doors[i].GetComponent<doorBehavior>().close || doors[i].GetComponent<doorBehavior>().moving)
  824. {
  825. doorLock[i] = 0;
  826. }
  827. }
  828. // 检查停车终端等待开门车辆
  829. for (int i = 0; i < termState.Length; i++)
  830. {
  831. if (termState[i] != 0)
  832. continue;
  833. if (i < numOfParkTerm)
  834. {
  835. if (waitingForDoors[i] == 1)
  836. {
  837. //找机会开门。机械手,扫描板与二楼中跑车不在上空可开门。否则等待或移动空闲设备到旁边
  838. int armState = machineArm.GetComponent<machineArmBehavior>().state;
  839. int armTerm = machineArm.GetComponent<machineArmBehavior>().GetNearestTerm();
  840. if (armTerm == i)
  841. {
  842. if (armState == 0)
  843. {
  844. // 往右移
  845. if (i > 0 && doors[i - 1].GetComponent<doorBehavior>().close && !doors[i - 1].GetComponent<doorBehavior>().moving)
  846. {
  847. float diff = machineArm.transform.position.x - termX[i - 1];
  848. machineArm.GetComponent<machineArmBehavior>().ArmMove(0, diff, 0);
  849. }
  850. else if (i < termState.Length - 1 && doors[i + 1].GetComponent<doorBehavior>().close && !doors[i + 1].GetComponent<doorBehavior>().moving)
  851. {
  852. float diff = machineArm.transform.position.x - termX[i + 1];
  853. machineArm.GetComponent<machineArmBehavior>().ArmMove(0, diff, 0);
  854. }
  855. }
  856. else
  857. {
  858. Debug.Log("机械手位置异常");
  859. }
  860. continue;
  861. }
  862. int boardState = scanBoard.GetComponent<scanBoardBehavior>().state;
  863. int boardTerm = scanBoard.GetComponent<scanBoardBehavior>().GetNearestTerm();
  864. if (boardTerm == i)
  865. {
  866. if (boardState == 0)
  867. {
  868. // 往右移
  869. if (i > 0 && doors[i - 1].GetComponent<doorBehavior>().close && !doors[i - 1].GetComponent<doorBehavior>().moving)
  870. {
  871. float diff = scanBoard.transform.position.x - termX[i - 1];
  872. if (Mathf.Abs(diff) > 0.25f)
  873. {
  874. scanBoard.transform.Translate(Vector3.left * scanBoard.GetComponent<scanBoardBehavior>().speedX * Time.deltaTime);
  875. }
  876. }
  877. else if (i < termState.Length - 1 && doors[i + 1].GetComponent<doorBehavior>().close && !doors[i + 1].GetComponent<doorBehavior>().moving)
  878. {
  879. float diff = scanBoard.transform.position.x - termX[i + 1];
  880. if (Mathf.Abs(diff) > 0.25f)
  881. {
  882. scanBoard.transform.Translate(Vector3.right * scanBoard.GetComponent<scanBoardBehavior>().speedX * Time.deltaTime);
  883. }
  884. }
  885. }
  886. else
  887. {
  888. Debug.Log("扫描板位置异常");
  889. }
  890. continue;
  891. }
  892. bool transCheck = true;
  893. for (int j = 0; j < transferVehicles.Length; j++)
  894. {
  895. int index = transferVehicles[j].GetComponent<transferBehavior>().GetNearestTerm();
  896. int transState = transferVehicles[j].GetComponent<transferBehavior>().state;
  897. // 上方有中跑车
  898. if (i == index)
  899. {
  900. if (transState == 0)
  901. {
  902. // 往右移
  903. if (i > 0 && doors[i - 1].GetComponent<doorBehavior>().close && !doors[i - 1].GetComponent<doorBehavior>().moving)
  904. {
  905. float diff = transferVehicles[j].transform.position.x - termX[i - 1];
  906. transferVehicles[j].GetComponent<transferBehavior>().TransferMove(0, diff, 0);
  907. }
  908. else if (i < termState.Length - 1 && doors[i + 1].GetComponent<doorBehavior>().close && !doors[i + 1].GetComponent<doorBehavior>().moving)
  909. {
  910. float diff = transferVehicles[j].transform.position.x - termX[i + 1];
  911. transferVehicles[j].GetComponent<transferBehavior>().TransferMove(0, diff, 0);
  912. }
  913. }
  914. else
  915. {
  916. Debug.Log("中跑车位置异常");
  917. transCheck &= false;
  918. }
  919. continue;
  920. }
  921. }
  922. if (!transCheck) continue;
  923. // 检查通过后开门
  924. doors[i].GetComponent<doorBehavior>().close = false;
  925. waitingForDoors[i] = 0;
  926. }
  927. // 终端车辆已停好且未处理,关门扫描后发停车指令
  928. else if (objs[i] != null && objs[i].transform.position.z <= -2.9f && termState[i] == 0)
  929. {
  930. if (!doors[i].GetComponent<doorBehavior>().moving)
  931. {
  932. if (!doors[i].GetComponent<doorBehavior>().close)
  933. {
  934. doors[i].GetComponent<doorBehavior>().close = true;
  935. }
  936. else if (scanned[i] == 1)
  937. {
  938. scanned[i] = 0;
  939. termState[i] = 1;
  940. eventGenerator.GetComponent<eventGenerateBehavior>().ClearObject(i + 1, true);
  941. //Debug.Log(i + 1);
  942. }
  943. else if (scanned[i] == 0)
  944. {
  945. if (scanBoard.GetComponent<scanBoardBehavior>().state == 0 && !scanBoard.GetComponent<scanBoardBehavior>().scanned)
  946. {
  947. if (scanBoard.GetComponent<scanBoardBehavior>().GetNearestTerm() < i)
  948. scanBoard.GetComponent<scanBoardBehavior>().targetTerm = i + 2;
  949. else
  950. scanBoard.GetComponent<scanBoardBehavior>().targetTerm = i;
  951. }
  952. else if (scanBoard.GetComponent<scanBoardBehavior>().scanned)
  953. {
  954. scanned[i] = 1;
  955. scanBoard.GetComponent<scanBoardBehavior>().scanned = false;
  956. }
  957. }
  958. }
  959. }
  960. }
  961. // 检查取车终端是否空闲,且有人等待,有则发送取车指令
  962. else if (i >= numOfParkTerm && termState[i] == 0 && objs[i] != null && objs[i].transform.position.z <= 0.5f)
  963. {
  964. if (movingCars != null && movingCars.Count > 0)
  965. {
  966. termState[i] = 2;
  967. eventGenerator.GetComponent<eventGenerateBehavior>().ClearObject(i + 1, true);
  968. }
  969. }
  970. }
  971. }
  972. // Update is called once per frame
  973. void Update()
  974. {
  975. if (initialized)
  976. {
  977. numOfParkTerm = eventGenerator.GetComponent<eventGenerateBehavior>().numOfParkTerm;
  978. if (movingCars == null)
  979. {
  980. movingCars = new List<CarInfo>();
  981. }
  982. try
  983. {
  984. UpdateCmd();
  985. }
  986. catch (System.Exception e) { Debug.Log(e.StackTrace + "\n异常。。。。。\n" + e.Message); }
  987. CheckState();
  988. TransferMove();
  989. //BasicStateMachine();
  990. ////控制频率,30s生成停取指令
  991. //if ((Time.time - prevTime) > cmdInterval)
  992. //{
  993. // //elevator_left.GetComponent<elevatorBehavior>().targetFloor = Random.Range(1, 5);
  994. // int termRndNum = Mathf.FloorToInt(Random.value * 6);
  995. // //Debug.Log(termRndNum);
  996. // if (termState[termRndNum] == 0)
  997. // {
  998. // //if (termRndNum > 2 && movingCars.Count > 0)
  999. // // termState[termRndNum] = 2;
  1000. // if (termRndNum <= 2)
  1001. // termState[termRndNum] = 1;
  1002. // }
  1003. // prevTime = Time.time;
  1004. //}
  1005. }
  1006. }
  1007. }
  1008. public class CarInfo
  1009. {
  1010. public CarInfo(GameObject handle, int floor, int space)
  1011. {
  1012. this.handle = handle;
  1013. this.floor = floor;
  1014. this.space = space;
  1015. pos = new Vector3((space - 1) * 35.0f / 14.0f - 6.3f, (floor - 1) * 3.0f, -7.9f);
  1016. }
  1017. public CarInfo(GameObject handle, Vector3 pos)
  1018. {
  1019. this.handle = handle;
  1020. this.pos = pos;
  1021. floor = Mathf.RoundToInt(pos.y / 3.0f + 1);
  1022. space = Mathf.RoundToInt((pos.x + 6.3f) * 15.0f / 35.0f + 1);
  1023. }
  1024. public override bool Equals(object obj)
  1025. {
  1026. if (obj == null) return false;
  1027. //return rndValue == ((CarInfo)obj).rndValue;
  1028. //Debug.Log(handle);
  1029. //Debug.Log(((CarInfo)obj).handle);
  1030. return handle.Equals(((CarInfo)obj).handle);
  1031. }
  1032. public override int GetHashCode()
  1033. {
  1034. return handle.GetHashCode();
  1035. }
  1036. public GameObject handle;
  1037. public int floor;
  1038. public int space;
  1039. public Vector3 pos;
  1040. public int state; // 0 idle, 1 attached, 2 in space
  1041. }