|
|
@@ -8,12 +8,16 @@ |
|
|
|
* @copyright Copyright (c) 2025 |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#include "../../system/inc/system.h" |
|
|
|
#include "../../bsp/inc/time.h" |
|
|
|
#include "../inc/plsr.h" |
|
|
|
#include "../../bsp/inc/gpio.h" |
|
|
|
|
|
|
|
/* 求x绝对值 */ |
|
|
|
#define ABS(x) do { \ |
|
|
|
if ((x) < 0) \ |
|
|
|
((x) = 0 - (x)); \ |
|
|
|
} while(0) |
|
|
|
|
|
|
|
typedef struct { |
|
|
|
int32_t inc; |
|
|
@@ -119,14 +123,14 @@ PLSR plsr = { |
|
|
|
.segment[0].pulseFreq = 2000, |
|
|
|
.segment[0].pulseNumber = 1000, |
|
|
|
.segment[0].jumpNumber = 0, |
|
|
|
.segment[1].pulseFreq = 3000, |
|
|
|
.segment[1].pulseFreq = 1000, |
|
|
|
.segment[1].pulseNumber = 2000, |
|
|
|
.segment[1].jumpNumber = 0, |
|
|
|
.segment[2].pulseFreq = 6000, |
|
|
|
.segment[2].pulseNumber = 2000, |
|
|
|
.segment[2].jumpNumber = 0, |
|
|
|
.segment[3].pulseFreq = 9000, |
|
|
|
.segment[3].pulseNumber = 100000, |
|
|
|
.segment[3].pulseFreq = 6000, |
|
|
|
.segment[3].pulseNumber = 10000, |
|
|
|
.segment[3].jumpNumber = 0, |
|
|
|
.startRunSegment = 0 |
|
|
|
}; |
|
|
@@ -161,14 +165,9 @@ void PlsrModuleTask(void *pArg) |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
// ReadRegisterParam(&plsr); |
|
|
|
|
|
|
|
if (plsr.sendEnableState != 1) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
CumulativePulseCount = tim2OverCnt * (TIM2->ARR + 1) + TIM2->CNT; |
|
|
|
|
|
|
|
plsr.monitorPulseNum = CumulativePulseCount; |
|
|
|
|
|
|
|
if (plsr.segment[currentSegment].pulseFreq <= 1000) |
|
|
|
pulseFreq = 2625 - 1; |
|
|
|
else if ((plsr.segment[currentSegment].pulseFreq > 1000) |
|
|
@@ -177,18 +176,23 @@ void PlsrModuleTask(void *pArg) |
|
|
|
else |
|
|
|
continue; |
|
|
|
|
|
|
|
/* 配置输入输出相关端子 */ |
|
|
|
configPulsePort(&plsr); |
|
|
|
|
|
|
|
if (plsr.outMode == ABSOLUTE_MODE) |
|
|
|
{ |
|
|
|
if (plsr.monitorPulseNum |
|
|
|
if (plsr.monitorPulseNum |
|
|
|
>= plsr.segment[currentSegment].pulseNumber) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* 配置输入输出相关端子 */ |
|
|
|
if ((plsr.sendEnableState != 1) || (RunFlag == 1)) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
configPulsePort(&plsr); |
|
|
|
|
|
|
|
if ((plsr.accDecSpeedMode == STRAIGHT_LINE_MODE) && (RunFlag == 0)) |
|
|
|
{ |
|
|
|
RunFlag = 1; |
|
|
@@ -273,6 +277,33 @@ static void TimCallBack(void *pArg) |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
case 3: ///< 减速 |
|
|
|
{ |
|
|
|
if ((currentSpeed - currentSeg.dec) < plsr.segment[currentSegment].pulseFreq) |
|
|
|
{ |
|
|
|
currentSpeed = plsr.segment[currentSegment].pulseFreq; |
|
|
|
|
|
|
|
currentSeg.currentPart = 2; |
|
|
|
} |
|
|
|
else |
|
|
|
currentSpeed -= currentSeg.dec; |
|
|
|
|
|
|
|
if (currentSpeed <= 1000) |
|
|
|
tim10.setPsc(&tim10, 2625 - 1); |
|
|
|
else |
|
|
|
tim10.setPsc(&tim10, 3 - 1); |
|
|
|
|
|
|
|
reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); |
|
|
|
tim10.setPer(&tim10, reloadValue - 1); |
|
|
|
tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); |
|
|
|
allcount++; |
|
|
|
pulseCount++; |
|
|
|
|
|
|
|
if (pulseCount > currentSeg.firstPartNum - 1) |
|
|
|
pulseCount = 0; |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
default: |
|
|
|
{ |
|
|
|
break; |
|
|
@@ -369,6 +400,7 @@ static void ReadRegisterParam(PLSR *plsr) |
|
|
|
|
|
|
|
static void configPulsePort(PLSR *plsr) |
|
|
|
{ |
|
|
|
uint8_t pulseDir = 1; |
|
|
|
gpio_obj_t *outDirPort = NULL; |
|
|
|
|
|
|
|
outDirPort = get_gpio_obj("gpio_h"); |
|
|
@@ -376,12 +408,16 @@ static void configPulsePort(PLSR *plsr) |
|
|
|
if ((outDirPort == NULL) || (plsr == NULL)) |
|
|
|
return; |
|
|
|
|
|
|
|
if (plsr->segment[currentSegment].pulseNumber >= 0) |
|
|
|
pulseDir = 1; |
|
|
|
else |
|
|
|
pulseDir = 0; |
|
|
|
|
|
|
|
switch (plsr->dirPort) |
|
|
|
{ |
|
|
|
case OUTPUT_DIR_PORT_Y12: |
|
|
|
{ |
|
|
|
outDirPort->gpio_init(outDirPort, gpio_pin_9, "OUT_PP", "UP"); |
|
|
|
outDirPort->gpio_set_output(outDirPort, gpio_pin_9, 1); |
|
|
|
break; |
|
|
|
} |
|
|
|
case OUTPUT_DIR_PORT_Y13: |
|
|
@@ -399,20 +435,35 @@ static void configPulsePort(PLSR *plsr) |
|
|
|
outDirPort->gpio_init(outDirPort, gpio_pin_6, "OUT_PP", "UP"); |
|
|
|
break; |
|
|
|
} |
|
|
|
default: |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
// switch (plsr->outputPort) |
|
|
|
// { |
|
|
|
// case OUTPUT_PORT_Y0: |
|
|
|
// { |
|
|
|
// outDirPort->gpio_init(outDirPort, gpio_pin) |
|
|
|
// } |
|
|
|
// } |
|
|
|
outDirPort->gpio_set_output(outDirPort, outDirPort->gpio_pin, |
|
|
|
plsr->dirLogic ^ pulseDir); |
|
|
|
if (currentSegment == plsr->startRunSegment) |
|
|
|
return; |
|
|
|
|
|
|
|
if (((plsr->segment[currentSegment].pulseNumber >= 0) |
|
|
|
&& (plsr->segment[plsr->previouSegment].pulseNumber >= 0))) |
|
|
|
return; |
|
|
|
|
|
|
|
if ((plsr->segment[currentSegment].pulseNumber < 0) |
|
|
|
&& (plsr->segment[plsr->previouSegment].pulseNumber < 0)) |
|
|
|
return; |
|
|
|
|
|
|
|
// tim10.pwmSetPulse(&tim10, 65535); |
|
|
|
OSTimeDly(plsr->dirDelayTime); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void ComputePartNum(PLSR *plsr, SEGMENT_PART *segmentPart) |
|
|
|
{ |
|
|
|
uint32_t target = plsr->segment[currentSegment].pulseFreq; |
|
|
|
uint32_t targetPulseCount = plsr->segment[currentSegment].pulseNumber; |
|
|
|
ABS(targetPulseCount); |
|
|
|
|
|
|
|
accSpeedSlope = plsr->defaultSpeed / plsr->defaultAccSpeedTime; |
|
|
|
decSpeedSlope = plsr->defaultSpeed / plsr->defaultDecSpeedTime; |
|
|
|
|
|
|
@@ -438,7 +489,11 @@ static void ComputePartNum(PLSR *plsr, SEGMENT_PART *segmentPart) |
|
|
|
decTime = (currentSpeed - target) / decSpeedSlope; |
|
|
|
segmentPart->firstPartNum = (target + currentSpeed) / 2 * decTime / 1000; |
|
|
|
} |
|
|
|
segmentPart->secondPartNum = plsr->segment[currentSegment].pulseNumber |
|
|
|
else |
|
|
|
{ |
|
|
|
segmentPart->firstPartNum = 0; |
|
|
|
} |
|
|
|
segmentPart->secondPartNum = targetPulseCount |
|
|
|
- segmentPart->firstPartNum |
|
|
|
- segmentPart->thirdPartNum; |
|
|
|
} |
|
|
@@ -449,10 +504,23 @@ static void FirstSegmentProc(SEGMENT_PART *segmentPart) |
|
|
|
int32_t targetFreq = 0; |
|
|
|
int32_t slope = 0; |
|
|
|
int32_t diffValue = 0; |
|
|
|
static int32_t incAll = 0; |
|
|
|
|
|
|
|
if (currentSpeed == plsr.segment[currentSegment].pulseFreq) |
|
|
|
segmentPart->currentState = 1; |
|
|
|
else if ((currentSpeed < plsr.segment[currentSegment].pulseFreq)) |
|
|
|
segmentPart->currentState = 2; |
|
|
|
else |
|
|
|
segmentPart->currentState = 3; |
|
|
|
|
|
|
|
switch (segmentPart->currentState) |
|
|
|
{ |
|
|
|
/* 匀速处理 */ |
|
|
|
case 1 : |
|
|
|
{ |
|
|
|
segmentPart->currentPart = 2; |
|
|
|
break; |
|
|
|
} |
|
|
|
/* 加速处理 */ |
|
|
|
case 2 : |
|
|
|
{ |
|
|
|
targetFreq = plsr.segment[currentSegment].pulseFreq; |
|
|
@@ -460,30 +528,37 @@ static void FirstSegmentProc(SEGMENT_PART *segmentPart) |
|
|
|
segmentPart->inc = ((targetFreq - currentSpeed) / slope) |
|
|
|
* ((targetFreq + currentSpeed) / 2) / 1000; |
|
|
|
segmentPart->inc = (targetFreq - currentSpeed) / segmentPart->inc; |
|
|
|
if ((currentSpeed + segmentPart->inc) >= plsr.segment[currentSegment].pulseFreq) |
|
|
|
{ |
|
|
|
diffValue = currentSpeed + segmentPart->inc - plsr.segment[currentSegment].pulseFreq; |
|
|
|
segmentPart->inc = segmentPart->inc - diffValue; |
|
|
|
} |
|
|
|
incAll += segmentPart->inc; |
|
|
|
if (currentSpeed <= 1000) |
|
|
|
{ |
|
|
|
tim10.setPsc(&tim10, 2625 - 1); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
tim10.setPsc(&tim10, 3 - 1); |
|
|
|
} |
|
|
|
if (currentSpeed != 0) |
|
|
|
tim10.setPer(&tim10, 168000000 / (TIM10->PSC + 1) / currentSpeed); |
|
|
|
else |
|
|
|
tim10.setPer(&tim10, 1000); |
|
|
|
tim10.registerIrq(&tim10, TimCallBack, ¤tSeg.currentPart); |
|
|
|
tim10.open(&tim10); |
|
|
|
tim10.openIrq(&tim10); |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
/* 减速处理 */ |
|
|
|
case 3 : |
|
|
|
{ |
|
|
|
targetFreq = plsr.segment[currentSegment].pulseFreq; |
|
|
|
slope = plsr.defaultSpeed / plsr.defaultDecSpeedTime; |
|
|
|
segmentPart->dec = ((currentSpeed - targetFreq) / slope) |
|
|
|
* ((currentSpeed + targetFreq) / 2) / 1000; |
|
|
|
segmentPart->dec = (currentSpeed - targetFreq) / segmentPart->dec; |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (currentSpeed <= 1000) |
|
|
|
{ |
|
|
|
tim10.setPsc(&tim10, 2625 - 1); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
tim10.setPsc(&tim10, 3 - 1); |
|
|
|
} |
|
|
|
if (currentSpeed != 0) |
|
|
|
tim10.setPer(&tim10, 168000000 / (TIM10->PSC + 1) / currentSpeed); |
|
|
|
else |
|
|
|
tim10.setPer(&tim10, 1000); |
|
|
|
tim10.registerIrq(&tim10, TimCallBack, ¤tSeg.currentPart); |
|
|
|
tim10.open(&tim10); |
|
|
|
tim10.openIrq(&tim10); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|