M12-05P/app/user/MotorCtrl.c

156 lines
3.3 KiB
C
Raw Normal View History

2024-01-05 16:19:49 +08:00
#include "MotorCtrl.h"
#include "hwCtrl.h"
#include "PINdef.h"
#include "appTask.h"
//#include "pfdl.h"
2024-07-27 19:55:12 +08:00
volatile uint8_t MotorState[6],MotorStateReal[6];
2024-05-23 20:07:39 +08:00
uint16_t current1;
uint8_t OC1flag;
2024-07-27 00:45:26 +08:00
uint16_t OC_Count1=0;
uint8_t act_step[6];
2024-01-05 16:19:49 +08:00
void MotorValueInit(void)
{
uint8_t i = 0;
for (i = 0; i < 6; i++)
{
MotorState[i] = ACT_NOACT;
MotorStateReal[i] = ACT_NOACT;
}
}
void setMotorState(uint8_t motorid,uint8_t act)
{
if (act <= ACT_XH && motorid < 6)
{
MotorState[motorid] = act;
2024-07-27 00:45:26 +08:00
act_step[motorid] = 0;
2024-01-05 16:19:49 +08:00
}
}
2024-10-26 22:23:45 +08:00
extern uint8_t key_p1_pressed, key_p2_pressed;
extern uint8_t key_p1_released, key_p2_released;
void MotorCtrl(void)//10ms
2024-01-05 16:19:49 +08:00
{
2024-10-26 22:23:45 +08:00
static uint8_t MotorRunState = 0;
static uint16_t p1_cnt, p2_cnt;
switch (MotorRunState)
2024-01-05 16:19:49 +08:00
{
2024-10-26 22:23:45 +08:00
case 0:
if (key_p1_pressed == 1 && p1_cnt < 0xffff)
2024-01-06 17:05:59 +08:00
{
2024-10-26 22:23:45 +08:00
p1_cnt++;
if (p1_cnt >= 150)
2024-05-23 20:07:39 +08:00
{
2024-10-26 22:23:45 +08:00
setMotorState(MOTOR1, ACT_XQ);
MotorRunState = 1;
//key_p1_released = 0;
key_p2_released = 0;
2024-05-23 20:07:39 +08:00
}
2024-10-26 22:23:45 +08:00
key_p1_released = 0;
2024-01-05 16:19:49 +08:00
}
2024-10-26 22:23:45 +08:00
if (key_p2_pressed == 1 && p2_cnt < 0xffff)
2024-01-06 17:05:59 +08:00
{
2024-10-26 22:23:45 +08:00
p2_cnt++;
if (p2_cnt >= 150)
2024-05-23 20:07:39 +08:00
{
2024-10-26 22:23:45 +08:00
setMotorState(MOTOR1, ACT_XH);
MotorRunState = 1;
key_p1_released = 0;
//key_p2_released = 0;
2024-05-23 20:07:39 +08:00
}
2024-10-26 22:23:45 +08:00
key_p2_released = 0;
}
if (key_p1_released == 1 && p1_cnt < 100 && p1_cnt > 0)
{
key_p1_released = 0;
setMotorState(MOTOR1, ACT_XQ);
MotorRunState = 2;
p1_cnt = 0;
}
if (key_p2_released == 1 && p2_cnt < 100 && p2_cnt > 0)
{
key_p2_released = 0;
setMotorState(MOTOR1, ACT_XH);
MotorRunState = 2;
p2_cnt = 0;
2024-01-05 16:19:49 +08:00
}
break;
2024-10-26 22:23:45 +08:00
case 1://长按
if (key_p1_released == 1 || key_p2_released == 1 || OC1flag)
2024-07-27 00:45:26 +08:00
{
2024-10-26 22:23:45 +08:00
key_p1_released = 0;
key_p2_released = 0;
setMotorState(MOTOR1, ACT_NOACT);
MotorRunState = 3;
2024-07-27 19:55:12 +08:00
OC1flag = 0;
2024-07-27 00:45:26 +08:00
}
break;
2024-10-26 22:23:45 +08:00
case 2://短按
if (key_p1_pressed == 1 || key_p2_pressed == 1 || OC1flag)
2024-07-27 00:45:26 +08:00
{
2024-10-26 22:23:45 +08:00
key_p1_pressed = 0;
key_p2_pressed = 0;
setMotorState(MOTOR1, ACT_NOACT);
MotorRunState = 3;
2024-07-27 19:55:12 +08:00
OC1flag = 0;
2024-07-27 00:45:26 +08:00
}
2024-07-27 19:55:12 +08:00
break;
2024-10-26 22:23:45 +08:00
case 3:
key_p1_pressed = 0;
key_p2_pressed = 0;
key_p1_released = 0;
key_p2_released = 0;
p1_cnt = 0;
p2_cnt = 0;
MotorRunState = 0;
break;
2024-07-27 00:45:26 +08:00
default:
break;
}
2024-10-26 22:23:45 +08:00
MotorStateReal[MOTOR1] = MotorState[MOTOR1];
2024-07-27 19:55:12 +08:00
2024-01-05 16:19:49 +08:00
MOTOR1Ctrl(MotorStateReal[MOTOR1]);
2024-10-26 22:23:45 +08:00
//MOTOR2Ctrl(MotorStateReal[MOTOR2]);
2024-05-23 22:22:05 +08:00
//MOTOR1Ctrl(MotorState[MOTOR1]);
2024-01-05 16:19:49 +08:00
}
2024-05-25 13:58:01 +08:00
uint16_t getOC_th(void)
{
2024-10-26 22:23:45 +08:00
2024-11-11 22:14:57 +08:00
return 100U;
2024-05-25 13:58:01 +08:00
}
2024-01-05 16:19:49 +08:00
2024-05-23 20:07:39 +08:00
void CurrentDetecte(void)
{
2024-07-27 00:45:26 +08:00
2024-05-25 13:58:01 +08:00
uint16_t OC_th;
2024-05-23 20:07:39 +08:00
current1 = getAdval(ADCH_RLY1);
2024-05-25 13:58:01 +08:00
OC_th = getOC_th();
if (current1 > OC_th && OC1flag == 0)
2024-05-23 20:07:39 +08:00
{
OC_Count1++;
if (OC_Count1 >= 200)
{
OC_Count1 = 0;
OC1flag = 1;
}
}
else
{
OC_Count1 = 0;
}
2024-10-26 22:23:45 +08:00
2024-05-23 20:07:39 +08:00
}
2024-01-05 16:19:49 +08:00