test_frelon_control.cpp 8.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
22
#include "FrelonInterface.h"
23 24 25 26 27 28
#include "lima/CtControl.h"
#include "lima/CtAcquisition.h"
#include "lima/CtImage.h"
#include "lima/CtSaving.h"
#include "lima/CtSpsImage.h"
#include "lima/AcqState.h"
29 30

using namespace lima;
31
using namespace std;
32 33 34

DEB_GLOBAL(DebModTest);

ahoms's avatar
ahoms committed
35 36 37 38
//*********************************************************************
//* ImageStatusCallback
//*********************************************************************

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class ImageStatusCallback : public CtControl::ImageStatusCallback
{
	DEB_CLASS(DebModTest, "ImageStatusCallback");

public:
	ImageStatusCallback(CtControl& ct, AcqState& acq_state);
	virtual ~ImageStatusCallback();

protected:
	virtual void imageStatusChanged(
				const CtControl::ImageStatus& img_status);

private:
	CtControl& m_ct;
	AcqState& m_acq_state;

	int m_nb_frames;
};

ImageStatusCallback::ImageStatusCallback(CtControl& ct, AcqState& acq_state)
59
	: m_ct(ct), m_acq_state(acq_state), m_nb_frames(0)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
	DEB_CONSTRUCTOR();
}

ImageStatusCallback::~ImageStatusCallback()
{
	DEB_DESTRUCTOR();
}

void ImageStatusCallback::imageStatusChanged(
				const CtControl::ImageStatus& img_status)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(img_status);

	int last_acq_frame_nb = img_status.LastImageAcquired;
	int last_saved_frame_nb = img_status.LastImageSaved;

	if (last_acq_frame_nb == 0) {
		CtAcquisition *ct_acq = m_ct.acquisition();
		ct_acq->getAcqNbFrames(m_nb_frames);
	}

83 84 85 86 87 88
	if ((last_acq_frame_nb == m_nb_frames - 1) &&
	    (m_acq_state.get() == AcqState::Acquiring)) {
		DEB_ALWAYS() << "All frames acquired!";
		m_acq_state.set(AcqState::Saving);
	}

89
	if (last_saved_frame_nb == m_nb_frames - 1) {
90
		DEB_ALWAYS() << "All frames saved!";
91 92 93
		m_acq_state.set(AcqState::Finished);
	}
}
94

ahoms's avatar
ahoms committed
95 96 97 98 99

//*********************************************************************
//* FrelonAcq
//*********************************************************************

100
class FrelonAcq
101
{
102 103 104 105
	DEB_CLASS(DebModTest, "FrelonAcq");
public:
	FrelonAcq(int espia_dev_nb);
	~FrelonAcq();
106

107 108 109 110 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 137
	void initSaving(string dir, string prefix, string suffix, int idx, 
			CtSaving::FileFormat fmt, CtSaving::SavingMode mode,
			int frames_per_file);

	void setExpTime(double exp_time);
	void setNbAcqFrames(int nb_acq_frames);

	void start();
	void wait();
	void run();

	void setBin(Bin& bin);
	void setRoi(Roi& roi);

private:
	void printDefaults();

	Espia::Dev		m_edev;
	Espia::Acq		m_acq;
	Espia::BufferMgr	m_buffer_cb_mgr;
	Espia::SerialLine	m_eserline;
	Frelon::Camera		m_cam;
	BufferCtrlMgr		m_buffer_mgr;
	Frelon::Interface	m_hw_inter;
	AcqState		m_acq_state;

	CtControl		*m_ct;
	CtAcquisition		*m_ct_acq;
	CtSaving		*m_ct_saving;
	CtImage			*m_ct_image;
	CtBuffer		*m_ct_buffer;
138
#ifdef WITH_SPS_IMAGE
ahoms's avatar
ahoms committed
139
	CtSpsImage		*m_ct_display;
140
#endif
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

	ImageStatusCallback	*m_img_status_cb;
};

