process.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 "motor_control.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_IBLed_count = 0;
  19. uint16_t g_period1000ms = 0;
  20. uint8_t g_detectTime = 0; /*状态检测时间*/
  21. uint16_t g_angleRead_Interval = 5; //读取编码器角度间隔
  22. uint16_t g_angleRead_Count = 0; //读取编码器角度计数
  23. uint16_t g_ibRead_count =0; //ibutton 油品读取计数
  24. uint32_t g_poweroff_count = 0; /*外部电源停止后、每秒计数加1*/
  25. #define STATUS_DETECTINTERVAL (10) /*10ms */
  26. uint8_t g_runstate; //运行状态,切换LED灯光
  27. volatile uint8_t g_lockstatus = STATUS_UNKOWN;
  28. volatile uint8_t g_coverstatus = STATUS_COVERCLOSE;
  29. //电机状态
  30. uint8_t g_motorstate = MOTOR_INIT;
  31. uint8_t g_runReady = 0;
  32. uint16_t g_runTime = 0;
  33. static float g_target_position = 0; // 动作时,PID目标位置
  34. static uint8_t g_motor_dir = MOTOR_TUNR_NULL; // 电机动作时的转动方向 ,0 不动 1 正向, 2 反向
  35. //状态持续计数,用来起到滤波作用
  36. //uint8_t tmp_status;
  37. uint8_t g_lockcount[STATUS_LOCKALL]={0};
  38. uint8_t g_covercount[STATUS_COVERALL]={0};
  39. static float s_angle = 0.0;
  40. //static float tmp_angle = 0.0;
  41. static uint8_t get_lockstatus(void);
  42. //static PID g_pid = {0};
  43. //static uint8_t tmp_speed;
  44. static uint8_t tmp_pwmout;
  45. //static uint8_t tmp_time = 0;
  46. static void update_runstate()
  47. {
  48. switch(g_lockstatus){
  49. case STATUS_INTERMEDIATE:
  50. g_runstate = STATE_INTERMEDIATE;
  51. break;
  52. case STATUS_LOCK:
  53. g_runstate = STATE_LOCK;
  54. break;
  55. case STATUS_UNLOCK:
  56. g_runstate = STATE_UNLOCK;
  57. break;
  58. case STATUS_SAMPLE:
  59. g_runstate = STATE_SAMPLE;
  60. break;
  61. case STATUS_UNKOWN:
  62. g_runstate = STATE_UNKOWN;
  63. break;
  64. case STATUS_ENCODER_ERROR:
  65. g_runstate = STATE_EXCEPTION;
  66. break;
  67. default:
  68. break;
  69. };
  70. if(STATUS_COVEROPEN == g_coverstatus){
  71. g_runstate = STATE_OPENCOVER;
  72. }
  73. }
  74. void Process_Init(void)
  75. {
  76. /*初始化控制变量.*/
  77. g_blinkLedTime = 0;
  78. g_blinkLedTgtTime = BLINK_LED_DFTT;
  79. g_detectTime = 0;
  80. g_runstate = STATE_LOCK;
  81. g_angleRead_Interval = 5;
  82. g_angleRead_Count = 0;
  83. g_ibRead_count =0;
  84. // s_angle = AngleSensor_GetAngle();
  85. // g_lockstatus=get_lockstatus();
  86. // g_coverstatus=Gpio_IsOpenCover()>0?STATUS_COVEROPEN:STATUS_COVERCLOSE;
  87. g_motorstate = MOTOR_READY;
  88. /*上电默认LED点亮.*/
  89. // update_runstate();
  90. }
  91. void Process_RunPrd(void)
  92. {
  93. /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
  94. if (g_blinkLedTime >= g_blinkLedTgtTime)
  95. {
  96. g_blinkLedTime = 0;
  97. g_blinkLedTgtTime = BLINK_LED_DFTT;
  98. g_coverstatus=Gpio_IsOpenCover();
  99. update_runstate();
  100. switch(g_runstate){
  101. case STATE_LOCK:
  102. REDLED_OFF;
  103. GREENLED_TOGGLE;
  104. break;
  105. case STATE_UNLOCK:
  106. GREENLED_OFF;
  107. REDLED_TOGGLE;
  108. break;
  109. case STATE_INTERMEDIATE:
  110. REDGREEN_TOGGLE;
  111. break;
  112. case STATE_SAMPLE:
  113. REDLED_OFF;
  114. GREENLED_TOGGLE;
  115. g_blinkLedTgtTime = 200;
  116. break;
  117. case STATE_UNKOWN:
  118. REDLED_OFF;
  119. GREENLED_ON;
  120. break;
  121. case STATE_OPENCOVER:
  122. case STATE_EXCEPTION:
  123. default:
  124. GREENLED_OFF;
  125. REDLED_ON;
  126. break;
  127. };
  128. if(g_ibRead_count >= 1000){
  129. g_ibRead_count = 0;
  130. //在控制电机转动时, 不对ibutton 进行读取
  131. if((MOTOR_READY == g_motorstate) || (MOTOR_STOPED == g_motorstate)){
  132. IBRead_OilType();
  133. }
  134. }
  135. if(g_IBLed_count >= 500){
  136. g_IBLed_count = 0;
  137. if(OIL_TYPE_NULL != IBGet_OilType()){
  138. IB_RED_OFF;
  139. IB_GREEN_TOGGLE;
  140. }else{
  141. IB_GREEN_OFF;
  142. IB_RED_TOGGLE;
  143. }
  144. }
  145. //printADCValue();
  146. //printMotorCurrent();
  147. //printf(" Motor Current:%f mA \r\n", getMotorCurrent());
  148. //W25Q64_PrintInfo();
  149. //AngleSensor_PrintInfo();
  150. }
  151. //
  152. if(g_period1000ms >= 1000){
  153. g_period1000ms=0;
  154. //Storage_CountReduce();
  155. //g_poweroff_count++;
  156. printf(" opencover : %d \r\n", g_coverstatus);
  157. //printMotorCurrent();
  158. //printf(" Battery Voltage:%f V \r\n", getBatteryVoltage());
  159. //RTCx_PrintDateTime();
  160. //AngleSensor_PrintInfo();
  161. //IB_Print();
  162. }
  163. }
  164. static uint8_t get_motor_dir(float target){
  165. if(target > s_angle ){
  166. return MOTOR_TUNR_P; // 正转
  167. }else if(target < s_angle){
  168. return MOTOR_TUNR_N; // 反转
  169. }
  170. return MOTOR_TUNR_NULL;
  171. }
  172. void Process_Timer1_CB(void)
  173. {
  174. if (g_blinkLedTime < 0xFFFF)
  175. {
  176. g_blinkLedTime++;
  177. }
  178. if(g_detectTime < 0xFF){
  179. g_detectTime++;
  180. }
  181. if(g_runReady){
  182. g_runTime++;
  183. }
  184. if(g_period1000ms < 0xFFFF)
  185. {
  186. g_period1000ms++;
  187. }
  188. if(g_ibRead_count < 0xFFFF){
  189. g_ibRead_count++;
  190. }
  191. if(g_IBLed_count < 0xFFFF){
  192. g_IBLed_count++;
  193. }
  194. g_angleRead_Count++;
  195. if(g_angleRead_Count >= g_angleRead_Interval){
  196. g_angleRead_Count=0;
  197. AngleSensor_Read();
  198. }
  199. }
  200. void Process_Timer0_CB(void)
  201. {
  202. AngleSensor_Read();
  203. }
  204. void Process_MotorControl(float angle)
  205. {
  206. //printf("P_M angle: %f \r\n", angle);
  207. s_angle = angle;
  208. if(s_angle < 0){ //处理异常
  209. if((MOTOR_READY != g_motorstate) && (MOTOR_STOPED != g_motorstate)){
  210. Motor_Brake();
  211. g_motorstate = MOTOR_STOPED;
  212. g_runReady =1;
  213. g_runTime=0;
  214. }
  215. }
  216. g_lockstatus=get_lockstatus();
  217. //处理开关锁
  218. switch(g_motorstate){
  219. case MOTOR_READY:
  220. break;
  221. case MOTOR_LOCKING:
  222. if(/*(STATUS_LOCK == g_lockstatus)|| */(STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
  223. Motor_Brake();
  224. g_motorstate = MOTOR_STOPED;
  225. g_runReady =1;
  226. g_runTime=0;
  227. }else{
  228. if(angle < g_target_position){
  229. tmp_pwmout = MC_Calculate(g_target_position, angle, getMotorCurrent());
  230. if(MOTOR_TUNR_P == g_motor_dir){
  231. Motor_Positive(tmp_pwmout);
  232. }else if(MOTOR_TUNR_N == g_motor_dir){
  233. Motor_Negative(tmp_pwmout);
  234. }
  235. }else{
  236. Motor_Brake();
  237. g_motorstate = MOTOR_STOPED;
  238. g_runReady =1;
  239. g_runTime=0;
  240. }
  241. }
  242. break;
  243. case MOTOR_UNLOCKING:
  244. if(/*(STATUS_UNLOCK == g_lockstatus)|| */(STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
  245. Motor_Brake();
  246. g_motorstate = MOTOR_STOPED;
  247. g_runReady =1;
  248. g_runTime=0;
  249. }else{
  250. if(angle > g_target_position){
  251. tmp_pwmout = MC_Calculate(g_target_position, angle, getMotorCurrent());
  252. if(MOTOR_TUNR_P == g_motor_dir){
  253. Motor_Positive(tmp_pwmout);
  254. }else if(MOTOR_TUNR_N == g_motor_dir){
  255. Motor_Negative(tmp_pwmout);
  256. }
  257. }else{
  258. Motor_Brake();
  259. g_motorstate = MOTOR_STOPED;
  260. g_runReady =1;
  261. g_runTime=0;
  262. }
  263. }
  264. break;
  265. case MOTOR_SAMPLEING:
  266. if(/*(STATUS_SAMPLE == g_lockstatus)|| */(STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
  267. Motor_Brake();
  268. g_motorstate = MOTOR_STOPED;
  269. g_runReady =1;
  270. g_runTime=0;
  271. }else{
  272. if(angle > g_target_position){
  273. tmp_pwmout = MC_Calculate(g_target_position, angle, getMotorCurrent());
  274. if(MOTOR_TUNR_P == g_motor_dir){
  275. Motor_Positive(tmp_pwmout);
  276. }else if(MOTOR_TUNR_N == g_motor_dir){
  277. Motor_Negative(tmp_pwmout);
  278. }
  279. }else{
  280. Motor_Brake();
  281. g_motorstate = MOTOR_STOPED;
  282. g_runReady =1;
  283. g_runTime=0;
  284. }
  285. }
  286. break;
  287. case MOTOR_STOPED:
  288. if(g_runTime >= 5000){ //5S
  289. g_motorstate = MOTOR_READY;
  290. g_angleRead_Interval = 500;
  291. }
  292. g_angleRead_Interval = 200;
  293. break;
  294. default:
  295. break;
  296. };
  297. }
  298. uint8_t get_lockstatus(void)
  299. {
  300. static float _tmpf_min=0;
  301. static float _tmpf_max = 0;
  302. uint8_t status = STATUS_UNKOWN;
  303. if(s_angle<0){
  304. return STATUS_ENCODER_ERROR;
  305. }
  306. if((config->lock_threshold - config->unlock_threshold) > 10.0){
  307. if((s_angle > config->lock_threshold)){
  308. status = STATUS_LOCK;
  309. }else if(s_angle < config->unlock_threshold){
  310. status = STATUS_UNLOCK;
  311. }else {
  312. if(fabsf(config->sample_threshold1 - config->sample_threshold2) > 10.0 )
  313. {
  314. if(config->sample_threshold1 > config->sample_threshold2){
  315. _tmpf_max = config->sample_threshold1;
  316. _tmpf_min = config->sample_threshold2;
  317. }else{
  318. _tmpf_max = config->sample_threshold2;
  319. _tmpf_min = config->sample_threshold1;
  320. }
  321. if((s_angle > _tmpf_min) && (s_angle < _tmpf_max)){
  322. status = STATUS_SAMPLE;
  323. }else{
  324. status = STATUS_INTERMEDIATE;
  325. }
  326. }else{ //不支持取样
  327. status = STATUS_INTERMEDIATE;
  328. }
  329. }
  330. }
  331. return status;
  332. }
  333. uint16_t Process_GetOilType(void)
  334. {
  335. return IBGet_OilType();
  336. }
  337. uint8_t Process_GetLockStatus(void)
  338. {
  339. return get_lockstatus();
  340. }
  341. uint8_t Process_GetCoverStatus(void)
  342. {
  343. if(Gpio_IsOpenCover()){
  344. return STATUS_COVEROPEN;
  345. }else{
  346. return STATUS_COVERCLOSE;
  347. }
  348. }
  349. uint8_t Process_OpLock(uint8_t speed)
  350. {
  351. if((MOTOR_READY == g_motorstate) && (STATUS_LOCK != g_lockstatus)){
  352. g_motorstate = MOTOR_LOCKING;
  353. g_runReady =1;
  354. g_runTime=0;
  355. g_angleRead_Interval = 5;
  356. g_target_position = config->lock_threshold+3;
  357. MC_Init(s_angle);
  358. g_motor_dir = get_motor_dir(g_target_position);
  359. printf("Process_OpLock\r\n");
  360. }
  361. return 0;
  362. }
  363. uint8_t Process_OpUnlock(uint8_t speed)
  364. {
  365. // uint8_t id = 0;
  366. if((MOTOR_READY == g_motorstate) && (STATUS_UNLOCK != g_lockstatus)){
  367. g_motorstate = MOTOR_UNLOCKING;
  368. g_runReady =1;
  369. g_runTime=0;
  370. g_angleRead_Interval = 5;
  371. g_target_position = config->unlock_threshold-3;
  372. MC_Init(s_angle);
  373. g_motor_dir = get_motor_dir(g_target_position);
  374. printf("Process_OpUnlock\r\n");
  375. }
  376. return 0;
  377. }
  378. uint8_t Process_OpSample(uint8_t speed)
  379. {
  380. // uint8_t id = 0;
  381. // 参数不符合要求
  382. if(fabsf(config->sample_threshold1 - config->sample_threshold2) <= 10.0 ){
  383. return 0;
  384. }
  385. if((MOTOR_READY == g_motorstate) && (STATUS_LOCK == g_lockstatus)){
  386. g_motorstate = MOTOR_SAMPLEING;
  387. g_runReady =1;
  388. g_runTime=0;
  389. g_angleRead_Interval = 5;
  390. g_target_position = (config->sample_threshold1 + config->sample_threshold2)/2;
  391. MC_Init(s_angle);
  392. g_motor_dir = get_motor_dir(g_target_position);
  393. printf("Process_OpSample \r\n");
  394. }
  395. return 0;
  396. }
  397. float Process_GetAngle(void)
  398. {
  399. return s_angle;
  400. }
  401. uint8_t Process_AngleCalibration(void)
  402. {
  403. float angle_raw = AngleSensor_GetAngleRaw();
  404. if(angle_raw >= 0){
  405. config->angle_offset = (200 - angle_raw);
  406. AngleSensor_Setoffset(config->angle_offset);
  407. }
  408. return 0;
  409. }
  410. uint8_t Process_CalibrationThreshold(uint8_t param)
  411. {
  412. if(0 == param){
  413. config->unlock_threshold = s_angle;
  414. }else if(1 == param){
  415. config->lock_threshold = s_angle;
  416. }else if(2 == param){
  417. config->sample_threshold1 = s_angle;
  418. }else if(3 == param){
  419. config->sample_threshold2 = s_angle;
  420. }
  421. return 0x00;
  422. }
  423. void Process_Storage(void)
  424. {
  425. static uint8_t pre_lstatus = STATUS_INTERMEDIATE;
  426. static uint8_t pre_cstatus = STATUS_COVEROPEN;
  427. if(Gpio_IsDC24()){
  428. pre_lstatus = g_lockstatus;
  429. pre_cstatus = g_coverstatus;
  430. }else{
  431. if(pre_lstatus != g_lockstatus){
  432. if(STATUS_LOCK == g_lockstatus){
  433. Storage_AddItem(ITEM_RECORD, EVENT_MANUAL_LOCK);
  434. }else if(STATUS_UNLOCK == g_lockstatus){
  435. Storage_AddItem(ITEM_RECORD, EVENT_MANUAL_UNLOCK);
  436. }
  437. pre_lstatus = g_lockstatus;
  438. }
  439. if(pre_cstatus != g_coverstatus){
  440. if(STATUS_COVEROPEN == g_coverstatus){
  441. Storage_AddItem(ITEM_RECORD, EVENT_OPENCOVER);
  442. }
  443. pre_cstatus = g_coverstatus;
  444. }
  445. }
  446. }
  447. void Process_Poweroff(void)
  448. {
  449. static float battery_v = 0.0;
  450. if(Gpio_IsDC24()){
  451. g_poweroff_count = 0;
  452. }else{
  453. battery_v = getBatteryVoltage();
  454. if((g_poweroff_count >= 2*60*60) || (battery_v < (3.3))){ // 2小时后或电池电压小于3.3V 关机
  455. g_poweroff_count = 0;
  456. Storage_Save();
  457. mdelay(500);
  458. LDOEn_DISABLE;
  459. //NVIC_SystemReset();
  460. //printf(" NVIC_SystemReset \r\n");
  461. }
  462. }
  463. }
  464. void Process_MotorP(uint8_t speed)
  465. {
  466. if(speed > 0){
  467. Motor_Positive(speed);
  468. }else{
  469. Motor_Brake();
  470. }
  471. }
  472. void Process_MotorN(uint8_t speed)
  473. {
  474. if(speed > 0){
  475. Motor_Negative(speed);
  476. }else{
  477. Motor_Brake();
  478. }
  479. }