WPOCT Software Developer's Kit (SDK)
SDK For using Wasatch Photonics OCT Spectrometers
WPScanController.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <string>
9 #include <atomic>
10 #include <vector>
11 #include <iostream>
12 #include <fstream>
13 #include <mutex>
14 //#include <OCTTypeDefs.h>
15 
16 #include "IWPOCTController.h"
17 #include "Serial.h"
18 
19 // Forward class declarations.
20 class SimpleTimer;
21 class ShutterDevice;
22 class CallbackInfo;
23 
24 using namespace std;
25 
29 {
30 public:
33 
35  virtual ~WPScanController();
36 
37  // Events
38 
39  // Getters
40 
41  bool IsPortOpen () const override { return _portOpen; }
42 
43  bool IsLightOn (int index) override;
44  bool IsScanning () const override { return _isScanning; }
45 
46  int GetBaudRate () const override { return _baudRate; }
47  int GetNumStopBits () const override { return _numStopBits; }
48  int GetPortTimeout () const override { return _portTimeout; }
49  bool GetMonitorPort () const override { return _monitorConnection; }
50  bool GetReadResponse () const override { return _readResponse; }
51  const char* GetVersion () const override { return _version.c_str(); }
52 
53  int GetAScans () const override { return _aScans; }
54  int GetBScans () const override { return _bScans; }
55  int GetFoci () const override { return _foci; }
56  int GetDelay () const override { return _delay; }
57  int GetPulse () const override { return _pulse; }
58  int GetPhase () const override { return _phase; }
59  int GetArmPosition () const override { return _pathMotorPos; }
60  void GetScanArea (int* xStart, int* xStop, int* yStart, int* yStop) const override;
61  void GetMirror (int* major, int* minor) const override;
62  bool GetSwapXY () const override { return _swapXY; }
63 
64  const char* GetPortName () override;
65 
66  const char* GetLastCommandSent () const override;
67  const char* GetLastResponse () const override;
68 
69  // Get the min max ramp values.
70  int GetXRampMinPos() const override { return _minXRampPos; }
71  int GetXRampMaxPos() const override { return _maxXRampPos; }
72  int GetYRampMinPos() const override { return _minYRampPos; }
73  int GetYRampMaxPos() const override { return _maxYRampPos; }
74 
75  int GetXRampStartPos() const override { return _xRampStartPos; }
76  int GetXRampStopPos () const override { return _xRampStopPos; }
77  int GetYRampStartPos() const override { return _yRampStartPos; }
78  int GetYRampStopPos () const override { return _yRampStopPos; }
79 
80  // Get max values.
81  int GetPathMotorPosMax () const override { return _pathMotorPosMax; }
82  int GetQuarterHalfWavePosMax() const override { return _quarterHalfWavePosMax; }
83 
84  // Setters
85  bool SetAScans (int value) override;
86  bool SetBScans (int value) override;
87  bool SetFoci (int value) override;
88  bool SetDelay (int value) override;
89  bool SetPulse (int value) override;
90  bool SetPhase (int value) override;
91 
92  // Set the min max ramp values.
93  bool SetXRampMinPos(int value) override;
94  bool SetXRampMaxPos(int value) override;
95  bool SetYRampMinPos(int value) override;
96  bool SetYRampMaxPos(int value) override;
97 
98  // Position the reference arm at the specified location.
99  bool SetArmPosition (int position) override;
100  bool SetScanArea (int xStart, int xStop, int yStart, int yStop) override;
101  bool SetScanArea (int xStart, int xStop, int yStart, int yStop, int numPasses) override;
102  bool SetMirror (int major, int minor) override;
103  bool SetSwapXY (bool value) override { _swapXY = value; return true; }
104  bool SetReadResponse (bool value) override { _readResponse = value; return true; }
105 
106  // Set max values.
107  bool SetPathMotorPosMax (int value) override { _pathMotorPosMax = value; return true; }
108  bool SetQuarterHalfWavePosMax(int value) override { _quarterHalfWavePosMax = value; return true; }
109 
110  // Serial port commands.
111 
114  bool OpenPort (const char* portName, int baudRate, int numStopBits,
115  int timeout, bool monitorConnection) override;
117  bool OpenPort (int baudRate, int numStopBits, int timeout, bool monitorConnection) override;
119  bool OpenPort () override;
120 
121  void ReadControllerVersion(bool &result);
122 
124  bool ClosePort () override;
125 
126  bool SendCommand (const char* command, char* response, int maxResponseSize) override;
127  bool SendCommand (const char* command) override;
128 
129  // Scanner commands.
130 
131  // Light commands.
132  bool TurnLightOn (int num) override;
133  bool TurnLightOff (int num) override;
134  bool TurnLightsOff () override;
135 
137  bool StartScan () override;
139  bool StopScan () override;
140 
142  bool ParkArm () override;
143 
145  bool SendMotorsHome () override;
146 
147  // Optical switch commands.
148  int GetActiveOpticalPort () override;
149  bool SetActiveOpticalPort (int port) override;
150  bool ToggleOpticalSwitch () override;
151 
152  // Shutter commands.
153  bool IsShutterOpen (int ID, bool* open) override;
154  bool OpenShutter (int ID) override;
155  bool CloseShutter (int ID) override;
156 
157  // Status
158  virtual IWPOCTController::ErrorCode GetLastError () const override { return _lastError; }
159  virtual IWPOCTController::CurrentStates GetCurrentStatus() const override { return _currentStatus; }
160 
161  virtual void SetSerialPortLostCallback (NotifyErrorCallback notifyError) override;
162  virtual void SetSerialPortLostCallback2 (SendNoParams serialPortLost, void* context) override;
163 
164  void CheckPingResponse ();
165 
166 protected:
167  const int PathMotorSpeedMax = 2900;
168  const int QuarterHalfWaveSpeedMax = 10000;
169  const int PathMotorAccelMax = 10000;
170  const int QuarterHalfWaveAccelMax = 10000;
171 
172  bool SendStringToWasatchPort (std::string rawCommand);
173  bool SendStringToCameraPort (std::string rawCommand);
174  bool SendCommandToPort (std::string command);
175  bool SendShutterCommand (std::string& inCommand);
176 
177  void ReadResponse (std::string &finalCommand);
178 
179  bool CheckWasatchCommandValid(std::string& inCommand, std::string& outCommand);
180  bool CheckCameraCommandValid (std::string& inCommand, std::string& outCommand);
181 
182  bool ChangePortMonitoring (bool isOn);
183  bool ReestablishConnection ();
184  void FormatVersionString ();
185 
186  bool FormAScansCmd (vector<std::string>& words, string& outCommand);
187  bool FormBScansCmd (vector<std::string>& words, string& outCommand);
188 
189  bool FormDelayCmd (vector<std::string>& words, string& outCommand);
190  bool FormPulseCmd (vector<std::string>& words, string& outCommand);
191  bool FormPhaseCmd (vector<std::string>& words, string& outCommand);
192  bool FormFociCmd (vector<std::string>& words, string& outCommand);
193  bool FormFocusCmd (vector<std::string>& words, string& outCommand);
194  bool FormScanCmd (vector<std::string>& words, string& outCommand);
195  bool FormMirrorCmd (vector<std::string>& words, string& outCommand);
196  bool FormVerboseCmd (vector<std::string>& words, string& outCommand);
197  bool FormPauseTimeoutCmd (vector<std::string>& words, string& outCommand);
198  bool FormSetOpticalPortCmd(vector<std::string>& words, string& outCommand);
199 
200  bool FormMghCmd (vector<std::string>& words, string& outCommand);
201  bool FormMstopCmd (vector<std::string>& words, string& outCommand);
202  bool FormMgrCmd (vector<std::string>& words, string& outCommand);
203  bool FormMg2Cmd (vector<std::string>& words, string& outCommand);
204  bool FormMihCmd (vector<std::string>& words, string& outCommand);
205  bool FormMasetCmd (vector<std::string>& words, string& outCommand);
206  bool FormMssetCmd (vector<std::string>& words, string& outCommand);
207  bool FormCameraCommand(vector<std::string>& words, string& outCommand);
208 
209  bool RunSetShutterCmd(vector<std::string>& words);
210  bool RunGetShutterCmd(vector<std::string>& words, bool& open);
211 
212  int CheckMotorPosLimits (vector<std::string>& words);
213  int CheckRelativeMotorPosLimits(vector<std::string>& words);
214  int CheckMotorAccelLimits (vector<std::string>& words);
215  int CheckMotorSpeedLimits (vector<std::string>& words);
216  void RecordMotorPos (std::vector<std::string> & words, int pos);
217  void RecordRelativeMotorPos (std::vector<std::string> & words, int relativePos);
218 
219  bool FormPrampCmd (vector<std::string>& words, string& outCommand);
220  bool FormXYRampCmd (vector<std::string>& words, string& outCommand);
221  bool FormXRampCmd (vector<std::string>& words, string& outCommand);
222  bool FormYRampCmd (vector<std::string>& words, string& outCommand);
223 
224  bool FormOut1Cmd (vector<std::string>& words, string& outCommand);
225  bool FormOut2Cmd (vector<std::string>& words, string& outCommand);
226 
227  bool FormTRetCmd (vector<std::string>& words, string& outCommand);
228  bool FormTriggerCmd (vector<std::string>& words, string& outCommand);
229  bool FormTrigDelayModeCmd(vector<std::string>& words, string& outCommand);
230  bool FormTrigDelayCmd (vector<std::string>& words, string& outCommand);
231  bool FormADivCmd (vector<std::string>& words, string& outCommand);
232  bool FormAHoldCmd (vector<std::string>& words, string& outCommand);
233  bool FormBHoldCmd (vector<std::string>& words, string& outCommand);
234  bool FormRRampCmd (vector<std::string>& words, string& outCommand);
235  bool FormRotCrossCmd (vector<std::string>& words, string& outCommand);
236 
237 private:
238  const int MinRadialScanRadius = 10;
239  static const int MaxShutterDevices = 4;
240 
242  std::atomic<IWPOCTController::ErrorCode> _lastError;
244  std::atomic<IWPOCTController::CurrentStates> _currentStatus;
245 
246  CSerial* _serialPort;
247  std::string _portName;
248  std::string _version;
249  std::string _finalCommand;
251  std::string _commandResponse;
253  std::string _pingCommandResponse;
254  std::vector<std::string> _commandsToIssue;
255  std::mutex _serialPortMutex;
256  SimpleTimer* _monitorTimer;
257  ShutterDevice* _shutterDevices[MaxShutterDevices];
258 
259  bool _portOpen;
260  bool _openPortIfNeeded;
261  bool _isInitializing;
262  bool _isExiting;
263 
264  std::atomic<bool> _swapXY;
265  std::atomic<bool> _isScanning;
266  std::atomic<bool> _readResponse;
267 
268  int _portReadDelay;
269  int _baudRate;
270  int _numStopBits;
271  int _portTimeout;
272  bool _monitorConnection;
273 
274  // Default limits of sweep.
275  int _minXRampPos;
276  int _maxXRampPos;
277  int _minYRampPos;
278  int _maxYRampPos;
279 
280  // Actual sweep values.
281  int _xRampStartPos;
282  int _xRampStopPos;
283  int _yRampStartPos;
284  int _yRampStopPos;
285 
286  int _numScans;
287  int _opticalPort;
288  int _aScans;
289  int _bScans;
290  int _delay;
291  int _verbose;
292  int _pulse;
293  int _phase;
294  int _ptimeout;
295  int _foci;
296  int _focus;
297  int _mirrorMajor;
298  int _mirrorMinor;
299  int _tRet;
300  int _trigger;
301  int _trdMode;
302  int _trDelay;
303  int _aDiv;
304  int _aHold;
305  int _bHold;
306  int _shutterSerialNum;
307  int _shutterOpenClose;
308 
309  // Motor positions.
310  int _pathMotorPos;
311  int _quarterMotorPos;
312  int _halfMotorPos;
313 
314  // Max values set by user.
315  int _pathMotorPosMax;
316  int _quarterHalfWavePosMax;
317 
319  SendNoParams _serialPortLostNotify;
320  void* _serialPortLostNotifyContext;
322  NotifyErrorCallback _notifyErrorCallback;
323 
325  void Init ();
326  void InitShutters ();
327 
328  void Sleep (int ms);
329  bool InitMotors ();
330  void SendCommandsToGalvo ();
331  void AddCommand (std::string command);
332  bool WordIsMotorRef (string& input);
333  bool WordStandsForAll (string& input);
334  void SetStatusFlags (string& command);
335  bool SendCommand (string& command);
336 
337  int SetWithinMinMax (string& input, int minValue, int maxValue);
338  int SetWithinMinMax (int input, int minValue, int maxValue);
339  int SetWithinPathMotorMinMax (string& input, int minValue, int maxValue);
340  int SetWithinQuarterMotorMinMax (string& input, int minValue, int maxValue);
341  int SetWithinHalfMotorMinMax (string& input, int minValue, int maxValue);
342  int SetRRampRadius (string& radius, int centerX, int centerY);
343 
344  int SetXRampStart (int value);
345  int SetXRampStop (int value);
346  int SetYRampStart (int value);
347  int SetYRampStop (int value);
348 
349  bool ParseBool (string& input);
350  int ParseInteger (string& input);
351  double ParseDouble (string& input);
352 
353  ShutterDevice* GetOrCreateShutterDevice(int serialNum);
354  void DeleteShutterDevices();
355 
356  void OutputDebugMsg (const char* str)
357  {
358 #ifdef _DEBUG
359  // Only call OutputDebugString when in debug as this call has some overhead.
360  OutputDebugStringA( str );
361 #endif
362  }
363 
364 };
365 
366 static void MonitorTimer_Tick(CallbackInfo *pInfo);
Interface of the IWPOCTController struct.
void(* SendNoParams)(void *)
Typedefs for callbacks.
Definition: IWPOCTController.h:9
Interface of the CSerial class.
Definition: Serial.h:22
Used for callbacks, in particular for the SimpleTimer class.
Definition: CallbackInfo.h:10
Class that controls a Picard Industries USB shutter device.
Definition: ShutterDevice.h:14
Definition: SimpleTimer.h:27
Class that converts a scanned image from one format to another.
Definition: WPScanController.h:29
int GetYRampStartPos() const override
Get the y ramp start position.
Definition: WPScanController.h:77
int GetArmPosition() const override
Get the reference arm position.
Definition: WPScanController.h:59
bool GetSwapXY() const override
Gets the value of the SwapXY flag.
Definition: WPScanController.h:62
int GetXRampStopPos() const override
Get the x ramp stop position.
Definition: WPScanController.h:76
bool GetMonitorPort() const override
Gets the flag that determines if serial port is constantly being monitored for a connection.
Definition: WPScanController.h:49
bool SetReadResponse(bool value) override
Sets the flag that determines if the response from the serial port is read after a command is issued.
Definition: WPScanController.h:104
int GetQuarterHalfWavePosMax() const override
Gets the max possible value of the quarter and half wave motors’ position.
Definition: WPScanController.h:82
bool SetQuarterHalfWavePosMax(int value) override
Sets the max possible value of the quarter and half wave motor position.
Definition: WPScanController.h:108
int GetXRampMinPos() const override
Get the x ramp min position.
Definition: WPScanController.h:70
bool SetSwapXY(bool value) override
Sets the SwapXY flag.
Definition: WPScanController.h:103
int GetXRampStartPos() const override
Get the x ramp start position.
Definition: WPScanController.h:75
int GetXRampMaxPos() const override
Get the x ramp max position.
Definition: WPScanController.h:71
virtual IWPOCTController::ErrorCode GetLastError() const override
Gets the result of the most current operation.
Definition: WPScanController.h:158
int GetBScans() const override
Gets the number of B-scans output by the controller.
Definition: WPScanController.h:54
bool SetPathMotorPosMax(int value) override
Sets the max possible value of the path motor.
Definition: WPScanController.h:107
int GetFoci() const override
Get the foci value.
Definition: WPScanController.h:55
int GetNumStopBits() const override
Gets the number of stop bits used to communicate with the controller.
Definition: WPScanController.h:47
const char * GetVersion() const override
Get the firmware version of the controller.
Definition: WPScanController.h:51
virtual IWPOCTController::CurrentStates GetCurrentStatus() const override
Gets the current status of the controller.
Definition: WPScanController.h:159
bool IsScanning() const override
Returns the scanning state.
Definition: WPScanController.h:44
int GetPulse() const override
Gets the size of the timing pulse in milliseconds that is used to trigger an A-scan.
Definition: WPScanController.h:57
int GetPathMotorPosMax() const override
Gets the max possible value of the path motor.
Definition: WPScanController.h:81
int GetYRampMaxPos() const override
Get the y ramp max position.
Definition: WPScanController.h:73
bool GetReadResponse() const override
Gets the flag that determines if the response is read from the serial port after each command is sent...
Definition: WPScanController.h:50
int GetPhase() const override
Get the phase delay.
Definition: WPScanController.h:58
bool IsPortOpen() const override
Returns whether the port is open or closed.
Definition: WPScanController.h:41
int GetDelay() const override
Gets the delay in milliseconds between the end of one pulse and the start of another.
Definition: WPScanController.h:56
int GetBaudRate() const override
Gets the baud rate used to communicate with the controller.
Definition: WPScanController.h:46
int GetYRampMinPos() const override
Get the y ramp min position.
Definition: WPScanController.h:72
int GetPortTimeout() const override
Gets the timeout period of the serial port in milliseconds.
Definition: WPScanController.h:48
int GetAScans() const override
Gets the number of A-scans per B-scan output by the controller.
Definition: WPScanController.h:53
int GetYRampStopPos() const override
Get the y ramp stop position.
Definition: WPScanController.h:78
The WPOCT Controller is packaged as a toolkit and provides a set of application programming interface...
Definition: IWPOCTController.h:75
ErrorCode
The following are error codes that are used within the controller.
Definition: IWPOCTController.h:86
CurrentStates
The following are status codes that are used within the controller.
Definition: IWPOCTController.h:104