TubeAngleController.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "stdafx.h"
  2. #include "TubeAngleController.h"
  3. #include "ICommunicateEntity.h"
  4. #include "IOInterfaceMapper.h"
  5. #include "IServoDrive.h"
  6. using namespace DIOS::Dev::Detail::MachineryECOM;
  7. const float PULSE_DUTYCYCLE_MIN = 0.2f;
  8. const float PULSE_DUTYCYCLE_MAX = 0.95f;
  9. const float PULSE_DUTYCYCLE_DEF = 0.7f;
  10. TubeAngleController::TubeAngleController()
  11. :m_communicate(nullptr),
  12. m_driveNumber(-1),
  13. m_pulseDutyCycle(PULSE_DUTYCYCLE_DEF),
  14. m_servo(nullptr)
  15. {
  16. }
  17. TubeAngleController::~TubeAngleController()
  18. {
  19. }
  20. std::string TubeAngleController::CLASSID()
  21. {
  22. return "E040F04B-538E-48D0-95CD-9CA7367DD24A";
  23. }
  24. void TubeAngleController::Initialize(const std::string &name)
  25. {
  26. SetName(name);
  27. }
  28. void TubeAngleController::AttachServoDrive(IServoDrive *servodrive)
  29. {
  30. m_servo = servodrive;
  31. }
  32. void TubeAngleController::SetPulseOneCircle(unsigned short pulseonecircle)
  33. {
  34. if (m_servo)
  35. {
  36. RS232_PARAM param;
  37. if (m_servo->MakePulseOnCirclePacket(pulseonecircle, param.rs485))
  38. {
  39. DoWrite485(param);
  40. }
  41. }
  42. }
  43. void TubeAngleController::SetPulseDutyCycle(float dutyCycle)
  44. {
  45. m_pulseDutyCycle = dutyCycle;
  46. if (dutyCycle < PULSE_DUTYCYCLE_MIN || dutyCycle > PULSE_DUTYCYCLE_MAX)
  47. {
  48. m_pulseDutyCycle = PULSE_DUTYCYCLE_DEF;
  49. }
  50. }
  51. void TubeAngleController::OnCommunicationEstablished(ICommunicateEntity *communicate)
  52. {
  53. m_communicate = communicate;
  54. if (!m_communicate)
  55. {
  56. return;
  57. }
  58. ActivePWMMode();
  59. InitializeServo();
  60. }
  61. void TubeAngleController::StopRotation()
  62. {
  63. GPIO_DO_PARAM pwmparam;
  64. pwmparam.active_level = 0x00;
  65. m_communicate->GPIO_DO_Ctrl(OP_WRITE, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_NONE, pwmparam);
  66. //ServoOn(FALSE);
  67. }
  68. void TubeAngleController::Rotate(int direction, int steps, DWORD pwmperiod, float pulseDutyCycle)
  69. {
  70. bool clockWise = direction > 0 ? false : true;
  71. SetRotateOrientation(clockWise);
  72. if (pulseDutyCycle < PULSE_DUTYCYCLE_MIN || pulseDutyCycle > PULSE_DUTYCYCLE_MAX)
  73. {
  74. pulseDutyCycle = m_pulseDutyCycle;
  75. }
  76. GPIO_DO_PARAM pwmparam;
  77. pwmparam.pwm_step.pwmstep_int = (int)steps;
  78. pwmparam.peroid.pwmperiod_short = (unsigned short)pwmperiod;
  79. pwmparam.effective.pwmperiod_short = (unsigned short)(pwmperiod * pulseDutyCycle);
  80. pwmparam.output_mode = (unsigned char)GOM_PWM;
  81. pwmparam.active_level = 0x01;
  82. m_communicate->GPIO_DO_Ctrl(OP_SET, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_SET_PWM_DELETE, pwmparam);
  83. m_communicate->GPIO_DO_Ctrl(OP_SET, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_SET_MODE, pwmparam);
  84. ServoOn(TRUE);
  85. if (!IsBrakeOpening())
  86. {
  87. if (gmotionLog) gmotionLog->Error("[TubeAngleController::Rotate][Brak is not opening, no pwm will be sent]");
  88. return;
  89. }
  90. m_communicate->GPIO_DO_Ctrl(OP_SET, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_SET_PWM_PARAM_TABLE, pwmparam);
  91. m_communicate->GPIO_DO_Ctrl(OP_WRITE, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_NONE, pwmparam);
  92. }
  93. void TubeAngleController::SetRotateOrientation(bool clockWise)
  94. {
  95. GPIO_DO_PARAM doparam;
  96. doparam.active_level = clockWise ? 0x0 : 0x1;
  97. m_communicate->GPIO_DO_Ctrl(OP_WRITE, (GPIO_DO_ID)m_functionIds[ID_TUBE_ANGLE_DIRECTION], DO_ATTR_NONE, doparam);
  98. }
  99. void TubeAngleController::ClearSignal()
  100. {
  101. GPIO_DO_PARAM doparam;
  102. doparam.autoupload_switch = GAUTO_DO_UPLOAD_OFF;
  103. m_communicate->GPIO_DO_Ctrl(OP_SET, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_SET_PWM_DELETE, doparam);
  104. m_communicate->GPIO_DO_Ctrl(OP_SET, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_SET_AUTO_UPLOAD, doparam);
  105. }
  106. void TubeAngleController::AppendPWM(int steps, int period, float pulseDutyCycle)
  107. {
  108. if (pulseDutyCycle < PULSE_DUTYCYCLE_MIN || pulseDutyCycle > PULSE_DUTYCYCLE_MAX)
  109. {
  110. pulseDutyCycle = m_pulseDutyCycle;
  111. }
  112. GPIO_DO_PARAM pwmparam;
  113. pwmparam.pwm_step.pwmstep_int = (int)steps;
  114. pwmparam.peroid.pwmperiod_short = (unsigned short)period;
  115. pwmparam.effective.pwmperiod_short = (unsigned short)(period * pulseDutyCycle);
  116. pwmparam.output_mode = (unsigned char)GOM_PWM;
  117. m_communicate->GPIO_DO_Ctrl(OP_SET, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_SET_PWM_PARAM_TABLE, pwmparam);
  118. }
  119. void TubeAngleController::ClearServoDriveWarning()
  120. {
  121. RS232_PARAM param;
  122. if (m_servo && m_servo->MakeClearWarningPacket(param.rs485))
  123. {
  124. DoWrite485(param);
  125. }
  126. }
  127. void TubeAngleController::SetServoStatus(int svostatus)
  128. {
  129. ServoOn(svostatus != 0);
  130. }
  131. void TubeAngleController::InitializeServo()
  132. {
  133. if (m_servo)
  134. {
  135. RS232_PARAM param;
  136. if (m_servo->MakeServoOnEnablePacket(param.rs485))
  137. {
  138. DoWrite485(param);
  139. }
  140. ServoOn(FALSE);
  141. }
  142. }
  143. void TubeAngleController::ActivePWMMode()
  144. {
  145. GPIO_DO_PARAM doparam;
  146. doparam.output_mode = (unsigned char)GOM_PWM;
  147. m_communicate->GPIO_DO_Ctrl(OP_SET, (GPIO_DO_ID)m_communicateInterfaceID, DO_ATTR_SET_MODE, doparam);
  148. }
  149. void TubeAngleController::ServoOn(BOOL servoon)
  150. {
  151. RS232_PARAM param;
  152. if (m_servo->MakeServoOnPacket(servoon,param.rs485))
  153. {
  154. DoWrite485(param);
  155. }
  156. else
  157. {
  158. if (m_functionIds.find(ID_TUBE_ANGLE_SERVO_ON) != m_functionIds.end()
  159. && m_functionIds[ID_TUBE_ANGLE_SERVO_ON] > 0)
  160. {
  161. GPIO_DO_PARAM doparam;
  162. doparam.active_level = servoon ? 0x1 : 0x0;
  163. m_communicate->GPIO_DO_Ctrl(OP_WRITE, (GPIO_DO_ID)m_functionIds[ID_TUBE_ANGLE_SERVO_ON], DO_ATTR_NONE, doparam);
  164. }
  165. }
  166. }
  167. void TubeAngleController::DoWrite485(RS232_PARAM &param)
  168. {
  169. m_communicate->RS232_Ctrl(OP_WRITE, (RS232_ID)m_functionIds[ID_RS232], RS232_ATTR_NONE, param);
  170. Sleep(300);
  171. }
  172. bool TubeAngleController::IsBrakeOpening()
  173. {
  174. bool bRes = false;
  175. GPIO_DI_PARAM params;
  176. params.input_mode = GIM_DIGITAL;
  177. params.active_level = 0x0;
  178. if (IOInterfaceMapper::ID_TUBE_ANGLE_BRAKE > 0)
  179. {
  180. for (int i = 0; i < 10; i++)
  181. {
  182. m_communicate->GPIO_DI_Ctrl(OP_READ, (GPIO_DI_ID)IOInterfaceMapper::ID_TUBE_ANGLE_BRAKE, DI_ATTR_NONE, params);
  183. if (params.active_level == 1) //1:ɲ³µËÉ¿ª 0:ɲ³µ±ÕºÏ
  184. {
  185. bRes = true;
  186. break;
  187. }
  188. Sleep(50);
  189. }
  190. }
  191. else
  192. {
  193. bRes = true;
  194. }
  195. return bRes;
  196. }