Scope
XYZControlGalil.cpp
1 #include "stdafx.h"
2 #include "XYZControlGalil.h"
3 #include "GalilController.h"
4 #include "parameters/Devices.h"
5 #include "controllers/ScopeLogger.h"
6 
7 // Only use this code if we need it
8 #ifdef SCOPE_USE_GALIL_XYZSTAGE
9 
10 namespace scope {
11 
12 XYZControlGalil::XYZControlGalil()
13  : gc(nullptr)
14  , xcountspermicron(1.0)
15  , ycountspermicron(1.0)
16  , zcountspermicron(1.0) {
17 }
18 
19 XYZControlGalil::~XYZControlGalil() {
20  StopPolling();
21 }
22 
23 void XYZControlGalil::Initialize(parameters::XYZControlGalil& _params) {
24  xcountspermicron = _params.xcountspermicron();
25  ycountspermicron = _params.ycountspermicron();
26  zcountspermicron = _params.zcountspermicron();
27 
28  try {
29  gc = std::unique_ptr<GalilController>(new GalilController(_params.comstring()));
30 
31  ScopeLogger scope_logger;
32  std::wstringstream msg;
33  msg << L"Connected XYControlGalil to Galil controller, library version " << gc->LibraryVersion() << L"\n" , gc->Connection();
34  scope_logger.Log(msg.str(), log_info);
35 
36  gc->Command(L"SH"); // SH: servo here = switch on motor, set zero position
37  gc->Command(L"WT100");
38  gc->Command(L"SB1"); // SB: sets bit 1 -> loosens z axis brake
39  gc->Command(L"WT500"); // WT: wait 500ms for the brake to loose
40 
41  // call base class Initialize, which connects ScopeValues, sets initialized to true and starts the polling thread
42  XYZControl::Initialize(_params);
43 
44  scope_logger.Log(L"Initialized XYZControlGalil", log_info);
45 
46  }
47  catch (...) {
48  initialized = false;
49  ScopeExceptionHandler(__FUNCTION__, true, true);
50  }
51  SetZero();
52 }
53 
54 void XYZControlGalil::UpdatePositionValues() {
55  double p = gc->CommandValue(L"PAX=?"); // PAX=?: Position absolute x
56  xyzpos[0]->Set(p/xcountspermicron);
57  p = gc->CommandValue(L"PAY=?"); // PAY=?: Position absolute y
58  xyzpos[1]->Set(p/ycountspermicron);
59  p = gc->CommandValue(L"PAZ=?"); // PAZ=?: Position absolute z
60  xyzpos[2]->Set(p/zcountspermicron);
61 }
62 
63 double XYZControlGalil::CurrentXPosition() {
64  double pos = 0;
65  try {
66  pos = gc->CommandValue(L"PAX=?"); // PAX=?: gives position absolute x
67  pos /= xcountspermicron;
68  xyzpos[0]->Set(pos);
69  } catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
70  return pos;
71 }
72 
73 double XYZControlGalil::CurrentYPosition() {
74  double pos = 0;
75  try {
76  pos = gc->CommandValue(L"PAY=?"); // PAY=?: gives position absolute y
77  pos /= ycountspermicron;
78  xyzpos[1]->Set(pos);
79  } catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
80  return pos;
81 }
82 
83 double XYZControlGalil::CurrentZPosition() {
84  double pos = 0;
85  try {
86  pos = gc->CommandValue(L"PAZ=?"); // PAZ=?: gives position absolute z
87  pos /= zcountspermicron;
88  xyzpos[2]->Set(pos);
89  } catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
90  return pos;
91 }
92 
93 void XYZControlGalil::SetZeroXAxis() {
94  try {
95  gc->Command(L"DPX=0"); // DP: define position = set current motor and command position
96  UpdatePositionValues();
97  }
98  catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
99 }
100 
101 void XYZControlGalil::SetZeroYAxis() {
102  try {
103  gc->Command(L"DPY=0"); // DP: define position = set current motor and command position
104  UpdatePositionValues();
105  }
106  catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
107 }
108 
109 void XYZControlGalil::SetZeroZAxis() {
110  try {
111  gc->Command(L"DPZ=0"); // DP: define position = set current motor and command position
112  UpdatePositionValues();
113  }
114  catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
115 }
116 
117 void XYZControlGalil::MoveRelative(const double& _xrel, const double& _yrel, const double& _zrel) {
118  try {
119  if ( (abs(_xrel) > 2000) || (abs(_yrel) > 2000) || (abs(_zrel) > 2000) )
120  throw ScopeException("XYZControlGalil::MoveRelative travel range too large");
121  int32_t countsx = round2i32(static_cast<double>(xcountspermicron) * _xrel);
122  int32_t countsy = round2i32(static_cast<double>(ycountspermicron) * _yrel);
123  int32_t countsz = round2i32(static_cast<double>(zcountspermicron) * _zrel);
124  std::wstringstream cmd;
125  cmd << L"PR " << countsx << L"," << countsy << L"," << countsz;
126  DBOUT(L"XYZControlGalil::MoveRelative " << cmd.str());
127  gc->Command(cmd.str()); // PR: Position relative = move relative, in encoder counts
128  gc->Command(L"BG"); // BG: Begin move
129  gc->Command(L"MG TIME"); // just make sure the motion is complete when MoveSlow returns
130  } catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
131  UpdatePositionValues();
132 }
133 
134 void XYZControlGalil::MoveAbsolute(const double& _xabs, const double& _yabs, const double& _zabs) {
135  try {
136  if ( (abs(_xabs) > 20000) || (abs(_yabs) > 20000) || (abs(_zabs) > 20000) )
137  throw ScopeException("XYZControlGalil::MoveAbsolute travel range too large");
138  int32_t countsx = round2i32(static_cast<double>(xcountspermicron) * _xabs);
139  int32_t countsy = round2i32(static_cast<double>(ycountspermicron) * _yabs);
140  int32_t countsz = round2i32(static_cast<double>(zcountspermicron) * _zabs);
141  std::wstringstream cmd; // PA: Position absolute = move absolute, in encoder counts
142  cmd << L"PA " << countsx << L"," << countsy << L"," << countsz;
143  gc->Command(cmd.str());
144  gc->Command(L"BG"); // BG: Begin move
145  // Motion complete does not work with external commands!
146  // gc->Ctrl()->command("MC"); // MC: Motion complete = wait with next command execution until current motion finished
147  gc->Command(L"MG TIME"); // just make sure the motion is complete when MoveAbsolute returns
148  } catch (...) { ScopeExceptionHandler(__FUNCTION__, true, true); }
149  UpdatePositionValues();
150 }
151 
152 }
153 
154 #endif
This is the include file for standard system include files, or project specific include files that ar...
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
void ScopeExceptionHandler(const std::string &_origin, const bool &_log, const bool &_showmessagebox, const bool &_trace, const bool &_rethrow)
Handles all exceptions and does nice logging.
virtual void Initialize(parameters::XYZControl &_params)
Initialize hardware.
Definition: XYZControl.cpp:16