VEXPController.cpp 9.03 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
28
namespace vexp {

const string VEXPController::TYPE = "vexp_controller";

VEXPController::VEXPController(const string& name) :
legoc's avatar
legoc committed
29
	ExperimentController(name), m_running(true), m_errorDuringCalculation(false) {
legoc's avatar
legoc committed
30

31
32
	m_loopThread.reset();

legoc's avatar
legoc committed
33
34
	setFamily(family::HIDDEN);

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

legoc's avatar
legoc committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	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");

66
	calculationStatus.init(this, NOSAVE, "calculation_status");
legoc's avatar
legoc committed
67
	errorMessage.init(this, NOSAVE, "error_message");
legoc's avatar
legoc committed
68
69
70
71
72
73

	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
74
75
76
77
78
79
80
81
82
83
84

	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
85
86
87
}

VEXPController::~VEXPController() {
legoc's avatar
legoc committed
88
89
90
91
92
93
94
95
96

	m_running = false;

	// Get the mutex and notify the condition.
	{
		mutex::scoped_lock lock(m_mutex);
		m_waitCondition.notify_one();
	}

97
	if (m_loopThread.get() != 0) {
98
99
		m_loopThread->join();
	}}
legoc's avatar
legoc committed
100
101
102
103

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
void VEXPController::refreshKi(float64 value) {

	// calca1, calca2, calcStatus are calculated by refresh.
	incidentBeam->calcki = value;

	a1 = incidentBeam->calca1();
	a2 = incidentBeam->calca2();

	// Copy the status.
	calculationStatus.set(incidentBeam->calcStatus);
}

void VEXPController::refreshKf(float64 value) {

	// calca1, calca2, calcStatus are calculated by refresh.
	scatteredBeam->calckf = value;

	a5 = scatteredBeam->calca5();
	a6 = scatteredBeam->calca6();

	// Copy the status.
	calculationStatus.set(scatteredBeam->calcStatus);
}

legoc's avatar
legoc committed
137
138
139
140
void VEXPController::refreshFloat64Property(SimpleProperty<float64>& property, float64 value) {

	// Update the value.
	property = value;
legoc's avatar
legoc committed
141
142
143
144
145
146
}

void VEXPController::refreshFloat64PropertyWithCalculate(SimpleProperty<float64>& property, float64 value) {

	// Update the value.
	property = value;
legoc's avatar
legoc committed
147
148
149
150
151
152

	// Get the mutex and notify the condition.
	{
		mutex::scoped_lock lock(m_mutex);
		m_waitCondition.notify_one();
	}
legoc's avatar
legoc committed
153
154
155
156
}

void VEXPController::postConfiguration() {

legoc's avatar
legoc committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	// 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();

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Not used anymore

//	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());
191
192
193
194
195
196
197
198
199
200
201
202
203
204

	// 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);
legoc's avatar
legoc committed
205
206
207
208

	registerUpdater(sample->uRef, &VEXPController::updateUMatrix, this);
	registerUpdater(sample->b, &VEXPController::updateBMatrix, this);

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
// Not used anymore

//	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);

legoc's avatar
legoc committed
237
238
239
240
241
242
	registerRefresher(ax, &VEXPController::refreshFloat64Property, this, tasSettings->ax);
	registerRefresher(ay, &VEXPController::refreshFloat64Property, this, tasSettings->ay);
	registerRefresher(az, &VEXPController::refreshFloat64Property, this, tasSettings->az);
	registerRefresher(bx, &VEXPController::refreshFloat64Property, this, tasSettings->bx);
	registerRefresher(by, &VEXPController::refreshFloat64Property, this, tasSettings->by);
	registerRefresher(bz, &VEXPController::refreshFloat64Property, this, tasSettings->bz);
243
244
245
246
247
	registerRefresher(ki, &VEXPController::refreshKi, this);
	registerRefresher(kf, &VEXPController::refreshKf, this);

//	// Start the thread.
//	m_loopThread.reset(new thread(bind(&VEXPController::loop, this)));
legoc's avatar
legoc committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
}

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) {
281
		//state = BAD_PARAMETERS;
legoc's avatar
legoc committed
282
283
	}
	else {
284
		//state = PARAMETERS_OK;
legoc's avatar
legoc committed
285
286
287
288
289
290
291
292
293
	}

	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
294

legoc's avatar
legoc committed
295
296
	// Update the error message.
	errorMessage = aspect->getStringParam();
legoc's avatar
legoc committed
297
298
299
}

}