From ba4a9f0c58a19c9a88457f908451c2aebf72b255 Mon Sep 17 00:00:00 2001 From: 1923777848 <1923777848@qq.com> Date: Tue, 12 Aug 2025 13:45:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9B=B4=E7=BA=BF=E5=8A=A0?= =?UTF-8?q?=E5=87=8F=E9=80=9F=E5=A4=9A=E6=AE=B5=E8=B7=B3=E8=BD=AC=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TrainCamp_zhangcheng_PLSR/app/src/plsr.c | 98 +++++++++++++----------- TrainCamp_zhangcheng_PLSR/bsp/src/time.c | 14 +++- 2 files changed, 67 insertions(+), 45 deletions(-) diff --git a/TrainCamp_zhangcheng_PLSR/app/src/plsr.c b/TrainCamp_zhangcheng_PLSR/app/src/plsr.c index c825d1a..ad8409c 100644 --- a/TrainCamp_zhangcheng_PLSR/app/src/plsr.c +++ b/TrainCamp_zhangcheng_PLSR/app/src/plsr.c @@ -99,7 +99,7 @@ int32_t uniformTime = 0; int32_t uniformPulse = 0; uint8_t currentSegment = 0; uint32_t currentSpeed = 0; - +uint8_t RunFlag = 0; int32_t pulseFreq = 168 - 1; uint32_t pulseReload = 20; @@ -109,16 +109,29 @@ SEGMENT_PART currentSeg = { .currentPart = 1 }; PLSR plsr = { - .segmentAllNum = 1, - .segment[0].jumpNumber = 0 - }; + .segmentAllNum = 4, + .defaultSpeed = 1000, + .defaultAccSpeedTime = 5, + .defaultDecSpeedTime = 10, + .segment[0].pulseFreq = 2000, + .segment[0].pulseNumber = 1000, + .segment[0].jumpNumber = 0, + .segment[1].pulseFreq = 3000, + .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 = 2000, + .segment[3].jumpNumber = 0, + .startRunSegment = 0 +}; void PlsrModuleTask(void *pArg) { int8_t res; uint8_t pArgment = 0; -uint8_t RunFlag = 0; - uint8_t nextSegment = plsr.startRunSegment; @@ -134,28 +147,25 @@ uint8_t RunFlag = 0; tim10.pwmInit(&tim10); tim10.pwmSetPulse(&tim10, TIM10->ARR); - plsr.sendEnableState = 0; + plsr.sendEnableState = 1; plsr.accDecSpeedMode = STRAIGHT_LINE_MODE; - plsr.segment[0].pulseFreq = 2000; - plsr.segment[0].pulseNumber = 3000; - plsr.defaultSpeed = 1000; - plsr.defaultAccSpeedTime = 10; - plsr.defaultDecSpeedTime = 10; + + currentSegment = plsr.startRunSegment; + while (1) { // ReadRegisterParam(&plsr); - currentSegment = plsr.currentSegment; - + if (plsr.sendEnableState != 1) { continue; } if (plsr.segment[currentSegment].pulseFreq <= 5000) - pulseFreq = 1680 - 1; + pulseFreq = 3360 - 1; else if ((plsr.segment[currentSegment].pulseFreq > 5000) && (plsr.segment[currentSegment].pulseFreq <= 100000)) - pulseFreq = 84 - 1; + pulseFreq = 2 - 1; else continue; @@ -216,9 +226,7 @@ uint8_t RunFlag = 0; void TimCallBack(void *pArg) { uint16_t reloadValue = 0; - uint32_t diffValue; static int32_t pulseCount = 0; - static uint8_t endFlag = 0; pulseCount++; if (currentSeg.currentPart == 1) @@ -227,23 +235,20 @@ void TimCallBack(void *pArg) { case 2: ///< 加速 { - if ((currentSpeed + currentSeg.inc) >= plsr.segment[currentSegment].pulseFreq) + if ((currentSpeed + currentSeg.inc) >= plsr.segment[currentSegment].pulseFreq + || (pulseCount > currentSeg.firstPartNum - 1)) { - diffValue = currentSpeed + currentSeg.inc - plsr.segment[currentSegment].pulseFreq; - currentSeg.inc = currentSeg.inc - diffValue; + currentSpeed = plsr.segment[currentSegment].pulseFreq; + + currentSeg.currentPart = 2; + pulseCount = 0; } - currentSpeed += currentSeg.inc; + else + currentSpeed += currentSeg.inc; reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); tim10.setPer(&tim10, reloadValue - 1); - tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); - - if (pulseCount > currentSeg.firstPartNum - 1) - { - currentSeg.currentPart = 2; - pulseCount = 0; - tim10.registerIrq(&tim10, TimCallBack, ¤tSeg.currentPart); - } + tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); break; } default: @@ -254,23 +259,30 @@ void TimCallBack(void *pArg) } else if (currentSeg.currentPart == 2) { - reloadValue = 168000000 / (TIM10->PSC + 1) / (currentSpeed); - tim10.setPer(&tim10, reloadValue - 1); - tim10.pwmSetPulse(&tim10, TIM10->ARR / 2 + 1); - - if (pulseCount > currentSeg.secondPartNum - 1) + if (pulseCount > currentSeg.secondPartNum - 4) { - currentSeg.currentPart = 3; - pulseCount = 0; - tim10.registerIrq(&tim10, TimCallBack, ¤tSeg.currentPart); + if ((currentSegment == (plsr.segmentAllNum -1)) + && (plsr.segment[currentSegment].jumpNumber == 0)) + { + currentSeg.currentPart = 3; + pulseCount = 0; + currentSeg.dec = currentSpeed / currentSeg.thirdPartNum; + return; + } - currentSeg.dec = currentSpeed / currentSeg.thirdPartNum; + currentSeg.currentPart = 1; + pulseCount = 0; + RunFlag = 0; + if (plsr.segment[currentSegment].jumpNumber == 0) + currentSegment++; + else + currentSegment = plsr.segment[currentSegment].jumpNumber; } } else if (currentSeg.currentPart == 3) { if (((currentSpeed - currentSeg.dec) <= 0) - || (pulseCount > currentSeg.thirdPartNum - 1)) + || (pulseCount > currentSeg.thirdPartNum)) { pulseCount = 0; currentSeg.currentPart = 1; @@ -423,16 +435,14 @@ static void FirstSegmentProc(SEGMENT_PART *segmentPart) incAll += segmentPart->inc; if (plsr.segment[currentSegment].pulseFreq <= 5000) { - tim10.setPsc(&tim10, 1680 - 1); + tim10.setPsc(&tim10, 3360 - 1); if (currentSpeed != 0) - tim10.setPer(&tim10, 100000 / currentSpeed); + tim10.setPer(&tim10, 50000 / currentSpeed); else tim10.setPer(&tim10, 1000); tim10.registerIrq(&tim10, TimCallBack, ¤tSeg.currentPart); tim10.open(&tim10); tim10.openIrq(&tim10); - - } else { diff --git a/TrainCamp_zhangcheng_PLSR/bsp/src/time.c b/TrainCamp_zhangcheng_PLSR/bsp/src/time.c index c53adb5..17e8e1f 100644 --- a/TrainCamp_zhangcheng_PLSR/bsp/src/time.c +++ b/TrainCamp_zhangcheng_PLSR/bsp/src/time.c @@ -70,19 +70,32 @@ int8_t TimInit(TIM_OBJ *tim, uint16_t psc, uint32_t per) if (tim->TIM == TIM2) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; } else if (tim->TIM == TIM10) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10,ENABLE); + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; } else if (tim->TIM == TIM11) + { RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11,ENABLE); + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; + } + else if (tim->TIM == TIM13) + { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE); + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; + } + else if (tim->TIM == TIM14) + { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); + TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; + } else return -1; @@ -92,7 +105,6 @@ int8_t TimInit(TIM_OBJ *tim, uint16_t psc, uint32_t per) else TIM_InternalClockConfig(tim->TIM); - TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period=per; TIM_TimeBaseInitStructure.TIM_Prescaler=psc;