12 default_timeout = 1000;
19 default_timeout = 1000;
27 void ESPMotorCtrl::getStageName(
int axis, std::string &sname)
29 std::string com, resp;
34 if(makeCom(com, axis,
"ZX?") < 0)
36 errStr <<
"Error making command ZX?" << __FILE__ <<
" " << __LINE__ <<
'\n';
40 sendCommand(com, resp, default_timeout);
43 errc = getError(axis);
47 errStr <<
"An error occurred (" << errc <<
") " << __FILE__ <<
" " << __LINE__ <<
'\n';
55 sname = resp.substr(3, resp.length()-3);
59 sname = resp.substr(4, resp.length()-4);
69 errStr <<
"Already initialized. Can't change number of axes " << __FILE__ <<
" " << __LINE__ <<
'\n';
85 std::string com, resp, err;
91 errStr <<
"Not initialized. Must set number of axes. "<< __FILE__ <<
" " << __LINE__ <<
'\n';
96 std::cout <<
"Power on initialization\n";
102 if(
stageName[i] ==
"" && errStr.str() !=
"")
104 errStr <<
"Error getting stagename " << i <<
" " << __FILE__ <<
" " << __LINE__ <<
'\n';
110 std::cout <<
"Got stage names\n";
116 if(getCtrlState(i+1, state) != 0)
118 errStr <<
"Error getting controller " << i+1 <<
" state." << __FILE__ <<
" " << __LINE__ <<
"\n";
128 if(state[0] ==
'0' || (state[0] ==
'1' && state[1] ==
'1'))
132 errStr <<
"Power on homing error " << __FILE__ <<
" " << __LINE__ <<
"\n";
143 std::cout <<
"Power on init complete\n";
149 char ESPMotorCtrl::getError(
int axis)
151 std::string com, resp;
156 errStr <<
"Bad axis " << __FILE__ <<
" " << __LINE__ <<
'\n';
160 if(makeCom(com, axis,
"TE") < 0)
162 errStr <<
"Error making command TE" << __FILE__ <<
" " << __LINE__ <<
'\n';
166 sendCommand(com, resp, default_timeout);
179 std::string ESPMotorCtrl::getStageName(
int axis)
181 if(axis < 1 || axis > numAxes)
183 errStr <<
"Bad Axis " << __FILE__ <<
" " << __LINE__ <<
'\n';
190 int ESPMotorCtrl::getCtrlState(
int axis, std::string &state)
192 std::string com, resp;
195 if(makeCom(com, axis,
"TS") < 0)
197 errStr <<
"Error making command TS" << __FILE__ <<
" " << __LINE__ <<
'\n';
201 sendCommand(com, resp, default_timeout);
204 std::string rcom, rval;
206 splitResponse(raxis, rcom, rval, resp);
210 errStr <<
"An Error occurred " << __FILE__ <<
" " << __LINE__ <<
'\n';
216 errStr <<
"Wrong axis returned " << __FILE__ <<
" " << __LINE__ <<
'\n';
222 errStr <<
"Wrong command returned " << __FILE__ <<
" " << __LINE__ <<
'\n';
226 if(rval.length() != 6)
228 errStr <<
"Incorrect response length "<< __FILE__ <<
" " << __LINE__ <<
'\n';
232 state = rval.substr(4, 2);
238 double ESPMotorCtrl::getCurPos(
int axis)
240 std::string com, resp;
242 if(makeCom(com, axis,
"TP") < 0)
244 errStr <<
"Error making command TP" << __FILE__ <<
" " << __LINE__ <<
'\n';
248 sendCommand(com, resp, default_timeout);
251 std::string rcom, rval;
252 splitResponse(raxis, rcom, rval, resp);
256 errStr <<
"An Error occurred " << __FILE__ <<
" " << __LINE__ <<
'\n';
262 errStr <<
"Wrong axis returned " << __FILE__ <<
" " << __LINE__ <<
'\n';
268 errStr <<
"Wrong command returned " << __FILE__ <<
" " << __LINE__ <<
'\n';
272 return strtod(rval.c_str(),0);
276 int ESPMotorCtrl::home(
int axis)
278 std::string com, resp;
280 if(makeCom(com, axis,
"OR") < 0)
282 errStr <<
"Error making command OR" << __FILE__ <<
" " << __LINE__ <<
'\n';
288 if(sendCommand(com) < 0)
290 errStr <<
"Error sending command OR" << __FILE__ <<
" " << __LINE__ <<
'\n';
294 char errc = getError(axis);
297 errStr <<
"An error occurred in homing (" << errc <<
") " << __FILE__ <<
" " << __LINE__ <<
'\n';
305 int ESPMotorCtrl::stop(
int axis)
315 if(makeCom(com, axis,
"ST") < 0)
317 errStr <<
"Error making command ST" << __FILE__ <<
" " << __LINE__ <<
'\n';
318 errStr <<
"Sending 0ST" << __FILE__ <<
" " << __LINE__ <<
'\n';
323 if(sendCommand(com) < 0)
325 errStr <<
"Error sending command ST" << __FILE__ <<
" " << __LINE__ <<
'\n';
329 char errc = getError(axis);
332 errStr <<
"An error occurred in stopping (" << errc <<
") " << __FILE__ <<
" " << __LINE__ <<
'\n';
339 int ESPMotorCtrl::gotoAbsPos(
int axis,
double apos)
343 if(makeCom(com, axis,
"PA", apos) < 0)
345 errStr <<
"Error making command PA " << __FILE__ <<
" " << __LINE__ <<
'\n';
351 if(sendCommand(com) < 0)
353 errStr <<
"Error sending command PA " << __FILE__ <<
" " << __LINE__ <<
'\n';
357 char errc = getError(axis);
360 errStr <<
"An error occurred in goto abs. pos. (" << errc <<
") " << __FILE__ <<
" " << __LINE__ <<
'\n';
367 int ESPMotorCtrl::gotoRelPos(
int axis,
double apos)
373 if(fabs(apos) < MOVE_RESOLUTION)
return 0;
375 if(makeCom(com, axis,
"PR", apos) < 0)
377 errStr <<
"Error making command PR " << __FILE__ <<
" " << __LINE__ <<
'\n';
383 if(sendCommand(com) < 0)
385 errStr <<
"Error sending command PR" << __FILE__ <<
" " << __LINE__ <<
'\n';
389 char errc = getError(axis);
392 errStr <<
"An error occurred in goto rel. pos. (" << errc <<
") " << __FILE__ <<
" " << __LINE__ <<
'\n';
400 int ESPMotorCtrl::makeCom(std::string &str,
int axis,
const char *com)
404 if(axis < 1 || axis > numAxes)
406 errStr <<
"Bad axis " << __FILE__ <<
" " << __LINE__ <<
'\n';
419 int ESPMotorCtrl::makeCom(std::string &str,
int axis,
const char *com,
int val)
424 if(axis < 1 || axis > numAxes)
426 errStr <<
"Bad axis " << __FILE__ <<
" " << __LINE__ <<
'\n';
436 snprintf(tmp, 10,
"%i", val);
443 int ESPMotorCtrl::makeCom(std::string &str,
int axis,
const char *com,
double val)
448 if(axis < 1 || axis > numAxes)
450 errStr <<
"Bad axis " << __FILE__ <<
" " << __LINE__ <<
'\n';
460 snprintf(tmp, 10,
"%f", val);
467 int ESPMotorCtrl::makeCom(std::string &str,
int axis,
const char *com, std::string &val)
472 if(axis < 1 || axis > numAxes)
474 errStr <<
"Bad axis " << __FILE__ <<
" " << __LINE__ <<
'\n';
489 int ESPMotorCtrl::splitResponse(
int &axis, std::string &com, std::string &val, std::string &resp)
491 if(resp.length() < 3)
493 errStr <<
"Invalid response " << __FILE__ <<
" " << __LINE__;
499 errStr <<
"Invalid response " << __FILE__ <<
" " << __LINE__;
508 axis = resp[0] -
'0';
512 axis = atoi(resp.substr(0,2).c_str());
518 com = resp.substr(1,2);
519 if(resp.length() < 4 ) val =
"";
520 else val = resp.substr(3, resp.length()-3);
524 if(resp.length() < 4)
526 errStr <<
"Invalid response " << __FILE__ <<
" " << __LINE__;
531 com = resp.substr(2,2);
532 if(resp.length() < 5) val =
"";
533 else val = resp.substr(4, resp.length()-4);
540 int ESPMotorCtrl::sendCommand(std::string &com, std::string &resp,
int timeout __attribute__((unused)))
542 std::cout << com <<
"\n";
547 int ESPMotorCtrl::sendCommand(std::string &com)
549 std::cout << com <<
"\n";
555 std::cout <<
"ESP moveStart\n";
int numAxes
Number of motor controllers.
Declarations for a Newport ESP motor controller.
std::vector< int > axisAddress
The controller address of each axis.
int powerOnInit()
Checks the controllers for current errors, and populates the stageName vector.
ESPMotorCtrl()
Default constructor.
virtual void moveStart()
Called before starting a move. Empty here, to be overridden.
std::vector< std::string > stageName
The names of the connected motors.
The namespace of VisAO software.
int setNumAxes(int no)
Sets the number of axes. Can only be called when uninitialized.
virtual ~ESPMotorCtrl()
Destructor.