test_frelon_spectroscopy.cpp 5.9 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/>.
//###########################################################################
ahoms's avatar
ahoms committed
22
#include "FrelonInterface.h"
23
24
25
#include "lima/HwBufferSave.h"
#include "lima/AcqState.h"
#include "processlib/PoolThreadMgr.h"
26

ahoms's avatar
ahoms committed
27
28
29
using namespace lima;
using namespace std;

30
31
DEB_GLOBAL(DebModTest);

32
33
34
35
36
37
38

class TestFrameCallback : public HwFrameCallback
{
	DEB_CLASS(DebModTest, "TestFrameCallback");

public:
	TestFrameCallback(Frelon::Interface& hw_inter, 
39
			  HwBufferSave& buffer_save, AcqState& acq_state);
40
41
42
43
44
45
46
47
48
	~TestFrameCallback();

	const HwFrameInfoType& getBufferInfo();

protected:
	virtual bool newFrameReady(const HwFrameInfoType& frame_info);

private:
	Frelon::Interface& m_hw_inter;
49
	HwBufferSave& m_buffer_save;
50
51
52
53
54
55
56
57
58
59
	AcqState& m_acq_state;

	void *m_buffer_ptr;
	int m_nb_frames;
	int m_nb_stripes;
	FrameDim m_buffer_dim;
	HwFrameInfoType m_buffer_info;
};

TestFrameCallback::TestFrameCallback(Frelon::Interface& hw_inter, 
60
				     HwBufferSave& buffer_save, 
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
				     AcqState& acq_state)
	: m_hw_inter(hw_inter), m_buffer_save(buffer_save), 
	  m_acq_state(acq_state)
{
	DEB_CONSTRUCTOR();

	m_buffer_ptr = NULL;
	m_nb_frames = 1;
	m_nb_stripes = 1;
}

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

bool TestFrameCallback::newFrameReady(const HwFrameInfoType& frame_info)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(frame_info);

	if (!frame_info.isValid()) {
		DEB_ERROR() << "Acquisition aborted";
		m_acq_state.set(AcqState::Aborted);
		return false;
	}

	int acq_frame_nb = frame_info.acq_frame_nb;
	if (acq_frame_nb == 0) {
		HwBufferCtrlObj *hw_buffer;
		m_hw_inter.getHwCtrlObj(hw_buffer);
		hw_buffer->getNbConcatFrames(m_nb_stripes);

		HwSyncCtrlObj *hw_sync;
		m_hw_inter.getHwCtrlObj(hw_sync);
		hw_sync->getNbFrames(m_nb_frames);
	}

	if (acq_frame_nb % m_nb_stripes == 0)
		m_buffer_ptr = frame_info.frame_ptr;

	if (((acq_frame_nb + 1) % m_nb_stripes) == 0) {
		Point factor(1, m_nb_stripes);
seb's avatar
seb committed
104
		m_buffer_dim = frame_info.frame_dim * factor;
105
106
		m_buffer_info = frame_info;
		m_buffer_info.frame_ptr = m_buffer_ptr;
seb's avatar
seb committed
107
		m_buffer_info.frame_dim = m_buffer_dim;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
		m_buffer_info.valid_pixels *= m_nb_stripes;

		DEB_ALWAYS() << "Buffer ready: " << m_buffer_info;
	}

	if (acq_frame_nb == m_nb_frames - 1) {
		DEB_ALWAYS() << "Acq finished!";
		m_acq_state.set(AcqState::Finished);
	}

	return true;
}

const HwFrameInfoType& TestFrameCallback::getBufferInfo()
{
	return m_buffer_info;
}

