Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkGalilMotor.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkGalilMotor.h"
14 
15 #include <chrono>
16 #include <thread>
17 
18 #include <math.h>
19 
20 #include "gclib.h"
21 #include "gclib_errors.h"
22 
23 #include "gclibo.h"
24 
26 m_ComPort(0),
27 m_BaudRate(0),
28 m_ErrorMessage("undefined"),
29 m_CurrentWavelength(0),
30 m_HomePosition(32561)
31 {
32 }
33 
35 {
36  this->CloseConnection();
37  MITK_INFO << "[GalilMotor Debug] destroyed that motor";
38 }
39 
40 void mitk::GalilMotor::LoadResorceFile(std::string filename, std::string* lines)
41 {
42  us::ModuleResource resorceFile = us::GetModuleContext()->GetModule()->GetResource(filename);
43  std::string line;
44  if (resorceFile.IsValid() && resorceFile.IsFile())
45  {
46  us::ModuleResourceStream stream(resorceFile);
47  while (std::getline(stream, line))
48  {
49  *lines = *lines + line + "\n";
50  }
51  }
52  else
53  {
54  MITK_ERROR << "Resource file was not valid";
55  }
56 }
57 
58 bool mitk::GalilMotor::OpenConnection(std::string configuration)
59 {
60  m_GalilSystem = 0;
61  m_ReturnCode = G_NO_ERROR;
62  LoadResorceFile(configuration + ".xml", &m_XmlOpoConfiguration);
63  TiXmlDocument xmlDoc;
64 
65  if (xmlDoc.Parse(m_XmlOpoConfiguration.c_str(), 0, TIXML_ENCODING_UTF8))
66  {
67  TiXmlElement* root = xmlDoc.FirstChildElement("Motor");
68  if (root)
69  {
70  TiXmlElement* elementNode = root->FirstChildElement("Coefficients");
71  TiXmlElement* element = elementNode->FirstChildElement("lambda0");
72  m_WavelengthToStepCalibration[0] = std::stod(element->GetText());
73  element = elementNode->FirstChildElement("lambda1");
74  m_WavelengthToStepCalibration[1] = std::stod(element->GetText());
75  element = elementNode->FirstChildElement("lambda2");
76  m_WavelengthToStepCalibration[2] = std::stod(element->GetText());
77  element = elementNode->FirstChildElement("lambda3");
78  m_WavelengthToStepCalibration[3] = std::stod(element->GetText());
79  element = elementNode->FirstChildElement("lambda4");
80  m_WavelengthToStepCalibration[4] = std::stod(element->GetText());
81  element = elementNode->FirstChildElement("lambda5");
82  m_WavelengthToStepCalibration[5] = std::stod(element->GetText());
83  element = elementNode->FirstChildElement("lambda6");
84  m_WavelengthToStepCalibration[6] = std::stod(element->GetText());
85  element = elementNode->FirstChildElement("home");
86  m_HomePosition = std::stod(element->GetText());
87  elementNode = root->FirstChildElement("Signal");
88  element = elementNode->FirstChildElement("Signal-low");
89  m_MinWavelength = std::stod(element->GetText());
90  element = elementNode->FirstChildElement("Signal-default");
91  m_CurrentWavelength = std::stod(element->GetText());
92  element = elementNode->FirstChildElement("Signal-high");
93  m_MaxWavelength = std::stod(element->GetText());
94  elementNode = root->FirstChildElement("Seriell");
95  element = elementNode->FirstChildElement("PortNumber");
96  m_ComPort = std::stoi(element->GetText());
97  element = elementNode->FirstChildElement("Baud");
98  m_BaudRate = std::stoi(element->GetText());
99  }
100  }
101  else
102  {
103  MITK_ERROR << "[GalilMotor Debug] Could not load configuration xml ";
104  }
105  std::string openCommand("COM" + std::to_string(m_ComPort) + " --baud " + std::to_string(m_BaudRate) + " --subscribe ALL --direct");
106  MITK_INFO << "[Galil Debug] before GOpen(" << openCommand << ") = " << m_ReturnCode << "; m_GalilSystem = " << m_GalilSystem;
107  m_ReturnCode = GOpen(openCommand.c_str(), &m_GalilSystem);
108  MITK_INFO << "[Galil Debug] after GOpen = " << m_ReturnCode << "; m_GalilSystem = " << m_GalilSystem;
109  this->Home();
110  return true;
111 }
112 
114 {
115  if (m_GalilSystem)
116  {
117  this->Home();
118  m_ReturnCode = GClose(m_GalilSystem);
119  m_GalilSystem = 0;
120  return true;
121  }
122  else
123  return false;
124 }
125 
127 {
128  double posDouble = 0;
130  posDouble += (m_WavelengthToStepCalibration[1] * wavelength);
131  posDouble += (m_WavelengthToStepCalibration[2] * std::pow(wavelength, 2));
132  posDouble += (m_WavelengthToStepCalibration[3] * std::pow(wavelength, 3));
133  posDouble += (m_WavelengthToStepCalibration[4] * std::pow(wavelength, 4));
134  posDouble += (m_WavelengthToStepCalibration[5] * std::pow(wavelength, 5));
135  posDouble += (m_WavelengthToStepCalibration[6] * std::pow(wavelength, 6));
136  int pos = posDouble;
137  return pos;
138 }
139 
140 bool mitk::GalilMotor::TuneToWavelength(double wavelength, bool isRecalibrating)
141 {
142  std::string positionCommand;
143  if (!isRecalibrating)
144  {
145  positionCommand = "pos=" + std::to_string(this->GetPositionFromWavelength(wavelength));
146  }
147  else
148  {
149  int posAbs = wavelength - m_HomePosition;
150  positionCommand = "pos=" + std::to_string(posAbs);
151  }
152 
153  m_ReturnCode = GCmd(m_GalilSystem, "AB"); // Abort any running programs
154  MITK_INFO << "[Galil Debug] after AB: " << m_ReturnCode << "";
155  m_ReturnCode = GCmd(m_GalilSystem, "BV"); // Burn all variables
156  MITK_INFO << "[Galil Debug] after BV: " << m_ReturnCode << "";
157  m_ReturnCode = GProgramDownload(m_GalilSystem, "", 0); // Erase the program on the system
158  MITK_INFO << "[Galil Debug] after empty GProgramDownload: " << m_ReturnCode << "";
159 
160  m_ReturnCode = GCmd(m_GalilSystem, positionCommand.c_str());
161  MITK_INFO << "[Galil Debug] after sending tune position(" << positionCommand << "): " << m_ReturnCode << "";
162 
163  std::string galilProgramSTUNE;
164  this->LoadResorceFile("STUNE.dmc", &galilProgramSTUNE);
165  m_ReturnCode = GProgramDownload(m_GalilSystem, galilProgramSTUNE.c_str(), 0);
166  MITK_INFO << "[Galil Debug] after STUNE progam: " << m_ReturnCode << galilProgramSTUNE;
167 
168  GSleep(10);
169  m_ReturnCode = GCmd(m_GalilSystem, "XQ#STUNE");
170  MITK_INFO << "[Galil Debug] after sending XQ#STUNE = " << m_ReturnCode;
171  GSleep(10000);
172  int success, pos = -1;
173  m_ReturnCode = GCmdI(m_GalilSystem, "suc=?", &success);
174  MITK_INFO << "[Galil Debug] after asking suc=? = " << m_ReturnCode << "; successfulTune = " << success;
175  m_ReturnCode = GCmdI(m_GalilSystem, "pos=?", &pos);
176  MITK_INFO << "[Galil Debug] after asking pos=? = " << m_ReturnCode << "; pos = " << pos;
177  if (success == 1)
178  {
179  m_CurrentWavelength = wavelength;
180  return true;
181  }
182  else
183  return false;
184 }
185 
186 bool mitk::GalilMotor::FastTuneWavelengths(std::vector<double> wavelengthList)
187 {
188  m_ReturnCode = GCmd(m_GalilSystem, "AB"); // Abort any running programs
189  GSleep(1000);
190  MITK_INFO << "[Galil Debug] after AB: " << m_ReturnCode << "";
191  m_ReturnCode = GCmd(m_GalilSystem, "BV"); // Burn all variables
192  GSleep(2000);
193  MITK_INFO << "[Galil Debug] after BV: " << m_ReturnCode << "";
194 
195  m_ReturnCode = GProgramDownload(m_GalilSystem, "", 0); // Erase the program on the system
196  MITK_INFO << "[Galil Debug] after empty GProgramDownload: " << m_ReturnCode << "";
197  GSleep(1000);
198  std::string positionsCommand("#AUTO\n#PARA\nDA pos[0]\npoints = "+std::to_string(wavelengthList.size())+"\nDM pos[" + std::to_string(wavelengthList.size()) + "];\n");
199 
200  for (int wavelengthIterator = 0; wavelengthIterator < wavelengthList.size(); wavelengthIterator++)
201  {
202  positionsCommand += "pos[" + std::to_string(wavelengthIterator) + "]=" + std::to_string(this->GetPositionFromWavelength(wavelengthList[wavelengthIterator])) + "\n";
203  }
204  positionsCommand += "EN\n";
205 
206  m_ReturnCode = GProgramDownload(m_GalilSystem, positionsCommand.c_str(), 0);
207  MITK_INFO << "[Galil Debug] after sending positions command(" << positionsCommand << "): " << m_ReturnCode << "";
208  GSleep(1000);
209  m_ReturnCode = GCmd(m_GalilSystem, "XQ"); // FTUNE
210  MITK_INFO << "[Galil Debug] after asking XQ#PARA = " << m_ReturnCode;
211  m_ReturnCode = GProgramDownload(m_GalilSystem, "", 0); // Erase the program on the system
212  MITK_INFO << "[Galil Debug] after empty GProgramDownload: " << m_ReturnCode << "";
213  GSleep(1000);
214  std::string galilProgramFTUNE;
215  this->LoadResorceFile("FTUNE.dmc", &galilProgramFTUNE);
216  m_ReturnCode = GProgramDownload(m_GalilSystem, galilProgramFTUNE.c_str(), 0);
217  MITK_INFO << "[Galil Debug] after FTUNE progam upload: " << m_ReturnCode << "";
218 
219  GSleep(1000);
220  m_ReturnCode = GCmd(m_GalilSystem, "XQ"); // FTUNE
221  MITK_INFO << "[Galil Debug] after asking XQ#FTUNE = " << m_ReturnCode;
222  GSleep(10000);
223  int count = -1;
224  m_ReturnCode = GCmdI(m_GalilSystem, "count=?", &count);
225  MITK_INFO << "[Galil Debug] cycles = " << count;
226  m_ReturnCode = GCmdI(m_GalilSystem, "pos=?", &count);
227  MITK_INFO << "[Galil Debug] pos = " << count;
228  m_ReturnCode = GCmdI(m_GalilSystem, "points=?", &count);
229  MITK_INFO << "[Galil Debug] points = " << count;
230 
231  if (count > 0)
232  return true;
233  else
234  return false;
235 }
236 
238 {
239  m_ReturnCode = GCmd(m_GalilSystem, "AB"); // Abort any running programs
240  MITK_INFO << "[Galil Debug] after AB: " << m_ReturnCode << "";
241  GSleep(1000);
242  m_ReturnCode = GCmd(m_GalilSystem, "BV"); // Burn all variables
243  MITK_INFO << "[Galil Debug] after BV: " << m_ReturnCode << "";
244  GSleep(1000);
245  m_ReturnCode = GProgramDownload(m_GalilSystem, "", 0); // Erase the program on the system
246  MITK_INFO << "[Galil Debug] after empty GProgramDownload: " << m_ReturnCode << "";
247 
248  std::string galilProgram;
249  this->LoadResorceFile("GHOME.dmc", &galilProgram);
250 
251  m_ReturnCode = GProgramDownload(m_GalilSystem, galilProgram.c_str(), 0);
252  MITK_INFO << "[Galil Debug] after home GProgramDownloadFile = " << m_ReturnCode << "; m_GalilSystem = " << m_GalilSystem;
253 
254  m_ReturnCode = GCmd(m_GalilSystem, "XQ"); // HOME
255  GSleep(15000);
256  int val = -2;
257  m_ReturnCode = GCmdI(m_GalilSystem, "suc=?", &val);
258 
259  MITK_INFO << "[Galil Debug] after home execution = " << m_ReturnCode << "; m_GalilSystem = " << m_GalilSystem << " homed = " << val;
260 
261  return true;
262 }
263 
265 {
266  return m_MinWavelength;
267 }
268 
270 {
271  return m_MaxWavelength;
272 }
273 
275 {
276  return m_CurrentWavelength;
277 }
virtual bool OpenConnection(std::string configuration)
static char * line
Definition: svm.cpp:2870
virtual bool CloseConnection()
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void LoadResorceFile(std::string filename, std::string *lines)
double m_CurrentWavelength
int GetPositionFromWavelength(double wavelength)
bool FastTuneWavelengths(std::vector< double > wavelengthList)
std::string m_XmlOpoConfiguration
Module * GetModule() const
double m_WavelengthToStepCalibration[7]
double GetCurrentWavelength()
bool TuneToWavelength(double wavelength, bool isRecalibrating)
virtual bool Home()
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.