FrelonAcq::FrelonAcq(int espia_dev_nb)
	: m_edev(espia_dev_nb), 
	  m_acq(m_edev), 
	  m_buffer_cb_mgr(m_acq), 
	  m_eserline(m_edev),
	  m_cam(m_eserline), 
	  m_buffer_mgr(m_buffer_cb_mgr),
	  m_hw_inter(m_acq, m_buffer_mgr, m_cam),
	  m_ct(NULL), m_img_status_cb(NULL)
{
	DEB_CONSTRUCTOR();

	AutoPtr<CtControl> ct = new CtControl(&m_hw_inter);

ahoms's avatar
ahoms committed
159 160 161 162
	m_ct_acq     = ct->acquisition();
	m_ct_saving  = ct->saving();
	m_ct_image   = ct->image();
	m_ct_buffer  = ct->buffer();
163
#ifdef WITH_SPS_IMAGE
ahoms's avatar
ahoms committed
164
	m_ct_display = ct->display();
165
#endif
166 167 168 169 170 171

	printDefaults();

	AutoPtr<ImageStatusCallback> img_status_cb;
	img_status_cb = new ImageStatusCallback(*ct, m_acq_state);
	ct->registerImageStatusCallback(*img_status_cb);
172
#ifdef WITH_SPS_IMAGE
ahoms's avatar
ahoms committed
173 174
	m_ct_display->setNames("_ccd_ds_", "frelon_live");
	m_ct_display->setActive(true);
175
#endif
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	DEB_TRACE() << "All is OK!";
	m_ct = ct.forget();
	m_img_status_cb = img_status_cb.forget();
}

FrelonAcq::~FrelonAcq()
{
	DEB_DESTRUCTOR();

	delete m_img_status_cb;
	delete m_ct;
}

void FrelonAcq::printDefaults()
{
	DEB_MEMBER_FUNCT();
192 193

	AcqMode acq_mode;
194
	m_ct_acq->getAcqMode(acq_mode);
195 196
	DEB_TRACE() << "Default " << DEB_VAR1(acq_mode);

197
	ImageType image_type;
198
	m_ct_image->getImageType(image_type);
199 200 201
	DEB_TRACE() << "Default " << DEB_VAR1(image_type);

	Size max_size;
202
	m_ct_image->getMaxImageSize(max_size);
203 204
	DEB_TRACE() << "Default " << DEB_VAR1(max_size);

205 206 207 208
	CtImage::ImageOpMode img_op_mode;
	m_ct_image->getMode(img_op_mode);
	DEB_TRACE() << "Default " << DEB_VAR1(img_op_mode);

209
	Bin bin;
210
	m_ct_image->getBin(bin);
211 212 213
	DEB_TRACE() << "Default " << DEB_VAR1(bin);

	Roi roi;
214
	m_ct_image->getRoi(roi);
215 216
	DEB_TRACE() << "Default " << DEB_VAR1(roi);
	
217
	double exp_time;
218
	m_ct_acq->getAcqExpoTime(exp_time);
219 220
	DEB_TRACE() << "Default " << DEB_VAR1(exp_time);

221
	int nb_frames;
222
	m_ct_acq->getAcqNbFrames(nb_frames);
223 224
	DEB_TRACE() << "Default " << DEB_VAR1(nb_frames);

225
}
226

227 228 229
void FrelonAcq::start()
{
	DEB_MEMBER_FUNCT();
230

231 232
	m_ct->prepareAcq();
	m_acq_state.set(AcqState::Acquiring);
233
	DEB_TRACE() << "Starting acquisition";
234 235 236 237 238 239 240
   	m_ct->startAcq();
}

void FrelonAcq::wait()
{
	DEB_MEMBER_FUNCT();
	m_acq_state.waitNot(AcqState::Acquiring | AcqState::Saving);
241
	DEB_TRACE() << "Acquisition finished";
242 243 244 245 246 247 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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
}

