RADMotionModel.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #include "stdafx.h"
  2. #include "RADMotionModel.h"
  3. #include "MotionStages.h"
  4. #include "RADMotionStageArgs.h"
  5. #include "IMachineryManager.h"
  6. #include "ITubeAngleController.h"
  7. #include "ITubeHeightController.h"
  8. #include "ISensorADController.h"
  9. #include "ISensorEncoderController.h"
  10. #include "IPositionManager.h"
  11. #include "DigitalTwinLogger.h"
  12. #include "TubeHorizontalController.h"
  13. #include "TubeLineMotionSwitchController.h"
  14. #include "ConfigurerMotion.h"
  15. #include "ConfigurerWS.h"
  16. using namespace DIOS::Dev::Detail::MachineryECOM;
  17. RADMotionModel::RADMotionModel()
  18. :m_StageName(""),
  19. m_stageArgs(new RADMotionStageArgs()),
  20. m_coordinates(nullptr),
  21. m_motorTubeAngle(nullptr),
  22. m_motorTubeHeight(nullptr),
  23. m_adDetectorHeight(nullptr),
  24. m_encoderTubeAngle(nullptr),
  25. m_encoderTubeHeight(nullptr),
  26. m_motorTubeHorizontal(nullptr),
  27. m_adTubeHorizontal(nullptr),
  28. m_encoderTubeHorizontal(nullptr),
  29. m_tubeLineMotionSwitch(nullptr)
  30. {
  31. }
  32. RADMotionModel::~RADMotionModel()
  33. {
  34. if (m_stageArgs)
  35. {
  36. delete m_stageArgs;
  37. m_stageArgs = nullptr;
  38. }
  39. }
  40. void RADMotionModel::ChangeStage(const std::string &stageName)
  41. {
  42. m_StageName = stageName;
  43. OnMotionStage(m_StageName);
  44. }
  45. std::string RADMotionModel::GetStageName()
  46. {
  47. return m_StageName;
  48. }
  49. IMotionStageArgs *RADMotionModel::GetStageArgs()
  50. {
  51. return m_stageArgs;
  52. }
  53. void RADMotionModel::OnMotionStage(const std::string &name)
  54. {
  55. if (gmotionLog) gmotionLog->Info("[RADMotionModel][OnMotionStage]->[Enter][{$}]", name.c_str());
  56. if (name == RAD_STAGE_CLEAR_PARAMS)
  57. {
  58. OnStageClearParams();
  59. }
  60. else if (name == RAD_STAGE_STOP_MOVE)
  61. {
  62. OnStageStopMove();
  63. }
  64. else if (name == RAD_STAGE_CENTER_ADJUST)
  65. {
  66. OnStageCenterAdjust();
  67. }
  68. else if (name == RAD_STAGE_MOVE_TUBE_HEIGHT)
  69. {
  70. OnMotionStageMoveTubeHeight();
  71. }
  72. else if (name == RAD_STAGE_MOVE_TUBE_HORIZONTAL)
  73. {
  74. OnMotionStageMoveTubeHorizontal();
  75. }
  76. else if (name == RAD_STAGE_MOVE_TUBE_ROTATION)
  77. {
  78. OnMotionStageMoveTubeRotation();
  79. }
  80. else if (name == RAD_STAGE_ADJUST_SID_MOVE)
  81. {
  82. OnMotionStageMove2SID(m_CurWS);
  83. }
  84. if (gmotionLog) gmotionLog->Info("[RADMotionModel][OnMotionStage]->[Exit][{$}]", name.c_str());
  85. }
  86. void RADMotionModel::Initialize(IMachineryManager *machineryManager, IPositionManager *coordinates)
  87. {
  88. m_coordinates = coordinates;
  89. m_motorTubeAngle = (ITubeAngleController *)(machineryManager->Resove(CONTROLLER_TUBE_ANGLE));
  90. m_motorTubeHeight = (ITubeHeightController *)(machineryManager->Resove(CONTROLLER_TUBE_HEIGHT));
  91. m_adDetectorHeight = (ISensorADController *)(machineryManager->Resove(CONTROLLER_DETECTOR_HEIGHT_AD));
  92. m_encoderTubeAngle = (ISensorEncoderController *)(machineryManager->Resove(CONTROLLER_TUBE_ANGLE_ENCODER));
  93. m_encoderTubeHeight = (ISensorEncoderController *)(machineryManager->Resove(CONTROLLER_TUBE_HEIGHT_ENCODER));
  94. m_motorTubeHorizontal = (ITubeHeightController*)(machineryManager->Resove(CONTROLLER_TUBE_HORIZONTAL));
  95. m_encoderTubeHorizontal = (ISensorEncoderController*)(machineryManager->Resove(CONTROLLER_TUBE_HORIZONTAL_ENCODER));
  96. m_tubeLineMotionSwitch = (IOutputController*)(machineryManager->Resove(CONTROLLER_TUBE_LINE_MOTION_SWTICH));
  97. assert(m_motorTubeAngle);
  98. assert(m_motorTubeHeight);
  99. assert(m_adDetectorHeight);
  100. assert(m_encoderTubeAngle);
  101. assert(m_encoderTubeHeight);
  102. assert(m_motorTubeHorizontal);
  103. assert(m_encoderTubeHorizontal);
  104. }
  105. void RADMotionModel::LoadMachineryParams(ResDataObject &params)
  106. {
  107. m_MachineryParams = params;
  108. }
  109. void RADMotionModel::LoadModelParams(ResDataObject &params)
  110. {
  111. }
  112. void RADMotionModel::SetTechnicalParams(ResDataObject &params)
  113. {
  114. m_technicalParams = params;
  115. DigitalTwinLogger::Instance()->ReceiveExamMode("RAD");
  116. }
  117. void RADMotionModel::OnFeedbackMotionParams(ResDataObject &params)
  118. {
  119. }
  120. BOOL RADMotionModel::GetMotionParams(ResDataObject &params)
  121. {
  122. return TRUE;
  123. }
  124. void RADMotionModel::OnStageClearParams()
  125. {
  126. m_motorTubeAngle->ClearSignal();
  127. m_encoderTubeHeight->ClearTrigger();
  128. m_encoderTubeHorizontal->ClearTrigger();
  129. }
  130. void RADMotionModel::OnStageStopMove()
  131. {
  132. m_motorTubeAngle->StopRotation();
  133. m_motorTubeHeight->StopMove();
  134. m_motorTubeHorizontal->StopMove();
  135. auto tubeAngleSensorValue = m_coordinates->GetCurrentSensorValue(TOMO_TUBE_ANGLE);
  136. auto tubeHeightSensorValue = m_coordinates->GetCurrentSensorValue(TOMO_TUBE_HEIGHT);
  137. DigitalTwinLogger::Instance()->ReceiveTomoStartPosition(tubeAngleSensorValue, tubeHeightSensorValue);
  138. DigitalTwinLogger::Instance()->ReceiveMotionStopPosition(tubeAngleSensorValue, tubeHeightSensorValue);
  139. DigitalTwinLogger::Instance()->SaveMotionRecord();
  140. }
  141. void RADMotionModel::OnStageCenterAdjust()
  142. {
  143. auto tubeAngleSensorValue = m_coordinates->GetCurrentSensorValue(TOMO_TUBE_ANGLE);
  144. auto tubeHeightSensorValue = m_coordinates->GetCurrentSensorValue(TOMO_TUBE_HEIGHT);
  145. auto detectorHeight = m_coordinates->GetCurrentPhysical(TOMO_DETECTOR_HEIGHT);
  146. DigitalTwinLogger::Instance()->ReceiveMotionInitialPosition(tubeAngleSensorValue, tubeHeightSensorValue);
  147. DigitalTwinLogger::Instance()->ReceiveDetectorHeight(detectorHeight);
  148. AlignTubeHeightToDetector();
  149. RotateTubeToExposure();
  150. }
  151. void RADMotionModel::AlignTubeHeightToDetector()
  152. {
  153. float despos = 1.0;
  154. float tubeCurPos = 1.0;
  155. if (m_CurWS == WS_WALL)
  156. {
  157. despos = m_coordinates->GetCurrentPhysical(TOMO_DETECTOR_HEIGHT);
  158. tubeCurPos = m_coordinates->GetCurrentPhysical(TOMO_TUBE_HEIGHT);
  159. }
  160. else if (m_CurWS == WS_TABLE)
  161. {
  162. despos = m_coordinates->GetCurrentPhysical(TOMO_DETECTOR_HORIZONTAL);
  163. tubeCurPos = m_coordinates->GetCurrentAbsolutePhysical(TOMO_TUBE_HORIZONTAL);
  164. }
  165. if (m_technicalParams.GetFirstOf("IsTubeSameHeightWithDetector") >= 0)
  166. {
  167. if (!atoi(m_technicalParams["IsTubeSameHeightWithDetector"]))
  168. {
  169. if (m_technicalParams.GetFirstOf("TubeDesHeight") >= 0)
  170. {
  171. auto desHeigt = strtof((const char*)m_technicalParams["TubeDesHeight"], nullptr);
  172. if (desHeigt >= 1.0f)
  173. {
  174. tubeCurPos = desHeigt;
  175. }
  176. }
  177. }
  178. }
  179. if (gmotionLog) gmotionLog->Info("[RADMotionModel][AlignTubeToDetector]->[{$}][Tube cur pos:{$:f3}, des pos:{$:f3}]", m_CurWS.c_str(), tubeCurPos, despos);
  180. if (m_CurWS == WS_WALL)
  181. {
  182. int direction = JudgeDirectionInTubeHeightAxis(tubeCurPos, despos);
  183. MoveTubeHeight(direction, abs(despos - tubeCurPos));
  184. }
  185. else if (m_CurWS == WS_TABLE)
  186. {
  187. int direction = JudgeDirectionInTubeHorizontalAxis(tubeCurPos, despos);
  188. MoveTubeHorizontal(direction, abs(despos - tubeCurPos));
  189. }
  190. }
  191. void RADMotionModel::RotateTubeToExposure()
  192. {
  193. float tubeAngle = m_coordinates->GetCurrentPhysical(TOMO_TUBE_ANGLE);
  194. float exposureAngle = GetExposureAngle();
  195. //if (m_technicalParams.GetFirstOf("TubeDesAngle") >= 0)
  196. //{
  197. // exposureAngle = strtof((const char*)m_technicalParams["TubeDesAngle"], nullptr);
  198. //}
  199. if (gmotionLog) gmotionLog->Info("[RADMotionModel][RotateTubeToExposure]->[{$}][Tube des angle:{$:f3}]", m_CurWS.c_str(), exposureAngle);
  200. int direction = JudgeDirectionInTubeAngleAxis(tubeAngle, exposureAngle);
  201. RotateTubeAngle(direction, abs(tubeAngle - exposureAngle));
  202. }
  203. void RADMotionModel::MoveTubeHeight(int direction, float offset)
  204. {
  205. auto tolerance = ConfigurerMotion::GetMotionToleranceLine();
  206. if (offset < tolerance)
  207. {
  208. if (gmotionLog) gmotionLog->Info("[RADMotionModel][MoveTubeHeight]->[ Offset is {$:f6}, less than {$:f6}m, no motion]", offset, tolerance);
  209. return;
  210. }
  211. if (m_tubeLineMotionSwitch)
  212. {
  213. m_tubeLineMotionSwitch->OutputSignal(false);
  214. }
  215. //float speed = (float)atof((const char *)m_MachineryParams["MotionSpeed"]);
  216. //auto period = m_coordinates->ConvertMotorSpeed(CONTROLLER_TUBE_HEIGHT, speed);
  217. auto step = m_coordinates->ConvertMotorStepValue(CONTROLLER_TUBE_HEIGHT, offset);
  218. auto period = ConfigurerMotion::GetTubeHeightNormalPeriod();
  219. if (period < 50 || (period - 50) < 30)
  220. {
  221. period = 50;
  222. }
  223. m_motorTubeHeight->Move(direction, step, period);
  224. }
  225. void RADMotionModel::RotateTubeAngle(int direction, float offset)
  226. {
  227. //auto period = (DWORD)atoi((const char *)m_MachineryParams["TubeAnglePeriodP0"]);
  228. auto period = (DWORD)ConfigurerMotion::GetTubeRotateResetPeriod();
  229. auto step = m_coordinates->ConvertMotorStepValue(CONTROLLER_TUBE_ANGLE, offset);
  230. m_motorTubeAngle->Rotate(direction, step, period);
  231. }
  232. float RADMotionModel::GetExposureAngle()
  233. {
  234. float exposureangle = (float)atof((const char*)m_MachineryParams["TubeAngleOfRAD"]);
  235. if (m_CurWS == WS_WALL)
  236. {
  237. exposureangle = ConfigurerWS::GetDefaultAngleWall();
  238. }
  239. if (m_CurWS == WS_TABLE)
  240. {
  241. exposureangle = ConfigurerWS::GetDefaultAngleTable();
  242. }
  243. if (gmotionLog) gmotionLog->Info("[RADMotionModel][GetExposureAngle]->[{$}][Tube des angle:{$:f3}]", m_CurWS.c_str(), exposureangle);
  244. return exposureangle;
  245. }
  246. int RADMotionModel::JudgeDirectionInTubeHeightAxis(float current, float target)
  247. {
  248. //auto positive = (int)atoi((const char *)m_MachineryParams["TubeHeightAxisPositiveDirection"]);
  249. auto positive = ConfigurerMotion::GetTubeHeightAxisPositiveDirection();
  250. auto direction = positive > 0 ? 1 : -1;
  251. if (current > target)
  252. {
  253. return 1 * direction;
  254. }
  255. if (current < target)
  256. {
  257. return -1 * direction;
  258. }
  259. return 0;
  260. }
  261. int RADMotionModel::JudgeDirectionInTubeAngleAxis(float current, float target)
  262. {
  263. //auto positive = (int)atoi((const char *)m_MachineryParams["TubeRotateAxisPositiveDirection"]);
  264. auto positive = ConfigurerMotion::GetTubeRotateAxisPositiveDirection();
  265. auto direction = positive > 0 ? 1 : -1;
  266. if (current > target)
  267. {
  268. return -1 * direction;
  269. }
  270. if (current < target)
  271. {
  272. return 1 * direction;
  273. }
  274. return 0;
  275. }
  276. void RADMotionModel::SwitchScanningComponents(int nSwitch)
  277. {
  278. }
  279. void RADMotionModel::OnMotionStageMoveTubeHeight()
  280. {
  281. if (m_tubeLineMotionSwitch)
  282. {
  283. m_tubeLineMotionSwitch->OutputSignal(false);
  284. }
  285. if (m_motorTubeHeight)
  286. {
  287. //auto period = (int)atoi((const char*)m_MachineryParams["ResetHeightPeriod"]);
  288. auto period = ConfigurerMotion::GetTubeHeightResetPeriod();
  289. if (gmotionLog) gmotionLog->Info("[RADMotionModel][OnMotionStageMoveTubeHeight]->[ Org:{$:d}, Period:{$:d}]", m_stageArgs->TubeHeightDirection, period);
  290. m_motorTubeHeight->Move(m_stageArgs->TubeHeightDirection, m_stageArgs->TubeHeightStep, period);
  291. }
  292. }
  293. void RADMotionModel::OnMotionStageMoveTubeHorizontal()
  294. {
  295. if (m_tubeLineMotionSwitch)
  296. {
  297. m_tubeLineMotionSwitch->OutputSignal(true);
  298. }
  299. if (m_motorTubeHorizontal)
  300. {
  301. //auto period = (int)atoi((const char*)m_MachineryParams["ResetHorizontalPeriod"]);
  302. auto period = ConfigurerMotion::GetTubeHorizontalResetPeriod();
  303. if (gmotionLog) gmotionLog->Info("[RADMotionModel][OnMotionStageMoveTubeHorizontal]->[ Org:{$:d}, Period:{$:d}]", m_stageArgs->TubeHorizontalMoveDirection, period);
  304. m_motorTubeHorizontal->Move(m_stageArgs->TubeHorizontalMoveDirection, m_stageArgs->TubeHorizontalMoveStep, period);
  305. }
  306. }
  307. void RADMotionModel::OnMotionStageMoveTubeRotation()
  308. {
  309. if (m_motorTubeAngle)
  310. {
  311. //auto period = (int)atoi((const char*)m_MachineryParams["ResetRotatePeriod"]);
  312. auto period = ConfigurerMotion::GetTubeRotateResetPeriod();
  313. m_motorTubeAngle->Rotate(m_stageArgs->TubeAngleDirection, m_stageArgs->TubeAngleStep, period);
  314. }
  315. }
  316. void RADMotionModel::SwitchWorkstation(string ws)
  317. {
  318. m_CurWS = ws;
  319. }
  320. int RADMotionModel::JudgeDirectionInTubeHorizontalAxis(float current, float target)
  321. {
  322. //auto positive = (int)atoi((const char*)m_MachineryParams["TubeHorizontalAxisPositiveDirection"]);
  323. auto positive = ConfigurerMotion::GetTubeHorizontalAxisPositiveDirection();
  324. auto direction = positive > 0 ? 1 : -1;
  325. if (current > target)
  326. {
  327. return 1 * direction;
  328. }
  329. if (current < target)
  330. {
  331. return -1 * direction;
  332. }
  333. return 0;
  334. }
  335. void RADMotionModel::MoveTubeHorizontal(int direction, float offset)
  336. {
  337. auto tolerance = ConfigurerMotion::GetMotionToleranceLine();
  338. if (offset < tolerance)
  339. {
  340. if (gmotionLog) gmotionLog->Info("[RADMotionModel][MoveTubeHorizontal]->[ Offset is {$:f6}, less than {$:f6}m, no motion]", offset, tolerance);
  341. return;
  342. }
  343. if (m_tubeLineMotionSwitch)
  344. {
  345. m_tubeLineMotionSwitch->OutputSignal(true);
  346. }
  347. auto step = m_coordinates->ConvertMotorStepValue(CONTROLLER_TUBE_HORIZONTAL, offset);
  348. auto period = ConfigurerMotion::GetTubeHorizontalNormalPeriod();
  349. if (period < 50 || (period - 50) < 30)
  350. {
  351. period = 50;
  352. }
  353. m_motorTubeHorizontal->Move(direction, step, period);
  354. }
  355. void RADMotionModel::OnMotionStageMove2SID(string ws)
  356. {
  357. if (gmotionLog) gmotionLog->Info("[RADMotionModel][OnMotionStageMove2SID]->[{$}]", ws.c_str());
  358. if (ws == WS_WALL)
  359. {
  360. //float desPos = atof((const char*)m_MachineryParams["SID_Wall"]);
  361. float desPos = ConfigurerWS::GetDefaultSIDWall();
  362. float tubeHorizontalPos = m_coordinates->GetCurrentAbsolutePhysical(TOMO_TUBE_HORIZONTAL);
  363. int direction = JudgeDirectionInTubeHorizontalAxis(tubeHorizontalPos, desPos);
  364. MoveTubeHorizontal(direction, abs(tubeHorizontalPos - desPos));
  365. }
  366. else if (ws == WS_TABLE)
  367. {
  368. //float desPos = atof((const char*)m_MachineryParams["SID_Table"]);
  369. float desPos = ConfigurerWS::GetDefaultSIDTable();
  370. float tubeHeightPos = m_coordinates->GetCurrentPhysical(TOMO_TUBE_HEIGHT);
  371. int direction = JudgeDirectionInTubeHeightAxis(tubeHeightPos, desPos);
  372. MoveTubeHeight(direction, abs(tubeHeightPos - desPos));
  373. }
  374. }