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

TestCaseMatlabDataPoolPID_1.cpp

00001 // 00002 // Copyright 2004 P&P Software GmbH - All Rights Reserved 00003 // 00004 // TestCaseMatlabDataPoolPID_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_BasicDataPool.h" 00014 #include "../Data/DC_MatlabDataPoolPID.h" 00015 #include "../Data/ControlBlock.h" 00016 #include "TestCaseMatlabDataPoolPID_1.h" 00017 00018 TestCaseMatlabDataPoolPID_1::TestCaseMatlabDataPoolPID_1(void) : 00019 TestCaseGenericSetUp(ID_MATLABDATAPOOLPID*10+1,"TestCaseMatlabDataPoolPID_1") { 00020 return; 00021 } 00022 00023 void TestCaseMatlabDataPoolPID_1::runTestCase(void) { 00024 const unsigned int numParams=3; 00025 const TD_DataPoolId inputId =0; 00026 const TD_DataPoolId outputId =sizeof(TD_Float); 00027 DC_BasicDataPool *pDataPool=new DC_BasicDataPool; 00028 DC_MatlabDataPoolPID *pMPID =new DC_MatlabDataPoolPID; 00029 00030 // Initialize the data pool 00031 pDataPool->setDataPoolSize(2*sizeof(TD_Float)); 00032 CC_RootObject::setDataPool(pDataPool); 00033 00034 // Verify correctness of class identifier 00035 if (pMPID->getClassId()!=ID_MATLABDATAPOOLPID) { 00036 setTestResult(TEST_FAILURE,"Wrong class identifier"); 00037 return; 00038 } 00039 00040 // A newly created DC_MatlabDataPoolPID shall not be configured 00041 if (pMPID->isObjectConfigured()) { 00042 setTestResult(TEST_FAILURE,"Newly created control block is not configured"); 00043 return; 00044 } 00045 00046 // Link the input and verify the block is not yet configured 00047 pMPID->setInputLink(0,inputId); 00048 if (pMPID->isObjectConfigured()) { 00049 setTestResult(TEST_FAILURE,"Wrong configuration status"); 00050 return; 00051 } 00052 00053 // Link the output and verify the block is now configured 00054 pMPID->setOutputLink(0,outputId); 00055 if (!pMPID->isObjectConfigured()) { 00056 setTestResult(TEST_FAILURE,"Failed to configure the control block"); 00057 return; 00058 } 00059 00060 // Verify the correctness of the number of parameters 00061 if (pMPID->getNumberOfParameters()!=numParams) { 00062 setTestResult(TEST_FAILURE,"Wrong number of parameters"); 00063 return; 00064 } 00065 00066 #ifdef HAVE_SIMULINK 00067 // Initialize the model's parameters 00068 const TD_Float defaultParameters[]={(TD_Float)-1.0,(TD_Float)1.0,(TD_Float)1.0}; 00069 00070 for (unsigned int i=0;i<numParams;i++) 00071 pMPID->setParameter(i,defaultParameters[i]); 00072 00073 // The following array contains the expected output values. These 00074 // are correct if and only if the initial value of Kp, Ki and Kd 00075 // (see pid.c and DC_MatlabDataPoolPID.cpp) are, respectively, -1.0, 1.0 00076 // and 1.0. The value of the time step shall be 0.25. 00077 const TD_Float expectedResults[]={(TD_Float)3.0,(TD_Float)-0.75, 00078 (TD_Float)3.0,(TD_Float)-1.0,(TD_Float)0.25}; 00079 const TD_Float *pRes =expectedResults; 00080 00081 // Set the input to a fixed value and verify that 00082 // the model is invoked and properly working. 00083 // Initially the parameters are left at their default 00084 // values (see pid.c and DC_MatlabDataPoolPID.cpp). 00085 pDataPool->setValue(inputId,(TD_Float)1.0); 00086 00087 pMPID->propagate(); 00088 if (fabsf(pDataPool->getFloatValue(outputId)-*pRes++)>=FLT_EPSILON) { 00089 setTestResult(TEST_FAILURE,"Control block returned wrong value"); 00090 return; 00091 } 00092 00093 pMPID->propagate(); 00094 if (fabsf(pDataPool->getFloatValue(outputId)-*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(pDataPool->getFloatValue(outputId)-*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(pDataPool->getFloatValue(outputId)-*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(pDataPool->getFloatValue(outputId)-*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