選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

208 行
5.3KB

  1. #ifndef STEPPER_H
  2. #define STEPPER_H
  3. #include <stdint.h>
  4. #include "periph/gpio.h"
  5. #include "periph/uart.h"
  6. #include "sensor.h"
  7. #include "byteorder.h"
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "event.h"
  12. #include "event/timeout.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define STEPPER_PARAM_VEL_MAX (25)
  17. #define STEPPER_PARAM_ACC_MAX (200)
  18. #define STEPPER_PARAM_VELOCITY_DEFAULT (0)
  19. #define STEPPER_PARAM_MICROSTEP_DEFAULT (16)
  20. #define STEPPER_PARAM_DIVISION_DEFAULT (200)
  21. #define STEPPER_PARAM_ANGLE_DEFAULT (0)
  22. #define STEPPER_PARAM_HOMING_ANGLE_DEFAULT (20)
  23. #define STEPPER_UART_SLAVE_ADDR (0x0)
  24. #define STEPPER_UART_SYNC (0x05)
  25. #define STEPPER_UART_REG_GCONF (0x00)
  26. #define STEPPER_UART_REG_GSTAT (0x01)
  27. #define STEPPER_UART_REG_IFCNT (0x02)
  28. #define STEPPER_UART_REG_SLAVECONF (0x03)
  29. #define STEPPER_GCONF_BIT_I_SCALE_ANALOG (0x0 << 0)
  30. #define STEPPER_GCONF_BIT_INTERNAL_RSENSE (0x1 << 1) //
  31. #define STEPPER_GCONF_BIT_SPREADCYCLE (0x1 << 2) // enable spreadcycle
  32. #define STEPPER_GCONF_BIT_SHAFT (0x0 << 3) // Non inverse
  33. #define STEPPER_GCONF_BIT_PDN_DISABLE (0x1 << 6) // Set to 1 when using UART
  34. #define STEPPER_GCONF_BIT_MSTEP_SELECT (0x1 << 7)
  35. // default datagram => 05 ff 00 00 00 01 01 bb
  36. #define STEPPER_GCONF_ADDR (0x00) // 0x00
  37. #define STEPPER_GCONF_VALUES (0xC1000000UL) //(0x83800000UL) // 000001C1 set bit 0,6,7,8. 6: PDN disable is require for UART operation.
  38. // default datagram => 05 ff 6c 17 01 00 53 3e
  39. #define STEPPER_CHOPCONF_ADDR (0x6c) // 6C
  40. #define STEPPER_CHOPCONF_VALUES (0xB5810134UL)//(0xCA00004CUL) // 32000053 set dedge=0x1, intpol=0x1, mres=0x2, hstrt=0x5, toff=0x3. others are 0x0.
  41. //#define STEPPER_CHOPCONF_VALUES [0x32,0x00,0x00,0x53] // set dedge=0x1, intpol=0x1, mres=0x2, hstrt=0x5, toff=0x3. others are 0x0.
  42. /**
  43. * @brief Status and error codes
  44. */
  45. enum {
  46. STEPPER_OK = 0,
  47. STEPPER_ERR = -1,
  48. STEPPER_DISABLED = -2,
  49. };
  50. /**
  51. * @brief Direction for stepper
  52. */
  53. typedef enum {
  54. STEPPER_FORWARD = 0x0,
  55. STEPPER_REVERSE = 0x1,
  56. } stepper_direction_t;
  57. /**
  58. * @brief Data structure holding the stepperice parameters needed for initialization
  59. */
  60. typedef struct {
  61. gpio_t step_pin;
  62. gpio_t dir_pin;
  63. gpio_t enable_pin;
  64. uint8_t uart;
  65. uint8_t vel_max;
  66. uint8_t acc_max;
  67. uint8_t microstep;
  68. uint16_t division;
  69. float angle;
  70. } stepper_params_t;
  71. /**
  72. * @brief Device Descriptor for Stepper controller
  73. */
  74. typedef struct {
  75. stepper_params_t p;
  76. float velocity_setpoint;
  77. stepper_direction_t direction;
  78. int16_t step_count;
  79. int16_t step_setpoint;
  80. uint32_t time_step;
  81. float step_angle;
  82. bool enable;
  83. event_t event;
  84. event_timeout_t event_timeout;
  85. event_queue_t *queue;
  86. thread_t *thread;
  87. thread_flags_t flags;
  88. char id;
  89. } stepper_t;
  90. typedef struct {
  91. event_queue_t* queue;
  92. sensor_t* sensor;
  93. } stepper_init_t;
  94. /**
  95. * @brief Initialize the stepper
  96. *
  97. * @param[out] stepper struct describing the stepper
  98. * @param[in] params static configuration parameters of stepper
  99. *
  100. * @return STEPPER_OK on success
  101. * @return STEPPER_ERR on error
  102. */
  103. int stepper_init(stepper_t *stepper, const stepper_params_t *params);
  104. /**
  105. * @brief Instruct the stepper to move to setpoint
  106. *
  107. * @param[in] stepper struct describing the stepper
  108. * @param[in] count stepcount setpoint
  109. * @param[in] time_step time span in which to complete the move
  110. */
  111. void stepper_set_setpoint(stepper_t *stepper, int16_t count, uint32_t time_step);
  112. //void stepper_set_velocity(stepper_t *stepper, float velocity);
  113. /**
  114. * @brief Change rotation direction of stepper
  115. *
  116. * @param[in] stepper struct describing the stepper
  117. * @param[in] direction move direction of stepper
  118. */
  119. void stepper_set_direction(stepper_t *stepper, stepper_direction_t direction);
  120. //int stepper_time_step(stepper_t *stepper);
  121. int32_t stepper_get_count(stepper_t *stepper, float angle);
  122. /**
  123. * @brief Update next action.
  124. * Schedules a new event or when the set point is reached, signal the controller.
  125. *
  126. * @param[in] stepper struct describing the stepper
  127. */
  128. void stepper_update(stepper_t *stepper);
  129. /**
  130. * @brief Return the current angle in radiansa
  131. *
  132. * @param[in] stepper struct describing the stepper
  133. * @param[out] angle angle in radians
  134. */
  135. void stepper_get_angle(stepper_t *stepper, float *angle);
  136. /**
  137. * @brief Move stepper by a single step.
  138. *
  139. * @param[in] stepper struct describing the stepper
  140. */
  141. int stepper_step(stepper_t *stepper);
  142. /**
  143. * @brief write to stepper register
  144. *
  145. * @param[in] stepper struct describing the stepper
  146. * @param[in] reg register adress
  147. * @param[in] data data to write in register
  148. */
  149. void stepper_write(stepper_t *stepper, uint8_t reg, le_uint32_t *data);
  150. /**
  151. * @brief trigger read from stepper register
  152. *
  153. * @param[in] stepper struct describing the stepper
  154. * @param[in] reg register adress
  155. *
  156. * @note This function does not parse the answer from the stepper. Use a logic analyser for that.
  157. */
  158. void stepper_read(stepper_t *stepper, uint8_t reg);
  159. /**
  160. * @brief enable stepper motor
  161. *
  162. * @param[in] stepper struct describing the stepper
  163. */
  164. void stepper_enable(stepper_t *stepper);
  165. /**
  166. * @brief disable stepper motor
  167. *
  168. * @param[in] stepper struct describing the stepper
  169. */
  170. void stepper_disable(stepper_t *stepper);
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif /* STEPPER_H */