From da604b760c72886bf16128b91c21f49298613c9a Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 9 Oct 2010 13:53:55 +0200 Subject: add absolute position control --- AT91SAM7S256/Source/d_output.c | 76 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) (limited to 'AT91SAM7S256/Source/d_output.c') diff --git a/AT91SAM7S256/Source/d_output.c b/AT91SAM7S256/Source/d_output.c index 63613f9..b6c72ab 100644 --- a/AT91SAM7S256/Source/d_output.c +++ b/AT91SAM7S256/Source/d_output.c @@ -35,6 +35,13 @@ #define COAST_MOTOR_MODE 0 +#define BOUND(var, minmax) do { \ + if ((var) > (minmax)) \ + (var) = (minmax); \ + else if ((var) < -(minmax)) \ + (var) = -(minmax); \ +} while (0) + void dOutputRampDownSynch(UBYTE MotorNr); typedef struct @@ -278,7 +285,12 @@ void dOutputSetPIDParameters(UBYTE MotorNr, UBYTE NewRegPParameter, UBYTE NewReg void dOutputSetTachoLimit(UBYTE MotorNr, ULONG BlockTachoCntToTravel) { MOTORDATA * pMD = &(MotorData[MotorNr]); - if (BlockTachoCntToTravel == 0) + if (pMD->RegulationMode & REGSTATE_POSITION) + { + pMD->MotorRunForever = 0; + pMD->MotorTachoCountToRun = BlockTachoCntToTravel; + } + else if (BlockTachoCntToTravel == 0) { pMD->MotorRunForever = 1; } @@ -664,6 +676,11 @@ void dOutputRampDownFunction(UBYTE MotorNr) void dOutputTachoLimitControl(UBYTE MotorNr) { MOTORDATA * pMD = &(MotorData[MotorNr]); + if (pMD->RegulationMode & REGSTATE_POSITION) + { + /* No limit when doing absolute position regulation. */ + return; + } if (pMD->MotorRunForever == 0) { if (pMD->RegulationMode & REGSTATE_SYNCHRONE) @@ -761,7 +778,11 @@ void dOutputRegulateMotor(UBYTE MotorNr) UBYTE SyncMotorTwo; MOTORDATA * pMD = &(MotorData[MotorNr]); - if (pMD->RegulationMode & REGSTATE_REGULATED) + if (pMD->RegulationMode & REGSTATE_POSITION) + { + dOutputAbsolutePositionRegulation(MotorNr); + } + else if (pMD->RegulationMode & REGSTATE_REGULATED) { dOutputCalculateMotorPosition(MotorNr); } @@ -779,6 +800,57 @@ void dOutputRegulateMotor(UBYTE MotorNr) } } +/* Absolute position regulation. */ +void dOutputAbsolutePositionRegulation(UBYTE MotorNr) +{ + /* Inputs: + * - CurrentCaptureCount: current motor position. + * - MotorTachoCountToRun: wanted position. + * + * - RegPParameter, RegIParameter, RegDParameter: PID parameters. + * + * Outputs: + * - MotorActualSpeed: power to be applied to motor. + * - MotorOverloaded: set if MotorActualSpeed reached maximum. + * + * - OldPositionError: remember last error, used for D part. + * - AccError: Accumulated error, used for I part. + */ + int PositionError; + int PValue, DValue, IValue, TotalRegValue; + + MOTORDATA *pMD = &MotorData[MotorNr]; + + PositionError = pMD->MotorTachoCountToRun - pMD->CurrentCaptureCount; + BOUND (PositionError, 32000); + + PValue = PositionError * pMD->RegPParameter / REG_CONST_DIV; + BOUND (PValue, REG_MAX_VALUE); + + DValue = (PositionError - pMD->OldPositionError) * pMD->RegDParameter / REG_CONST_DIV; + pMD->OldPositionError = PositionError; + + pMD->AccError = (pMD->AccError * 3 + PositionError) / 4; + BOUND (pMD->AccError, 800); + + IValue = pMD->AccError * pMD->RegIParameter / REG_CONST_DIV; + BOUND (IValue, REG_MAX_VALUE); + + TotalRegValue = (PValue + IValue + DValue) / 2; + if (TotalRegValue > MAXIMUM_SPEED_FW) + { + TotalRegValue = MAXIMUM_SPEED_FW; + pMD->MotorOverloaded = 1; + } + else if (TotalRegValue < MAXIMUM_SPEED_RW) + { + TotalRegValue = MAXIMUM_SPEED_RW; + pMD->MotorOverloaded = 1; + } + + pMD->MotorActualSpeed = TotalRegValue; +} + /* Regulation function used when Position regulation is enabled */ /* The regulation form only control one motor at a time */ void dOutputCalculateMotorPosition(UBYTE MotorNr) -- cgit v1.2.3