SlsDetectorCamera.h 8.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//###########################################################################
// 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/>.
//###########################################################################

23
24
#ifndef __SLS_DETECTOR_CAMERA_H
#define __SLS_DETECTOR_CAMERA_H
25

26
#include "SlsDetectorArgs.h"
27
#include "SlsDetectorReceiver.h"
28
#include "SlsDetectorCPUAffinity.h"
29
#include "SlsDetectorModel.h"
30

31
#include "sls/Detector.h"
32

33
#include "lima/HwMaxImageSizeCallback.h"
34
#include "lima/Event.h"
35

36
#include <queue>
37
38
39
40
41
42
43

namespace lima 
{

namespace SlsDetector
{

44
45
class Eiger;

46
class Camera : public HwMaxImageSizeCallbackGen, public EventCallbackGen
47
{
48
49
50
	DEB_CLASS_NAMESPC(DebModCamera, "Camera", "SlsDetector");

public:
51
52
	typedef Defs::TrigMode TrigMode;
	typedef Defs::Settings Settings;
53
54
	typedef Defs::DACIndex DACIndex;
	typedef Defs::ADCIndex ADCIndex;
55
	typedef Defs::DetStatus DetStatus;
56

57
	Camera(std::string config_fname, int det_id = 0);
58
	Camera(const Camera& o) = delete;
59
60
	virtual ~Camera();

61
62
63
64
	Type getType();

	Model *getModel()
	{ return m_model; }
65

66
	NameList getHostnameList()
67
68
69
70
71
	{ return m_input_data->host_name_list; }

	int getNbDetModules()
	{ return m_input_data->host_name_list.size(); }

72
73
	int getNbRecvs()
	{ return m_recv_list.size(); }
74

75
76
	Receiver* getRecv(int i)
	{ return m_recv_list[i]; }
77

78
79
	BufferMgr *getBuffer()
	{ return &m_buffer; }
80

81
82
83
	void setModuleActive(int mod_idx, bool  active);
	void getModuleActive(int mod_idx, bool& active);

84
85
86
	void setPixelDepth(PixelDepth  pixel_depth);
	void getPixelDepth(PixelDepth& pixel_depth);

87
88
	void setRawMode(bool  raw_mode);
	void getRawMode(bool& raw_mode);
89

90
91
92
	AcqState getAcqState();
	void waitAcqState(AcqState state);
	AcqState waitNotAcqState(AcqState state);
93

94
95
96
97
98
	ImageType getImageType() const
	{ return m_image_type; }

	void getFrameDim(FrameDim& frame_dim, bool raw = false)
	{ m_model->getFrameDim(frame_dim, raw); }
99

100
101
	FrameMap *getFrameMap()
	{ return &m_frame_map; }
102
103
104
105

	void putCmd(const std::string& s, int idx = -1);
	std::string getCmd(const std::string& s, int idx = -1);

106
	int getFramesCaught();
107
	DetStatus getDetStatus();
108
	DetStatus getDetTrigStatus();
109

110
111
	void setTrigMode(TrigMode  trig_mode);
	void getTrigMode(TrigMode& trig_mode);
112
113
	void setNbFrames(FrameType  nb_frames);
	void getNbFrames(FrameType& nb_frames);
114
115
	void setExpTime(double  exp_time);
	void getExpTime(double& exp_time);
116
117
	void setLatTime(double  lat_time);
	void getLatTime(double& lat_time);
118
119
120
	void setFramePeriod(double  frame_period);
	void getFramePeriod(double& frame_period);

Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
121
122
123
	void setSkipFrameFreq(FrameType  skip_frame_freq);
	void getSkipFrameFreq(FrameType& skip_frame_freq);

124
125
	// setDAC: mod_idx: 0-N=module, -1=all
	void setDAC(int mod_idx, DACIndex dac_idx, int  val, 
126
		    bool milli_volt = false);
127
	void getDAC(int mod_idx, DACIndex dac_idx, int& val, 
128
129
130
131
		    bool milli_volt = false);
	void getDACList(DACIndex dac_idx, IntList& val_list,
			bool milli_volt = false);

132
	void getADC(int mod_idx, ADCIndex adc_idx, int& val);
133
134
	void getADCList(ADCIndex adc_idx, IntList& val_list);

135
136
	void setSettings(Settings  settings);
	void getSettings(Settings& settings);
137

138
139
140
	void setTolerateLostPackets(bool  tol_lost_packets);
	void getTolerateLostPackets(bool& tol_lost_packets);

141
142
	int getNbBadFrames(int item_idx);
	void getBadFrameList(int item_idx, int first_idx, int last_idx,
143
			     IntList& bad_frame_list);
144
	void getBadFrameList(int item_idx, IntList& bad_frame_list);
145

146
147
148
	void prepareAcq();
	void startAcq();
	void stopAcq();
149

150
151
	void triggerFrame();

152
153
154
	void registerTimeRangesChangedCallback(TimeRangesChangedCallback& cb);
	void unregisterTimeRangesChangedCallback(TimeRangesChangedCallback& cb);

155
	void getStats(Stats& stats, int recv_idx=-1);
156

157
158
159
	void setPixelDepthCPUAffinityMap(PixelDepthCPUAffinityMap aff_map);
	void getPixelDepthCPUAffinityMap(PixelDepthCPUAffinityMap& aff_map);

160
	GlobalCPUAffinityMgr::ProcessingFinishedEvent *
161
162
		getProcessingFinishedEvent();

163
	void reportException(Exception& e, std::string name);
164

165
private:
166
	typedef std::map<int, int> RecvPortMap;
167
	typedef std::queue<int> FrameQueue;
168
	typedef std::vector<AutoPtr<Receiver> > RecvList;
169

170
171
172
173
174
175
	struct AppInputData
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Camera::AppInputData", 
				  "SlsDetector");
	public:
		std::string config_file_name;
176
		NameList host_name_list;
177
178
179
180
181
		RecvPortMap recv_port_map;
		AppInputData(std::string cfg_fname);
		void parseConfigFile();
	};

182
183
184
185
186
187
	class AcqThread : public Thread
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Camera::AcqThread", 
				  "SlsDetector");
	public:
		AcqThread(Camera *cam);
