7 #include "PlusConfigure.h" 10 #include "vtkIGSIOAccurateTimer.h" 19 #define STEPPERDEBUGMSG 0 33 static const char*
CALIB_MSG_PROBE_TO_REF =
"Step the probe to the reference position (12.5 mm). An amber light should replace the green light.";
34 static const char*
CALIB_MSG_PROBE_TO_HOME =
"Step the probe to the home position. The light should revert to green and flashes twice.";
37 static const char*
CALIB_MSG_GRID_TO_REF =
"Step the template grid to the reference position (12.5 mm). An amber light should replace the green light.";
38 static const char*
CALIB_MSG_GRID_TO_HOME =
"Step the template grid to the home position. The light should revert to green and flashes twice.";
41 static const char*
CALIB_MSG_ROTATION_TO_REF =
"Rotate the probe to the reference position (10.5 deg or more). An amber light should replace the green light.";
107 m_IsCalibrated =
false;
108 m_RepeatedPositionErrorCount = 0;
112 InitializeCriticalSection(&m_CriticalSection);
120 m_StepperCOMPort->
Close();
121 delete m_StepperCOMPort;
122 m_StepperCOMPort = NULL;
125 DeleteCriticalSection(&m_CriticalSection);
131 std::ostringstream strComPort;
132 strComPort <<
"COM" << COMPort;
137 LOG_ERROR(
"Unable to set serial port number, stepper already connected!");
141 this->m_StepperCOMPort->SetPortName(strComPort.str());
151 LOG_ERROR(
"Unable to set serial port speed, stepper already connected!");
164 this->m_StepperCOMPort->
Open();
169 LOG_ERROR(
"Stepper is not alive");
175 LOG_ERROR(
"Stepper COM port handle is not alive");
180 LOG_ERROR(
"Stepper is not calibrated");
190 this->m_StepperCOMPort->
Close();
198 int iVerHi=0;
int iVerLo=0;
int iModelNum=0;
int iSerialNum=0;
201 LOG_ERROR(
"Failed to get version info from stepper!");
205 std::ostringstream strVersion;
206 strVersion << iVerHi <<
"." << iVerLo;
207 version = strVersion.str();
209 std::ostringstream strModel;
210 strModel << iModelNum;
211 model = strModel.str();
213 std::ostringstream strSerial;
214 strSerial << iSerialNum;
215 serial = strSerial.str();
223 std::vector<BYTE> vDecodedMessage;
225 EnterCriticalSection(&m_CriticalSection);
230 if (vDecodedMessage.size() >= 2 )
232 Status = vDecodedMessage[1];
237 LeaveCriticalSection(&m_CriticalSection);
244 std::vector<BYTE> vDecodedMessage;
246 EnterCriticalSection(&m_CriticalSection);
251 if (vDecodedMessage.size() >= 7 )
253 iVerHi = vDecodedMessage[1];
254 iVerLo = vDecodedMessage[2];
255 iModelNum = vDecodedMessage[3] * 256 + vDecodedMessage[4];
256 iSerialNum = vDecodedMessage[5] * 256 + vDecodedMessage[6];
261 LeaveCriticalSection(&m_CriticalSection);
268 std::vector<BYTE> vDecodedMessage;
270 EnterCriticalSection(&m_CriticalSection);
275 if (vDecodedMessage.size() >= 5)
277 count = vDecodedMessage[1] * 256 + vDecodedMessage[2];
278 dist = vDecodedMessage[3] * 256 + vDecodedMessage[4];
279 scale = (double)(dist/100.0) / (double)
count;
284 LeaveCriticalSection(&m_CriticalSection);
291 count = 0; dist = 0; scale = 0;
294 m_ProbeScale = scale;
304 count = 0; dist = 0; scale = 0;
317 count = 0; dist = 0; scale = 0;
320 m_RotationScale = scale;
333 if ( ! m_IsCalibrated )
335 LOG_ERROR(
"Cannot get encoder values, the stepper is not calibrated");
339 if ( m_ProbeScale == 0 || m_GridScale == 0 || m_RotationScale == 0 )
344 std::vector<BYTE> vDecodedMessage;
348 EnterCriticalSection(&m_CriticalSection);
350 LeaveCriticalSection(&m_CriticalSection);
352 if (vDecodedMessage.size() >= 7)
354 PPosition = (static_cast<short>(vDecodedMessage[1]*256 + vDecodedMessage[2]))*m_ProbeScale;
355 GPosition = (static_cast<short>(vDecodedMessage[3]*256 + vDecodedMessage[4]))*m_GridScale;
356 RPosition = (static_cast<short>(vDecodedMessage[5]*256 + vDecodedMessage[6]))*m_RotationScale;
357 m_RepeatedPositionErrorCount = 0;
362 m_RepeatedPositionErrorCount++;
367 m_RepeatedPositionErrorCount = 0;
378 std::vector<BYTE> vDecodedMessage;
380 EnterCriticalSection(&m_CriticalSection);
385 if (vDecodedMessage.size() >= 4)
387 PState = vDecodedMessage[1];
388 GState = vDecodedMessage[2];
389 RState = vDecodedMessage[3];
394 LeaveCriticalSection(&m_CriticalSection);
401 std::vector<BYTE> vDecodedMessage;
403 EnterCriticalSection(&m_CriticalSection);
408 if (vDecodedMessage.size() >= 2)
410 State = vDecodedMessage[1];
415 LeaveCriticalSection(&m_CriticalSection);
423 int PState = 0, GState = 0, RState = 0;
424 this->m_IsCalibrated =
false;
430 PState == 5 && GState == 5 && RState == 9)
432 this->m_IsCalibrated =
true;
437 PState == 0 && GState == 5 && RState == 9)
439 this->m_IsCalibrated =
true;
444 this->m_IsCalibrated =
true;
448 return this->m_IsCalibrated;
465 int PState = 0, GState = 0, RState = 0;
467 this->m_IsCalibrated =
false;
521 this->m_IsCalibrated =
true;
529 EnterCriticalSection(&m_CriticalSection);
532 std::vector<BYTE> ackMessage;
541 LeaveCriticalSection(&m_CriticalSection);
549 EnterCriticalSection(&m_CriticalSection);
552 std::vector<BYTE> ackMessage;
561 LeaveCriticalSection(&m_CriticalSection);
569 EnterCriticalSection(&m_CriticalSection);
572 std::vector<BYTE> ackMessage;
581 LeaveCriticalSection(&m_CriticalSection);
589 EnterCriticalSection(&m_CriticalSection);
592 std::vector<BYTE> vDecodedMessage;
593 vDecodedMessage.reserve(50);
596 if (vDecodedMessage.size() >= 2)
598 scaleFactor = vDecodedMessage[1];
604 LeaveCriticalSection(&m_CriticalSection);
616 return ( scaleFactor > 0 );
625 LOG_ERROR(
"Cannot move probe, the stepper is not motorized");
632 double count (0), dist(0), scale(0);
635 LOG_ERROR(
"Unable to get probe reference data");
642 double pPosition(0), gPosition(0), pRotation(0);
643 unsigned long positionRequestNumber(0);
644 if ( !this->
GetEncoderValues(pPosition, gPosition, pRotation, positionRequestNumber) )
646 LOG_ERROR(
"Unable to get probe position");
651 double deltaPosMm = (PositionInMm - pPosition);
654 const int direction = (deltaPosMm >= 0.0 ? 1 : 0 );
657 const int steps = static_cast<int>(fabs(deltaPosMm*scaleFactor)+0.5);
661 unsigned char c1,c2, c3, c4, c5, c6, c7, c8;
662 std::string sMessage;
664 sMessage.push_back(
'7');
665 sMessage.push_back(
'C');
667 sMessage.push_back(c1);
668 sMessage.push_back(c2);
670 sMessage.push_back(c3);
671 sMessage.push_back(c4);
673 sMessage.push_back(c5);
674 sMessage.push_back(c6);
675 ck = 0x7c + direction + (steps/256) + (steps%256);
678 sMessage.push_back(c7);
679 sMessage.push_back(c8);
682 STEPPERCOMMAND moveProbe = (static_cast<STEPPERCOMMAND>(sMessage.c_str()));
684 std::vector<BYTE> vDecodedMessage;
686 EnterCriticalSection(&m_CriticalSection);
690 while ( ReturnCode == -1 )
694 if (vDecodedMessage.size() >= 2 )
696 ReturnCode = vDecodedMessage[1];
700 vtkIGSIOAccurateTimer::GetInstance()->Delay(0.5);
702 LeaveCriticalSection(&m_CriticalSection);
714 EnterCriticalSection(&m_CriticalSection);
718 std::vector<BYTE> ackMessage;
726 LeaveCriticalSection(&m_CriticalSection);
734 EnterCriticalSection(&m_CriticalSection);
738 std::vector<BYTE> ackMessage;
746 LeaveCriticalSection(&m_CriticalSection);
754 EnterCriticalSection(&m_CriticalSection);
758 std::vector<BYTE> ackMessage;
766 LeaveCriticalSection(&m_CriticalSection);
774 EnterCriticalSection(&m_CriticalSection);
778 std::vector<BYTE> ackMessage;
786 LeaveCriticalSection(&m_CriticalSection);
805 if ( ! m_IsCalibrated )
811 std::vector<BYTE> StepperMessage;
815 StepperMessage.clear();
827 std::cout<<
"\n" << std::flush;
834 const char*
p = command;
839 std::cout <<
"->" << std::flush;
848 std::cout <<
b << std::flush;
856 std::cout <<
" " << std::flush;
864 stepperAnswer.clear();
868 std::cout <<
"<-" << std::flush;
871 while ( m_StepperCOMPort->
Read(buff) && buff !=
ETX)
873 if (stepperAnswer.max_size() > stepperAnswer.size())
875 stepperAnswer.push_back(buff);
878 std::cout << buff << std::flush;
885 stepperAnswer.push_back(buff);
889 std::cout <<
" " << std::flush;
899 if (ackMessage.size() >= 5
901 ( ackMessage[1] ==
'F' && ackMessage[2] ==
'A' ) )
904 if ( command[0] == ackMessage[3] && command[1] == ackMessage[4] )
912 std::vector<BYTE> StepperMessage;
925 DecodedMessage.clear();
927 std::vector<BYTE> StepperMessage;
928 StepperMessage.reserve(50);
934 StepperMessage.clear();
938 if (StepperMessage.size() >= 3 )
940 if (StepperMessage[0] ==
STX &&
941 StepperMessage[1] == Response[0] &&
942 StepperMessage[2] == Response[1])
956 std::cout<<
"\n" << std::flush;
965 if ( StepperMessage.size() < 2 || StepperMessage[1] ==
'F' )
970 std::vector<BYTE> DecodedMessage;
982 DecodedMessage.clear();
984 if (StepperMessage.size() >= 3 )
986 if ( StepperMessage[0] ==
STX &&
1001 else if (StepperMessage[0] ==
STX 1019 DecodedMessage.clear();
1020 DecodedMessage.reserve(50);
1022 if ( StepperMessage.size() < 3 )
1028 unsigned int msglen = this->
AsciiToBin(StepperMessage[
i], StepperMessage[
i+1]);
1029 DecodedMessage.push_back(msglen);
1030 msglen = msglen & 0x7;
1032 for( msglen ; msglen != 0; --msglen)
1035 if ( StepperMessage.size() >
i+1)
1037 DecodedMessage.push_back(this->
AsciiToBin(StepperMessage[
i], StepperMessage[
i+1]));
1046 double count = 0, dist = 0, scale = 0;
1057 while ( m_StepperCOMPort->
Read(buff)){}
1064 unsigned char c1,c2, c3, c4, c5, c6;
1066 ck = 0xfa + static_cast<unsigned int>(opcode);
1069 sMessage->push_back(c1);
1070 sMessage->push_back(c2);
1073 sMessage->push_back(c3);
1074 sMessage->push_back(c4);
1077 sMessage->push_back(c5);
1078 sMessage->push_back(c6);
static const char * CALIB_MSG_ROTATION_TO_BASE
static const char * CALIB_MSG_ROTATION_TO_REF
virtual PlusStatus IsStepperAlive()
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_GRID_REFERENCE_DATA
int Write(const BYTE *data, int numberOfBytesToWrite)
static const char * CALIB_MSG_GRID_TO_REF
virtual PlusStatus InitializeStepper(std::string &CalibMsg)
static const char * CALIB_MSG_PROBE_TO_BASE
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_POSITION_DATA_2
PlusStatus GetMotorizationScaleFactor(int &scaleFactor)
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_IS_MOTORIZED
void SetMaxReplyTime(int maxreply)
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_COUNTERS_INVALID
virtual PlusStatus Connect()
int Read(BYTE *data, int maxNumberOfBytesToRead)
void GetPositionMessage(std::vector< BYTE > &StepperMessage, std::vector< BYTE > &DecodedMessage)
PlusCmsBrachyStepper(unsigned long COMPort=1, unsigned long BaudRate=19200)
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_POSITION_DATA_BUTTON_OFF
void GetStepperMessage(STEPPERCOMMAND command, const char *Response, std::vector< BYTE > &DecodedMessage)
bool IsStepperCalibrated()
PlusStatus StepperRotateCalibrationEnable()
static const char * CALIB_MSG_GRID_TO_HOME
void StepperInstruction(STEPPERCOMMAND command)
void SendPositionRequestCommand(STEPPERCOMMAND command, std::vector< BYTE > &vRawMessage)
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_ROTATION_REFERENCE_DATA
PlusStatus GetCalibrationState(int &PState, int &GState, int &RState)
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_MOVE_COMPLETE
bool IsStepperACKRecieved(std::vector< BYTE > &ackMessage, STEPPERCOMMAND command)
PlusStatus GetGridReferenceData(double &count, double &dist, double &scale)
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_POSITION_DATA_1
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_ROTATE_STATE
virtual PlusStatus SetCOMPort(unsigned long COMPort)
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_ROTATION_REFERENCE_DATA
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_PROBE_REFERENCE_DATA
Class for reading and writing data through the serial (RS-232) port.
Initial rotation matrix b
static const char * CALIB_MSG_GRID_TO_BASE
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_DISABLE_ROTATE_CALIBRATION
virtual PlusStatus GetDeviceModelInfo(std::string &version, std::string &model, std::string &serial)
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_CALIBRATION_STATES
void ReadStepperAnswer(std::vector< BYTE > &stepperAnswer)
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_ROTATE_STATE
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_VERSION_INFO
const char * STEPPERRESPCODE
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_STATUS_INFO
PlusStatus GetStatusInfo(unsigned int &Status)
static const char * CALIB_MSG_PROBE_TO_REF
void SetScalingParameters()
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_PROBE_REFERENCE_DATA
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_STATUS_INFO
PlusStatus GetProbeReferenceData(double &count, double &dist, double &scale)
PlusStatus StepperButtonDisable()
static void BinToAscii(unsigned int n, unsigned char *c1, unsigned char *c2)
virtual PlusStatus ResetStepper()
static unsigned int AsciiToBin(unsigned int i, unsigned int j)
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_POSITION_DATA_BUTTON_ON
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_RESET_SYSTEM
virtual PlusStatus SetBaudRate(unsigned long BaudRate)
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_CALIBRATION_STATES
PlusStatus GetRotationReferenceData(double &count, double &dist, double &scale)
static const char * CALIB_MSG_PROBE_TO_HOME
PlusStatus GetVersionInfo(int &iVerHi, int &iVerLo, int &iModelNum, int &iSerialNum)
virtual PlusStatus GetEncoderValues(double &PPosition, double &GPosition, double &RPosition, unsigned long &PositionRequestNumber)
unsigned long m_PositionRequestNumber
void SendStepperCommand(STEPPERCOMMAND command, STEPPERRESPCODE Response, std::vector< BYTE > &vRawMessage)
PlusStatus MoveProbeToPosition(double PositionInMm, int &ReturnCode)
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_BUTTON_ENABLE
const char * STEPPERCOMMAND
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_MOTOR_OFF
Phidget_ChannelClass uint32_t * count
PlusStatus StepperRotateCalibrationDisable()
static const char * CALIB_MSG_COMPLETED
static const int MAX_REPEATED_POSITION_ERROR_COUNT
void CreateAckMessage(BYTE opcode, std::string *sMessage)
BRACHY_STEPPER_TYPE m_BrachyStepperType
PlusStatus GetRotateState(int &State)
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_MOTOR_ON
void DecodeStepperMessage(std::vector< BYTE > StepperMessage, std::vector< BYTE > &DecodedMessage)
bool IsStepperMotorized()
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_MOTORIZATION_CODE
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_BUTTON_DISABLE
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_GRID_REFERENCE_DATA
virtual PlusStatus Disconnect()
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_ENABLE_ROTATE_CALIBRATION
virtual ~PlusCmsBrachyStepper()
PlusStatus GetReferenceData(STEPPERCOMMAND command, STEPPERRESPCODE respcode, double &count, double &dist, double &scale)
PlusStatus StepperButtonEnable()
static PlusCmsBrachyStepper::STEPPERCOMMAND SC_VERSION_INFO
void SetSerialPortSpeed(DWORD speed)
void SendAckMessage(std::vector< BYTE > StepperMessage)
static PlusCmsBrachyStepper::STEPPERRESPCODE SRC_POSITION_DATA_BUTTON_LATCH
bool IsHandleAlive() const
PlusStatus TurnMotorOff()
static const char * CALIB_MSG_ROTATION_TO_HOME