servocom_app.h 7.8 KB

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