188
189

		void queueFinishedFrame(FrameType frame);
190
191
		virtual void start(AutoMutex& l);
		void stop(AutoMutex& l, bool wait);
192

193
194
	protected:
		virtual void threadFunction();
195

196
	private:
197
198
		typedef std::pair<bool, bool> Status;

199
200
201
202
203
204
		class ExceptionCleanUp : Thread::ExceptionCleanUp
		{
			DEB_CLASS_NAMESPC(DebModCamera, 
					  "Camera::AcqThread::ExceptionCleanUp",
					  "SlsDetector");
		public:
205
			ExceptionCleanUp(AcqThread& thread, AutoMutex& l);
206
			virtual ~ExceptionCleanUp();
207
208
		private:
			AutoMutex& m_lock;
209
210
		};

211
		Status newFrameReady(FrameType frame);
212
213
		void startAcq();
		void stopAcq();
214
		void cleanUp(AutoMutex& l);
215

216
		Camera *m_cam;
217
		Cond& m_cond;
218
		AcqState& m_state;
219
		FrameQueue m_frame_queue;
220
	};
221

222
	friend class BufferMgr;
223
	friend class Model;
224
	friend class Receiver;
225
	friend class GlobalCPUAffinityMgr;
226
	friend class Reconstruction;
227

228
229
	friend class Eiger;

230
231
	void setModel(Model *model);

232
233
234
	AutoMutex lock()
	{ return AutoMutex(m_cond.mutex()); }

235
	void updateImageSize();
236
	void updateTimeRanges();
237
	void updateCPUAffinity(bool recv_restarted);
238
	void setRecvCPUAffinity(const RecvCPUAffinityList& recv_affinity_list);
239

240
241
242
243
244
	static sls::ns NSec(double x)
	{
		std::chrono::duration<double> sec(x);
		return std::chrono::duration_cast<sls::ns>(sec);
	}
245

246
	AcqState getEffectiveState();
247
248

	void removeSharedMem();
249
250
	void createReceivers();

251
	DetFrameImagePackets readRecvPackets();
252
253
	void publishFrame(FrameType frame);
	void assemblePackets(DetFrameImagePackets&& det_frame_packets);
254

255
256
257
	bool checkLostPackets();
	FrameType getLastReceivedFrame();

258
	void waitLastSkippedFrame();
259
	void processLastSkippedFrame(int recv_idx);
260

261
262
263
264
	void getSortedBadFrameList(IntList first_idx, IntList last_idx,
				   IntList& bad_frame_list );
	void getSortedBadFrameList(IntList& bad_frame_list)
	{ getSortedBadFrameList(IntList(), IntList(), bad_frame_list); }
265

266
267
	std::string execCmd(const std::string& s, bool put, int idx = -1);

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
	template <class T>
	void putNbCmd(const std::string& cmd, T val, int idx = -1)
	{
		std::ostringstream os;
		os << cmd << " " << val;
		putCmd(os.str(), idx);
	}

	template <class T>
	T getNbCmd(const std::string& cmd, int idx = -1)
	{
		std::string ans = getCmd(cmd, idx);
		std::istringstream is(ans);
		T val;
		is >> val;
		return val;
	}
285

286
287
288
	void setReceiverFifoDepth(int fifo_depth);
	void resetFramesCaught();

289
	int m_det_id;
290
	Model *m_model;
291
	Cond m_cond;
292
	AutoPtr<AppInputData> m_input_data;
293
	AutoPtr<sls::Detector> m_det;
294
295
	FrameMap m_frame_map;
	RecvList m_recv_list;
296
	TrigMode m_trig_mode;
Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
297
298
299
	FrameType m_lima_nb_frames;
	FrameType m_det_nb_frames;
	FrameType m_skip_frame_freq;
300
	SortedIntList m_missing_last_skipped_frame;
301
	double m_last_skipped_frame_timeout;
302
	double m_exp_time;
303
	double m_lat_time;
304
	double m_frame_period;
305
	Settings m_settings;
306
	BufferMgr m_buffer;
307
	PixelDepth m_pixel_depth;
308
	ImageType m_image_type;
309
	bool m_raw_mode;
310
	AcqState m_state;
311
	Timestamp m_next_ready_ts;
312
313
	double m_new_frame_timeout;
	double m_abort_sleep_time;
314
	bool m_tol_lost_packets;
315
	FrameArray m_prev_ifa;
316
	TimeRangesChangedCallback *m_time_ranges_cb;
317
	PixelDepthCPUAffinityMap m_cpu_affinity_map;
318
	GlobalCPUAffinityMgr m_global_cpu_affinity_mgr;
319
	AutoPtr<AcqThread> m_acq_thread;
320
	FramePacketMap m_frame_packet_map;
321
322
323
324
325
326
327
};

} // namespace SlsDetector

} // namespace lima


328
#endif // __SLS_DETECTOR_CAMERA_H