aboutsummaryrefslogtreecommitdiff
path: root/AT91SAM7S256/Source/d_output.c
diff options
context:
space:
mode:
authorNicolas Schodet2010-10-09 13:53:55 +0200
committerNicolas Schodet2011-01-21 23:41:18 +0100
commitda604b760c72886bf16128b91c21f49298613c9a (patch)
tree240daac276587b7fac8279d177dd23681050d0ec /AT91SAM7S256/Source/d_output.c
parent1b20bc3d4974a6f03031056910a95ca615a7a5ef (diff)
add absolute position control
Diffstat (limited to 'AT91SAM7S256/Source/d_output.c')
-rw-r--r--AT91SAM7S256/Source/d_output.c76
1 files changed, 74 insertions, 2 deletions
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)