Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

TestCaseMatlabDataItemPID_1.cpp

00001 // 00002 // Copyright 2004 P&P Software GmbH - All Rights Reserved 00003 // 00004 // TestCaseMatlabDataItemPID_1.cpp 00005 // 00006 // Version 1.0 00007 // Date 29.10.03 (Version 1.0) 00008 // Author R. Totaro 00009 00010 #include "../GeneralInclude/CompilerSwitches.h" 00011 #include "../GeneralInclude/ClassId.h" 00012 #include "../GeneralInclude/Constants.h" 00013 #include "../Data/DC_DataItem.h" 00014 #include "../Data/ControlBlock.h" 00015 #include "../Data/DC_MatlabDataItemPID.h" 00016 #include "TestCaseMatlabDataItemPID_1.h" 00017 00018 TestCaseMatlabDataItemPID_1::TestCaseMatlabDataItemPID_1(void) : 00019 TestCaseGenericSetUp(ID_MATLABDATAITEMPID*10+1,"TestCaseMatlabDataItemPID_1") { 00020 return; 00021 } 00022 00023 void TestCaseMatlabDataItemPID_1::runTestCase(void) { 00024 TD_Float input; 00025 TD_Float output; 00026 const unsigned int numParams=3; 00027 DC_DataItem *pInput =new DC_DataItem(&input); 00028 DC_DataItem *pOutput =new DC_DataItem(&output); 00029 DC_MatlabDataItemPID *pMPID =new DC_MatlabDataItemPID; 00030 00031 // Verify correctness of class identifier 00032 if (pMPID->getClassId()!=ID_MATLABDATAITEMPID) { 00033 setTestResult(TEST_FAILURE,"Wrong class identifier"); 00034 return; 00035 } 00036 00037 // A newly created DC_MatlabDataItemPID shall not be configured 00038 if (pMPID->isObjectConfigured()) { 00039 setTestResult(TEST_FAILURE,"Newly created control block is not configured"); 00040 return; 00041 } 00042 00043 // Link the input and verify the block is not yet configured 00044 pMPID->setInputLink(0,pInput); 00045 if (pMPID->isObjectConfigured()) { 00046 setTestResult(TEST_FAILURE,"Wrong configuration status"); 00047 return; 00048 } 00049 00050 // Link the output and verify the block is now configured 00051 pMPID->setOutputLink(0,pOutput); 00052 if (!pMPID->isObjectConfigured()) { 00053 setTestResult(TEST_FAILURE,"failed to configure the control block"); 00054 return; 00055 } 00056 00057 // Verify the correctness of the number of parameters 00058 if (pMPID->getNumberOfParameters()!=numParams) { 00059 setTestResult(TEST_FAILURE,"Wrong number of parameters"); 00060 return; 00061 } 00062 00063 #ifdef HAVE_SIMULINK 00064 // Initialize the model's parameters 00065 const TD_Float defaultParameters[]={(TD_Float)-1.0,(TD_Float)1.0,(TD_Float)1.0}; 00066 00067 for (unsigned int i=0;i<numParams;i++) 00068 pMPID->setParameter(i,defaultParameters[i]); 00069 00070 // The following array contains the expected output values. These 00071 // are correct if and only if the initial value of Kp, Ki and Kd 00072 // (see pid.c and DC_MatlabDataItemPID.cpp) are, respectively, -1.0, 1.0 00073 // and 1.0. The value of the time step shall be 0.25. 00074 const TD_Float expectedResults[]={(TD_Float)3.0,(TD_Float)-0.75, 00075 (TD_Float)3.0,(TD_Float)-1.0,(TD_Float)0.25}; 00076 const TD_Float *pRes =expectedResults; 00077 00078 // Set the input to a fixed value and verify that 00079 // the model is invoked and properly working. 00080 // Initially the parameters are left at their default 00081 // values (see pid.c and DC_MatlabDataItemPID.cpp). 00082 input=(TD_Float)1.0; 00083 00084 // This is only to avoid an annoying warning 00085 output=(TD_Float)0.0; 00086 00087 pMPID->propagate(); 00088 if (fabsf(output-*pRes++)>=FLT_EPSILON) { 00089 setTestResult(TEST_FAILURE,"Control block returned wrong value"); 00090 return; 00091 } 00092 00093 pMPID->propagate(); 00094 if (fabsf(output-*pRes++)>=FLT_EPSILON) { 00095 setTestResult(TEST_FAILURE,"Control block returned wrong value"); 00096 return; 00097 } 00098 00099 // Let's reset the model and verify that we get the right output 00100 // value after a propagate. 00101 pMPID->reset(); 00102 pMPID->propagate(); 00103 if (fabsf(output-*pRes++)>=FLT_EPSILON) { 00104 setTestResult(TEST_FAILURE,"Control block reset failed"); 00105 return; 00106 } 00107 00108 // We now reset the model and set the value of Kd to 0.0f 00109 pMPID->reset(); 00110 pMPID->setParameter(2,(TD_Float)0.0); 00111 pMPID->propagate(); 00112 if (fabsf(output-*pRes++)>=FLT_EPSILON) { 00113 setTestResult(TEST_FAILURE,"Failed to set the D gain"); 00114 return; 00115 } 00116 00117 // We do the same with Kp 00118 pMPID->reset(); 00119 pMPID->setParameter(0,(TD_Float)0.0); 00120 pMPID->propagate(); 00121 pMPID->propagate(); 00122 if (fabsf(output-*pRes++)>=FLT_EPSILON) { 00123 setTestResult(TEST_FAILURE,"Failed to set the P gain"); 00124 return; 00125 } 00126 #else 00127 // This is done to prevent the test coverage results from 00128 // degrading when Simulink is not available. These two calls 00129 // become a no-op. 00130 pMPID->propagate(); 00131 pMPID->reset(); 00132 #endif 00133 00134 setTestResult(TEST_SUCCESS,"Test Successful"); 00135 return; 00136 }
Copyright 2003 P&P Software GmbH - All Rights Reserved