ahoms's avatar
ahoms committed
126
127
void test_frelon_spectroscopy()
{
128
129
	DEB_GLOBAL_FUNCT();

130
	DebParams::disableTypeFlags(DebParams::AllFlags);
131

ahoms's avatar
ahoms committed
132
133
134
135
136
137
138
	Espia::Dev dev(0);
	Espia::Acq acq(dev);
	Espia::BufferMgr buffer_cb_mgr(acq);
	Espia::SerialLine ser_line(dev);
	Frelon::Camera cam(ser_line);
	BufferCtrlMgr buffer_mgr(buffer_cb_mgr);

139
	DEB_ALWAYS() << "Creating the Hw Interface ...";
ahoms's avatar
ahoms committed
140
	Frelon::Interface hw_inter(acq, buffer_mgr, cam);
141
	DEB_TRACE() << "Done!";
ahoms's avatar
ahoms committed
142

143
	HwBufferSave buffer_save(HwBufferSave::EDF, "big_img", 0, ".edf", true, 1);
144
145
146
	AcqState acq_state;
	TestFrameCallback cb(hw_inter, buffer_save, acq_state);

ahoms's avatar
ahoms committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	HwDetInfoCtrlObj *hw_det_info;
	hw_inter.getHwCtrlObj(hw_det_info);

	HwBufferCtrlObj *hw_buffer;
	hw_inter.getHwCtrlObj(hw_buffer);

	HwSyncCtrlObj *hw_sync;
	hw_inter.getHwCtrlObj(hw_sync);

	HwBinCtrlObj *hw_bin;
	hw_inter.getHwCtrlObj(hw_bin);

	HwRoiCtrlObj *hw_roi;
	hw_inter.getHwCtrlObj(hw_roi);

162
163
164
	HwFlipCtrlObj *hw_flip;
	hw_inter.getHwCtrlObj(hw_flip);

165
166
167
	hw_buffer->registerFrameCallback(cb);

	DEB_ALWAYS() << "Setting Chans 3 & 4";
ahoms's avatar
ahoms committed
168
169
	cam.setInputChan(Frelon::Chan34);

170
	Flip flip(true, false);
171
172
173
	DEB_ALWAYS() << "Setting flip mode to " << flip;
	hw_flip->setFlip(flip);

ahoms's avatar
ahoms committed
174
	Bin bin(1, 64);
175
	DEB_ALWAYS() << "Setting binning to " << bin;
ahoms's avatar
ahoms committed
176
177
178
	hw_bin->setBin(bin);

	Roi roi(Point(0, 30), Size(2048, 1));
179
	DEB_ALWAYS() << "Checking " << DEB_VAR1(roi);
ahoms's avatar
ahoms committed
180
181
	Roi real_roi;
	hw_roi->checkRoi(roi, real_roi);
182
183
	DEB_TRACE() << "Got " << DEB_VAR1(real_roi);

184
	DEB_ALWAYS() << "Setting " << DEB_VAR1(roi);
185
186
187
188
	hw_roi->setRoi(roi);
	hw_roi->getRoi(real_roi);
	DEB_TRACE() << "Got " << DEB_VAR1(real_roi);

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
	DEB_TRACE() << "Setting Kinetic roi";
	cam.setRoiMode(Frelon::Kinetic);

	int nb_stripes = 10000;
	int nb_buffers = 1;

	DEB_ALWAYS() << "Setting stripe concatenation (big buffer)";
	FrameDim fdim;
	hw_buffer->getFrameDim(fdim);
	fdim.setSize(real_roi.getSize());
	hw_buffer->setFrameDim(fdim);
	hw_buffer->setNbConcatFrames(nb_stripes);
	hw_buffer->setNbBuffers(nb_buffers);

	double exp_time = 1e-6;
	int nb_frames = nb_buffers * nb_stripes;
	hw_sync->setExpTime(exp_time);
	hw_sync->setNbFrames(nb_frames);

	Timestamp t0, t1;

	DEB_ALWAYS() << "Starting acquisition";
	t0 = Timestamp::now();
212
	acq_state.set(AcqState::Acquiring);
213
	hw_inter.startAcq();
214
	acq_state.waitNot(AcqState::Acquiring);
215
	t1 = Timestamp::now();
216
217
218
	if (acq_state.get() == AcqState::Aborted)
		THROW_HW_ERROR(Error) << "Acquisition aborted!";

219
	DEB_ALWAYS() << "Acquisition finished after " << (t1 - t0) << " sec";
ahoms's avatar
ahoms committed
220

221
222
223
	DEB_ALWAYS() << "Saving buffer";
	buffer_save.writeFrame(cb.getBufferInfo());
	DEB_TRACE() << "Done!";
ahoms's avatar
ahoms committed
224
225
226
227
}

int main(int argc, char *argv[])
{
228
229
	DEB_GLOBAL_FUNCT();

ahoms's avatar
ahoms committed
230
231
232
	try {
		test_frelon_spectroscopy();
	} catch (Exception& e) {
233
		DEB_ERROR() << "LIMA Exception: " << e;
ahoms's avatar
ahoms committed
234
235
236
	}
	return 0;
}