VEXPController.cpp 7.6 KB
Newer Older
legoc's avatar
legoc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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 "VEXPController.h"
#include "controllers/common/family/Families.h"

legoc's avatar
legoc committed
22
23
using namespace boost;

legoc's avatar
legoc committed
24
25
26
27
namespace vexp {

const string VEXPController::TYPE = "vexp_controller";

legoc's avatar
legoc committed
28
29
30
const int32 VEXPController::PARAMETERS_OK = 1;
const int32 VEXPController::BAD_PARAMETERS = 2;

legoc's avatar
legoc committed
31
VEXPController::VEXPController(const string& name) :
legoc's avatar
legoc committed
32
	ExperimentController(name), m_running(true), m_errorDuringCalculation(false) {
legoc's avatar
legoc committed
33
34
35

	setFamily(family::HIDDEN);

legoc's avatar
legoc committed
36
37
38
39
40
41
42
43
44
45
46
47
	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");
legoc's avatar
legoc committed
48
49
50
	u.init(this, NOSAVE, "Umatrix");
	b.init(this, NOSAVE, "Bmatrix");

legoc's avatar
legoc committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	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");
legoc's avatar
legoc committed
69
70
71
72
73
74

	tasSettings.init(this, "tas_settings");
	sample.init(this, "sample");
	scattering.init(this, "scattering");
	incidentBeam.init(this, "incident_beam");
	scatteredBeam.init(this, "scattered_beam");
legoc's avatar
legoc committed
75
76
77
78
79
80
81
82
83
84
85

	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) {
legoc's avatar
legoc committed
86
87
88
}

VEXPController::~VEXPController() {
legoc's avatar
legoc committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

	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());
104
	u.sendEvent();
legoc's avatar
legoc committed
105
106
107
108
109
}

void VEXPController::updateBMatrix() {
	b.update(tasSettings->b());
	b.setSize(tasSettings->b.getSize());
110
	b.sendEvent();
legoc's avatar
legoc committed
111
112
113
114
115
116
117
118
119
120
121
122
}

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();
	}
legoc's avatar
legoc committed
123
124
125
126
}

void VEXPController::postConfiguration() {

legoc's avatar
legoc committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	// 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.
legoc's avatar
legoc committed
161
162
163
164
165
166
167
	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);
legoc's avatar
legoc committed
168
169
	registerPropertyCopierByUpdate(tasSettings->ay, ay);
	registerPropertyCopierByUpdate(tasSettings->az, az);
legoc's avatar
legoc committed
170
	registerPropertyCopierByUpdate(tasSettings->bx, bx);
legoc's avatar
legoc committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	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;
legoc's avatar
legoc committed
251

legoc's avatar
legoc committed
252
253
	// Update the error message.
	errorMessage = aspect->getStringParam();
legoc's avatar
legoc committed
254
255
256
}

}