Scope
XYZControlSutter.cpp
1 #include "stdafx.h"
2 #include "XYZControlSutter.h"
3 #include "controllers/ScopeLogger.h"
4 
5 #ifdef SCOPE_USE_SUTTER_XYZSTAGE
6 
7 namespace scope {
8 
9 XYZControlSutter::XYZControlSutter(void)
10  : sc(nullptr)
11  , microstepspermicron(5) {
12 }
13 
14 XYZControlSutter::~XYZControlSutter(void) {
15  StopPolling();
16 }
17 
18 void XYZControlSutter::Initialize(parameters::XYZControlSutter& _params) {
19  microstepspermicron = _params.microstepspermicron();
20 
21  try {
22  sc = std::unique_ptr<SutterController>(new SutterController(_params.comstring()));
23 
24  ScopeLogger scope_logger;
25  scope_logger.Log(L"Connected XYControlSutter to Sutter controller", log_info);
26 
27  // call base class Initialize, which connects ScopeValues, sets initialized to true, and starts the polling thread
28  XYZControl::Initialize(_params);
29 
30  SetZero();
31 
32  UpdateStatus();
33  UpdatePositionValues();
34 
35  scope_logger.Log( L"Initialized XYZControlSutter", log_info);
36 
37  }
38  catch (...) {
39  initialized = false;
40  ScopeExceptionHandler(__FUNCTION__, true, true);
41  }
42 }
43 
44 uint16_t XYZControlSutter::BytesToInt16(const std::vector<BYTE>& _bytevec, const uint32_t& _from) {
45  if ( _bytevec.size() < (_from+2) )
46  return 0;
47 
48  SutterValue16 b; // the union does the magic
49  b.intval = 0;
50  b.charval[0] = _bytevec[_from];
51  b.charval[1] = _bytevec[_from+1];
52  return b.intval;
53 }
54 
55 std::vector<BYTE> XYZControlSutter::Int16ToBytes(const uint16_t& _i) {
56  SutterValue16 b;
57  b.intval = _i;
58  std::vector<BYTE> bytevec(2,0);
59  bytevec[0] = b.charval[0];
60  bytevec[1] = b.charval[1];
61  return bytevec;
62 }
63 
64 uint32_t XYZControlSutter::BytesToInt32(const std::vector<BYTE>& _bytevec, const uint32_t& _from) {
65  if ( _bytevec.size() < (_from+4) )
66  return 0;
67 
68  SutterValue32 b; // the union does the magic
69  b.intval = 0;
70  b.charval[0] = _bytevec[_from];
71  b.charval[1] = _bytevec[_from+1];
72  b.charval[2] = _bytevec[_from+2];
73  b.charval[3] = _bytevec[_from+3];
74  return b.intval;
75 }
76 
77 std::vector<BYTE> XYZControlSutter::Int32ToBytes(const uint32_t& _i) {
78  SutterValue32 b;
79  b.intval = _i;
80  std::vector<BYTE> bytevec(4,0);
81  bytevec[0] = b.charval[0];
82  bytevec[1] = b.charval[1];
83  bytevec[2] = b.charval[2];
84  bytevec[3] = b.charval[3];
85  return bytevec;
86 }
87 
88 void XYZControlSutter::UpdateStatus() {
89  std::vector<BYTE> ret = sc->Command("s\r", 33); // return is 32 bytes status information plus trailing CR
90 
91  stepdiv = BytesToInt16(ret, 24);
92  stepmult = BytesToInt16(ret, 26);
93  speed = BytesToInt16(ret, 28);
94  version = BytesToInt16(ret, 30);
95 }
96 
97 void XYZControlSutter::SetVelocity( const uint16_t& _vel) {
98  try {
99  // Get current velocity resolution (whatever is set on the hand controller)
100  UpdateStatus();
101 
102  uint16_t devvel(_vel);
103  const bool highres = (speed > 32786);
104 
105  // higher velocities are not recommended
106  if ( highres && (devvel > 1310) )
107  devvel = 1310;
108  if ( !highres && (devvel > 3000) )
109  devvel = 3000;
110 
111  // Set highest bit for highres mode (50 microsteps/step)
112  if ( highres )
113  devvel += 32786;
114 
115  std::string cmd = "V";
116  cmd.append(reinterpret_cast<char*>(Int16ToBytes(devvel).data()));
117  cmd.append("\r");
118 
119  std::vector<BYTE> ret = sc->Command(cmd, 1);
120  }
121  catch (...) { ScopeExceptionHandler(__FUNCTION__); }
122 }
123 
124 void XYZControlSutter::UpdatePositionValues() {
125  try {
126  std::vector<BYTE> ret = sc->Command("c\r", 13); // return ist 3*4 bytes plus trailing CR = 13 bytes
127 
128  uint32_t x = BytesToInt32(ret, 0);
129  uint32_t y = BytesToInt32(ret, 4);
130  uint32_t z = BytesToInt32(ret, 8);
131 
132  xyzpos[0]->Set(static_cast<double>(x) / microstepspermicron);
133  xyzpos[1]->Set(static_cast<double>(y) / microstepspermicron);
134  xyzpos[2]->Set(static_cast<double>(z) / microstepspermicron);
135  }
136  catch (...) { ScopeExceptionHandler(__FUNCTION__); }
137 }
138 
139 void XYZControlSutter::SetZero() {
140  try {
141  sc->Command("o\r", 1); // return is only CR
142  }
143  catch (...) { ScopeExceptionHandler(__FUNCTION__); }
144 }
145 
146 void XYZControlSutter::MoveRelative(const double& _xrel, const double& _yrel, const double& _zrel) {
147  try {
148  // Get current position
149  std::vector<BYTE> ret = sc->Command("c\r", 13); // return ist 3*4 bytes plus trailing CR = 13 bytes
150 
151  double xum = BytesToInt32(ret, 0) / microstepspermicron;
152  double yum= BytesToInt32(ret, 4) / microstepspermicron;
153  double zum = BytesToInt32(ret, 8) / microstepspermicron;
154 
155  std::string cmd("m");
156  cmd.append(reinterpret_cast<char*>(Int32ToBytes(round2ui32((xum+_xrel)*microstepspermicron)).data()));
157  cmd.append(reinterpret_cast<char*>(Int32ToBytes(round2ui32((yum+_yrel)*microstepspermicron)).data()));
158  cmd.append(reinterpret_cast<char*>(Int32ToBytes(round2ui32((zum+_zrel)*microstepspermicron)).data()));
159  cmd.append("\r");
160 
161  ret = sc->Command(cmd, 1);
162  }
163  catch (...) { ScopeExceptionHandler(__FUNCTION__); }
164 
165  UpdatePositionValues();
166 }
167 
168 void XYZControlSutter::MoveAbsolute(const double& _xabs, const double& _yabs, const double& _zabs) {
169  try {
170  std::string cmd("m");
171  cmd.append(reinterpret_cast<char*>(Int32ToBytes(round2ui32(_xabs*microstepspermicron)).data()));
172  cmd.append(reinterpret_cast<char*>(Int32ToBytes(round2ui32(_yabs*microstepspermicron)).data()));
173  cmd.append(reinterpret_cast<char*>(Int32ToBytes(round2ui32(_zabs*microstepspermicron)).data()));
174  cmd.append("\r");
175 
176  std::vector<BYTE> ret = sc->Command(cmd, 1);
177  }
178  catch (...) { ScopeExceptionHandler(__FUNCTION__); }
179 
180  UpdatePositionValues();
181 }
182 
183 
184 }
185 
186 #endif
This is the include file for standard system include files, or project specific include files that ar...
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