123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- #include "process.h"
- #include "gpio.h"
- #include "ac780x_gpio.h"
- #include "string.h"
- #include "adc.h"
- #include "Motor.h"
- #include "W25Q64.h"
- #include "AngleSensor.h"
- #include "pid.h"
- #include "cfg.h"
- #include "Rtcx.h"
- #include "storage.h"
- #include "math.h"
- uint8_t g_devicebusy = 0;
- uint16_t g_blinkLedTime = 0; /*LED闪烁频率控制时间*/
- uint16_t g_blinkLedTgtTime = BLINK_LED_DFTT; /*LED目标闪烁频率*/
- uint16_t g_period1000ms = 0;
- uint8_t g_detectTime = 0; /*状态检测时间*/
- uint32_t g_poweroff_count = 0; /*外部电源停止后、每秒计数加1*/
- #define STATUS_DETECTINTERVAL (10) /*10ms */
- uint8_t g_runstate; //运行状态,切换LED灯光
- uint8_t g_lockstatus = STATUS_UNKOWN;
- uint8_t g_coverstatus = STATUS_COVERCLOSE;
- //电机状态
- uint8_t g_motorstate = MOTOR_INIT;
- uint8_t g_runReady = 0;
- uint16_t g_runTime = 0;
- static float g_sample_position = 0; // 触发取样时,PID目标位置,取两阀值的均值
- //uint8_t g_sampleing_direction = 0; //触发取样时,电机运行方向, 0 正转, 1反转
- //状态持续计数,用来起到滤波作用
- //uint8_t tmp_status;
- uint8_t g_lockcount[STATUS_LOCKALL]={0};
- uint8_t g_covercount[STATUS_COVERALL]={0};
- static float s_angle = 0.0;
- //static float tmp_angle = 0.0;
- static uint8_t get_lockstatus();
- static PID g_pid = {0};
- static uint8_t tmp_speed;
- //static uint8_t tmp_time = 0;
- static void update_runstate()
- {
- switch(g_lockstatus){
- case STATUS_INTERMEDIATE:
- g_runstate = STATE_INTERMEDIATE;
- break;
- case STATUS_LOCK:
- g_runstate = STATE_LOCK;
- break;
- case STATUS_UNLOCK:
- g_runstate = STATE_UNLOCK;
- break;
- case STATUS_SAMPLE:
- g_runstate = STATE_SAMPLE;
- break;
- case STATUS_UNKOWN:
- g_runstate = STATE_EXCEPTION;
- break;
- default:
- break;
- };
-
- }
- void Process_Init(void)
- {
- /*初始化控制变量.*/
- g_blinkLedTime = 0;
- g_blinkLedTgtTime = BLINK_LED_DFTT;
-
- g_detectTime = 0;
- g_runstate = STATE_LOCK;
- s_angle = AngleSensor_GetAngle();
- g_lockstatus=get_lockstatus();
-
- g_coverstatus=Gpio_IsOpenCover()>0?STATUS_COVEROPEN:STATUS_COVERCLOSE;
- g_motorstate = MOTOR_READY;
-
- /*上电默认LED点亮.*/
- update_runstate();
- }
- void Process_RunPrd(void)
- {
-
- /*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
- if (g_blinkLedTime >= g_blinkLedTgtTime)
- {
- g_blinkLedTime = 0;
- g_blinkLedTgtTime = BLINK_LED_DFTT;
- update_runstate();
-
- switch(g_runstate){
- case STATE_LOCK:
- REDLED_OFF;
- GREENLED_TOGGLE;
- break;
- case STATE_UNLOCK:
- GREENLED_OFF;
- REDLED_TOGGLE;
- break;
- case STATE_INTERMEDIATE:
- REDGREEN_TOGGLE;
- break;
- case STATE_SAMPLE:
- REDLED_OFF;
- GREENLED_TOGGLE;
- g_blinkLedTgtTime = 200;
- break;
- case STATE_OPENCOVER:
- REDLED_OFF;
- GREENLED_ON;
- break;
- case STATE_EXCEPTION:
- default:
- GREENLED_OFF;
- REDLED_ON;
- break;
-
- };
-
-
- //printADCValue();
- //printMotorCurrent();
- //printf(" Motor Current:%f mA \r\n", getMotorCurrent());
-
- //W25Q64_PrintInfo();
- //AngleSensor_PrintInfo();
-
- }
-
-
- //
- if(g_period1000ms >= 1000){
- g_period1000ms=0;
-
- //Storage_CountReduce();
-
- //g_poweroff_count++;
-
- if(Gpio_IsDC24()){
- printf(" DC24 Supply \r\n");
-
- }else{
- printf(" Battery Supply \r\n");
-
- }
-
- //printf(" Battery Voltage:%f V \r\n", getBatteryVoltage());
-
- //RTCx_PrintDateTime();
- //AngleSensor_PrintInfo();
-
-
- }
-
-
-
- /*
-
- //10ms
- if(g_detectTime >= 10){
- g_detectTime = 0;
-
- //锁状态
- _status=get_lockstatus();
- for(i=0; i<STATUS_LOCKALL; i++){
- if(_status == i){
- g_lockcount[i]++;
- if(g_lockcount[i] >= 5){
- g_lockstatus = i;
- }
-
- }else{
- g_lockcount[i]=0;
- }
- }
-
- //上盖状态
- _status=Gpio_IsOpenCover()>0?STATUS_COVEROPEN:STATUS_COVERCLOSE;
- for(i=0; i<STATUS_COVERALL; i++){
- if(_status == i){
- g_covercount[i]++;
- if(g_covercount[i] >= 10){
- g_coverstatus = i;
- }
-
- }else{
- g_covercount[i]=0;
- }
- }
-
- }
-
- */
-
- }
- void Process_Timer10msCB(void)
- {
- s_angle = AngleSensor_GetAngle();
- g_lockstatus=get_lockstatus();
- g_coverstatus=Gpio_IsOpenCover()>0?STATUS_COVEROPEN:STATUS_COVERCLOSE;
-
- //处理开关锁
- switch(g_motorstate){
- case MOTOR_READY:
- break;
- case MOTOR_LOCKING:
- if((STATUS_LOCK == g_lockstatus)|| (STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
- Motor_Stop();
- g_motorstate = MOTOR_STOPED;
- g_runReady =1;
- g_runTime=0;
- }else{
- PID_Calc(&g_pid, config->lock_threshold, s_angle);
-
- if(g_pid.error > 0){
- tmp_speed =(uint8_t)(g_pid.output);
- tmp_speed = tmp_speed<50?50:tmp_speed;
- Motor_Positive(tmp_speed);
-
- }else{
- Motor_Stop();
- g_motorstate = MOTOR_STOPED;
- g_runReady =1;
- g_runTime=0;
- }
-
- }
-
- break;
- case MOTOR_UNLOCKING:
- if((STATUS_UNLOCK == g_lockstatus)|| (STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
- Motor_Stop();
- g_motorstate = MOTOR_STOPED;
- g_runReady =1;
- g_runTime=0;
- }else{
- PID_Calc(&g_pid, config->unlock_threshold, s_angle);
-
- if(g_pid.error < 0){
- tmp_speed =(uint8_t)(-g_pid.output);
- tmp_speed = tmp_speed<50?50:tmp_speed;
- Motor_Negative(tmp_speed);
-
- }else{
- Motor_Stop();
- g_motorstate = MOTOR_STOPED;
- g_runReady =1;
- g_runTime=0;
- }
-
- }
- break;
-
- case MOTOR_SAMPLEING:
- if((STATUS_SAMPLE == g_lockstatus)|| (STATUS_UNKOWN == g_lockstatus) ||(g_runTime >= 2000)){
- Motor_Stop();
- g_motorstate = MOTOR_STOPED;
- g_runReady =1;
- g_runTime=0;
- }else{
- PID_Calc(&g_pid, g_sample_position, s_angle);
-
- if(g_pid.error < 0){
- tmp_speed =(uint8_t)(-g_pid.output);
- tmp_speed = tmp_speed<50?50:tmp_speed;
- Motor_Negative(tmp_speed);
-
- }else{
- Motor_Stop();
- g_motorstate = MOTOR_STOPED;
- g_runReady =1;
- g_runTime=0;
- }
-
- }
- break;
- case MOTOR_STOPED:
- if(g_runTime >= 5000){ //5S
- g_motorstate = MOTOR_READY;
- }
-
- break;
- default:
- break;
- };
-
- //读取时间
- }
- uint8_t get_lockstatus()
- {
- static float _tmpf_min=0;
- static float _tmpf_max = 0;
- uint8_t status = STATUS_UNKOWN;
- if(s_angle<0){
- return status;
- }
-
-
- if((config->lock_threshold - config->unlock_threshold) > 10.0){
-
- if((s_angle > config->lock_threshold)){
- status = STATUS_LOCK;
- }else if(s_angle < config->unlock_threshold){
- status = STATUS_UNLOCK;
- }else {
-
- if(fabsf(config->sample_threshold1 - config->sample_threshold2) > 10.0 )
- {
- if(config->sample_threshold1 > config->sample_threshold2){
- _tmpf_max = config->sample_threshold1;
- _tmpf_min = config->sample_threshold2;
- }else{
- _tmpf_max = config->sample_threshold2;
- _tmpf_min = config->sample_threshold1;
- }
-
- if((s_angle > _tmpf_min) && (s_angle < _tmpf_max)){
- status = STATUS_SAMPLE;
- }else{
- status = STATUS_INTERMEDIATE;
- }
-
- }else{ //不支持取样
- status = STATUS_INTERMEDIATE;
- }
-
- }
-
- }
-
- return status;
- }
- uint8_t Process_GetLockStatus(void)
- {
- return get_lockstatus();
- }
-
- uint8_t Process_GetCoverStatus(void)
- {
- if(Gpio_IsOpenCover()){
- return STATUS_COVEROPEN;
- }else{
- return STATUS_COVERCLOSE;
- }
- }
- uint8_t Process_OpLock(uint8_t speed)
- {
-
- if((MOTOR_READY == g_motorstate) & (STATUS_LOCK != g_lockstatus)){
- //Motor_Positive(speed);
- g_motorstate = MOTOR_LOCKING;
- g_runReady =1;
- g_runTime=0;
-
- PID_Init(&g_pid, 5, 1, 0, 30, 100);
- }
-
- return 0;
- }
- uint8_t Process_OpUnlock(uint8_t speed)
- {
-
- if((MOTOR_READY == g_motorstate) & (STATUS_UNLOCK != g_lockstatus)){
- //Motor_Negative(speed);
- g_motorstate = MOTOR_UNLOCKING;
- g_runReady =1;
- g_runTime=0;
-
- PID_Init(&g_pid,5, 1, 0, 30, 100);
- }
- return 0;
- }
- uint8_t Process_OpSample(uint8_t speed)
- {
- // 参数不符合要求
- if(fabsf(config->sample_threshold1 - config->sample_threshold2) <= 10.0 ){
- return 0;
- }
-
- if((MOTOR_READY == g_motorstate) && (STATUS_LOCK == g_lockstatus)){
- //Motor_Negative(speed);
- g_motorstate = MOTOR_SAMPLEING;
- g_runReady =1;
- g_runTime=0;
-
- g_sample_position = (config->sample_threshold1 + config->sample_threshold2)/2;
-
- PID_Init(&g_pid,5, 1, 0, 30, 100);
-
- }
- return 0;
- }
- float Process_GetAngle(void)
- {
- return s_angle;
- }
- uint8_t Process_AngleCalibration(void)
- {
- float angle_raw = AngleSensor_GetAngleRaw();
- if(angle_raw >= 0){
- config->angle_offset = (200 - angle_raw);
- AngleSensor_Setoffset(config->angle_offset);
- }
-
- return 0;
- }
- uint8_t Process_CalibrationThreshold(uint8_t param)
- {
- if(0 == param){
- config->unlock_threshold = s_angle;
- }else if(1 == param){
- config->lock_threshold = s_angle;
- }else if(2 == param){
- config->sample_threshold1 = s_angle;
- }else if(3 == param){
- config->sample_threshold2 = s_angle;
- }
-
- return 0x00;
- }
- void Process_Storage(void)
- {
- static uint8_t pre_lstatus = STATUS_INTERMEDIATE;
- static uint8_t pre_cstatus = STATUS_COVEROPEN;
-
- if(Gpio_IsDC24()){
-
- pre_lstatus = g_lockstatus;
- pre_cstatus = g_coverstatus;
-
-
- }else{
-
- if(pre_lstatus != g_lockstatus){
- if(STATUS_LOCK == g_lockstatus){
-
- Storage_AddItem(ITEM_RECORD, EVENT_MANUAL_LOCK);
-
- }else if(STATUS_UNLOCK == g_lockstatus){
- Storage_AddItem(ITEM_RECORD, EVENT_MANUAL_UNLOCK);
- }
-
- pre_lstatus = g_lockstatus;
-
- }
-
- if(pre_cstatus != g_coverstatus){
- if(STATUS_COVEROPEN == g_coverstatus){
- Storage_AddItem(ITEM_RECORD, EVENT_OPENCOVER);
- }
-
- pre_cstatus = g_coverstatus;
-
- }
-
- }
-
-
- }
- void Process_Poweroff(void)
- {
- static float battery_v = 0.0;
- if(Gpio_IsDC24()){
-
- g_poweroff_count = 0;
- }else{
-
- battery_v = getBatteryVoltage();
-
- if((g_poweroff_count >= 2*60*60) || (battery_v < (3.3))){ // 2小时后或电池电压小于3.3V 关机
-
- g_poweroff_count = 0;
- Storage_Save();
- mdelay(500);
- LDOEn_DISABLE;
- //NVIC_SystemReset();
- //printf(" NVIC_SystemReset \r\n");
-
- }
- }
- }
|