Commit d3286b86 authored by yannick legoc's avatar yannick legoc
Browse files

Spy in VEXPController

parent e987655b
......@@ -21,12 +21,12 @@
#include "controllers/common/axis/AxisController.h"
#include "controllers/common/axis/VirtualAxisController.h"
#include <rapidjson/stringbuffer.h>
#include <cameo/cameo.h>
#include <memory>
#include <utility>
#include <thread>
#include <atomic>
#include <rapidjson/stringbuffer.h>
namespace nomad3d {
......
......@@ -18,15 +18,16 @@
#include "VEXPController.h"
#include "controllers/common/family/Families.h"
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/reader.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/writer.h>
using namespace boost;
using namespace cameo;
using namespace std;
using namespace rapidjson;
namespace vexp {
......@@ -87,6 +88,8 @@ VEXPController::VEXPController(const string& name) :
errorMessage.init(this, NOSAVE, "error_message");
moveModel.init(this, SAVE, "move_model");
spyPeriod.init(this, SAVE, "spy_period");
qhScan.init(this, NOSAVE, "qh_scan");
qkScan.init(this, NOSAVE, "qk_scan");
qlScan.init(this, NOSAVE, "ql_scan");
......@@ -127,6 +130,11 @@ VEXPController::VEXPController(const VEXPController& controller) :
}
VEXPController::~VEXPController() {
m_running.store(false);
if (m_periodicUpdateThread.get() != nullptr) {
m_periodicUpdateThread->join();
}
}
void VEXPController::updateUMatrix() {
......@@ -191,37 +199,37 @@ void VEXPController::refreshFloat64PropertyToMoveModel(SimpleProperty<float64>&
void VEXPController::updateScanReference(int index) {
if (index == 0) {
qhScan.update(scanController->reference.get(0));
updateFloat64PropertyValue(qhScan, scanController->reference.get(0));
}
else if (index == 1) {
qkScan.update(scanController->reference.get(1));
updateFloat64PropertyValue(qkScan, scanController->reference.get(1));
}
else if (index == 2) {
qlScan.update(scanController->reference.get(2));
updateFloat64PropertyValue(qlScan, scanController->reference.get(2));
}
else if (index == 3) {
enScan.update(scanController->reference.get(3));
updateFloat64PropertyValue(enScan, scanController->reference.get(3));
}
}
void VEXPController::updateScanDelta(int index) {
if (index == 0) {
dqhScan.update(scanController->delta.get(0));
updateFloat64PropertyValue(dqhScan, scanController->delta.get(0));
}
else if (index == 1) {
dqkScan.update(scanController->delta.get(1));
updateFloat64PropertyValue(dqkScan, scanController->delta.get(1));
}
else if (index == 2) {
dqlScan.update(scanController->delta.get(2));
updateFloat64PropertyValue(dqlScan, scanController->delta.get(2));
}
else if (index == 3) {
denScan.update(scanController->delta.get(3));
updateFloat64PropertyValue(denScan, scanController->delta.get(3));
}
}
void VEXPController::updateNbPoints(int index) {
nbPointsScan.update(scanController->nSteps.get(0));
updateInt32PropertyValue(nbPointsScan, scanController->nSteps.get(0));
}
void VEXPController::updateScanStatus() {
......@@ -231,42 +239,42 @@ void VEXPController::updateScanStatus() {
if (running) {
// Get ki, kf, fixed, ss when the scan is starting.
kiScan = ki();
kfScan = kf();
fxScan = fx();
ssScan = ss();
updateFloat64PropertyValue(kiScan, ki());
updateFloat64PropertyValue(kfScan, kf());
updateInt32PropertyValue(fxScan, fx());
updateInt32PropertyValue(ssScan, ss());
}
}
void VEXPController::updateProperties() {
// 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());
updateFloat64PropertyValue(as, tasSettings->as());
updateFloat64PropertyValue(bs, tasSettings->bs());
updateFloat64PropertyValue(cs, tasSettings->cs());
updateFloat64PropertyValue(aa, tasSettings->aa());
updateFloat64PropertyValue(bb, tasSettings->bb());
updateFloat64PropertyValue(cc, tasSettings->cc());
updateFloat64PropertyValue(ax, tasSettings->ax());
updateFloat64PropertyValue(ay, tasSettings->ay());
updateFloat64PropertyValue(az, tasSettings->az());
updateFloat64PropertyValue(bx, tasSettings->bx());
updateFloat64PropertyValue(by, tasSettings->by());
updateFloat64PropertyValue(bz, tasSettings->bz());
updateUMatrix();
updateBMatrix();
qh.update(scattering->qh());
qk.update(scattering->qk());
ql.update(scattering->ql());
en.update(scattering->en());
qm.update(scattering->qm());
fx.update(scattering->fx());
ss.update(scattering->sens());
updateFloat64PropertyValue(qh, scattering->qh());
updateFloat64PropertyValue(qk, scattering->qk());
updateFloat64PropertyValue(ql, scattering->ql());
updateFloat64PropertyValue(en, scattering->en());
updateFloat64PropertyValue(qm, scattering->qm());
updateInt32PropertyValue(fx, scattering->fx());
updateInt32PropertyValue(ss, scattering->sens());
ki.update(incidentBeam->ki());
kf.update(scatteredBeam->kf());
updateFloat64PropertyValue(ki, incidentBeam->ki());
updateFloat64PropertyValue(kf, scatteredBeam->kf());
a1.update(a1Controller->position());
a2.update(a2Controller->position());
......@@ -276,49 +284,73 @@ void VEXPController::updateProperties() {
a5.update(a5Controller->position());
a6.update(a6Controller->position());
qhScan.update(scanController->reference.get(0));
qkScan.update(scanController->reference.get(1));
qlScan.update(scanController->reference.get(2));
enScan.update(scanController->reference.get(3));
updateFloat64PropertyValue(qhScan, scanController->reference.get(0));
updateFloat64PropertyValue(qkScan, scanController->reference.get(1));
updateFloat64PropertyValue(qlScan, scanController->reference.get(2));
updateFloat64PropertyValue(enScan, scanController->reference.get(3));
dqhScan.update(scanController->delta.get(0));
dqkScan.update(scanController->delta.get(1));
dqlScan.update(scanController->delta.get(2));
denScan.update(scanController->delta.get(3));
updateFloat64PropertyValue(dqhScan, scanController->delta.get(0));
updateFloat64PropertyValue(dqkScan, scanController->delta.get(1));
updateFloat64PropertyValue(dqlScan, scanController->delta.get(2));
updateFloat64PropertyValue(denScan, scanController->delta.get(3));
nbPointsScan.update(scanController->nSteps.get(0));
updateInt32PropertyValue(nbPointsScan, scanController->nSteps.get(0));
runningScan.update(scanController->commandStatus.isRunning());
}
void VEXPController::updateFloat64PropertyValue(SimpleProperty<float64>& property, float64 value) {
property.update(value);
}
void VEXPController::updateFloat64Property(SimpleProperty<float64>& property, SimpleProperty<float64>& sourceProperty) {
updateFloat64PropertyValue(property, sourceProperty());
}
void VEXPController::updateInt32PropertyValue(SimpleProperty<int32>& property, int32 value) {
property.update(value);
}
void VEXPController::updateInt32Property(SimpleProperty<int32>& property, SimpleProperty<int32>& sourceProperty) {
updateInt32PropertyValue(property, sourceProperty());
}
void VEXPController::registerFloat64PropertyUpdater(SimpleProperty<float64>& property, SimpleProperty<float64>& targetProperty) {
registerUpdater(property, &VEXPController::updateFloat64Property, this, targetProperty, property);
}
void VEXPController::registerInt32PropertyUpdater(SimpleProperty<int32>& property, SimpleProperty<int32>& targetProperty) {
registerUpdater(property, &VEXPController::updateInt32Property, this, targetProperty, property);
}
void VEXPController::postConfiguration() {
// Register the property updaters.
registerPropertyCopierByUpdate(tasSettings->as, as);
registerPropertyCopierByUpdate(tasSettings->bs, bs);
registerPropertyCopierByUpdate(tasSettings->cs, cs);
registerPropertyCopierByUpdate(tasSettings->aa, aa);
registerPropertyCopierByUpdate(tasSettings->bb, bb);
registerPropertyCopierByUpdate(tasSettings->cc, cc);
registerPropertyCopierByUpdate(tasSettings->ax, ax);
registerPropertyCopierByUpdate(tasSettings->ay, ay);
registerPropertyCopierByUpdate(tasSettings->az, az);
registerPropertyCopierByUpdate(tasSettings->bx, bx);
registerPropertyCopierByUpdate(tasSettings->by, by);
registerPropertyCopierByUpdate(tasSettings->bz, bz);
registerFloat64PropertyUpdater(tasSettings->as, as);
registerFloat64PropertyUpdater(tasSettings->bs, bs);
registerFloat64PropertyUpdater(tasSettings->cs, cs);
registerFloat64PropertyUpdater(tasSettings->aa, aa);
registerFloat64PropertyUpdater(tasSettings->bb, bb);
registerFloat64PropertyUpdater(tasSettings->cc, cc);
registerFloat64PropertyUpdater(tasSettings->ax, ax);
registerFloat64PropertyUpdater(tasSettings->ay, ay);
registerFloat64PropertyUpdater(tasSettings->az, az);
registerFloat64PropertyUpdater(tasSettings->bx, bx);
registerFloat64PropertyUpdater(tasSettings->by, by);
registerFloat64PropertyUpdater(tasSettings->bz, bz);
registerUpdater(sample->uRef, &VEXPController::updateUMatrix, this);
registerUpdater(sample->b, &VEXPController::updateBMatrix, this);
registerPropertyCopierByUpdate(scattering->qh, qh);
registerPropertyCopierByUpdate(scattering->qk, qk);
registerPropertyCopierByUpdate(scattering->ql, ql);
registerPropertyCopierByUpdate(scattering->en, en);
registerPropertyCopierByUpdate(scattering->qm, qm);
registerPropertyCopierByUpdate(scattering->fx, fx);
registerPropertyCopierByUpdate(scattering->sens, ss);
registerFloat64PropertyUpdater(scattering->qh, qh);
registerFloat64PropertyUpdater(scattering->qk, qk);
registerFloat64PropertyUpdater(scattering->ql, ql);
registerFloat64PropertyUpdater(scattering->en, en);
registerFloat64PropertyUpdater(scattering->qm, qm);
registerInt32PropertyUpdater(scattering->fx, fx);
registerInt32PropertyUpdater(scattering->sens, ss);
registerPropertyCopierByUpdate(incidentBeam->ki, ki);
registerPropertyCopierByUpdate(scatteredBeam->kf, kf);
registerFloat64PropertyUpdater(incidentBeam->ki, ki);
registerFloat64PropertyUpdater(scatteredBeam->kf, kf);
registerPropertyCopierByUpdate(a1Controller->position, a1);
registerPropertyCopierByUpdate(a2Controller->position, a2);
......@@ -348,6 +380,11 @@ void VEXPController::postConfiguration() {
registerStatus(scanController, &VEXPController::updateScanStatus, this);
updateProperties();
m_spyPublisher = application::Publisher::create("vexp_spy");
m_running.store(true);
m_periodicUpdateThread.reset(new std::thread(std::bind(&VEXPController::periodicUpdate, this)));
}
void VEXPController::calculate(float64 ki, float64 kf, float64 qh, float64 qk, float64 ql, int32 fx, int32 ss) {
......@@ -442,4 +479,73 @@ void VEXPController::calculate(float64 ki, float64 kf, float64 qh, float64 qk, f
// }
}
std::string VEXPController::getSpyData() {
PrettyWriter<StringBuffer> writer(s);
s.Clear();
writer.StartObject();
writer.Key("as"); writer.Double(as());
writer.Key("bs"); writer.Double(bs());
writer.Key("cs"); writer.Double(cs());
writer.Key("aa"); writer.Double(aa());
writer.Key("bb"); writer.Double(bb());
writer.Key("cc"); writer.Double(cc());
writer.Key("ax"); writer.Double(ax());
writer.Key("ay"); writer.Double(ay());
writer.Key("az"); writer.Double(az());
writer.Key("bx"); writer.Double(bx());
writer.Key("by"); writer.Double(by());
writer.Key("bz"); writer.Double(bz());
writer.Key("qh"); writer.Double(qh());
writer.Key("qk"); writer.Double(qk());
writer.Key("ql"); writer.Double(ql());
writer.Key("en"); writer.Double(en());
writer.Key("qm"); writer.Double(qm());
writer.Key("fx"); writer.Int(fx());
writer.Key("ss"); writer.Int(ss());
writer.Key("ki"); writer.Double(ki());
writer.Key("kf"); writer.Double(kf());
writer.Key("qhScan"); writer.Double(qhScan());
writer.Key("qkScan"); writer.Double(qkScan());
writer.Key("qlScan"); writer.Double(qlScan());
writer.Key("enScan"); writer.Double(enScan());
writer.Key("dqhScan"); writer.Double(dqhScan());
writer.Key("dqkScan"); writer.Double(dqkScan());
writer.Key("dqlScan"); writer.Double(dqlScan());
writer.Key("denScan"); writer.Double(denScan());
writer.Key("nbPointsScan"); writer.Int(nbPointsScan());
writer.Key("kiScan"); writer.Double(kiScan());
writer.Key("kfScan"); writer.Double(kfScan());
writer.Key("fxScan"); writer.Int(fxScan());
writer.Key("ssScan"); writer.Int(ssScan());
writer.EndObject();
return s.GetString();
}
void VEXPController::periodicUpdate() {
while (m_running.load()) {
std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::seconds(spyPeriod()));
std::lock_guard<std::mutex> lock(m_publisherMutex);
// Send using the publisher.
string spyData = getSpyData();
m_spyPublisher->send(spyData);
}
}
}
......@@ -25,10 +25,12 @@
#include "controllers/tas/common/IncidentBeam.h"
#include "controllers/tas/common/TasScatteredBeam.h"
#include "controllers/common/scanlegacy/ParameterizableScan1D.h"
#include <boost/thread/thread.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <cameo/cameo.h>
#include <rapidjson/stringbuffer.h>
#include <memory>
#include <unordered_map>
#include <thread>
#include <atomic>
namespace vexp {
......@@ -63,6 +65,14 @@ public:
void updateProperties();
void updateFloat64PropertyValue(SimpleProperty<float64>& property, float64 value);
void updateFloat64Property(SimpleProperty<float64>& property, SimpleProperty<float64>& sourceProperty);
void updateInt32PropertyValue(SimpleProperty<int32>& property, int32 value);
void updateInt32Property(SimpleProperty<int32>& property, SimpleProperty<int32>& sourceProperty);
void registerFloat64PropertyUpdater(SimpleProperty<float64>& property, SimpleProperty<float64>& targetProperty);
void registerInt32PropertyUpdater(SimpleProperty<int32>& property, SimpleProperty<int32>& targetProperty);
void updateScanReference(int index);
void updateScanDelta(int index);
void updateNbPoints(int index);
......@@ -72,6 +82,10 @@ public:
void calculate(float64 ki, float64 kf, float64 qh, float64 qk, float64 ql, int32 fx, int32 ss);
void periodicUpdate();
std::string getSpyData();
Property<float64> as;
Property<float64> bs;
Property<float64> cs;
......@@ -143,6 +157,15 @@ public:
Property<int32> nbPointsScan;
Property<bool> runningScan;
// Spy.
Property<int32> spyPeriod;
rapidjson::StringBuffer s;
std::atomic<bool> m_running;
std::unique_ptr<cameo::application::Publisher> m_spyPublisher;
std::mutex m_publisherMutex;
std::unique_ptr<std::thread> m_periodicUpdateThread;
// Controllers.
ControllerPtr<tas::TasSettings> tasSettings;
ControllerPtr<tas::Sample> sample;
......
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