|
@@ -10,6 +10,7 @@
|
|
|
#include "cfg.h"
|
|
|
#include "Rtcx.h"
|
|
|
#include "storage.h"
|
|
|
+#include "math.h"
|
|
|
|
|
|
uint8_t g_devicebusy = 0;
|
|
|
uint16_t g_blinkLedTime = 0; /*LED闪烁频率控制时间*/
|
|
@@ -31,6 +32,8 @@ 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;
|
|
@@ -57,6 +60,9 @@ static void update_runstate()
|
|
|
case STATUS_UNLOCK:
|
|
|
g_runstate = STATE_UNLOCK;
|
|
|
break;
|
|
|
+ case STATUS_SAMPLE:
|
|
|
+ g_runstate = STATE_SAMPLE;
|
|
|
+ break;
|
|
|
case STATUS_UNKOWN:
|
|
|
g_runstate = STATE_EXCEPTION;
|
|
|
break;
|
|
@@ -97,6 +103,7 @@ void Process_RunPrd(void)
|
|
|
if (g_blinkLedTime >= g_blinkLedTgtTime)
|
|
|
{
|
|
|
g_blinkLedTime = 0;
|
|
|
+ g_blinkLedTgtTime = BLINK_LED_DFTT;
|
|
|
update_runstate();
|
|
|
|
|
|
switch(g_runstate){
|
|
@@ -111,6 +118,11 @@ void Process_RunPrd(void)
|
|
|
case STATE_INTERMEDIATE:
|
|
|
REDGREEN_TOGGLE;
|
|
|
break;
|
|
|
+ case STATE_SAMPLE:
|
|
|
+ REDLED_OFF;
|
|
|
+ GREENLED_TOGGLE;
|
|
|
+ g_blinkLedTgtTime = 200;
|
|
|
+ break;
|
|
|
case STATE_OPENCOVER:
|
|
|
REDLED_OFF;
|
|
|
GREENLED_ON;
|
|
@@ -140,9 +152,9 @@ void Process_RunPrd(void)
|
|
|
if(g_period1000ms >= 1000){
|
|
|
g_period1000ms=0;
|
|
|
|
|
|
- Storage_CountReduce();
|
|
|
+ //Storage_CountReduce();
|
|
|
|
|
|
- g_poweroff_count++;
|
|
|
+ //g_poweroff_count++;
|
|
|
|
|
|
if(Gpio_IsDC24()){
|
|
|
printf(" DC24 Supply \r\n");
|
|
@@ -152,9 +164,9 @@ void Process_RunPrd(void)
|
|
|
|
|
|
}
|
|
|
|
|
|
- printf(" Battery Voltage:%f V \r\n", getBatteryVoltage());
|
|
|
+ //printf(" Battery Voltage:%f V \r\n", getBatteryVoltage());
|
|
|
|
|
|
- RTCx_PrintDateTime();
|
|
|
+ //RTCx_PrintDateTime();
|
|
|
//AngleSensor_PrintInfo();
|
|
|
|
|
|
|
|
@@ -220,7 +232,7 @@ void Process_Timer10msCB(void)
|
|
|
g_runReady =1;
|
|
|
g_runTime=0;
|
|
|
}else{
|
|
|
- PID_Calc(&g_pid, config->lock_threshold+3, s_angle);
|
|
|
+ PID_Calc(&g_pid, config->lock_threshold, s_angle);
|
|
|
|
|
|
if(g_pid.error > 0){
|
|
|
tmp_speed =(uint8_t)(g_pid.output);
|
|
@@ -244,7 +256,31 @@ void Process_Timer10msCB(void)
|
|
|
g_runReady =1;
|
|
|
g_runTime=0;
|
|
|
}else{
|
|
|
- PID_Calc(&g_pid, config->unlock_threshold-3, s_angle);
|
|
|
+ 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);
|
|
@@ -280,19 +316,42 @@ void Process_Timer10msCB(void)
|
|
|
|
|
|
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){
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -327,7 +386,7 @@ uint8_t Process_OpLock(uint8_t speed)
|
|
|
g_runReady =1;
|
|
|
g_runTime=0;
|
|
|
|
|
|
- PID_Init(&g_pid, 5, 10, 0, 80, 100);
|
|
|
+ PID_Init(&g_pid, 10, 0, 0, 50, 100);
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
@@ -342,21 +401,61 @@ uint8_t Process_OpUnlock(uint8_t speed)
|
|
|
g_runReady =1;
|
|
|
g_runTime=0;
|
|
|
|
|
|
- PID_Init(&g_pid,5, 10, 0, 80, 100);
|
|
|
+ PID_Init(&g_pid,10, 0, 0, 50, 100);
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-uint8_t Process_CalibrationThreshold(uint8_t islock)
|
|
|
+uint8_t Process_OpSample(uint8_t speed)
|
|
|
{
|
|
|
- if(islock){
|
|
|
-
|
|
|
- config->lock_threshold = AngleSensor_GetAngle();
|
|
|
+ // 参数不符合要求
|
|
|
+ if(fabsf(config->sample_threshold1 - config->sample_threshold2) <= 10.0 ){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
|
|
|
- }else{
|
|
|
- config->unlock_threshold = AngleSensor_GetAngle();
|
|
|
+ if((MOTOR_READY == g_motorstate) && (STATUS_LOCK == g_lockstatus)){
|
|
|
+ //Motor_Negative(speed);
|
|
|
+ g_motorstate = MOTOR_SAMPLEING;
|
|
|
+ g_runReady =1;
|
|
|
+ g_runTime=0;
|
|
|
+
|
|
|
+ PID_Init(&g_pid,10, 0, 0, 50, 100);
|
|
|
+
|
|
|
+ g_sample_position = (config->sample_threshold1 + config->sample_threshold2)/2;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|