scanBoardBehavior.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class scanBoardBehavior : MonoBehaviour
  5. {
  6. public GameObject ctrlr;
  7. public GameObject arm;
  8. public int targetTerm;
  9. /// <summary>
  10. /// 待controller清除
  11. /// </summary>
  12. public bool scanned;
  13. public bool avoiding;
  14. public float speedX;
  15. // 0空闲 1检查门状态加锁 2运动
  16. public int state;
  17. private static float[] termPosX = { -1.5f, 1.2f, 6.2f, 8.7f, 13.7f, 16.2f, 21.2f, 24f };
  18. /// <summary>
  19. /// 获取当前位置终端号,1-6
  20. /// </summary>
  21. /// <returns></returns>
  22. public int GetPos()
  23. {
  24. float dist = 99;
  25. int index = -1;
  26. for (int i = 0; i < termPosX.Length; i++)
  27. {
  28. if(Mathf.Abs(transform.position.x - termPosX[i]) < dist)
  29. {
  30. dist = Mathf.Abs(transform.position.x - termPosX[i]);
  31. index = i;
  32. }
  33. }
  34. return index;
  35. }
  36. /// <summary>
  37. /// 获取离扫描板最近终端编号0-5
  38. /// </summary>
  39. /// <returns></returns>
  40. public int GetNearestTerm()
  41. {
  42. // 已靠边
  43. if (transform.position.x < -1 || transform.position.x > 23)
  44. {
  45. return -1;
  46. }
  47. else
  48. {
  49. float diff = 99f;
  50. int index = -1;
  51. for (int i = 1; i < termPosX.Length-1; i++)
  52. {
  53. if (diff > Mathf.Abs(transform.position.x - termPosX[i]))
  54. {
  55. diff = Mathf.Abs(transform.position.x - termPosX[i]);
  56. index = i;
  57. }
  58. }
  59. return index-1;
  60. }
  61. }
  62. public void BoardMove(float diff, int toState)
  63. {
  64. if (diff > 0)
  65. {
  66. for (int i = termPosX.Length - 2; i >= 1; i--)
  67. {
  68. // 找到其右边第一个终端
  69. if (transform.position.x > termPosX[i])
  70. {
  71. // 未上锁等待
  72. if (ctrlr.GetComponent<controller>().doorLock[i - 1] != 1)
  73. {
  74. Debug.Log("扫描板等待锁门");
  75. return;
  76. }
  77. }
  78. }
  79. //diff = transform.position.x - termPosX[0];
  80. transform.Translate(Vector3.left * speedX * Time.deltaTime);
  81. }
  82. else
  83. {
  84. for (int i = 1; i < termPosX.Length - 1; i++)
  85. {
  86. // 找到其左边第一个终端
  87. if (transform.position.x < termPosX[i])
  88. {
  89. // 未上锁等待
  90. if (ctrlr.GetComponent<controller>().doorLock[i - 1] != 1)
  91. {
  92. Debug.Log("扫描板等待锁门");
  93. return;
  94. }
  95. }
  96. }
  97. //diff = transform.position.x - termPosX[termPosX.Length-1];
  98. transform.Translate(Vector3.right * speedX * Time.deltaTime);
  99. }
  100. if (Mathf.Abs(diff) < speedX * 0.1f)
  101. {
  102. state = toState;
  103. }
  104. }
  105. // Start is called before the first frame update
  106. void Start()
  107. {
  108. targetTerm = -1;
  109. speedX = 6f;
  110. state = 0;
  111. scanned = false;
  112. avoiding = false;
  113. }
  114. // Update is called once per frame
  115. void Update()
  116. {
  117. if (ctrlr.GetComponent<controller>().doors.Length != 6)
  118. return;
  119. // 与机械手存在碰撞风险,暂停扫描工作,专心避让
  120. if(arm.GetComponent<machineArmBehavior>().state == 11 || arm.GetComponent<machineArmBehavior>().state == 12 || arm.GetComponent<machineArmBehavior>().state == 13 ||
  121. arm.GetComponent<machineArmBehavior>().state == 26 || arm.GetComponent<machineArmBehavior>().state == 17 || arm.GetComponent<machineArmBehavior>().state == 18)
  122. {
  123. if (Mathf.Abs(transform.position.x - arm.GetComponent<machineArmBehavior>().transform.position.x) < 4f)
  124. {
  125. int term = GetNearestTerm();
  126. float diff = 0;
  127. if (term != -1)
  128. {
  129. // 往右
  130. diff = transform.position.x - termPosX[term];
  131. }
  132. BoardMove(diff, state);
  133. avoiding = true;
  134. return;
  135. }
  136. else
  137. {
  138. avoiding = false;
  139. }
  140. }
  141. switch (state)
  142. {
  143. case 0:
  144. if (targetTerm == -1)
  145. {
  146. //if (Mathf.Abs(transform.position.x - termPosX[0]) < Mathf.Abs(transform.position.x - termPosX[termPosX.Length - 1]))
  147. //{
  148. // //diff = transform.position.x - termPosX[0];
  149. // //if (diff < speedX * 0.05f)
  150. // //{
  151. // transform.Translate(Vector3.left * speedX * Time.deltaTime);
  152. // //}
  153. //}
  154. //else
  155. //{
  156. // //diff =
  157. // transform.Translate(Vector3.right * speedX * Time.deltaTime);
  158. //}
  159. }
  160. else
  161. {
  162. state = 1;
  163. }
  164. break;
  165. case 1:
  166. float diff = transform.position.x - termPosX[targetTerm];
  167. int occupiedPlace = -1;
  168. // 限定扫描范围
  169. for (int i = 0; i < termPosX.Length; i++)
  170. {
  171. if (diff > 0)
  172. {
  173. if (transform.position.x <= (termPosX[i] + 0.25f))
  174. {
  175. occupiedPlace = i; break;
  176. }
  177. }
  178. else
  179. {
  180. if (transform.position.x > (termPosX[i] - 0.25f))
  181. {
  182. occupiedPlace = i;
  183. }
  184. }
  185. }
  186. if (occupiedPlace < 1) occupiedPlace = 1;
  187. if (occupiedPlace > 6) occupiedPlace = 6;
  188. int targetClamp = targetTerm;
  189. targetClamp = targetClamp < 1 ? 1 : targetClamp;
  190. targetClamp = targetClamp > 6 ? 6 : targetClamp;
  191. // 等待关门,门上锁,检查上锁情况
  192. if (diff > 0)
  193. {
  194. for (int i = targetClamp; i <= occupiedPlace; i++)
  195. {
  196. // 未关门,停止动作等待
  197. if (!ctrlr.GetComponent<controller>().doors[i - 1].GetComponent<doorBehavior>().close) return;
  198. //else if (ctrlr.GetComponent<controller>().doorLock[i - 1] == 0)
  199. // ctrlr.GetComponent<controller>().doorLock[i - 1] = 1;
  200. }
  201. for (int i = targetClamp; i <= occupiedPlace; i++)
  202. {
  203. if (!ctrlr.GetComponent<controller>().doors[i - 1].GetComponent<doorBehavior>().close || !(ctrlr.GetComponent<controller>().doorLock[i - 1] == 1)) return;
  204. }
  205. }
  206. else
  207. {
  208. for (int i = occupiedPlace; i <= targetClamp; i++)
  209. {
  210. // 未关门,停止动作等待
  211. if (!ctrlr.GetComponent<controller>().doors[i - 1].GetComponent<doorBehavior>().close) return;
  212. //else if (ctrlr.GetComponent<controller>().doorLock[i - 1] == 0)
  213. // ctrlr.GetComponent<controller>().doorLock[i - 1] = 1;
  214. }
  215. for (int i = occupiedPlace; i <= targetClamp; i++)
  216. {
  217. if (!ctrlr.GetComponent<controller>().doors[i - 1].GetComponent<doorBehavior>().close || !(ctrlr.GetComponent<controller>().doorLock[i - 1] == 1)) return;
  218. }
  219. }
  220. // 等待机械手运动结束
  221. int armState = arm.GetComponent<machineArmBehavior>().state;
  222. if (armState == 0)
  223. {
  224. state = 2;
  225. }
  226. break;
  227. case 2:
  228. diff = transform.position.x - termPosX[targetTerm];
  229. BoardMove(diff,3);
  230. break;
  231. case 3:
  232. targetTerm = -1;
  233. state = 0;
  234. scanned = true;
  235. break;
  236. default: break;
  237. }
  238. }
  239. }