plan.c 29 KB

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