RP-01/cva_asw_m0146/src/MotorCtrl.c

721 lines
18 KiB
C
Raw Normal View History

2024-05-14 16:05:43 +08:00
#include "MotorCtrl.h"
#include "hwctrl.h"
2024-12-09 08:35:30 +08:00
#include "scm_canmatrix-binutil.h"
2024-06-18 16:03:46 +08:00
/*******************************************************************************
* the defines
******************************************************************************/
#define START_FLAG 0x55aa
#define STOP_FLAG 0xaa55
#define AUTOCAL_STOP 0
#define AUTOCAL_START 1
#define AUTOCAL_ACT1 2
#define AUTOCAL_ACT2 3
#define AUTOCAL_END 4
#define OC_10A 100
#define OC_30A 300
#define OC_500mS 500
#define OC_50mS 50
#define OC_TIME_200MS 200
#define HALLDELAYMAX 2
/*******************************************************************************
* the typedefs
******************************************************************************/
typedef struct
{
uint16_t MotorStopLoc1;
uint16_t MotorStopLoc2;
uint16_t MotorNowLoc;
}MOTOR_DATA;
typedef struct
{
uint16_t start_flag;
MOTOR_DATA MotorData[6];
uint16_t MemoryLoc[3][6];
uint16_t checksum;
uint16_t stop_flag;
}MEMORY_DATA;
/*******************************************************************************
* the globals
******************************************************************************/
MOTOR_DATA MotorData[6];
uint16_t MemoryLoc[3][6];
MEMORY_DATA MemoryData;
uint8_t OC1flag,OC2flag,OC3flag;
uint8_t MotorState[6],MotorStateReal[6];
uint16_t MotorHallLoc[6],MotorHardStop1[6],MotorHardStop2[6];
uint8_t MotorLearnState[6];
uint8_t MotorErr[6];
uint16_t MotorTarget[6] = {0};
uint16_t current1,current2,current3;
uint8_t stopflag = 0;
static uint8_t AutoCalState;
uint16_t HallErrorCount[6];
/*******************************************************************************
* the const
******************************************************************************/
/*******************************************************************************
* the local function prototypes
******************************************************************************/
static void MotorCtrl(void);
static void AutoCalCtrl(void);
static void MotorValueInit(void);
2024-12-09 08:35:30 +08:00
static void CurrentDetect(void);
2024-06-18 16:03:46 +08:00
/*******************************************************************************
* the local functions
******************************************************************************/
static void MotorValueInit(void)
{
uint8_t i;
for (i = 0; i < 6; i++)
{
MotorHallLoc[i] = 0x8000;
MotorErr[i] = 0;
}
//ReadMotorMemory();
}
static void AutoCalCtrl(void)
{
static uint16_t autocalcounter[3];
static uint8_t MotorArr1state,MotorArr2state,MotorArr3state;
static uint8_t wait1,wait2,wait3;
uint8_t i;
uint32_t temp;
switch (AutoCalState)
{
case AUTOCAL_STOP:
MotorArr1state = AUTOCAL_STOP;
MotorArr2state = AUTOCAL_STOP;
MotorArr3state = AUTOCAL_STOP;
return;
case AUTOCAL_START:
MotorArr1state = 1;
MotorArr2state = 1;
MotorArr3state = 1;
2024-12-09 08:35:30 +08:00
setMotorState(MotorHG,Motor_ACT_CW);
setMotorState(MotorTT,Motor_ACT_CW);
2024-06-18 16:03:46 +08:00
setMotorState(Motor5,Motor_ACT_CW);
autocalcounter[0] = 0;
autocalcounter[1] = 0;
autocalcounter[2] = 0;
AutoCalState++;
for (i = 0; i < 6; i++)
{
MotorHardStop1[i] = 0;
MotorHardStop2[i] = 0;
MotorHallLoc[i] = 0x8000;
}
break;
case AUTOCAL_ACT1:
if (MotorArr1state == 0 && MotorArr2state == 0 && MotorArr3state == 0)
{
for (i = 0; i < 6; i++)
{
temp = MotorHardStop1[i];
temp += MotorHardStop2[i];
setMotorTarget(i,temp/2);
}
}
break;
case AUTOCAL_ACT2:
break;
default:
break;
}
//1
switch (MotorArr1state)
{
case 1://Motor1 xq
autocalcounter[0]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop1[MotorHG] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state++;
wait1 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorHG,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[0] > 3000 || MotorErr[MotorHG] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state = 4;
wait1 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorHG,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
case 2://wait
wait1++;
if (wait1 > 50)
{
MotorArr1state++;
2024-12-09 08:35:30 +08:00
setMotorState(MotorHG,Motor_ACT_CCW);
2024-06-18 16:03:46 +08:00
autocalcounter[0] = 0;
}
break;
case 3://Motor1 xh
autocalcounter[0]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop2[MotorHG] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state++;
wait1 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorHG,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[0] > 3000 || MotorErr[MotorHG] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state = 4;
wait1 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorHG,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
case 4://wait
wait1++;
if (wait1 > 50)
{
MotorArr1state++;
2024-12-09 08:35:30 +08:00
setMotorState(MotorKB,Motor_ACT_CW);
2024-06-18 16:03:46 +08:00
autocalcounter[0] = 0;
}
break;
case 5://Motor2 xq
autocalcounter[0]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop1[MotorKB] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state++;
wait1 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorKB,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[0] > 3000 || MotorErr[MotorKB] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorKB,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
case 6://wait
wait1++;
if (wait1 > 50)
{
MotorArr1state++;
2024-12-09 08:35:30 +08:00
setMotorState(MotorKB,Motor_ACT_CCW);
2024-06-18 16:03:46 +08:00
autocalcounter[0] = 0;
}
break;
case 7:
autocalcounter[0]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop2[MotorKB] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state=0;
wait1 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorKB,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[0] > 3000 || MotorErr[MotorKB] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr1state = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorKB,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
default:
break;
}
//2
switch (MotorArr2state)
{
case 1://Motor3 xq
autocalcounter[1]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop1[MotorTT] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state++;
wait2 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorTT,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[1] > 3000 || MotorErr[MotorTT] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state = 4;
wait2 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorTT,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
case 2://wait
wait2++;
if (wait2 > 50)
{
MotorArr2state++;
2024-12-09 08:35:30 +08:00
setMotorState(MotorTT,Motor_ACT_CCW);
2024-06-18 16:03:46 +08:00
autocalcounter[1] = 0;
}
break;
case 3://Motor3 xh
autocalcounter[1]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop2[MotorTT] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state++;
wait2 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorTT,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[1] > 3000 || MotorErr[MotorTT] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state = 4;
wait2 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorTT,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
case 4://wait
wait2++;
if (wait2 > 50)
{
MotorArr2state++;
2024-12-09 08:35:30 +08:00
setMotorState(MotorZY,Motor_ACT_CW);
2024-06-18 16:03:46 +08:00
autocalcounter[1] = 0;
}
break;
case 5://Motor4 xq
autocalcounter[1]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop1[MotorZY] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state++;
wait2 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorZY,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[1] > 3000 || MotorErr[MotorZY] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorZY,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
case 6://wait
wait2++;
if (wait2 > 50)
{
MotorArr2state++;
2024-12-09 08:35:30 +08:00
setMotorState(MotorZY,Motor_ACT_CCW);
2024-06-18 16:03:46 +08:00
autocalcounter[1] = 0;
}
break;
case 7:
autocalcounter[1]++;
2024-12-09 08:35:30 +08:00
if (MotorHardStop2[MotorZY] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state=0;
wait2 = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorZY,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
2024-12-09 08:35:30 +08:00
if (autocalcounter[1] > 3000 || MotorErr[MotorZY] != 0)
2024-06-18 16:03:46 +08:00
{
MotorArr2state = 0;
2024-12-09 08:35:30 +08:00
setMotorState(MotorZY,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
}
break;
default:
break;
}
switch (MotorArr3state)
{
case 1://Motor5 xq
autocalcounter[2]++;
if (MotorHardStop1[Motor5] != 0)
{
MotorArr3state++;
wait3 = 0;
setMotorState(Motor5,Motor_ACT_NOACT);
}
if (autocalcounter[2] > 3000 || MotorErr[Motor5] != 0)
{
MotorArr3state = 4;
wait3 = 0;
setMotorState(Motor5,Motor_ACT_NOACT);
}
break;
case 2://wait
wait3++;
if (wait3 > 50)
{
MotorArr3state++;
setMotorState(Motor5,Motor_ACT_CCW);
autocalcounter[2] = 0;
}
break;
case 3://Motor5 xh
autocalcounter[2]++;
if (MotorHardStop2[Motor5] != 0)
{
MotorArr3state++;
wait3 = 0;
setMotorState(Motor5,Motor_ACT_NOACT);
}
if (autocalcounter[2] > 3000 || MotorErr[Motor5] != 0)
{
MotorArr3state = 4;
wait3 = 0;
setMotorState(Motor5,Motor_ACT_NOACT);
}
break;
case 4://wait
wait3++;
if (wait3 > 50)
{
MotorArr3state++;
setMotorState(Motor6,Motor_ACT_CW);
autocalcounter[2] = 0;
}
break;
case 5://Motor6 xq
autocalcounter[2]++;
if (MotorHardStop1[Motor6] != 0)
{
MotorArr3state++;
wait3 = 0;
setMotorState(Motor6,Motor_ACT_NOACT);
}
if (autocalcounter[2] > 3000 || MotorErr[Motor6] != 0)
{
MotorArr3state = 0;
setMotorState(Motor6,Motor_ACT_NOACT);
}
break;
case 6://wait
wait3++;
if (wait3 > 50)
{
MotorArr3state++;
setMotorState(Motor6,Motor_ACT_CCW);
autocalcounter[2] = 0;
}
break;
case 7:
autocalcounter[2]++;
if (MotorHardStop2[Motor6] != 0)
{
MotorArr3state=0;
wait3 = 0;
setMotorState(Motor6,Motor_ACT_NOACT);
}
if (autocalcounter[2] > 3000 || MotorErr[Motor6] != 0)
{
MotorArr3state = 0;
setMotorState(Motor6,Motor_ACT_NOACT);
}
break;
default:
break;
}
}
static void MotorCtrl(void)//10ms
{
uint8_t i;
//WriteMotorMemory(); //TODO
AutoCalCtrl();
if (OC1flag == 1)
{
OC1flag = 0;
if (MotorState[0] == Motor_ACT_CW)
{
MotorHardStop1[0] = MotorHallLoc[0];
}
else if (MotorState[0] == Motor_ACT_CCW)
{
MotorHardStop2[0] = MotorHallLoc[0];
}
else if (MotorState[1] == Motor_ACT_CW)
{
MotorHardStop1[1] = MotorHallLoc[1];
}
else if (MotorState[1] == Motor_ACT_CCW)
{
MotorHardStop2[1] = MotorHallLoc[1];
}
2024-12-09 08:35:30 +08:00
setMotorTarget(MotorHG,0);
setMotorTarget(MotorKB,0);
MotorState[MotorHG] = Motor_ACT_NOACT;
MotorState[MotorKB] = Motor_ACT_NOACT;
2024-06-18 16:03:46 +08:00
}
if (OC2flag == 1)
{
OC2flag = 0;
if (MotorState[2] == Motor_ACT_CW)
{
MotorHardStop1[2] = MotorHallLoc[2];
}
else if (MotorState[2] == Motor_ACT_CCW)
{
MotorHardStop2[2] = MotorHallLoc[2];
}
else if (MotorState[3] == Motor_ACT_CW)
{
MotorHardStop1[3] = MotorHallLoc[3];
}
else if (MotorState[3] == Motor_ACT_CCW)
{
MotorHardStop2[3] = MotorHallLoc[3];
}
2024-12-09 08:35:30 +08:00
setMotorTarget(MotorTT,0);
setMotorTarget(MotorZY,0);
MotorState[MotorTT] = Motor_ACT_NOACT;
MotorState[MotorZY] = Motor_ACT_NOACT;
2024-06-18 16:03:46 +08:00
}
if (OC3flag == 1)
{
OC3flag = 0;
if (MotorState[4] == Motor_ACT_CW)
{
MotorHardStop1[4] = MotorHallLoc[4];
}
else if (MotorState[4] == Motor_ACT_CCW)
{
MotorHardStop2[4] = MotorHallLoc[4];
}
else if (MotorState[5] == Motor_ACT_CW)
{
MotorHardStop1[5] = MotorHallLoc[5];
}
else if (MotorState[5] == Motor_ACT_CCW)
{
MotorHardStop2[5] = MotorHallLoc[5];
}
setMotorTarget(Motor5,0);
setMotorTarget(Motor6,0);
MotorState[Motor5] = Motor_ACT_NOACT;
MotorState[Motor6] = Motor_ACT_NOACT;
}
for (i = 0; i < 6; i++)
{
MotorStateReal[i] = MotorState[i];
if (MotorState[i] == Motor_ACT_NOACT && MotorHardStop1[i] != 0 && MotorHardStop2[i] != 0 )
{
if (MotorTarget[i]!=0 && MotorHardStop1[i] > MotorTarget[i] && MotorTarget[i] > MotorHardStop2[i])
{
if (MotorTarget[i] > MotorHallLoc[i]+10)
{
MotorStateReal[i] = Motor_ACT_CW;
}
else if (MotorTarget[i] < MotorHallLoc[i]-10)
{
MotorStateReal[i] = Motor_ACT_CCW;
}
else
{
MotorTarget[i] = 0;
}
}
}
else if (MotorHardStop1[i] != 0 && MotorHardStop2[i] != 0)
{
if (MotorHallLoc[i] > (MotorHardStop1[i]-20) && MotorStateReal[i] == Motor_ACT_CW)
{
MotorStateReal[i] = Motor_ACT_NOACT;
}
if (MotorHallLoc[i] < (MotorHardStop2[i] + 20) && MotorStateReal[i] == Motor_ACT_CCW)
{
MotorStateReal[i] = Motor_ACT_NOACT;
}
}
else
{
MotorTarget[i] = 0;
}
}
}
2024-12-09 08:35:30 +08:00
static void CurrentDetect(void)
{
2024-06-18 16:03:46 +08:00
2024-12-09 08:35:30 +08:00
}
2024-06-18 16:03:46 +08:00
/*******************************************************************************
* the global functions
******************************************************************************/
2024-05-14 16:05:43 +08:00
void MotorCtrl_Init(McuType *obj)
{
for (uint8_t i = 0; i < 6; i++)
{
hw_MotorCtrl(obj, i, Motor_ACT_NOACT);
}
2024-06-18 16:03:46 +08:00
MotorValueInit();
2024-05-14 16:05:43 +08:00
}
2024-12-09 08:35:30 +08:00
static void SetMotorMsg(void)
{
scm_canmatrix_tx.SCM_STATE.MOTOR_HG_STATE = MotorState[MotorHG];
scm_canmatrix_tx.SCM_STATE.MOTOR_KB_STATE = MotorState[MotorKB];
scm_canmatrix_tx.SCM_STATE.MOTOR_TT_STATE = MotorState[MotorTT];
scm_canmatrix_tx.SCM_STATE.MOTOR_ZY_STATE = MotorState[MotorZY];
}
2024-05-14 16:05:43 +08:00
void MotorCtrl_Maintask(McuType *obj)//10ms task
{
2024-06-18 16:03:46 +08:00
MotorCtrl();
for (Motor_ID_Type i = 0; i < MOTOR_NUM; i++)
{
hw_MotorCtrl(obj, i, MotorStateReal[i]);
}
2024-12-09 08:35:30 +08:00
SetMotorMsg();
2024-06-18 16:03:46 +08:00
}
void setMotorState(Motor_ID_Type motorid,Motor_ACT_Type act)
{
if (act < Motor_ACT_NUM && motorid < MOTOR_NUM)
{
MotorState[motorid] = act;
}
}
void setMotorTarget(uint8_t motorid,uint16_t target)
{
if (MotorHardStop1[motorid]!=0 && MotorHardStop2[motorid]!=0)
{
MotorTarget[motorid] = target;
}
else
{
MotorTarget[motorid] = 0;
}
}
void StartAutoCal(void)
{
AutoCalState = AUTOCAL_START;
}
void StopAutoCal(void)
{
uint8_t i;
if (AutoCalState != AUTOCAL_STOP)
2024-05-14 16:05:43 +08:00
{
2024-06-18 16:03:46 +08:00
AutoCalState = AUTOCAL_STOP;
2024-12-09 08:35:30 +08:00
setMotorState(MotorHG,Motor_ACT_NOACT);
setMotorState(MotorKB,Motor_ACT_NOACT);
setMotorState(MotorTT,Motor_ACT_NOACT);
setMotorState(MotorZY,Motor_ACT_NOACT);
2024-06-18 16:03:46 +08:00
setMotorState(Motor5,Motor_ACT_NOACT);
setMotorState(Motor6,Motor_ACT_NOACT);
}
for (i = 0; i < 6; i++)
{
if (MotorTarget[i] != 0)
{
MotorTarget[i] = 0;
stopflag = 1;
}
2024-05-14 16:05:43 +08:00
}
}
2024-06-18 16:03:46 +08:00
uint16_t getOverCurrentTh(uint8_t ch)
{
uint16_t th = 10;
switch (ch)
{
case 0:
2024-12-09 08:35:30 +08:00
if (MotorStateReal[MotorHG]!=Motor_ACT_NOACT)
2024-06-18 16:03:46 +08:00
{
th+=50;
}
2024-12-09 08:35:30 +08:00
if (MotorStateReal[MotorKB]!=Motor_ACT_NOACT)
2024-06-18 16:03:46 +08:00
{
th+=50;
}
break;
case 1:
2024-12-09 08:35:30 +08:00
if (MotorStateReal[MotorTT]!=Motor_ACT_NOACT)
2024-06-18 16:03:46 +08:00
{
th+=50;
}
2024-12-09 08:35:30 +08:00
if (MotorStateReal[MotorZY]!=Motor_ACT_NOACT)
2024-06-18 16:03:46 +08:00
{
th+=50;
}
break;
case 2:
if (MotorStateReal[Motor5]!=Motor_ACT_NOACT)
{
th+=50;
}
if (MotorStateReal[Motor6]!=Motor_ACT_NOACT)
{
th+=20;
}
break;
default:
break;
}
return th;
}
void HallDetecte(void)
{
static uint8_t HallLastState[6],HallDelay[6];
uint8_t i,hallstate;
for (i = 0; i < 6; i++)
{
hallstate = GetIOState(i+1);
if (hallstate != HallLastState[i])
{
HallDelay[i]++;
if (HallDelay[i] > HALLDELAYMAX)
{
if (MotorStateReal[i] == Motor_ACT_CW)
{
MotorHallLoc[i]++;
}
else if (MotorStateReal[i] == Motor_ACT_CCW)
{
MotorHallLoc[i]--;
}
HallLastState[i] = hallstate;
}
HallErrorCount[i] = 0;
MotorErr[i] = 0;
//EEL_SAVE_Counter = 1000;
//EEL_SAVE_REQUIRE_FLAG = 1;
}
else
{
HallDelay[i] = 0;
if (MotorStateReal[i] != Motor_ACT_NOACT)
{
HallErrorCount[i]++;
if (HallErrorCount[i] > 500)
{
HallErrorCount[i] = 500;
MotorErr[i] = 1;
}
}
}
}
/*
if (EEL_SAVE_Counter > 0)
{
EEL_SAVE_Counter--;
}
*/
}