process.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "process.h"
  2. #include "gpio.h"
  3. #include "ac780x_gpio.h"
  4. #include "string.h"
  5. #include "adc.h"
  6. #include "Motor.h"
  7. #include "W25Q64.h"
  8. #include "AngleSensor.h"
  9. #include "pid.h"
  10. #include "cfg.h"
  11. #include "Rtcx.h"
  12. #include "storage.h"
  13. #include "IB_Reader.h"
  14. #include "math.h"
  15. uint8_t g_devicebusy = 0;
  16. uint16_t g_blinkLedTime = 0; /*LED闪烁频率控制时间*/
  17. uint16_t g_blinkLedTgtTime = BLINK_LED_DFTT; /*LED目标闪烁频率*/
  18. uint16_t g_period1000ms = 0;
  19. uint8_t g_detectTime = 0; /*状态检测时间*/
  20. uint32_t g_poweroff_count = 0; /*外部电源停止后、每秒计数加1*/
  21. #define STATUS_DETECTINTERVAL (10) /*10ms */
  22. uint8_t g_runstate; //运行状态,切换LED灯光
  23. uint8_t g_lockstatus = STATUS_UNKOWN;
  24. uint8_t g_coverstatus = STATUS_COVERCLOSE;
  25. //电机状态
  26. uint8_t g_motorstate = MOTOR_INIT;
  27. uint8_t g_runReady = 0;
  28. uint16_t g_runTime = 0;
  29. static float g_sample_position = 0; // 触发取样时,PID目标位置,取两阀值的均值
  30. //uint8_t g_sampleing_direction = 0; //触发取样时,电机运行方向, 0 正转, 1反转
  31. //状态持续计数,用来起到滤波作用
  32. //uint8_t tmp_status;
  33. uint8_t g_lockcount[STATUS_LOCKALL]={0};
  34. uint8_t g_covercount[STATUS_COVERALL]={0};
  35. static float s_angle = 0.0;
  36. //static float tmp_angle = 0.0;
  37. static uint8_t get_lockstatus();
  38. static PID g_pid = {0};
  39. static uint8_t tmp_speed;
  40. //static uint8_t tmp_time = 0;
  41. static void update_runstate()
  42. {
  43. switch(g_lockstatus){
  44. case STATUS_INTERMEDIATE:
  45. g_runstate = STATE_INTERMEDIATE;
  46. break;
  47. case STATUS_LOCK:
  48. g_runstate = STATE_LOCK;
  49. break;
  50. case STATUS_UNLOCK:
  51. g_runstate = STATE_UNLOCK;
  52. break;
  53. case STATUS_SAMPLE:
  54. g_runstate = STATE_SAMPLE;
  55. break;
  56. case STATUS_UNKOWN:
  57. g_runstate = STATE_EXCEPTION;
  58. break;
  59. default:
  60. break;
  61. };
  62. }
  63. void Process_Init(void)
  64. {
  65. /*初始化控制变量.*/
  66. g_blinkLedTime = 0;
  67. g_blinkLedTgtTime = BLINK_LED_DFTT;
  68. g_detectTime = 0;
  69. g_runstate = STATE_LOCK;
  70. s_angle = AngleSensor_GetAngle();
  71. g_lockstatus=get_lockstatus();
  72. g_coverstatus=Gpio_IsOpenCover()>0?STATUS_COVEROPEN:STATUS_COVERCLOSE;
  73. g_motorstate = MOTOR_READY;
  74. /*上电默认LED点亮.*/
  75. update_runstate();
  76. }
  77. void Process_RunPrd(void)
  78. {
  79. /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
  80. if (g_blinkLedTime >= g_blinkLedTgtTime)
  81. {
  82. g_blinkLedTime = 0;
  83. g_blinkLedTgtTime = BLINK_LED_DFTT;
  84. update_runstate();
  85. switch(g_runstate){
  86. case STATE_LOCK:
  87. REDLED_OFF;
  88. GREENLED_TOGGLE;
  89. break;
  90. case STATE_UNLOCK:
  91. GREENLED_OFF;
  92. REDLED_TOGGLE;
  93. break;
  94. case STATE_INTERMEDIATE:
  95. REDGREEN_TOGGLE;
  96. break;
  97. case STATE_SAMPLE:
  98. REDLED_OFF;
  99. GREENLED_TOGGLE;
  100. g_blinkLedTgtTime = 200;
  101. break;
  102. case STATE_OPENCOVER:
  103. REDLED_OFF;
  104. GREENLED_ON;
  105. break;
  106. case STATE_EXCEPTION:
  107. default:
  108. GREENLED_OFF;
  109. REDLED_ON;
  110. break;
  111. };
  112. //printADCValue();
  113. //printMotorCurrent();
  114. //printf(" Motor Current:%f mA \r\n", getMotorCurrent());
  115. //W25Q64_PrintInfo();
  116. //AngleSensor_PrintInfo();
  117. }
  118. //
  119. if(g_period1000ms >= 1000){
  120. g_period1000ms=0;
  121. //Storage_CountReduce();
  122. //g_poweroff_count++;
  123. if(Gpio_IsDC24()){
  124. printf(" DC24 Supply \r\n");
  125. }else{
  126. printf(" Battery Supply \r\n");
  127. }
  128. //printf(" Battery Voltage:%f V \r\n", getBatteryVoltage());
  129. //RTCx_PrintDateTime();
  130. //AngleSensor_PrintInfo();
  131. //IB_Print();
  132. }
  133. /*
  134. //10ms
  135. if(g_detectTime >= 10){
  136. g_detectTime = 0;
  137. //锁状态
  138. _status=get_lockstatus();
  139. for(i=0; i<STATUS_LOCKALL; i++){
  140. if(_status == i){
  141. g_lockcount[i]++;
  142. if(g_lockcount[i] >= 5){
  143. g_lockstatus = i;
  144. }
  145. }else{
  146. g_lockcount[i]=0;
  147. }
  148. }
  149. //上盖状态
  150. _status=Gpio_IsOpenCover()>0?STATUS_COVEROPEN:STATUS_COVERCLOSE;
  151. for(i=0; i<STATUS_COVERALL; i++){
  152. if(_status == i){
  153. g_covercount[i]++;
  154. if(g_covercount[i] >= 10){
  155. g_coverstatus = i;
  156. }
  157. }else{
  158. g_covercount[i]=0;
  159. }
  160. }
  161. }
  162. */
  163. }
  164. void Process_Timer10msCB(void)
  165. {
  166. s_angle = AngleSensor_GetAngle();
  167. if(s_angle < 0){ //处理异常
  168. if((MOTOR_READY != g_motorstate) && (MOTOR_STOPED != g_motorstate)){
  169. Motor_Stop();
  170. g_motorstate = MOTOR_STOPED;
  171. g_runReady =1;
  172. g_runTime=0;
  173. }
  174. }
  175. g_lockstatus=get_lockstatus();
  176. g_coverstatus=Gpio_IsOpenCover()>0?STATUS_COVEROPEN:STATUS_COVERCLOSE;
  177. //处理开关锁
  178. switch(g_motorstate){
  179. case MOTOR_READY:
  180. break;
  181. case MOTOR_LOCKING:
  182. if((STATUS_LOCK == g_lockstatus)|| (STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
  183. Motor_Stop();
  184. g_motorstate = MOTOR_STOPED;
  185. g_runReady =1;
  186. g_runTime=0;
  187. }else{
  188. PID_Calc(&g_pid, config->lock_threshold+3, s_angle);
  189. if(g_pid.error > 0){
  190. tmp_speed =(uint8_t)(g_pid.output);
  191. tmp_speed = tmp_speed<80?80:tmp_speed;
  192. Motor_Positive(tmp_speed);
  193. }else{
  194. Motor_Stop();
  195. g_motorstate = MOTOR_STOPED;
  196. g_runReady =1;
  197. g_runTime=0;
  198. }
  199. }
  200. break;
  201. case MOTOR_UNLOCKING:
  202. if((STATUS_UNLOCK == g_lockstatus)|| (STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
  203. Motor_Stop();
  204. g_motorstate = MOTOR_STOPED;
  205. g_runReady =1;
  206. g_runTime=0;
  207. }else{
  208. PID_Calc(&g_pid, config->unlock_threshold-3, s_angle);
  209. if(g_pid.error < 0){
  210. tmp_speed =(uint8_t)(-g_pid.output);
  211. tmp_speed = tmp_speed<80?80:tmp_speed;
  212. tmp_speed = 100;
  213. Motor_Negative(tmp_speed);
  214. }else{
  215. Motor_Stop();
  216. g_motorstate = MOTOR_STOPED;
  217. g_runReady =1;
  218. g_runTime=0;
  219. }
  220. }
  221. break;
  222. case MOTOR_SAMPLEING:
  223. if((STATUS_SAMPLE == g_lockstatus)|| (STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
  224. Motor_Stop();
  225. g_motorstate = MOTOR_STOPED;
  226. g_runReady =1;
  227. g_runTime=0;
  228. }else{
  229. PID_Calc(&g_pid, g_sample_position, s_angle);
  230. if(g_pid.error < 0){
  231. tmp_speed =(uint8_t)(-g_pid.output);
  232. tmp_speed = tmp_speed<80?80:tmp_speed;
  233. Motor_Negative(tmp_speed);
  234. }else{
  235. Motor_Stop();
  236. g_motorstate = MOTOR_STOPED;
  237. g_runReady =1;
  238. g_runTime=0;
  239. }
  240. }
  241. break;
  242. case MOTOR_STOPED:
  243. if(g_runTime >= 5000){ //5S
  244. g_motorstate = MOTOR_READY;
  245. }
  246. break;
  247. default:
  248. break;
  249. };
  250. //读取时间
  251. }
  252. uint8_t get_lockstatus()
  253. {
  254. static float _tmpf_min=0;
  255. static float _tmpf_max = 0;
  256. uint8_t status = STATUS_UNKOWN;
  257. if(s_angle<0){
  258. return status;
  259. }
  260. if((config->lock_threshold - config->unlock_threshold) > 10.0){
  261. if((s_angle > config->lock_threshold)){
  262. status = STATUS_LOCK;
  263. }else if(s_angle < config->unlock_threshold){
  264. status = STATUS_UNLOCK;
  265. }else {
  266. if(fabsf(config->sample_threshold1 - config->sample_threshold2) > 10.0 )
  267. {
  268. if(config->sample_threshold1 > config->sample_threshold2){
  269. _tmpf_max = config->sample_threshold1;
  270. _tmpf_min = config->sample_threshold2;
  271. }else{
  272. _tmpf_max = config->sample_threshold2;
  273. _tmpf_min = config->sample_threshold1;
  274. }
  275. if((s_angle > _tmpf_min) && (s_angle < _tmpf_max)){
  276. status = STATUS_SAMPLE;
  277. }else{
  278. status = STATUS_INTERMEDIATE;
  279. }
  280. }else{ //不支持取样
  281. status = STATUS_INTERMEDIATE;
  282. }
  283. }
  284. }
  285. return status;
  286. }
  287. uint8_t Process_GetLockStatus(void)
  288. {
  289. return get_lockstatus();
  290. }
  291. uint8_t Process_GetCoverStatus(void)
  292. {
  293. if(Gpio_IsOpenCover()){
  294. return STATUS_COVEROPEN;
  295. }else{
  296. return STATUS_COVERCLOSE;
  297. }
  298. }
  299. uint8_t Process_OpLock(uint8_t speed)
  300. {
  301. if((MOTOR_READY == g_motorstate) && (STATUS_LOCK != g_lockstatus)){
  302. //Motor_Positive(speed);
  303. g_motorstate = MOTOR_LOCKING;
  304. g_runReady =1;
  305. g_runTime=0;
  306. PID_Init(&g_pid, 12, 1, 0, 30, 100);
  307. }
  308. return 0;
  309. }
  310. uint8_t Process_OpUnlock(uint8_t speed)
  311. {
  312. if((MOTOR_READY == g_motorstate) && (STATUS_UNLOCK != g_lockstatus)){
  313. //Motor_Negative(speed);
  314. g_motorstate = MOTOR_UNLOCKING;
  315. g_runReady =1;
  316. g_runTime=0;
  317. PID_Init(&g_pid,12, 1, 0, 50, 100);
  318. }
  319. return 0;
  320. }
  321. uint8_t Process_OpSample(uint8_t speed)
  322. {
  323. // 参数不符合要求
  324. if(fabsf(config->sample_threshold1 - config->sample_threshold2) <= 10.0 ){
  325. return 0;
  326. }
  327. if((MOTOR_READY == g_motorstate) && (STATUS_LOCK == g_lockstatus)){
  328. //Motor_Negative(speed);
  329. g_motorstate = MOTOR_SAMPLEING;
  330. g_runReady =1;
  331. g_runTime=0;
  332. g_sample_position = (config->sample_threshold1 + config->sample_threshold2)/2;
  333. PID_Init(&g_pid,10, 1, 0, 30, 100);
  334. }
  335. return 0;
  336. }
  337. float Process_GetAngle(void)
  338. {
  339. return s_angle;
  340. }
  341. uint8_t Process_AngleCalibration(void)
  342. {
  343. float angle_raw = AngleSensor_GetAngleRaw();
  344. if(angle_raw >= 0){
  345. config->angle_offset = (200 - angle_raw);
  346. AngleSensor_Setoffset(config->angle_offset);
  347. }
  348. return 0;
  349. }
  350. uint8_t Process_CalibrationThreshold(uint8_t param)
  351. {
  352. if(0 == param){
  353. config->unlock_threshold = s_angle;
  354. }else if(1 == param){
  355. config->lock_threshold = s_angle;
  356. }else if(2 == param){
  357. config->sample_threshold1 = s_angle;
  358. }else if(3 == param){
  359. config->sample_threshold2 = s_angle;
  360. }
  361. return 0x00;
  362. }
  363. void Process_Storage(void)
  364. {
  365. static uint8_t pre_lstatus = STATUS_INTERMEDIATE;
  366. static uint8_t pre_cstatus = STATUS_COVEROPEN;
  367. if(Gpio_IsDC24()){
  368. pre_lstatus = g_lockstatus;
  369. pre_cstatus = g_coverstatus;
  370. }else{
  371. if(pre_lstatus != g_lockstatus){
  372. if(STATUS_LOCK == g_lockstatus){
  373. Storage_AddItem(ITEM_RECORD, EVENT_MANUAL_LOCK);
  374. }else if(STATUS_UNLOCK == g_lockstatus){
  375. Storage_AddItem(ITEM_RECORD, EVENT_MANUAL_UNLOCK);
  376. }
  377. pre_lstatus = g_lockstatus;
  378. }
  379. if(pre_cstatus != g_coverstatus){
  380. if(STATUS_COVEROPEN == g_coverstatus){
  381. Storage_AddItem(ITEM_RECORD, EVENT_OPENCOVER);
  382. }
  383. pre_cstatus = g_coverstatus;
  384. }
  385. }
  386. }
  387. void Process_Poweroff(void)
  388. {
  389. static float battery_v = 0.0;
  390. if(Gpio_IsDC24()){
  391. g_poweroff_count = 0;
  392. }else{
  393. battery_v = getBatteryVoltage();
  394. if((g_poweroff_count >= 2*60*60) || (battery_v < (3.3))){ // 2小时后或电池电压小于3.3V 关机
  395. g_poweroff_count = 0;
  396. Storage_Save();
  397. mdelay(500);
  398. LDOEn_DISABLE;
  399. //NVIC_SystemReset();
  400. //printf(" NVIC_SystemReset \r\n");
  401. }
  402. }
  403. }