Skip to content. | Skip to navigation

Personal tools

presvac.cpp

presvac.cpp

presvac.cpp

/////////////////////////////////////////////////////////////////////////////
// PRESVAC.CPP
//
//  Last updated: 10/11/2003
//
//  Part of the source code for the M3 Program
//
//  This file contains code for the control of the pressure/vacuum behind
//  the tertiary mirror.
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// PresVacClockInterrupt - Called on each clock interrupt for pres/vac control
void PresVacClockInterrupt()
// Opens and closes pressure/vacuum valves with millisecond precision
// according to the pulse widths given by the control variables
// VacuumPulseWidth and PressurePulseWidth.
{
	static int vwidth,pwidth;	// Used to store a snapshot of the control variables

	if (PresVacMode == PVMAuto)
	{
		if ((msTimer - PulseTime) >= PulseCycle)
		{
			vwidth = VacuumPulseWidth;
			pwidth = PressurePulseWidth;
			PulseTime = msTimer;
	#ifdef ENABLEIO
			write_bit(2,!(vwidth > 0));
			write_bit(3,!(pwidth > 0));
	#endif
		}
	#ifdef ENABLEIO
		if (((msTimer - PulseTime) >= vwidth) && (vwidth < PulseCycle))
			write_bit(2,1);
		if (((msTimer - PulseTime) >= pwidth) && (pwidth < PulseCycle))
			write_bit(3,1);
	#endif
	}
}

/////////////////////////////////////////////////////////////////////////////
// CPresVac::Init - Initialize pressure/vacuum control
void CPresVac::Init()
{

/* These variables are initialized from M3.INI
	Kp = 0.0;
	Ki = 0.0;
	Kd = 0.0;
	VIl = 0.0;
	PIl = 0.0;
	IntegralThreshold = 0.0;
	PressureMinPulseWidth = 0;
	VacuumMinPulseWidth = 0;
	PulseCycle = 200;
*/

	PulseUpdateTime = msTimer;
	olderr = 0.0;
	target = 0.0;
  ave = 0;
	P = 0.0;
	I = 0.0;
	D = 0.0;
  // Change driver chip U3 reverse logic for PRES&VAC signals at start up.
	// Put them OFF (close both valves).
	SetDigBit(3);
	SetDigBit(2);

}

/////////////////////////////////////////////////////////////////////////////
// CPresVac::Update - Update pressure/vacuum control variables
void CPresVac::Update()
{
	char s[50];

	if ((msTimer - PulseUpdateTime) > PulseCycle)
	{
		PulseUpdateTime  = msTimer;
		ave = (DGHs[0].Reading + DGHs[1].Reading + DGHs[2].Reading) / 3.0;
		err = target - ave;
		PresVacChart.Add(ave);

    if (PresVacMode == PVMAuto)
    {
		  // Calc derivative term
		  D = Kd * (err - olderr);
		  olderr = err;

		  // Calc proportional term
		  P = Kp * err;

		  // Calc integral term
		  I = I + Ki * err;

		  if (fabs(err) > IntegralThreshold)
			  I = 0.0;

		  // Limit I to within +/- VIl/PIl
		  if (I > VIl)
			  I = VIl;
		  if (I < -PIl)
			  I = -PIl;
    }
    else
		{
      P = 0.0;
      I = 0.0;
      D = 0.0;
    }

		// Calculate pulse width
		disable();

		if ((P + I + D) > 0.0)
		{
			VacuumPulseWidth = PulseCycle * (P + I + D) + VacuumMinPulseWidth;
			PressurePulseWidth = PressureMinPulseWidth;
		}
		else if ((P + I + D) < 0.0)
		{
			PressurePulseWidth = -PulseCycle * (P + I + D) + PressureMinPulseWidth;
			VacuumPulseWidth = VacuumMinPulseWidth;
		}
		else
		{
			VacuumPulseWidth = VacuumMinPulseWidth;
			PressurePulseWidth = PressureMinPulseWidth;
		}

		// Limit pulse width to 0..PulseCycle
		if (VacuumPulseWidth > PulseCycle) VacuumPulseWidth = PulseCycle;
		if (PressurePulseWidth > PulseCycle) PressurePulseWidth = PulseCycle;

		enable();

		// Update screen
	  sprintf(s,"P:%7.3f",P);
	  PutString16(s,556,629,264,BLACK,SHECMEDGRAY);
	  sprintf(s,"I:%7.3f",I);
	  PutString16(s,556,629,280,BLACK,SHECMEDGRAY);
	  sprintf(s,"D:%7.3f",D);
		PutString16(s,556,629,296,BLACK,SHECMEDGRAY);
	  //sprintf(s,"Kp:%6.3f",Kp);
		//PutString16(s,556,629,264,BLACK,SHECMEDGRAY);
		//sprintf(s,"Ki:%6.3f",Ki);
		//PutString16(s,556,629,280,BLACK,SHECMEDGRAY);
		//sprintf(s,"Kd:%6.3f",Kd);
		//PutString16(s,556,629,296,BLACK,SHECMEDGRAY);
		sprintf(s,"%4d",PressurePulseWidth);
		PutString16(s,484,516,312,BLACK,SHECMEDGRAY);
		sprintf(s,"%4d",VacuumPulseWidth);
		PutString16(s,484,516,328,BLACK,SHECMEDGRAY);
	  // Display M3 average weight in Lbs
    sprintf(s,"%+7.3f",ave*49.0);
	  PutString16(s,532,588,216,BLACK,SHECMEDGRAY);
	}

	// Display pressure/vacuum valve status
	if (!GetDigBit(2)) // Vacuum
		PutChar16('þ',468,328,SHECGREEN,SHECMEDGRAY);
	else
		PutChar16('þ',468,328,BLACK,SHECMEDGRAY);
	if (!GetDigBit(3)) // Pressure
		PutChar16('þ',468,312,SHECGREEN,SHECMEDGRAY);
	else
		PutChar16('þ',468,312,BLACK,SHECMEDGRAY);
}

/////////////////////////////////////////////////////////////////////////////
// CPresVac::Display - Display infrequently changed information
void CPresVac::Display()
{
  char s[10];

	// Display Mode
	if (PresVacMode == PVMAuto)
		PutString16("AUTO",596,629,312,BLACK,SHECMEDGRAY);
	else
		PutString16(" OFF",596,629,312,BLACK,SHECMEDGRAY);
  // Display graph scale
  sprintf(s,"ñ%3.1f",PresVacChart.GetScale());
  PutString16(s,372,412,276,BLACK,SHECMEDGRAY);
}

Generated by GNU Enscript 1.6.5.2.
Document Actions