void FrelonAcq::run()
{
	DEB_MEMBER_FUNCT();
	start();
	wait();
}

void FrelonAcq::initSaving(string dir, string prefix, string suffix, int idx, 
			   CtSaving::FileFormat fmt, CtSaving::SavingMode mode,
			   int frames_per_file)
{
	DEB_MEMBER_FUNCT();

	m_ct_saving->setDirectory(dir);
 	m_ct_saving->setPrefix(prefix);
	m_ct_saving->setSuffix(suffix);
	m_ct_saving->setNextNumber(idx);
	m_ct_saving->setFormat(fmt);
	m_ct_saving->setSavingMode(mode);
	m_ct_saving->setFramesPerFile(frames_per_file);
}

void FrelonAcq::setExpTime(double exp_time)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(exp_time);

	m_ct_acq->setAcqExpoTime(exp_time);
}

void FrelonAcq::setNbAcqFrames(int nb_acq_frames)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(nb_acq_frames);

	m_ct_acq->setAcqNbFrames(nb_acq_frames);
}

void FrelonAcq::setBin(Bin& bin)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(bin);

	m_ct_image->setBin(bin);
}

void FrelonAcq::setRoi(Roi& roi)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(roi);

	m_ct_image->setRoi(roi);

}

ahoms's avatar
ahoms committed
299 300 301 302
//*********************************************************************
//* test_frelon_control
//*********************************************************************

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
void test_frelon_control(bool enable_debug = false)
{
	DEB_GLOBAL_FUNCT();

	if (!enable_debug) {
		DebParams::disableModuleFlags(DebParams::AllFlags);
	}

	DEB_ALWAYS() << "Creating FrelonAcq";
	int espia_dev_nb = 0;
	FrelonAcq acq(espia_dev_nb);
	DEB_ALWAYS() << "Done!";

	acq.initSaving("data", "img", ".edf", 0, CtSaving::EDF, 
		       CtSaving::AutoFrame, 1);

	DEB_ALWAYS() << "First run with default pars";
	acq.run();
	DEB_ALWAYS() << "Done!";

	double exp_time = 1e-6;
	acq.setExpTime(exp_time);

	int nb_acq_frames = 500;
	acq.setNbAcqFrames(nb_acq_frames);

	DEB_ALWAYS() << "Run " << DEB_VAR2(exp_time, nb_acq_frames);
	acq.run();
	DEB_ALWAYS() << "Done!";

	Bin bin(2);
	acq.setBin(bin);

	nb_acq_frames = 5;
	acq.setNbAcqFrames(nb_acq_frames);

	DEB_ALWAYS() << "Run " << DEB_VAR2(bin, nb_acq_frames);
	acq.run();
	DEB_ALWAYS() << "Done!";

	Roi roi = Roi(Point(256, 256), Size(512, 512));
	acq.setRoi(roi);

	DEB_ALWAYS() << "Run " << DEB_VAR1(roi);
	acq.run();
	DEB_ALWAYS() << "Done!";

	roi = Roi(Point(267, 267), Size(501, 501));
	acq.setRoi(roi);

	DEB_ALWAYS() << "Run " << DEB_VAR1(roi);
	acq.run();
	DEB_ALWAYS() << "Done!";
356 357 358

}

ahoms's avatar
ahoms committed
359 360 361 362 363

//*********************************************************************
//* main
//*********************************************************************

364 365 366 367
int main(int argc, char *argv[])
{
	DEB_GLOBAL_FUNCT();

368 369 370 371 372
	string par;
	if (argc > 1)
		par = argv[1];
	bool enable_debug = (par == "debug");

373
	try {
374
		test_frelon_control(enable_debug);
375 376 377 378 379
	} catch (Exception& e) {
		DEB_ERROR() << "LIMA Exception: " << e;
	}
	return 0;
}