ServoDriveJRui.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "stdafx.h"
  2. #include "ServoDriveJRui.h"
  3. using namespace DIOS::Dev::Detail::MachineryECOM;
  4. const unsigned char SERVO_CMD_WRITE_SINGLE = 0x6;
  5. static unsigned int crc_check(const unsigned char* data, size_t length)
  6. {
  7. int j;
  8. unsigned int reg_crc = 0xFFFF;
  9. while (length--)
  10. {
  11. reg_crc ^= *data++;
  12. for (j = 0; j<8; j++)
  13. {
  14. if (reg_crc & 0x01) /*LSB(bit 0 ) = 1 */
  15. {
  16. reg_crc = (reg_crc >> 1) ^ 0xA001;
  17. }
  18. else
  19. {
  20. reg_crc = (reg_crc >> 1);
  21. }
  22. }
  23. }
  24. return reg_crc;
  25. }
  26. ServoDriveJRui::ServoDriveJRui() :m_driveNumber(-1)
  27. {
  28. }
  29. ServoDriveJRui::~ServoDriveJRui()
  30. {
  31. }
  32. void ServoDriveJRui::Initialize(int driveNumber)
  33. {
  34. m_driveNumber = driveNumber;
  35. }
  36. bool ServoDriveJRui::MakeServoOnEnablePacket(std::basic_string<unsigned char> &outbuffer)
  37. {
  38. return false;
  39. }
  40. bool ServoDriveJRui::MakeServoOnPacket(BOOL servoon, std::basic_string<unsigned char> &outbuffer)
  41. {
  42. //unsigned short context = servoon ? 0x1 : 0x0;
  43. //return MakeRequest(SERVO_CMD_WRITE_SINGLE, 0x023C, context, outbuffer);
  44. return false;
  45. }
  46. bool ServoDriveJRui::MakePulseOnCirclePacket(unsigned short pulsenumber, std::basic_string<unsigned char> &outbuffer)
  47. {
  48. //return MakeRequest(SERVO_CMD_WRITE_SINGLE, 0x015C, pulsenumber, outbuffer);
  49. return false;
  50. }
  51. bool ServoDriveJRui::MakeClearWarningPacket(std::basic_string<unsigned char> &outbuffer)
  52. {
  53. return MakeRequest(SERVO_CMD_WRITE_SINGLE, 0x03, 0, outbuffer);
  54. }
  55. bool ServoDriveJRui::MakeRequest(UCHAR Cmd, USHORT Addr, USHORT Context, std::basic_string<unsigned char> &outbuffer)
  56. {
  57. if (m_driveNumber < 0)
  58. {
  59. return false;
  60. }
  61. outbuffer.clear();
  62. unsigned char target = (unsigned char)m_driveNumber;
  63. outbuffer.push_back(target);
  64. outbuffer.push_back(Cmd);
  65. SHORTDATA data;
  66. data.ShortPart = Addr;
  67. outbuffer.push_back(data.High);
  68. outbuffer.push_back(data.Low);
  69. data.ShortPart = Context;
  70. outbuffer.push_back(data.High);
  71. outbuffer.push_back(data.Low);
  72. USHORT Crc16 = crc_check(outbuffer.data(), outbuffer.length());
  73. unsigned char tmp[2] = { 0 };
  74. memcpy(&tmp[0], &Crc16, sizeof(USHORT));
  75. outbuffer.push_back(tmp[0]);
  76. outbuffer.push_back(tmp[1]);
  77. return true;
  78. }