axis_app.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "global.h"
  2. //轴实体
  3. axis_object_t axis_objects[AXIS_NUMBER];
  4. /**
  5. * 通过编号获取对象
  6. *
  7. * @author lxz
  8. *
  9. * @param index
  10. *
  11. * @return axis_t*
  12. */
  13. axis_object_t* axis_get_by_index(unsigned char index) {
  14. if (index < AXIS_NUMBER) {
  15. return &axis_objects[index];
  16. }
  17. return (void *)0;
  18. }
  19. /**
  20. * 通过轴号获取对象,必须在初始化后才能使用这个API
  21. *
  22. * @author lxz
  23. *
  24. * @param index
  25. *
  26. * @return axis_t*
  27. */
  28. axis_object_t* axis_get_by_axisno(unsigned char axisno) {
  29. unsigned char index = 0;
  30. while (index < AXIS_NUMBER) {
  31. if (axis_objects[index].axis_no == axisno) {
  32. return &axis_objects[index];
  33. }
  34. }
  35. return (void *)0;
  36. }
  37. typedef struct
  38. {
  39. unsigned short no;
  40. unsigned short speed;
  41. int position;
  42. int dst_position;
  43. unsigned short aac;
  44. unsigned short ddc;
  45. unsigned short max_acc;
  46. unsigned short max_dec;
  47. unsigned short min_speed;
  48. unsigned short max_speed;
  49. unsigned short time_uint;
  50. unsigned short speed_unit;
  51. } motion_parameter_t;
  52. /**
  53. * 轴初始化,包括参数初始化,回掉函数设置
  54. *
  55. * @author lxz
  56. *
  57. * @param void
  58. */
  59. void axis_app_init(void) {
  60. int index = 0;
  61. memset(&axis_objects[0], 0, sizeof(axis_objects));
  62. while (index < AXIS_NUMBER)
  63. {
  64. //注册中断与调用对象
  65. axis_object_t *axis = axis_get_by_index(index);
  66. axis->axis_no = index;
  67. hw_pwm_it_register(index, axis_it_handle, axis);
  68. axis->clock = hw_pwm_get_clk(index);
  69. axis->period = 1; //1个ms为一个时间单位
  70. axis->speed_unit = 1; //速度单位为1
  71. axis->min_steps = 1;
  72. axis->max_aac = 5000;
  73. axis->max_acc = 4000; //最大加速
  74. axis->max_dec = 4000; //最大减度
  75. axis->start_speed = 1000;
  76. axis->acc_time = 20;
  77. axis->dec_time = 20;
  78. axis->accdec_mode = 4; //加减模式清0
  79. axis->home_find_speed = 10;
  80. axis->home_high_speed = 100;
  81. axis->home_low_speed = 50;
  82. axis->inputs.home_high_speed_enable = 0;
  83. axis->home_offset = 0;
  84. axis->home_space = 200;
  85. axis->sw_positive_limit = 100000; //正软件限位
  86. axis->sw_negative_limit = -100; //负软件限位
  87. axis->position = 0; //当前位置清0
  88. // axis->accdec_mode = 0; //加减模式清0
  89. //默认正转
  90. axis_cw(axis);
  91. //axis_enable(axis);
  92. //(axis);
  93. index++;
  94. }
  95. axis_x = axis_get_by_index(0);
  96. axis_y = axis_get_by_index(1);
  97. axis_z = axis_get_by_index(2);
  98. //设置方向输出电平
  99. axis_x->inputs.dir_reverse = 1;
  100. axis_y->inputs.dir_reverse = 1;
  101. axis_z->inputs.dir_reverse = 1;
  102. //设置使能输出电平
  103. axis_x->inputs.en_reverse = 1;
  104. axis_y->inputs.en_reverse = 1;
  105. axis_z->inputs.en_reverse = 1;
  106. // if(X_USE_OLD_DRIVE) ResetServoDrv_CPU(X_AXIS);
  107. // if(Y_USE_OLD_DRIVE) ResetServoDrv_CPU(Y_AXIS);
  108. }
  109. /**
  110. * 轴应用函数
  111. *
  112. * @author lxz
  113. *
  114. * @param void
  115. */
  116. void axis_app_run(void) {
  117. int index = 0;
  118. while (index < AXIS_NUMBER) {
  119. axis_run(&axis_objects[index]);
  120. index++;
  121. }
  122. }