Listing 3: Derived-class functions for PID Control
PipingSet::PipingSet( double kP, double kI, double kD,
double lI, double cD) :
PidControl( kP, kI, kD, lI, cD )
{
CurrentMode = PIPE_MEASURE;
mbox = OSMboxCreate( (void*) 0 );
OSTaskCreate( (void(*) (void*))PipingSet::StartControlTask,
(void *)this, &TaskStack[CONTROL_STACK_SIZE],
PD_CONTROL_PRIO );
}
//-----------------------------------------------------------------
void PipingSet::SetSetpoint( double SetPoint )
{
OSMboxPost( mbox, (void*)CHANGE_SETPOINT_START );
PidControl::SetSetpoint( ( SetPoint / FullScale ) * 100 );
OSMboxPost( mbox, (void*)SETPOINT_COMPLETE );
}
//-----------------------------------------------------------------
void PipingSet::ControlTask( void )
{
UBYTE err;
double pressure;
int stop_control;
double gain;
float volts;
void *msg;
for(;;) {
msg = OSMboxPend( mbox, 0, &err );
if( (int ) msg == START_CONTROL ) {
CurrentMode = PIPE_CONTROL;
stop_control = FALSE;
while( !stop_control ) {
pressure = controlSensor->get_pressure();
gain = CalculateGain( ( pressure / FullScale ) * 100 );
volts = -( gain / 100 );
if( volts > 10 )
volts = 10;
else if( volts < -10 )
volts = -10;
SetServoVoltage( volts );
msg = OSMboxPend( mbox, 1, &err );
if( err == OS_NO_ERR ) {
if( (int)msg == STOP_CONTROL ) {
SetServoVoltage( 0.0 );
stop_control = true;
}
else if( (int)msg == CHANGE_SETPOINT_START ) {
do
msg = OSMboxPend( mbox, 0, &err );
while( (int)msg != SETPOINT_COMPLETE );
}
}
}
CurrentMode = PIPE_MEASURE;
}
}
}
//End of File