Pressure control loop The following example shows how to combine a analog input for pressure measurement and a syringe pump for flow generation to build up a pressure control loop for pressure controlled dosage.
#include <labbcan/common/labbcan_test_helpers.h>
#include <thread>
#include <usl/core/PollingTimer.h>
#include <usl/core/Thread.h>
#include <log4cplus/logger.h>
#include <diag/diag.h>
#include <iostream>
using namespace std::chrono_literals;
using namespace Usl;
#define BOOST_TEST_MODULE capi_dynamic_controller_test
#include <boost/test/unit_test.hpp>
using namespace boost::unit_test;
struct ArgsFixture {
ArgsFixture(): argc(framework::master_test_suite().argc),
argv(framework::master_test_suite().argv){}
int argc;
char **argv;
};
BOOST_GLOBAL_FIXTURE( ArgsFixture );
static long Result;
BOOST_FIXTURE_TEST_CASE(testCapiOpen, ArgsFixture)
{
using namespace boost::unit_test;
const char* ConfigPath = "config/testconfig_qmixsdk";
if (argc > 1)
{
ConfigPath = argv[1];
}
REQUIRE_ERR_NOERR(Result);
}
BOOST_AUTO_TEST_CASE(testCapiBuildController)
{
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
}
BOOST_AUTO_TEST_CASE(testSetControlParam)
{
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
}
BOOST_AUTO_TEST_CASE(testBusStart)
{
Result =
LCB_Log(
"C-API Log Test Message");
}
bool waitDosageFinished(
dev_hdl hPump, uint32_t TimeoutSeconds)
{
CPollingTimer Timer(TimeoutSeconds * 1000);
CPollingTimer MessageTimer(500);
do
{
CThread::sleep(100);
if (MessageTimer.isExpired())
{
double FillLevel;
BOOST_TEST_MESSAGE("Fill level: " << FillLevel);
MessageTimer.restart();
}
}
while ((1 == Result) && !Timer.isExpired());
return (Result == 0);
}
BOOST_AUTO_TEST_CASE(testCapiPreparePump)
{
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
double MaxFlow;
REQUIRE_ERR_NOERR(Result);
waitDosageFinished(Pump, 30);
}
BOOST_AUTO_TEST_CASE(testControlLoop)
{
double Setpoint = 2;
REQUIRE_ERR_NOERR(Result);
REQUIRE_ERR_NOERR(Result);
BOOST_TEST_MESSAGE("Control loop enabled. Loop will run for 60 seconds.");
double ActualValue;
double CurrentFlow;
for (int i = 0; i < 30; ++i)
{
BREAK_ON_ERROR(Result);
BOOST_TEST_MESSAGE("Actual value: " << ActualValue);
BREAK_ON_ERROR(Result);
BOOST_TEST_MESSAGE("Current flow: " << CurrentFlow);
std::this_thread::sleep_for(1s);
}
REQUIRE_ERR_NOERR(Result);
BOOST_TEST_MESSAGE("Control loop disabled");
REQUIRE_ERR_NOERR(Result);
}
BOOST_AUTO_TEST_CASE(testCapiClose)
{
std::this_thread::sleep_for(2s);
}