|
|
@@ -12,6 +12,33 @@ |
|
|
|
#include "../../bsp/inc/time.h" |
|
|
|
#include "../inc/plsr.h" |
|
|
|
#include "../../bsp/inc/gpio.h" |
|
|
|
#include "../../system/inc/usart.h" |
|
|
|
|
|
|
|
/* 速度波形发送任务使能 */ |
|
|
|
#define SPEED_SEND_TASK 0 |
|
|
|
|
|
|
|
#if (SPEED_SEND_TASK) |
|
|
|
#define SPEED_SEND_MODULE_PRIO 20 |
|
|
|
#define SPEED_SEND_MODULE_STACK_SIZE 200 |
|
|
|
static OS_STK SpeedSendModuleStack[SPEED_SEND_MODULE_STACK_SIZE]; |
|
|
|
static void SpeedSendModuleTask(void * pArg); |
|
|
|
void UsartSend(uint32_t data) |
|
|
|
{ |
|
|
|
extern usart_obj_t *usart1; |
|
|
|
uint8_t buff[4]; |
|
|
|
buff[0] = (uint8_t)((data) >> 24); |
|
|
|
buff[1] = (uint8_t)((data) >> 16); |
|
|
|
buff[2] = (uint8_t)((data) >> 8); |
|
|
|
buff[3] = (uint8_t)((data)); |
|
|
|
for (uint8_t i = 0; i < 4; i++) |
|
|
|
{ |
|
|
|
USART_SendData(USART1, buff[i]);//发送一个字节 |
|
|
|
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) != SET); |
|
|
|
} |
|
|
|
} |
|
|
|
#define USART_SEND_SPEED(x) UsartSend(x) |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
/* 求x绝对值 */ |
|
|
|
#define ABS(x) do { \ |
|
|
@@ -42,6 +69,7 @@ static void ReadRegisterParam(PLSR *plsr); |
|
|
|
static void configPulsePort(PLSR *plsr); |
|
|
|
static void ComputePartNum(PLSR *plsr, SEGMENT_PART *segmentPart); |
|
|
|
static void StartPulseOutput(void); |
|
|
|
static void StopPulseOutput(void); |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief PLSR功能初始化 |
|
|
@@ -73,6 +101,21 @@ RESUIL PLSRInit(void) |
|
|
|
return MODBUS_FALSE; |
|
|
|
} |
|
|
|
|
|
|
|
#if (SPEED_SEND_TASK) |
|
|
|
err = OSTaskCreate( |
|
|
|
SpeedSendModuleTask, |
|
|
|
NULL, |
|
|
|
&SpeedSendModuleStack[SPEED_SEND_MODULE_STACK_SIZE - 1], |
|
|
|
SPEED_SEND_MODULE_PRIO); |
|
|
|
|
|
|
|
if(err != OS_ERR_NONE) |
|
|
|
{ |
|
|
|
MODBUS_LOG("SpeedSendModule init fail\r\n"); |
|
|
|
OS_EXIT_CRITICAL(); |
|
|
|
return MODBUS_FALSE; |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
OS_EXIT_CRITICAL(); |
|
|
|
|
|
|
|
return MODBUS_TURE; |
|
|
@@ -120,30 +163,28 @@ PLSR plsr = { |
|
|
|
.defaultDecSpeedTime = 10, |
|
|
|
.segment[0].pulseFreq = 2000, |
|
|
|
.segment[0].pulseNumber = 1000, |
|
|
|
.segment[0].jumpNumber = 3, |
|
|
|
.segment[0].jumpNumber = 0, |
|
|
|
.segment[1].pulseFreq = 1000, |
|
|
|
.segment[1].pulseNumber = 2000, |
|
|
|
.segment[1].jumpNumber = 4, |
|
|
|
.segment[1].jumpNumber = 0, |
|
|
|
.segment[2].pulseFreq = 6000, |
|
|
|
.segment[2].pulseNumber = 2000, |
|
|
|
.segment[2].jumpNumber = 2, |
|
|
|
.segment[2].pulseNumber = 3000, |
|
|
|
.segment[2].jumpNumber = 0, |
|
|
|
.segment[3].pulseFreq = 6000, |
|
|
|
.segment[3].pulseNumber = 10000, |
|
|
|
.segment[3].pulseNumber = 100000, |
|
|
|
.segment[3].jumpNumber = 0, |
|
|
|
.startRunSegment = 1 |
|
|
|
.startRunSegment = 1, |
|
|
|
.outMode = ABSOLUTE_MODE, |
|
|
|
.accDecSpeedMode = STRAIGHT_LINE_MODE, |
|
|
|
.sendEnableState = 1 |
|
|
|
}; |
|
|
|
|
|
|
|
void PlsrModuleTask(void *pArg) |
|
|
|
static void PlsrModuleTask(void *pArg) |
|
|
|
{ |
|
|
|
int8_t res; |
|
|
|
uint8_t pArgment = 0; |
|
|
|
//ReadRegisterParam(&plsr); |
|
|
|
|
|
|
|
uint8_t nextSegment = plsr.startRunSegment; |
|
|
|
|
|
|
|
// ReadRegisterParam(&plsr); |
|
|
|
|
|
|
|
res = GetTimObj("tim2", &tim2); |
|
|
|
res = GetTimObj("tim10", &tim10); |
|
|
|
GetTimObj("tim2", &tim2); |
|
|
|
GetTimObj("tim10", &tim10); |
|
|
|
|
|
|
|
tim2.init(&tim2, 0, 50000 - 1); |
|
|
|
tim2.open(&tim2); |
|
|
@@ -155,16 +196,15 @@ void PlsrModuleTask(void *pArg) |
|
|
|
tim10.pwmInit(&tim10); |
|
|
|
tim10.pwmSetPulse(&tim10, TIM10->ARR); |
|
|
|
|
|
|
|
plsr.sendEnableState = 1; |
|
|
|
plsr.accDecSpeedMode = STRAIGHT_LINE_MODE; |
|
|
|
|
|
|
|
currentSegmentID = plsr.startRunSegment; |
|
|
|
currentSegment = currentSegmentID - 1; |
|
|
|
|
|
|
|
int tmpTIMARR = 0; |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
// ReadRegisterParam(&plsr); |
|
|
|
CumulativePulseCount = tim2OverCnt * (TIM2->ARR + 1) + TIM2->CNT; |
|
|
|
tmpTIMARR = TIM2->ARR + 1; |
|
|
|
CumulativePulseCount = tim2OverCnt * tmpTIMARR + TIM2->CNT; |
|
|
|
plsr.monitorPulseNum = CumulativePulseCount; |
|
|
|
|
|
|
|
if (plsr.segment[currentSegment].pulseFreq <= 1000) |
|
|
@@ -175,33 +215,45 @@ void PlsrModuleTask(void *pArg) |
|
|
|
else |
|
|
|
continue; |
|
|
|
|
|
|
|
/* 配置输入输出相关端子 */ |
|
|
|
if ((plsr.sendEnableState != 1) || (RunFlag == 1)) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
if (plsr.outMode == ABSOLUTE_MODE) |
|
|
|
{ |
|
|
|
if (plsr.monitorPulseNum |
|
|
|
>= plsr.segment[currentSegment].pulseNumber) |
|
|
|
== plsr.segment[currentSegment].pulseNumber) |
|
|
|
{ |
|
|
|
plsr.sendEnableState = 0; |
|
|
|
StopPulseOutput(); |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* 配置输入输出相关端子 */ |
|
|
|
if ((plsr.sendEnableState != 1) || (RunFlag == 1)) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
configPulsePort(&plsr); |
|
|
|
|
|
|
|
if ((plsr.accDecSpeedMode == STRAIGHT_LINE_MODE) && (RunFlag == 0)) |
|
|
|
{ |
|
|
|
RunFlag = 1; |
|
|
|
|
|
|
|
configPulsePort(&plsr); |
|
|
|
ComputePartNum(&plsr, ¤tSeg); |
|
|
|
StartPulseOutput(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static void SpeedSendModuleTask(void * pArg) |
|
|
|
{ |
|
|
|
while(1) |
|
|
|
{ |
|
|
|
#if (SPEED_SEND_TASK) |
|
|
|
if (plsr.sendEnableState == 1) |
|
|
|
USART_SEND_SPEED(currentSpeed); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
//1HZ |
|
|
|
//psc 1680 一次10us |
|
|
|
//per 50000 |
|
|
@@ -254,8 +306,11 @@ static void TimCallBack(void *pArg) |
|
|
|
pulseCount++; |
|
|
|
|
|
|
|
if (pulseCount > currentSeg.firstPartNum - 1) |
|
|
|
{ |
|
|
|
pulseCount = 0; |
|
|
|
|
|
|
|
// currentSeg.currentPart = 2; |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
case 3: ///< 减速 |
|
|
@@ -281,8 +336,11 @@ static void TimCallBack(void *pArg) |
|
|
|
pulseCount++; |
|
|
|
|
|
|
|
if (pulseCount > currentSeg.firstPartNum - 1) |
|
|
|
{ |
|
|
|
pulseCount = 0; |
|
|
|
|
|
|
|
// currentSeg.currentPart = 2; |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
default: |
|
|
@@ -326,12 +384,11 @@ static void TimCallBack(void *pArg) |
|
|
|
currentSpeed = 0; |
|
|
|
pulseCount = 0; |
|
|
|
currentSeg.currentPart = 1; |
|
|
|
|
|
|
|
tim10.setPer(&tim10, 100); |
|
|
|
tim10.pwmSetPulse(&tim10, 65535); |
|
|
|
tim10.closeIrq(&tim10); |
|
|
|
|
|
|
|
|
|
|
|
StopPulseOutput(); |
|
|
|
|
|
|
|
allcount++; |
|
|
|
plsr.sendEnableState = 0; |
|
|
|
return; |
|
|
|
} |
|
|
|
currentSpeed -= currentSeg.dec; |
|
|
@@ -352,15 +409,15 @@ static void TimCallBack(void *pArg) |
|
|
|
|
|
|
|
static void ReadRegisterParam(PLSR *plsr) |
|
|
|
{ |
|
|
|
int16_t *pDataRegister = (uint16_t *)(DATA_REGISTER_ADRR + 0x1000); |
|
|
|
int16_t *pDataRegister = (int16_t *)(DATA_REGISTER_ADRR + 0x1000); |
|
|
|
|
|
|
|
plsr->outputPort = pDataRegister[0]; |
|
|
|
plsr->dirPort = pDataRegister[1]; |
|
|
|
plsr->extPort = pDataRegister[2]; |
|
|
|
plsr->outputPort = (PLSR_OUTPUT_PORT)pDataRegister[0]; |
|
|
|
plsr->dirPort = (PLSR_OUTPUT_DIR_PORT)pDataRegister[1]; |
|
|
|
plsr->extPort = (PLSR_INPUT_EXT_PORT)pDataRegister[2]; |
|
|
|
plsr->dirDelayTime = pDataRegister[3]; |
|
|
|
plsr->dirLogic = pDataRegister[4]; |
|
|
|
plsr->accDecSpeedMode = pDataRegister[5]; |
|
|
|
plsr->outMode = pDataRegister[6]; |
|
|
|
plsr->dirLogic = (PLSR_OUTPUT_DIR_PORT_LOGIC)pDataRegister[4]; |
|
|
|
plsr->accDecSpeedMode = (PLSR_ACC_DEC_SPEED_MODE)pDataRegister[5]; |
|
|
|
plsr->outMode = (PLSR_OUTPUT_MODE)pDataRegister[6]; |
|
|
|
plsr->segmentAllNum = pDataRegister[7]; |
|
|
|
plsr->startRunSegment = pDataRegister[8]; |
|
|
|
plsr->defaultSpeed = *((uint32_t *)pDataRegister[9]); |
|
|
@@ -444,10 +501,14 @@ static void configPulsePort(PLSR *plsr) |
|
|
|
|
|
|
|
static void ComputePartNum(PLSR *plsr, SEGMENT_PART *segmentPart) |
|
|
|
{ |
|
|
|
uint32_t targetFreq = plsr->segment[currentSegment].pulseFreq; |
|
|
|
uint32_t targetPulseCount = plsr->segment[currentSegment].pulseNumber; |
|
|
|
int32_t targetFreq = plsr->segment[currentSegment].pulseFreq; |
|
|
|
int32_t targetPulseCount = plsr->segment[currentSegment].pulseNumber; |
|
|
|
|
|
|
|
if (plsr->outMode == ABSOLUTE_MODE) |
|
|
|
targetPulseCount -= plsr->monitorPulseNum; |
|
|
|
|
|
|
|
ABS(targetPulseCount); |
|
|
|
|
|
|
|
|
|
|
|
accSpeedSlope = plsr->defaultSpeed / plsr->defaultAccSpeedTime; |
|
|
|
decSpeedSlope = plsr->defaultSpeed / plsr->defaultDecSpeedTime; |
|
|
|
|
|
|
@@ -458,6 +519,11 @@ static void ComputePartNum(PLSR *plsr, SEGMENT_PART *segmentPart) |
|
|
|
{ |
|
|
|
decTime = targetFreq / decSpeedSlope; |
|
|
|
segmentPart->thirdPartNum = targetFreq / 2 * decTime / 1000; |
|
|
|
|
|
|
|
if (segmentPart->thirdPartNum > targetPulseCount) |
|
|
|
{ |
|
|
|
segmentPart->thirdPartNum = targetPulseCount; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@@ -478,6 +544,11 @@ static void ComputePartNum(PLSR *plsr, SEGMENT_PART *segmentPart) |
|
|
|
{ |
|
|
|
segmentPart->firstPartNum = 0; |
|
|
|
} |
|
|
|
|
|
|
|
if ((segmentPart->firstPartNum + segmentPart->thirdPartNum) |
|
|
|
>= (targetPulseCount )) |
|
|
|
segmentPart->firstPartNum = targetPulseCount - segmentPart->thirdPartNum; |
|
|
|
|
|
|
|
segmentPart->secondPartNum = targetPulseCount |
|
|
|
- segmentPart->firstPartNum |
|
|
|
- segmentPart->thirdPartNum; |
|
|
@@ -538,3 +609,13 @@ static void StartPulseOutput(void) |
|
|
|
tim10.open(&tim10); |
|
|
|
tim10.openIrq(&tim10); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void StopPulseOutput(void) |
|
|
|
{ |
|
|
|
currentSpeed = 0; |
|
|
|
tim10.setPer(&tim10, 10); |
|
|
|
tim10.pwmSetPulse(&tim10, 65535); |
|
|
|
tim10.close(&tim10); |
|
|
|
tim10.openIrq(&tim10); |
|
|
|
} |