servocom_app.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef __SERVOCOM__APP_H__
  2. #define __SERVOCOM__APP_H__
  3. #define SERVO_AXISNUM 3
  4. #define X_USERING_TARR 1
  5. #define Y_USERING_TARR 1
  6. #define Z_USERING_TARR 1
  7. #define SERVO_COM_CMD_NUMBER 15
  8. #define SERVO_X_COM_ID 1 //x轴,Y轴同时用时ID,不能一样。
  9. #define SERVO_Y_COM_ID 2
  10. #define SERVO_Z_COM_ID 3
  11. #define SERVO_ADDR_HAND 0x0200+0 //伺服握手地址
  12. #define SERVO_ADDR_DI_MODE_SW 0x0300+4 //模式切换功能配置,原故障清除的功能更过来的,极性修改0x0309,一起修改
  13. #define SERVO_ADDR_DO_TARR_OV 0x0300+24 //转矩到达功能设置,原故障告警的功能更改过来,极性修改0x0325,一起修改
  14. #define SERVO_ADDR_MODE_SET 0x0400+0 //模式设置,要用扭矩模式+位置模式4,正常时位置模式1
  15. #define SERVO_ADDR_MOTOR_DIR 0x0400+1 //电机旋转方向
  16. #define SERVO_ADDR_ENCODE_FB 0x0900+36 //位置反馈计数(编码器),32位。
  17. #define SERVO_ADDR_TARR_SEL 0x1500+0 //转矩指令来源选择,设置为0;0x1501,设置为,0,内部限制;
  18. #define SERVO_ADDR_LIMIT_SET 0x1500+5 //转矩下的转矩限制1和2
  19. #define SERVO_ADDR_SPEED_SET 0x1500+11 //转矩模式下正向速度限制。0x1512,负向速度选择。
  20. #define SERVO_ADDR_TARBASE_SET 0x1500+16 //转矩模式下的转矩基准值设置
  21. #define SERVO_ADDR_ALARM_REST 0x2000+3 //伺服故障复位,(有些故障无法复位)
  22. #define SERVO_ADDR_SERVO_RESTART 0x2000+4 //伺服软件重启
  23. #define SERVO_ADDR_ALARM_CODE 0x2100+8 //当前故障码
  24. #define SERVO_ADDR_VDI_VALUE 0x2000+50 //VDI的设置数值 2050
  25. #define SERVO_ADDR_VDI_USE 0x2300+0 //VDI使能,2300~23013
  26. #define DI__FUNCTION_POSTARRSEL 24 //位置+转矩切换
  27. #define DI__FUNCTION_TARRSEL 14 //转矩选择
  28. #define DI_NCLOSE 1 //常闭
  29. #define DI_NOPEN 0 //常开
  30. #define DO_FUNCTION_VALUE 9 //扭矩达到输出
  31. #define VDI_SON_FUNCTION 1 //VDI伺服使能
  32. #define VDI_CLRENCODE_FUNCTION 4 //VDI清除位置超差
  33. #define RUN_MODE 0 //1 //0为扭矩+位置模式、1为扭矩模式
  34. #define CTRL_TARR_MODE 2 //模式设置为2,转矩模式
  35. #define CTRL_POSTOTARR_MODE 5 //模式设置为5,扭矩+位置模式
  36. #define HANDSHAKE 1 //伺服握手
  37. #define DI_MODE_SW 2 //配置DI5为转矩模式+位置模式的切换输入。
  38. #define DO_TARR_SW 3 //配置DO3为转矩到达输出
  39. #define CTRL_MODE_SET 4 //伺服控制模式设置
  40. #define MOTOR_DIR_SET 5 //电机旋转方向设置。
  41. #define TARR_SOURCE_SEL 6 //转矩指令来源
  42. #define TARR_LIMIT12_SET 7 //转矩下速度选择
  43. #define SPEED_LIMIT_SET 8 //转矩下速度限制
  44. #define TARR_BASE_SET 9 //转矩基准值
  45. #define ALARM_REST 10 //伺服故障复位
  46. #define SERVO_RESTART 11 //伺服软件重启
  47. #define ALARM_CODE 12 //伺服故障码
  48. #define ENCODE_FB 13 //编码器反馈
  49. #define VDI_VALUE_SET 14 //VDI数值设置
  50. #define VDI_CONFIG_SET 15 //VDI配置
  51. #define START_SPEED user_datas[600]
  52. #define STOP_SPEED user_datas[601]
  53. #define LOW_SPEED user_datas[602]
  54. #define HIGH_SPEED user_datas[603]
  55. #define CHANGE_SPEED user_datas[604]
  56. #define SERVO_STEP user_datas[605]
  57. #define TARR_VALUE user_datas[606]
  58. #define LIMIT_SPEED user_datas[607]
  59. #define CUR_SPEED user_datas[608]
  60. #define ACCDECPULSE user_datas[609]
  61. #define SERVO_X_ALARMCODE user_datas[610]
  62. #define SERVO_POSTOTARR_MODE 0//伺服位置模式+转矩模式,转矩切换用
  63. #define SERVO_TARR_MODE 1//伺服转矩模式,用IO切换,转矩限制
  64. #define POS_MODE 0 //位置模式
  65. #define TARR_MODE 1 //转矩模式
  66. #define TARR_MODE_MAXLIMIT 1
  67. #define TARR_MODE_MINLIMIT 0
  68. #define ENCODE_17BIT 0x20000 //17位编码值
  69. #define SERVO_TARR_CW 0 //扭矩方式正转
  70. #define SERVO_TARR_CCW 1 //扭矩方式正转
  71. typedef struct servo_com_cmd_t
  72. {
  73. char cmdcode; //命令编号
  74. unsigned short address; //读、写的目标地址
  75. unsigned short number; //读、写长度
  76. unsigned char * value; //对于读操作,是数值,对于写操作是要发送的值
  77. char id; //设备ID
  78. char cmd; //发送的命令
  79. char dir; //0单次,1循环
  80. int respone_timeout; //响应超时时间
  81. void (* servo_on_respone)(struct modbus_master_cmd * cmd, unsigned char * respone, int length); //回调函数
  82. } servo_com_cmd_t;
  83. typedef struct servo_param_t
  84. {
  85. unsigned short handshake; //握手数值,读取伺服版本-1
  86. unsigned short di_function_set; //设置DI功能码,-2
  87. unsigned short di_polarity; //设置DI极性,0为常开、1为常闭。-3
  88. unsigned short do_function_set; //设置DO功能码,
  89. unsigned short do_polarity; //设置DO极性,0为常开、1为常闭。
  90. unsigned short ctrl_mode_set; //模式设置,扭矩模式+位置模式4,正常时位置模式1。
  91. unsigned short motor_dir_set; //电机方向设置
  92. unsigned short tarr_source_sel; //转矩指令来源选择
  93. unsigned short tarr_modea_sel; //转矩指令A来源选择
  94. unsigned short tarr_modeb_sel; //转矩指令B来源选择
  95. unsigned short tarr_limit_sel; //转矩限制源的选择
  96. unsigned short tarr_ai_sel; //转矩模拟量的选择
  97. unsigned short tarr_Plimit1_sel; //正转转矩限制1;
  98. unsigned short tarr_Nlimit1_sel; //反转转矩限制1;
  99. unsigned short tarr_Plimit2_sel; //正转转矩限制2;
  100. unsigned short tarr_Nlimit2_sel; //反转转矩限制2;
  101. unsigned short speed_limit_sel; //转速模式下速度限制来源选择
  102. unsigned short speed_ai_sel;
  103. unsigned short speed_plimit_set; //转矩模式下正向速度限制。
  104. unsigned short speed_nlimit_set; //转矩模式下正向速度限制。
  105. int tarr_limit_set; //转矩模式下的转矩限制设置。
  106. unsigned short tarr_basis; //转矩基准值;
  107. unsigned short tarr_basis_up; //tarr_basis+tarr_basis_up为有效值
  108. unsigned short tarr_basis_dw; //tarr_basi+starr_basis_dw为无效
  109. unsigned short alarmrest; //伺服故障复位,(有些故障无法复位)
  110. unsigned short restart; //伺服重新启动;
  111. unsigned short alarmcode; //告警编码
  112. unsigned short vdi_value; //VDI的输入数值
  113. unsigned short vdi_use; //vdi使用
  114. unsigned short vdi_init; //vdi上电初始值
  115. unsigned short vdo_use; //vdo使用
  116. unsigned short vdo_init; //vdo默认值
  117. unsigned short vdi1_function_set; //vdi1-功能
  118. unsigned short vdi1_polarity; //vdi1-极性
  119. unsigned short vdi2_function_set; //vdi2-功能
  120. unsigned short vdi2_polarity; //vdi2-极性
  121. unsigned short vdi3_function_set; //vdi3-功能
  122. unsigned short vdi3_polarity; //vdi3-极性
  123. unsigned short vdi4_function_set; //vdi4-功能
  124. unsigned short vdi4_polarity; //vdi4-极性
  125. long encode; //编码器反馈;
  126. long precode; //编码器反馈上一次的值
  127. unsigned short init_step; //初始话步骤
  128. unsigned char cmd_fb[SERVO_COM_CMD_NUMBER+1];//命令通讯反馈
  129. unsigned char IO_TO_COM; //IO与伺服通讯标志
  130. sw_timer_t timer; //定时器
  131. unsigned char com_number; //通讯次数
  132. unsigned char com_ok_number; //通讯连接次数
  133. unsigned char alarm_flag; //伺服故障标志
  134. unsigned char set_tarr_OKflag; ////0失败,为1为设置1个,2为成功
  135. unsigned short cycle_pulse; //细分
  136. unsigned short cycle_length; //周长
  137. unsigned char alarm_Reverse; //伺服告警电平,0,为常开,1为常闭。
  138. unsigned char run_mode; //0为扭矩+位置模式、1为扭矩模式
  139. unsigned char read_alarm_en; //告警读取使能;
  140. unsigned char read_encode_en; //告警编码使能;
  141. } servo_param_t;
  142. void set_com_servo_param(unsigned short axis,unsigned short cycle_pluse,unsigned short cycle_length);
  143. void servo_com_respone(modbus_master_cmd_t *cmd, unsigned char *respone, int length);
  144. void read_encode_value_cmd(unsigned short axis,unsigned short times);
  145. int axis_set_com_pos(unsigned short axis,long pos );
  146. int set_servo_postotarr_limit(unsigned short axis,int tarr,
  147. unsigned short P_freq,unsigned short N_freq,
  148. unsigned short dir);
  149. int set_servo_tarr_limit(unsigned short axis,int maxtarr,int mintarr,
  150. unsigned short P_freq,unsigned short N_freq,
  151. unsigned short dir);
  152. int servo_com_run(void);
  153. int get_alarm_code(unsigned short axis);
  154. int servo_com_get_alarm(unsigned short axis);
  155. int clr_com_servo_alarm(unsigned short axis);
  156. long get_encode_value(unsigned short axis);
  157. int get_tarr_set(unsigned short axis);
  158. void set_com_alarm_reverse(unsigned short axis,int value);
  159. int GetServoComState(unsigned short axis);
  160. void SetServoComUse(unsigned short axis,unsigned short enable);
  161. void ReadServoAlarm_Enable(unsigned short axis,unsigned short en);
  162. void ReadServoEncode_Enable(unsigned short axis,unsigned short en);
  163. void Set_VDI_Value(unsigned short axis,unsigned short vdi_value);
  164. extern servo_param_t servo_x,servo_y,servo_z;
  165. #endif