process.c 7.4 KB

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