plan.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. #include <rtthread.h>
  2. #include "main.h"
  3. #include "flash.h"
  4. #include "buffer.h"
  5. #include "button.h"
  6. #include "usart.h"
  7. #include <stdlib.h>
  8. #include "weight.h"
  9. #include "display.h"
  10. #include "plan.h"
  11. #include "xBeeAppEscapingMode.h"
  12. union frameinit_t dataframehead = {0x7E,0x00,0x01,0x01};
  13. unsigned char WeightConst_product[40] = {0xD2,0xBB,0xBA,0xC5,0xC5,0xA3,0xC9,0xE1,0x20,0x20}; //产品名称
  14. union plan_comp_t plan_comp = {0,0,0,0,0,0};
  15. union pf_comp_t pf_comp = {0,0,0,0,0};
  16. unsigned char WeightConst_Product_JS=0; //需要计算
  17. unsigned char WeightConst_Product_Run=0; //开始运行
  18. unsigned short WeightConst_Product_Run_time=0; //开始运行时间
  19. unsigned char WeightConst_Run=0; //整体开始运行
  20. unsigned char WeightConst_Stop = 0;
  21. unsigned char WeightConst_PlantempList[40]; //计划临时表
  22. unsigned char WeightConst_Nextfeed[40]; //临时表
  23. signed short WeightConst_NextWeightTarget; //目标重量
  24. unsigned char WeightConst_feed[40]; //原料名称
  25. signed short WeightConst_WeightTargetTemp; //目标重量
  26. signed short WeightConst_WeightTarget; //目标重量
  27. unsigned char CompleteTarget_order = 0xFD; //已完成的顺序
  28. signed short WeightConst_WeightTargetAllowMin; //目标重量的允许跳转范围
  29. signed short WeightConst_WeightTargetAllowMax; //目标重量的允许跳转范围
  30. unsigned char WeightConst_WeightHaveBegin = 0; //已经确认开始
  31. unsigned short WeightConst_DELAYTIME;
  32. unsigned short WeightConst_DELAYON = 0;
  33. unsigned char XbeeConst_lastsort; //保存刚刚收到的顺序号
  34. unsigned char XbeeConst_lastinorout; //保存刚刚收到的发料或加料
  35. unsigned char XbeeConst_FrameNum = 0; //帧编号
  36. unsigned char XbeeConst_CanSend=1;
  37. unsigned char getPlaning=0;
  38. static unsigned char XbeeConst_needRetry=0; //等待秒数
  39. rt_sem_t pop_product_sem = RT_NULL;
  40. rt_sem_t delay_key_sem = RT_NULL;
  41. unsigned char delayKeying = 0;
  42. //定义时间
  43. union TimeConst_t TimeConst={
  44. .data.year = 1,
  45. .data.month = 0,
  46. .data.date = 32,
  47. .data.hh = 0,
  48. .data.mm = 0,
  49. .data.ss = 0
  50. };
  51. static rt_thread_t tdelay_key_id = RT_NULL;
  52. static void delay_key_entry(void *parameter)
  53. {
  54. while(1)
  55. {
  56. rt_sem_take(delay_key_sem, RT_WAITING_FOREVER); //等待
  57. delayKeying =1;
  58. rt_thread_mdelay(3000);
  59. delayKeying =0;
  60. }
  61. }
  62. void delay_key_thread(void)
  63. {
  64. tdelay_key_id = rt_thread_create("delay_key",
  65. delay_key_entry, RT_NULL,
  66. 0x100, 6, 5);
  67. /* 如果获得线程控制块,启动这个线程 */
  68. if (tdelay_key_id != RT_NULL) rt_thread_startup(tdelay_key_id);
  69. }
  70. void setSendFrame_t(unsigned char Broadcast,unsigned char Radius,unsigned char TO) {
  71. userSendFrame_t.frameHead = 0x7E;//帧头
  72. userSendFrame_t.frameLen = 0;//数据长度Number of bytes between length and checksum fields.
  73. userSendFrame_t.frameType = 0x10;//帧类型,Transmit Request
  74. userSendFrame_t.frameID = 0x01;
  75. if (Broadcast == 1) {
  76. userSendFrame_t.des64DeviceAdd[0] = 0x00;//器件ID 高
  77. userSendFrame_t.des64DeviceAdd[1] = 0x00;//器件ID |
  78. userSendFrame_t.des64DeviceAdd[2] = 0x00;//器件ID |
  79. userSendFrame_t.des64DeviceAdd[3] = 0x00;//器件ID |
  80. userSendFrame_t.des64DeviceAdd[4] = 0x00;//器件ID |
  81. userSendFrame_t.des64DeviceAdd[5] = 0x00;//器件ID |
  82. userSendFrame_t.des64DeviceAdd[6] = 0xFF;//器件ID |
  83. userSendFrame_t.des64DeviceAdd[7] = 0xFF;//器件ID 低
  84. }
  85. else if (Broadcast == 2) {
  86. for (int i = 0; i < 8; i++)
  87. userSendFrame_t.des64DeviceAdd[i] = XbeeConst_CCAddress[i];
  88. }
  89. else {
  90. for (int i = 0; i < 8; i++)
  91. userSendFrame_t.des64DeviceAdd[i] = XbeeConst_CenterAddress[i];
  92. }
  93. userSendFrame_t.des16NetAdd = 0xFFFE ;//网络ID
  94. userSendFrame_t.broadcastRadius = Radius;//广播半径
  95. userSendFrame_t.transmitOptions = TO;
  96. }
  97. rt_sem_t setTIME_lock = RT_NULL;
  98. static void setTIME_entry(void *parameter) {
  99. while (1) {
  100. //定义时间
  101. rt_sem_take(setTIME_lock, RT_WAITING_FOREVER);
  102. if ((TimeConst.data.date == 29) && (TimeConst.data.month == 2) && (TimeConst.data.year % 4)>0 ) {
  103. TimeConst.data.date = 1;
  104. TimeConst.data.month ++;
  105. }
  106. else if ((TimeConst.data.date == 30) && (TimeConst.data.month == 2) && (TimeConst.data.year % 4) == 0 ) {
  107. TimeConst.data.date = 1;
  108. TimeConst.data.month ++;
  109. }
  110. else if ((TimeConst.data.date == 31) && ((TimeConst.data.month == 4) || (TimeConst.data.month == 6) ||
  111. (TimeConst.data.month == 9) || (TimeConst.data.month == 11)) ) {
  112. TimeConst.data.date = 1;
  113. TimeConst.data.month ++;
  114. }
  115. else if (TimeConst.data.date == 32) {
  116. TimeConst.data.date = 1;
  117. TimeConst.data.month ++;
  118. }
  119. if (TimeConst.data.month == 13) {
  120. TimeConst.data.month = 1;
  121. TimeConst.data.year ++;
  122. }
  123. TimeConst.data.ss++;
  124. if (TimeConst.data.ss==60) {
  125. TimeConst.data.ss = 0;
  126. TimeConst.data.mm ++;
  127. }
  128. if (TimeConst.data.mm==60) {
  129. TimeConst.data.mm = 0;
  130. TimeConst.data.hh ++;
  131. }
  132. if (TimeConst.data.hh==24) {
  133. TimeConst.data.hh = 0;
  134. TimeConst.data.date ++;
  135. }
  136. // if (TimeConst.data.year==1) sendgetTime();
  137. rt_sem_release(setTIME_lock);
  138. rt_thread_mdelay(998);
  139. }
  140. }
  141. static rt_thread_t tTimeid = RT_NULL;
  142. void setTIME_thread(void)
  143. {
  144. tTimeid = rt_thread_create("Timer", setTIME_entry, RT_NULL, 0x200, 2, 5);
  145. /* 如果获得线程控制块,启动这个线程 */
  146. if (tTimeid != RT_NULL) rt_thread_startup(tTimeid);
  147. }
  148. extern unsigned char iscowweight;
  149. void setWeightConst_initProduct(void) {
  150. rt_memset(WeightConst_product,0x20,40);
  151. rt_memset(WeightConst_feed,0x20,40);
  152. rt_memset(WeightConst_Nextfeed,0x20,40);
  153. if (WeightConst_SBType==1||WeightConst_SBType==6) {
  154. if (iscowweight==1)
  155. {
  156. WeightConst_feed[0]=0xCE;
  157. WeightConst_feed[1]=0xDE;
  158. WeightConst_feed[2]=0xB6;
  159. WeightConst_feed[3]=0xFA;
  160. WeightConst_feed[4]=0xB1;
  161. WeightConst_feed[5]=0xEA;
  162. WeightConst_feed[6]=0x20;
  163. WeightConst_feed[7]=0x20;
  164. }
  165. else if (!isWeight)
  166. {
  167. WeightConst_feed[0]=0xC7;
  168. WeightConst_feed[1]=0xEB;
  169. WeightConst_feed[2]=0xC8;
  170. WeightConst_feed[3]=0xA1;
  171. WeightConst_feed[4]=0xBC;
  172. WeightConst_feed[5]=0xC6;
  173. WeightConst_feed[6]=0xBB;
  174. WeightConst_feed[7]=0xAE;
  175. }
  176. else
  177. {
  178. WeightConst_feed[0]=0xD6;
  179. WeightConst_feed[1]=0xD8;
  180. WeightConst_feed[2]=0xC1;
  181. WeightConst_feed[3]=0xBF;
  182. WeightConst_feed[4]=0x20;
  183. WeightConst_feed[5]=0x20;
  184. WeightConst_feed[6]=0x20;
  185. WeightConst_feed[7]=0x20;
  186. }
  187. }
  188. else if (WeightConst_SBType==3) {
  189. WeightConst_feed[0]=0xC7;
  190. WeightConst_feed[1]=0xEB;
  191. WeightConst_feed[2]=0xC8;
  192. WeightConst_feed[3]=0xA1;
  193. WeightConst_feed[4]=0xBC;
  194. WeightConst_feed[5]=0xC6;
  195. WeightConst_feed[6]=0xBB;
  196. WeightConst_feed[7]=0xAE;
  197. }
  198. plan_comp.data.incount = 0; //原料数量1
  199. plan_comp.data.outcount = 0; //投喂数量1
  200. plan_comp.data.reget = 0; //计划是否是重取
  201. plan_comp.data.swpos = 0; //目标总量小数位
  202. plan_comp.data.sumweight = 0; //目标总量
  203. plan_comp.data.shifts = 0; //班次
  204. WeightConst_WeightTarget = 0; //目标总量
  205. pf_comp.data.addorout = 0; //加料发料
  206. // WeightConst_WeightTarget_ZF = 0; //目标总量正负
  207. pf_comp.data.tw_pos = 0; //目标总量小数位
  208. pf_comp.data.az_pos = 0; //误差小数位
  209. pf_comp.data.autosecond = 0; //目标重量的允许跳转时间 1- 3秒 2- 6秒 3- 9秒
  210. pf_comp.data.autozone = 0; ; //目标重量的允许跳转范围
  211. pf_comp.data.needBegin = 0;
  212. pf_comp.data.pfsort = 0xFE; //顺序
  213. pf_comp.data.delaytime = 0;
  214. pf_comp.data.JMP = 0;
  215. WeightConst_Product_Run = 0;
  216. XbeeConst_lastsort = 0;
  217. XbeeConst_lastinorout = 0;
  218. if (WeightConst_SBType>2 && WeightConst_SBType<7) {
  219. rt_event_send(&display_event, EVENT_clearall);
  220. rt_event_send(&display_event, EVENT_Current_noweight);
  221. }
  222. }
  223. void pop_product_entry(void *parameter) {
  224. while(1)
  225. {
  226. rt_sem_take(pop_product_sem, RT_WAITING_FOREVER); //等待串口消息
  227. // rt_sem_init(pop_product_sem,"pop_product_sem", 0, RT_IPC_FLAG_FIFO);
  228. for (int CheckWeight_i = 0; CheckWeight_i < 30; CheckWeight_i++) {
  229. WeightConst_AutoSingleNSecondNum[CheckWeight_i][0]=0;
  230. WeightConst_AutoSingleNSecondNum[CheckWeight_i][1]=0;
  231. WeightConst_AutoSingleNSecondNum[CheckWeight_i][2]=0;
  232. }
  233. while(1){
  234. WeightConst_WeightHaveBegin=0;
  235. if (tsdb_recordcount('p')>0) {
  236. findplan();
  237. unsigned char setWeightConst_length=0;
  238. setWeightConst_length = WeightConst_PlantempList[1];
  239. // rt_kprintf("pop:");
  240. // for(int i=0;i<setWeightConst_length;i++)
  241. // rt_kprintf("%02X ",WeightConst_PlantempList[i]);
  242. // rt_kprintf("\n");
  243. WeightConst_Run = 1;
  244. if (WeightConst_PlantempList[3]==0x12) {
  245. save_read_ts('p');
  246. // rt_kprintf("pop 0x12 \n");
  247. rt_sem_take(display_lock, RT_WAITING_FOREVER);
  248. setWeightConst_initProduct();
  249. rt_memset(WeightConst_product,0x20,40);
  250. rt_memset(WeightConst_feed,0x20,40);
  251. for (int i = 0; i <= setWeightConst_length-10; i++) {
  252. WeightConst_product[i] = WeightConst_PlantempList[i+4]; //产品名称
  253. if (WeightConst_SBType==1||WeightConst_SBType==6) {
  254. WeightConst_feed[i] = WeightConst_product[i]; //显示一秒牛舍
  255. }
  256. }
  257. plan_comp.Buffer[0] = WeightConst_PlantempList[setWeightConst_length-5+1]; //产品名称
  258. plan_comp.Buffer[1] = WeightConst_PlantempList[setWeightConst_length-5]; //产品名称
  259. for (int i = 2; i < 6; i++) {
  260. plan_comp.Buffer[i] = WeightConst_PlantempList[setWeightConst_length-5+i]; //产品名称
  261. }
  262. WeightConst_Product_Run_time = (plan_comp.data.incount<<8) || plan_comp.data.outcount;
  263. pf_comp.data.pfsort = 0xFD;
  264. WeightConst_Stop = 0;
  265. if (WeightConst_SBType>2 && WeightConst_SBType<7) {
  266. rt_event_send(&display_event, EVENT_PlanName);
  267. }
  268. rt_sem_release(display_lock);
  269. rt_thread_mdelay(1000);
  270. }
  271. else if (WeightConst_PlantempList[3]==0x15) {
  272. rt_sem_take(display_lock, RT_WAITING_FOREVER);
  273. rt_memset(WeightConst_feed,0x20,40);
  274. for (int i = 0; i <= setWeightConst_length-11; i++)
  275. WeightConst_feed[i] = WeightConst_PlantempList[i+4]; //饲料名称
  276. WeightConst_WeightTarget = WeightConst_PlantempList[setWeightConst_length-6]<<8 ; //目标总量
  277. WeightConst_WeightTarget = WeightConst_WeightTarget | WeightConst_PlantempList[setWeightConst_length-5];
  278. WeightConst_WeightTargetTemp = WeightConst_WeightTarget;
  279. for (int i = 0; i < 5; i++)
  280. pf_comp.Buffer[i] = WeightConst_PlantempList[setWeightConst_length-4 + i];
  281. // rt_kprintf("pop 15 pfsort:%02X \n",pf_comp.data.pfsort);
  282. if ( pf_comp.data.tW_minus ==1)
  283. WeightConst_WeightTarget = WeightConst_WeightTarget * -1; //目标总量正
  284. WeightConst_Product_Run = (pf_comp.data.pfsort<253);
  285. WeightConst_DELAYTIME = 0;
  286. WeightConst_DELAYON = 0;
  287. CanButton = 0x01;
  288. WeightConst_oksum=0;
  289. WeightConst_WeightOK = 0;
  290. if (pf_comp.data.firstReget) {
  291. pf_comp.data.needBegin = 0;
  292. plan_comp.data.reget = 0;
  293. WeightConst_Product_JS = 0;
  294. }
  295. else WeightConst_Product_JS = 1;
  296. if (pf_comp.data.needBegin == 1 && isTag==0 && pf_comp.data.addorout==1)
  297. {
  298. CompleteTarget_order = pf_comp.data.pfsort;
  299. write_Flash("completeorder", &CompleteTarget_order, sizeof(CompleteTarget_order));
  300. }
  301. if (pf_comp.data.needBegin == 0 || CompleteTarget_order == pf_comp.data.pfsort
  302. || (pf_comp.data.needBegin == 1 && isTag==0 && pf_comp.data.addorout==1))
  303. getTargetWeight();
  304. rt_sem_release(display_lock);
  305. if (pf_comp.data.pfsort>=253)
  306. {
  307. reset_read_ts('p');
  308. // setWeightConst_initProduct();
  309. }
  310. // if (pf_comp.data.needBegin == 1 && isTag==1 && WeightConst_WeightTarget_addorout==1)
  311. // ReadTag(1);
  312. if (WeightConst_SBType>2 && WeightConst_SBType<7) {
  313. // if (WeightConst_WeightTarget_order==0xFF && WeightConst_SBType==4)
  314. // checkfinish=1;
  315. if (WeightConst_Product_Run)
  316. {
  317. if (plan_comp.data.reget>0)
  318. rt_event_send(&display_event, EVENT_Current_noweight);
  319. else
  320. rt_event_send(&display_event, EVENT_Current);
  321. }
  322. else
  323. {
  324. rt_event_send(&display_event, EVENT_clearall);
  325. rt_event_send(&display_event, EVENT_Current_noweight);
  326. }
  327. if (findNextplan()>0)
  328. rt_event_send(&display_event, EVENT_Next);
  329. }
  330. break;
  331. }
  332. }
  333. rt_thread_mdelay(10);
  334. }
  335. }
  336. }
  337. void sendgetTime(void) { //向上位机取时间
  338. // key8 协议(取时间):0x7E 0x04 0x01 0xF8 0x01 0x01
  339. // 1位起始位 + 1位长度 + 1位源地址 + 1位命令 + 1帧编号 + 1校验
  340. unsigned char buf[6] ;
  341. unsigned char CRCNum=0;
  342. buf[0]= 0x7E;
  343. buf[1]= 0x04;
  344. buf[2]= XbeeConst_DeviceAddress;
  345. buf[3]= 0xF8;
  346. buf[4] = XbeeConst_FrameNum++;
  347. for (int i=0; i< 5; i++) CRCNum+=buf[i];
  348. buf[5] = 0xff - CRCNum;
  349. if(READ_BIT(RCC->CSR, RCC_CSR_IWDGRSTF) == RESET) //看门狗重启不需要获取时间
  350. {
  351. ts_pushArrary(&ts_keydb, buf, buf[1]+2);
  352. }
  353. if (tsdb_recordcount('p') > 0) {
  354. findF12();
  355. rt_event_send(&display_event, EVENT_PlanName);
  356. rt_thread_mdelay(1000);
  357. rt_sem_release(pop_product_sem);
  358. }
  359. __HAL_RCC_CLEAR_RESET_FLAGS();
  360. }
  361. void sendkey2(int BtnType) {
  362. /* key2 协议(上报2号按键):0x7E 0x0B 0x01 0xF2 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x01
  363. * 1位起始位 + 1位长度 + 1位源地址 + 1位命令 + 7位重量 + 1上次接收顺序号 + 1帧上次inorout + 1帧编号 + 1校验
  364. */
  365. unsigned char buf[17];
  366. unsigned char CRCNum;
  367. union timekeyweight_t xBytes;
  368. CRCNum = 0;
  369. xBytes = getTimeWeight(2);
  370. buf[0]= 0x7E;
  371. buf[1]= 0x0F;
  372. buf[2]= XbeeConst_DeviceAddress;
  373. buf[3]= BtnType;
  374. for (int i=9; i >0; i--) buf[i+3]= xBytes.Buffer[9-i];
  375. if (BtnType==0xF4) {
  376. beep();
  377. XbeeConst_lastsort=0xFD;
  378. buf[13] = 0x00; //上次接收到的顺序号
  379. buf[14] = 0x00; //上次接收到的顺序号
  380. }
  381. else {
  382. buf[13] = XbeeConst_lastsort; //上次接收到的顺序号
  383. buf[14] = XbeeConst_lastinorout; //上次接收到的顺序号
  384. }
  385. buf[15] = XbeeConst_FrameNum ++;
  386. for (int i=0; i< 16; i++) CRCNum+=buf[i];
  387. buf[16] = 0xff - CRCNum;
  388. ts_pushArrary(&ts_keydb, buf, buf[1]+2);
  389. if (BtnType==0xF4) {
  390. // WeightConst_Product_JS = 0;
  391. getPlaning = 1;
  392. rt_sem_take(weight_lock, RT_WAITING_FOREVER);
  393. WeightConst_WeightBegin = WeightConst_WeightCur;
  394. rt_sem_release(weight_lock);
  395. rt_sem_take(setTIME_lock, RT_WAITING_FOREVER);
  396. write_Flash("begintime", &TimeConst, sizeof(TimeConst));
  397. rt_sem_release(setTIME_lock);
  398. write_Flash("beginweight", &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin));
  399. write_Flash("TimeConst", &TimeConst, sizeof(TimeConst));
  400. }
  401. }
  402. void sendkey1(int BtnType, int remote) {
  403. /* key1 协议(上报1号按键):0x7E 0x0B 0x01 0xF1 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x01 0x01
  404. * 1位起始位 + 1位长度 + 1位源地址 + 1位命令 + 7位重量 + 1位任务顺序号 + 1帧编号 + 1校验
  405. */
  406. unsigned char buf[17];
  407. unsigned char CRCNum;
  408. union timekeyweight_t xBytes;
  409. CRCNum = 0;
  410. xBytes = getTimeWeight(BtnType);
  411. buf[0]= 0x7E;
  412. buf[1]= 0x0F;
  413. if (isCC)
  414. buf[2]= ((remote<< 4) & 0x0f) | (XbeeConst_DeviceAddress& 0x0f);
  415. else
  416. buf[2]= XbeeConst_DeviceAddress & 0x0f;
  417. buf[3]= BtnType;
  418. for (int i=9; i >0; i--) buf[i+3]= xBytes.Buffer[9-i];
  419. buf[13]= pf_comp.data.pfsort; //应该附上当前计划的当前料的顺序号
  420. buf[14]= 0x00; //应该附上当前计划的当前料的顺序号
  421. buf[15] = XbeeConst_FrameNum ++;
  422. for (int i=0; i< 16; i++)
  423. CRCNum+=buf[i];
  424. buf[16]= 0xff - CRCNum;
  425. ts_pushArrary(&ts_keydb, buf, buf[1]+2);
  426. CompleteTarget_order = pf_comp.data.pfsort;
  427. rt_sem_take(setTIME_lock, RT_WAITING_FOREVER);
  428. write_Flash("TimeConst", &TimeConst, sizeof(TimeConst));
  429. rt_sem_release(setTIME_lock);
  430. save_read_ts('p');
  431. beep();
  432. alarm_off();
  433. if (pf_comp.data.pfsort == 0xFE) {
  434. rt_sem_take(display_lock, RT_WAITING_FOREVER);
  435. setWeightConst_initProduct();
  436. WeightConst_Product_Run_time=0;
  437. rt_sem_release(display_lock);
  438. }
  439. else
  440. {
  441. WeightConst_Product_Run_time++;
  442. rt_sem_take(weight_lock, RT_WAITING_FOREVER);
  443. WeightConst_WeightBegin = WeightConst_WeightCur;
  444. rt_sem_release(weight_lock);
  445. rt_sem_take(setTIME_lock, RT_WAITING_FOREVER);
  446. write_Flash("begintime", &TimeConst, sizeof(TimeConst));
  447. rt_sem_release(setTIME_lock);
  448. write_Flash("beginweight", &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin));
  449. if (pf_comp.data.delaytime)
  450. WeightConst_DELAYON = 1;
  451. else
  452. {
  453. // WeightConst_Product_JS = 1;
  454. rt_sem_release(pop_product_sem); //弹出最新内容
  455. WeightConst_DELAYON = 0;
  456. }
  457. }
  458. }
  459. void getTargetWeight(void)
  460. {
  461. WeightConst_WeightHaveBegin = 1;
  462. if (pf_comp.data.tw_pos > WeightConst_WeightWS) { // 把目标的小数点转换成当前称的小数点位
  463. WeightConst_WeightTarget = WeightConst_WeightTarget/mypow(10,pf_comp.data.tw_pos - WeightConst_WeightWS);
  464. pf_comp.data.tw_pos = WeightConst_WeightWS;
  465. }
  466. else if (pf_comp.data.tw_pos < WeightConst_WeightWS) {
  467. WeightConst_WeightTarget = WeightConst_WeightTarget*mypow(10,WeightConst_WeightWS - pf_comp.data.tw_pos);
  468. pf_comp.data.tw_pos = WeightConst_WeightWS;
  469. }
  470. // WeightConst_WeightBegin = WeightConst_WeightCur;
  471. if (WeightConst_Product_JS == 1) { //这段计算出实际目标重量
  472. rt_sem_take(weight_lock, RT_WAITING_FOREVER);
  473. if (WeightConst_WeightBegin == -30000)
  474. WeightConst_WeightBegin = WeightConst_WeightCur;
  475. rt_sem_release(weight_lock);
  476. if (pf_comp.data.addorout == 0)
  477. WeightConst_WeightTarget = WeightConst_WeightBegin + WeightConst_WeightTarget;
  478. else
  479. WeightConst_WeightTarget = WeightConst_WeightBegin - WeightConst_WeightTarget;
  480. }
  481. else
  482. WeightConst_WeightBegin=-30000;
  483. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  484. if (pf_comp.data.az_pos > pf_comp.data.tw_pos) { // 把目标误差的小数点转换成当前称的小数点位
  485. pf_comp.data.autozone = pf_comp.data.autozone/mypow(10,pf_comp.data.az_pos-pf_comp.data.tw_pos);
  486. pf_comp.data.az_pos = pf_comp.data.tw_pos;
  487. }
  488. else if (pf_comp.data.az_pos < pf_comp.data.tw_pos) {
  489. pf_comp.data.autozone = pf_comp.data.autozone*mypow(10,pf_comp.data.tw_pos-pf_comp.data.az_pos);
  490. pf_comp.data.az_pos = pf_comp.data.tw_pos;
  491. }
  492. WeightConst_WeightTargetAllowMin = WeightConst_WeightTarget - pf_comp.data.autozone; //目标重量的允许跳转范围
  493. WeightConst_WeightTargetAllowMax = WeightConst_WeightTarget + pf_comp.data.autozone;
  494. WeightConst_AutoSingleNSecondPoint=0;
  495. WeightConst_oksum = 0;
  496. WeightConst_allsum = 0;
  497. // if (isTag==1)
  498. // {
  499. // Rec_watchTag_point=0; //接收标签
  500. // ReadTag_state=0; //读标签状态 0 没读或已读成功,1 开始读 ,2 正在读
  501. // WriteTag_state=0; //写标签状态 0 没写或已写成功,1 开始写 ,2 正在写
  502. // }
  503. //
  504. if (WeightConst_SBType>2 && WeightConst_SBType<7) {
  505. rt_event_send(&display_event, EVENT_Stop);
  506. }
  507. /*
  508. if (WeightConst_WeightTrg_address>0){
  509. //setTrg_Command(WeightConst_WeightTrg_address, 1); //打开对应地址的继电器
  510. sendControlCOMM(WeightConst_WeightTrg_address,1);
  511. sendControlCOMM(WeightConst_WeightTrg_address,1);
  512. sendControlCOMM(WeightConst_WeightTrg_address,1);
  513. }
  514. */
  515. }
  516. void SendWifi_entry(void *parameter) {
  517. while (1)
  518. {
  519. if ((XbeeConst_needRetry>(40+XbeeConst_DeviceAddress)) && XbeeConst_CanSend==0) { //需要2秒后重发
  520. if ((XbeeConst_FTxBuf0[4]&0xF0) == 0xF0) {
  521. XbeeConst_FTxBuf0[4]=XbeeConst_FTxBuf0[4]-16;
  522. XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]] = 0;
  523. for (int i=1; i< XbeeConst_FTxBuf0[0]; i++){
  524. XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]] += XbeeConst_FTxBuf0[i];
  525. }
  526. XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]] = 0xff - XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]];
  527. }
  528. if (XbeeConst_UseAPI>0) {
  529. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  530. setSendFrame_t(XbeeConst_FTxBuf0[4]==0xF8 || XbeeConst_FTxBuf0[4]==0xE8,0x00,0xC0);
  531. // setSendFrame_t(1,0x00,0xC0);
  532. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FTxBuf0);
  533. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_PTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  534. usart2_send_data[SendWifi_i-1] = XbeeConst_PTxBuf0[SendWifi_i];
  535. // {
  536. // usart2_send_data[SendWifi_i-1] = XbeeConst_PTxBuf0[SendWifi_i];
  537. // rt_kprintf(" %02X",usart2_send_data[SendWifi_i-1]);
  538. //}
  539. // rt_kprintf("\n");
  540. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  541. rt_thread_mdelay(50);
  542. rt_sem_release(uart2_lock);
  543. }
  544. else{
  545. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  546. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_FTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  547. usart2_send_data[SendWifi_i-1] = XbeeConst_FTxBuf0[SendWifi_i];
  548. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_FTxBuf0[0]);
  549. rt_thread_mdelay(50);
  550. rt_sem_release(uart2_lock);
  551. }
  552. XbeeConst_needRetry = 0; //重置重发定时计数
  553. XbeeConst_FrameNumLast = XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]-1];
  554. }
  555. else if (XbeeConst_CanSend==1 && tsdb_recordcount('k')>0) { //可以重发/队列大于0/开头是7e
  556. if (findkey()<0xFFFFFFFF)
  557. {
  558. XbeeConst_CanSend = 0; //锁定无线不可发送直至解锁
  559. XbeeConst_needRetry = 0; //重置重发定时计数
  560. XbeeConst_FrameNumLast = XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]-1];
  561. if (XbeeConst_UseAPI>0) {
  562. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  563. setSendFrame_t(XbeeConst_FTxBuf0[4]==0xF8,0x00,0xC0);
  564. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FTxBuf0);
  565. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_PTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  566. usart2_send_data[SendWifi_i-1] = XbeeConst_PTxBuf0[SendWifi_i];
  567. // {
  568. // usart2_send_data[SendWifi_i-1] = XbeeConst_PTxBuf0[SendWifi_i];
  569. // rt_kprintf(" %02X",usart2_send_data[SendWifi_i-1]);
  570. //}
  571. // rt_kprintf("\n");
  572. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  573. rt_thread_mdelay(50);
  574. rt_sem_release(uart2_lock);
  575. }
  576. else
  577. {
  578. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  579. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_FTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  580. usart2_send_data[SendWifi_i-1] = XbeeConst_FTxBuf0[SendWifi_i];
  581. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_FTxBuf0[0]);
  582. rt_thread_mdelay(50);
  583. rt_sem_release(uart2_lock);
  584. }
  585. }
  586. }
  587. else if (XbeeConst_CanSend==1 && tsdb_recordcount('w')>=4) { //可以重发/队列大于0/开头是7e
  588. if (findweight()>3){
  589. XbeeConst_CanSend = 0; //锁定无线不可发送直至解锁
  590. XbeeConst_needRetry = 0; //重置重发定时计数
  591. XbeeConst_FrameNumLast = XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]-1];
  592. if (XbeeConst_UseAPI>0) {
  593. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  594. setSendFrame_t(XbeeConst_FTxBuf0[4]==0xF8,0x00,0xC0);
  595. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FTxBuf0);
  596. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_PTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  597. usart2_send_data[SendWifi_i-1] = XbeeConst_PTxBuf0[SendWifi_i];
  598. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  599. rt_thread_mdelay(50);
  600. rt_sem_release(uart2_lock);
  601. }
  602. else{
  603. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  604. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_FTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  605. usart2_send_data[SendWifi_i-1] = XbeeConst_FTxBuf0[SendWifi_i];
  606. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_FTxBuf0[0]);
  607. rt_thread_mdelay(50);
  608. rt_sem_release(uart2_lock);
  609. }
  610. }
  611. }
  612. else if (XbeeConst_CanSend==1 && rb_full_count(SendWeightQueue)>0 && rb_peek(SendWeightQueue)==0x7e && iscowweight==1) { //可以重发/队列大于0/开头是7e
  613. XbeeConst_FTxBuf0[1] = rb_remove(SendWeightQueue);
  614. XbeeConst_FTxBuf0[2] = rb_remove(SendWeightQueue);
  615. XbeeConst_FTxBuf0[0] = XbeeConst_FTxBuf0[2] + 2;//取得长度
  616. for (int SendWifi_i = 3; SendWifi_i <= XbeeConst_FTxBuf0[0]; SendWifi_i++)//取得本帧剩余数据
  617. XbeeConst_FTxBuf0[SendWifi_i] = rb_remove(SendWeightQueue);
  618. XbeeConst_CanSend = 0; //锁定无线不可发送直至解锁
  619. XbeeConst_needRetry = 0; //重置重发定时计数
  620. XbeeConst_FrameNumLast = XbeeConst_FTxBuf0[XbeeConst_FTxBuf0[0]-1];
  621. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  622. if (XbeeConst_UseAPI>0) {
  623. setSendFrame_t(XbeeConst_FTxBuf0[4]==0xF8,0x00,0xC0);
  624. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FTxBuf0);
  625. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_PTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  626. usart2_send_data[SendWifi_i-1] = XbeeConst_PTxBuf0[SendWifi_i];
  627. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  628. }
  629. else
  630. {
  631. for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_FTxBuf0[0]; SendWifi_i++) //将缓冲区发出
  632. usart2_send_data[SendWifi_i-1] = XbeeConst_FTxBuf0[SendWifi_i];
  633. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_FTxBuf0[0]);
  634. }
  635. rt_thread_mdelay(50);
  636. rt_sem_release(uart2_lock);
  637. }
  638. XbeeConst_needRetry++;
  639. rt_thread_mdelay(100);
  640. }
  641. }
  642. static rt_thread_t SendWifiid = RT_NULL;
  643. void SendWifi_thread(void)
  644. {
  645. SendWifiid = rt_thread_create("SendWifi", SendWifi_entry, RT_NULL, 0x300,5, 5);
  646. /* 如果获得线程控制块,启动这个线程 */
  647. if (SendWifiid != RT_NULL) rt_thread_startup(SendWifiid);
  648. }
  649. static rt_thread_t pop_productid = RT_NULL;
  650. void pop_product_thread(void)
  651. {
  652. pop_productid = rt_thread_create("popproduct", pop_product_entry, RT_NULL, 0x400, 5, 5);
  653. /* 如果获得线程控制块,启动这个线程 */
  654. if (pop_productid != RT_NULL) rt_thread_startup(pop_productid);
  655. }