SlsDetectorCamera.h 8.17 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

30
#include "sls/Detector.h"
31

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

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

namespace lima 
{

namespace SlsDetector
{

43
44
class Eiger;

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

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

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

60
61
62
63
	Type getType();

	Model *getModel()
	{ return m_model; }
64

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

149
150
	void triggerFrame();

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

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

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

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

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

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

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

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

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

192
193
	protected:
		virtual void threadFunction();
194

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

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

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

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

221
	friend class BufferMgr;
222
	friend class Model;
223
	friend class Receiver;
224
	friend class GlobalCPUAffinityMgr;
225

226
227
	friend class Eiger;

228
229
	void setModel(Model *model);

230
231
232
	AutoMutex lock()
	{ return AutoMutex(m_cond.mutex()); }

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

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

244
	AcqState getEffectiveState();
245
246

	void removeSharedMem();
247
248
	void createReceivers();

249
250
251
	bool checkLostPackets();
	FrameType getLastReceivedFrame();

252
	void waitLastSkippedFrame();
253
	void processLastSkippedFrame(int recv_idx);
254

255
256
257
258
	void getSortedBadFrameList(IntList first_idx, IntList last_idx,
				   IntList& bad_frame_list );
	void getSortedBadFrameList(IntList& bad_frame_list)
	{ getSortedBadFrameList(IntList(), IntList(), bad_frame_list); }
259

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

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	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;
	}
279

280
281
282
	void setReceiverFifoDepth(int fifo_depth);
	void resetFramesCaught();

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

} // namespace SlsDetector

} // namespace lima


321
#endif // __SLS_DETECTOR_CAMERA_H