Commit 3c4a35ad authored by yannick legoc's avatar yannick legoc
Browse files

First version of D22AutoConfig controller

parent df525fc7
This diff is collapsed.
/NumorMessages.pb.cc
/NumorMessages.pb.h
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#include "controllers/common/family/Families.h"
#include <boost/lexical_cast.hpp>
#include <common/base/Date.h>
#include <InstrumentManager/InstrumentManager.h>
#include <controllers/common/acquisition/Count.h>
#include <controllers/common/acquisition/detector/DetectorElement.h>
#include "NumorMessages.pb.h"
#include "D22AutoConfig.h"
#include <fstream>
#include <iostream>
namespace d22 {
using namespace std;
using namespace common;
using namespace boost;
using namespace cameo;
const std::string AutoConfig::TYPE = "d22_auto_config";
const std::string AutoConfig::REMOTE_APPLICATION = "autoconfig";
const std::string AutoConfig::RESPONDER = "responder";
AutoConfig::AutoConfig(const std::string& name) :
ExperimentController(name), controller::Start(this), m_countSpy(nullptr) {
setFamily(family::ACQUISITION, family::SETTING);
serverEndpoint.init(this, SAVE, "cameo_server");
initialized.init(this, NOSAVE, "initialized");
distance.init(this, NOSAVE, "distance");
wavelength.init(this, NOSAVE, "wavelength");
collimation.init(this, NOSAVE, "collimation");
scatterModelType.init(this, NOSAVE, "scatter_model_type");
type.init(this, NOSAVE, "type");
sampleRadius.init(this, NOSAVE, "sample_radius");
m_d22settings.init(this, "settings");
m_driver.init(this, "driver");
}
AutoConfig::AutoConfig(const AutoConfig& controller) :
ExperimentController(controller), controller::Start(this), m_countSpy(nullptr) {
}
AutoConfig::~AutoConfig() {
if (m_remoteApplication.get() != NULL) {
// Stop the remote application.
m_remoteApplication->stop();
// Wait for the termination
application::State state = m_remoteApplication->waitFor();
cout << "Remote application " << m_remoteApplication->getName() << " terminated with state " << application::toString(state) << endl;
}
}
void AutoConfig::postConfiguration() {
if (m_server.get() == nullptr) {
// Create a new server if it is not already created.
// In case of a simulated server, avoid the real remote endpoint.
if (serverEndpoint() == "" || m_driver->state() != REAL_DEVICE_CONTAINER) {
m_server.reset(new Server(application::This::getServer().getEndpoint()));
} else {
m_server.reset(new Server(serverEndpoint()));
}
}
// Get the count spy.
m_countSpy = dynamic_cast<utilities::CountSpy*>(InstrumentManager::getInstance()->getFirstExperimentControllerByType(utilities::CountSpy::TYPE));
if (m_countSpy != nullptr) {
m_countSpy->attach(this);
}
initialized = initApplication();
}
bool AutoConfig::initApplication() {
// Do not initialize if it is already done
if (initialized()) {
return true;
}
// Start the remote server
if (!m_server->isAvailable(1000)) {
cout << "Remote server is not available" << endl;
return false;
}
cout << "Remote server is connected" << endl;
m_remoteApplication = m_server->connect(REMOTE_APPLICATION);
if (m_remoteApplication->exists()) {
// The application exists from a previous server session
m_remoteApplication->kill();
application::State state = m_remoteApplication->waitFor();
cout << "Terminated remote application with state " << application::toString(state) << endl;
}
m_remoteApplication = m_server->start(REMOTE_APPLICATION);
if (!m_remoteApplication->exists()) {
cout << "No remote application" << endl;
return false;
}
// Application initialized
initialized = true;
return true;
}
void AutoConfig::start() {
if (!initApplication()) {
return;
}
// Create the requester
unique_ptr<application::Requester> requester = application::Requester::create(*m_remoteApplication, RESPONDER);
if (requester.get() == nullptr) {
cout << "requester error" << endl;
return;
}
// Start date.
Date begin;
// Prepare the request.
lssautoconfig::NumorRequest request;
// Set the request parameters.
request.set_instrumentname("D22");
request.mutable_parameters()->set_distance(m_d22settings->detPosition());
request.mutable_parameters()->set_wavelength(m_d22settings->wavelengthPosition());
request.mutable_parameters()->set_collimation(m_d22settings->colSetupPosition());
request.set_type(lssautoconfig::NumorRequest_Type::NumorRequest_Type_Background);
// Get the data from the active count controller.
acquisition::Count* count = nullptr;
try {
count = dynamic_cast<acquisition::Count*>(InstrumentManager::getInstance()->getExperimentController(m_countSpy->countControllerName()));
}
catch (InstrumentManager::ControllerNotFound& e) {
log(Level::s_Error) << name << " missing controller " << m_countSpy->countControllerName() << endlog;
commandStatus.setError();
commandProgression = 100;
return;
}
int32 xSize = count->masterDetector->xSize();
int32 ySize = count->masterDetector->ySize();
request.set_xsize(xSize);
request.set_ysize(ySize);
cout << "Data size " << xSize << " x " << ySize << endl;
int32* data = count->masterDetector->data();
request.set_data(string(reinterpret_cast<const char *>(data), sizeof(int32) * xSize * ySize));
// Send the request.
requester->sendBinary(request.SerializeAsString());
// Wait for the response from the server.
string response;
requester->receiveBinary(response);
// Print the duration.
Date end;
double ms = (end - begin).getMilliseconds();
cout << "Request processed in " << ms << " ms" << endl;
lssautoconfig::Response autoConfigResponse;
autoConfigResponse.ParseFromString(response);
distance = autoConfigResponse.mutable_parameters()->distance();
wavelength = autoConfigResponse.mutable_parameters()->wavelength();
collimation = autoConfigResponse.mutable_parameters()->collimation();
scatterModelType = autoConfigResponse.scattermodeltype();
type = autoConfigResponse.type();
sampleRadius = autoConfigResponse.sampleradius();
}
}
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#ifndef D22_AUTOCONFIG_H
#define D22_AUTOCONFIG_H
#include <Controller.h>
#include <cameo/cameo.h>
#include <controllers/lss/d22/D22Settings.h>
#include <controllers/common/utilities/CountSpy.h>
#include <drivers/utilities/null/NullDriver.h>
namespace d22 {
class AutoConfig : public ExperimentController,
public controller::Start {
public:
//! Type of controller
static const std::string TYPE;
AutoConfig(const std::string& name);
AutoConfig(const AutoConfig& controller);
virtual ~AutoConfig();
virtual void postConfiguration();
virtual void start();
Property<std::string> serverEndpoint;
Property<bool> initialized;
Property<float64> distance;
Property<float64> wavelength;
Property<float64> collimation;
Property<std::string> scatterModelType;
Property<std::string> type;
Property<float64> sampleRadius;
private:
bool initApplication();
static const std::string REMOTE_APPLICATION;
static const std::string RESPONDER;
std::unique_ptr<cameo::Server> m_server;
std::unique_ptr<cameo::application::Instance> m_remoteApplication;
ControllerPtr<D22Settings> m_d22settings;
DriverPtr<driver::NullDriver> m_driver;
utilities::CountSpy* m_countSpy;
};
}
#endif
<module name="d22autoconfig">
<controller class="d22::AutoConfig"/>
<include path="$(NOMAD_HOME)/../NomadModules/src"/>
<script exec="protoc --cpp_out=. NumorMessages.proto"/>
</module>
package lssautoconfig;
option optimize_for = LITE_RUNTIME;
message InstrumentParameters {
required double distance = 1;
required double wavelength = 2;
required double collimation = 3;
}
message NumorRequest {
required string instrumentName = 1;
required InstrumentParameters parameters = 2;
enum Type {
SampleModel = 1;
Background = 2;
}
required Type type = 3;
required int32 xSize = 4;
required int32 ySize = 5;
required bytes data = 6;
}
message Response {
required InstrumentParameters parameters = 1;
required string scatterModelType = 2;
required string type = 3;
required double sampleRadius = 4;
}
d22_auto_config.distancePrefix=Distance
d22_auto_config.wavelengthPrefix=Wavelength
d22_auto_config.collimationPrefix=Collimation
d22_auto_config.scatter_model_typePrefix=Scatter Model Type
d22_auto_config.typePrefix=Type
d22_auto_config.sample_radiusPrefix=Sample Radius
\ No newline at end of file
<plugin>
<controller type="d22_auto_config" role="d22_auto_config1"/>
<number_of_lines nb_lines="1"/>
</plugin>
<controller_plugin_config type="d22_auto_config">
<image key="SETTINGS"/>
<settings view="d22_auto_configView.xml"/>
<command view="d22_auto_configCommandView.xml"/>
</controller_plugin_config>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<controller type="d22_auto_config">
<property name="distance" type="float64"/>
<property name="wavelength" type="float64"/>
<property name="collimation" type="float64"/>
<property name="scatter_model_type" type="string"/>
<property name="type" type="string"/>
<property name="sample_radius" type="float64"/>
</controller>
<plugin>
<controller type="d22_auto_config" role="d22_auto_config1"/>
<table_composite nbColumns="2">
<simple_label prefix="d22_auto_config.distancePrefix"/>
<label role="d22_auto_config1" property="distance"/>
<simple_label prefix="d22_auto_config.wavelengthPrefix"/>
<label role="d22_auto_config1" property="wavelength"/>
<simple_label prefix="d22_auto_config.collimationPrefix"/>
<label role="d22_auto_config1" property="collimation"/>
<simple_label prefix="d22_auto_config.scatter_model_typePrefix"/>
<label role="d22_auto_config1" property="scatter_model_type"/>
<simple_label prefix="d22_auto_config.typePrefix"/>
<label role="d22_auto_config1" property="type"/>
<simple_label prefix="d22_auto_config.sample_radiusPrefix"/>
<label role="d22_auto_config1" property="sample_radius"/>
</table_composite>
</plugin>
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