Commit ad1ea7f6 authored by legoc's avatar legoc
Browse files

First version of VEXPController.

parent 381774a3
......@@ -19,48 +19,143 @@
#include "VEXPController.h"
#include "controllers/common/family/Families.h"
using namespace boost;
namespace vexp {
const string VEXPController::TYPE = "vexp_controller";
const int32 VEXPController::PARAMETERS_OK = 1;
const int32 VEXPController::BAD_PARAMETERS = 2;
VEXPController::VEXPController(const string& name) :
ExperimentController(name) {
ExperimentController(name), m_running(true), m_errorDuringCalculation(false) {
setFamily(family::HIDDEN);
as.init(this, NOSAVE, "as", "AS");
bs.init(this, NOSAVE, "bs", "BS");
cs.init(this, NOSAVE, "cs", "CS");
aa.init(this, NOSAVE, "aa", "AA");
bb.init(this, NOSAVE, "bb", "BB");
cc.init(this, NOSAVE, "cc", "CC");
ax.init(this, NOSAVE, "ax", "AX");
ay.init(this, NOSAVE, "ay", "AY");
az.init(this, NOSAVE, "az", "AZ");
bx.init(this, NOSAVE, "bx", "BX");
by.init(this, NOSAVE, "by", "BY");
bz.init(this, NOSAVE, "bz", "BZ");
as.init(this, NOSAVE, "as");
bs.init(this, NOSAVE, "bs");
cs.init(this, NOSAVE, "cs");
aa.init(this, NOSAVE, "aa");
bb.init(this, NOSAVE, "bb");
cc.init(this, NOSAVE, "cc");
ax.init(this, NOSAVE, "ax");
ay.init(this, NOSAVE, "ay");
az.init(this, NOSAVE, "az");
bx.init(this, NOSAVE, "bx");
by.init(this, NOSAVE, "by");
bz.init(this, NOSAVE, "bz");
u.init(this, NOSAVE, "Umatrix");
b.init(this, NOSAVE, "Bmatrix");
qh.init(this, NOSAVE, "qh", "QH");
qk.init(this, NOSAVE, "qk", "QK");
ql.init(this, NOSAVE, "ql", "QL");
en.init(this, NOSAVE, "en", "EN");
qm.init(this, NOSAVE, "qm", "QM");
qh.init(this, NOSAVE, "qh");
qk.init(this, NOSAVE, "qk");
ql.init(this, NOSAVE, "ql");
en.init(this, NOSAVE, "en");
qm.init(this, NOSAVE, "qm");
ki.init(this, NOSAVE, "ki");
kf.init(this, NOSAVE, "kf");
a1.init(this, NOSAVE, "a1");
a2.init(this, NOSAVE, "a2");
a3.init(this, NOSAVE, "a3");
a4.init(this, NOSAVE, "a4");
a5.init(this, NOSAVE, "a5");
a6.init(this, NOSAVE, "a6");
state.init(this, NOSAVE, "state");
errorMessage.init(this, NOSAVE, "error_message");
tasSettings.init(this, "tas_settings");
sample.init(this, "sample");
scattering.init(this, "scattering");
incidentBeam.init(this, "incident_beam");
scatteredBeam.init(this, "scattered_beam");
a1Controller.init(this, "A1");
a2Controller.init(this, "A2");
a3Controller.init(this, "A3");
a4Controller.init(this, "A4");
a5Controller.init(this, "A5");
a6Controller.init(this, "A6");
}
VEXPController::VEXPController(const VEXPController& controller) :
ExperimentController(controller), m_running(false), m_errorDuringCalculation(false) {
}
VEXPController::~VEXPController() {
m_running = false;
// Get the mutex and notify the condition.
{
mutex::scoped_lock lock(m_mutex);
m_waitCondition.notify_one();
}
m_loopThread->join();
}
void VEXPController::updateUMatrix() {
u.update(sample->uRef());
u.setSize(sample->uRef.getSize());
}
void VEXPController::updateBMatrix() {
b.update(tasSettings->b());
b.setSize(tasSettings->b.getSize());
}
void VEXPController::refreshFloat64Property(SimpleProperty<float64>& property, float64 value) {
// Update the value.
property = value;
// Get the mutex and notify the condition.
{
mutex::scoped_lock lock(m_mutex);
m_waitCondition.notify_one();
}
}
void VEXPController::postConfiguration() {
// Copy the values.
as.update(tasSettings->as());
bs.update(tasSettings->bs());
cs.update(tasSettings->cs());
aa.update(tasSettings->aa());
bb.update(tasSettings->bb());
cc.update(tasSettings->cc());
ax.update(tasSettings->ax());
ay.update(tasSettings->ay());
az.update(tasSettings->az());
bx.update(tasSettings->bx());
by.update(tasSettings->by());
bz.update(tasSettings->bz());
updateUMatrix();
updateBMatrix();
qh.update(scattering->qh.setpoint());
qk.update(scattering->qk.setpoint());
ql.update(scattering->ql.setpoint());
en.update(scattering->en.setpoint());
qm.update(scattering->qm.setpoint());
ki.update(incidentBeam->ki.setpoint());
kf.update(scatteredBeam->kf.setpoint());
a1.update(a1Controller->position.setpoint());
a2.update(a2Controller->position.setpoint());
a3.update(a3Controller->position.setpoint());
a4.update(a4Controller->position.setpoint());
a5.update(a5Controller->position.setpoint());
a6.update(a6Controller->position.setpoint());
// Register the property updaters.
registerPropertyCopierByUpdate(tasSettings->as, as);
registerPropertyCopierByUpdate(tasSettings->bs, bs);
registerPropertyCopierByUpdate(tasSettings->cs, cs);
......@@ -68,12 +163,92 @@ void VEXPController::postConfiguration() {
registerPropertyCopierByUpdate(tasSettings->bb, bb);
registerPropertyCopierByUpdate(tasSettings->cc, cc);
registerPropertyCopierByUpdate(tasSettings->ax, ax);
registerPropertyCopierByUpdate(tasSettings->ax, ax);
registerPropertyCopierByUpdate(tasSettings->ax, ax);
registerPropertyCopierByUpdate(tasSettings->bx, bx);
registerPropertyCopierByUpdate(tasSettings->bx, bx);
registerPropertyCopierByUpdate(tasSettings->ay, ay);
registerPropertyCopierByUpdate(tasSettings->az, az);
registerPropertyCopierByUpdate(tasSettings->bx, bx);
registerPropertyCopierByUpdate(tasSettings->by, by);
registerPropertyCopierByUpdate(tasSettings->bz, bz);
registerUpdater(sample->uRef, &VEXPController::updateUMatrix, this);
registerUpdater(sample->b, &VEXPController::updateBMatrix, this);
registerPropertyCopierByUpdate(scattering->qh.setpoint, qh);
registerPropertyCopierByUpdate(scattering->qk.setpoint, qk);
registerPropertyCopierByUpdate(scattering->ql.setpoint, ql);
registerPropertyCopierByUpdate(scattering->en.setpoint, en);
registerPropertyCopierByUpdate(scattering->qm.setpoint, qm);
registerPropertyCopierByUpdate(incidentBeam->ki.setpoint, ki);
registerPropertyCopierByUpdate(scatteredBeam->kf.setpoint, kf);
registerPropertyCopierByUpdate(a1Controller->position.setpoint, a1);
registerPropertyCopierByUpdate(a2Controller->position.setpoint, a2);
registerPropertyCopierByUpdate(a3Controller->position.setpoint, a3);
registerPropertyCopierByUpdate(a4Controller->position.setpoint, a4);
registerPropertyCopierByUpdate(a5Controller->position.setpoint, a5);
registerPropertyCopierByUpdate(a6Controller->position.setpoint, a6);
// Register the refreshers.
registerRefresher(qh, &VEXPController::refreshFloat64Property, this, scattering->qh.setpoint);
registerRefresher(qk, &VEXPController::refreshFloat64Property, this, scattering->qk.setpoint);
registerRefresher(ql, &VEXPController::refreshFloat64Property, this, scattering->ql.setpoint);
registerRefresher(en, &VEXPController::refreshFloat64Property, this, scattering->en.setpoint);
registerRefresher(qm, &VEXPController::refreshFloat64Property, this, scattering->qm.setpoint);
registerRefresher(ki, &VEXPController::refreshFloat64Property, this, incidentBeam->ki.setpoint);
registerRefresher(kf, &VEXPController::refreshFloat64Property, this, scatteredBeam->kf.setpoint);
// Start the thread.
m_loopThread.reset(new thread(bind(&VEXPController::loop, this)));
}
void VEXPController::loop() {
while (m_running) {
// Calculate if the controller is still running.
if (m_running) {
calculate();
}
// Get the mutex and wait for a new calculation.
{
mutex::scoped_lock lock(m_mutex);
m_waitCondition.wait(lock);
}
}
}
void VEXPController::calculate() {
common::Date begin;
// We set the variable to false to check if an error occurs during the calculation.
m_errorDuringCalculation = false;
// Start the scattering controller to have the update of values.
scattering->startCommand(false);
// Check if an error occurred.
if (m_errorDuringCalculation) {
state = BAD_PARAMETERS;
}
else {
state = PARAMETERS_OK;
}
cout << "Calculate in " << (common::Date() - begin).getMilliseconds() << "ms" << endl;
}
void VEXPController::updateError(ChangeAspect* aspect) {
// This function should be called during the calculate call.
m_errorDuringCalculation = true;
// Update the error message.
errorMessage = aspect->getStringParam();
}
}
......@@ -24,6 +24,10 @@
#include "controllers/tas/common/Sample.h"
#include "controllers/tas/common/IncidentBeam.h"
#include "controllers/tas/common/TasScatteredBeam.h"
#include <boost/thread/thread.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <memory>
namespace vexp {
......@@ -34,8 +38,14 @@ public:
static const std::string TYPE;
VEXPController(const std::string& name);
VEXPController(const VEXPController& controller);
virtual ~VEXPController();
void updateUMatrix();
void updateBMatrix();
void refreshFloat64Property(SimpleProperty<float64>& property, float64 value);
virtual void postConfiguration();
Property<float64> as;
......@@ -50,19 +60,56 @@ public:
Property<float64> bx;
Property<float64> by;
Property<float64> bz;
ArrayProperty<float64> u;
ArrayProperty<float64> b;
Property<float64> qh;
Property<float64> qk;
Property<float64> ql;
Property<float64> en;
Property<float64> qm;
Property<float64> ki;
Property<float64> kf;
Property<float64> a1;
Property<float64> a2;
Property<float64> a3;
Property<float64> a4;
Property<float64> a5;
Property<float64> a6;
Property<int32> state;
Property<std::string> errorMessage;
ControllerPtr<tas::TasSettings> tasSettings;
ControllerPtr<tas::Sample> sample;
ControllerPtr<tas::Scattering> scattering;
ControllerPtr<tas::IncidentBeam> incidentBeam;
ControllerPtr<tas::TasScatteredBeam> scatteredBeam;
ControllerPtr<axis::AxisController> a1Controller;
ControllerPtr<axis::AxisController> a2Controller;
ControllerPtr<axis::AxisController> a3Controller;
ControllerPtr<axis::AxisController> a4Controller;
ControllerPtr<axis::AxisController> a5Controller;
ControllerPtr<axis::AxisController> a6Controller;
protected:
void loop();
void calculate();
virtual void updateError(ChangeAspect* aspect);
std::auto_ptr<boost::thread> m_loopThread;
boost::mutex m_mutex;
boost::condition m_waitCondition;
bool m_running;
bool m_errorDuringCalculation;
static const int32 PARAMETERS_OK;
static const int32 BAD_PARAMETERS;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment