process.c 9.8 KB

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