plan.c 30 KB

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