Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

113 řádky
2.9KB

  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. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #define STEPPER_PARAM_VEL_MAX (25)
  15. #define STEPPER_PARAM_ACC_MAX (200)
  16. #define STEPPER_PARAM_VELOCITY_DEFAULT (0)
  17. #define STEPPER_PARAM_MICROSTEP_DEFAULT (16)
  18. #define STEPPER_PARAM_DIVISION_DEFAULT (200)
  19. #define STEPPER_PARAM_ANGLE_DEFAULT (0)
  20. #define STEPPER_PARAM_HOMING_ANGLE_DEFAULT (20)
  21. #define STEPPER_UART_SLAVE_ADDR (0x0)
  22. #define STEPPER_UART_SYNC (0x05)
  23. #define STEPPER_UART_REG_GCONF (0x00)
  24. #define STEPPER_UART_REG_GSTAT (0x01)
  25. #define STEPPER_UART_REG_IFCNT (0x02)
  26. #define STEPPER_UART_REG_SLAVECONF (0x03)
  27. #define STEPPER_GCONF_BIT_I_SCALE_ANALOG (0x0 << 0)
  28. #define STEPPER_GCONF_BIT_INTERNAL_RSENSE (0x1 << 1) //
  29. #define STEPPER_GCONF_BIT_SPREADCYCLE (0x1 << 2) // enable spreadcycle
  30. #define STEPPER_GCONF_BIT_SHAFT (0x0 << 3) // Non inverse
  31. #define STEPPER_GCONF_BIT_PDN_DISABLE (0x1 << 6) // Set to 1 when using UART
  32. #define STEPPER_GCONF_BIT_MSTEP_SELECT (0x1 << 7)
  33. // default datagram => 05 ff 00 00 00 01 01 bb
  34. #define STEPPER_GCONF_ADDR (0x00) // 0x00
  35. #define STEPPER_GCONF_VALUES (0xC1000000UL) //(0x83800000UL) // 000001C1 set bit 0,6,7,8. 6: PDN disable is require for UART operation.
  36. // default datagram => 05 ff 6c 17 01 00 53 3e
  37. #define STEPPER_CHOPCONF_ADDR (0x6c) // 6C
  38. #define STEPPER_CHOPCONF_VALUES (0xB5810132UL)//(0xCA00004CUL) // 32000053 set dedge=0x1, intpol=0x1, mres=0x2, hstrt=0x5, toff=0x3. others are 0x0.
  39. //#define STEPPER_CHOPCONF_VALUES [0x32,0x00,0x00,0x53] // set dedge=0x1, intpol=0x1, mres=0x2, hstrt=0x5, toff=0x3. others are 0x0.
  40. /**
  41. * @brief Status and error codes
  42. */
  43. enum {
  44. STEPPER_OK = 0,
  45. STEPPER_NO = -1,
  46. };
  47. /**
  48. * @brief Direction for stepper
  49. */
  50. typedef enum {
  51. STEPPER_FORWARD = 0x0,
  52. STEPPER_REVERSE = 0x1,
  53. } stepper_direction_t;
  54. /**
  55. * @brief Data structure holding the device parameters needed for initialization
  56. */
  57. typedef struct {
  58. gpio_t step_pin;
  59. gpio_t dir_pin;
  60. uint8_t uart;
  61. uint8_t vel_max;
  62. uint8_t acc_max;
  63. uint8_t microstep;
  64. uint16_t division;
  65. float angle;
  66. float velocity;
  67. sensor_t sensor;
  68. float homing_angle;
  69. } stepper_params_t;
  70. /**
  71. * @brief Device Descriptor for Stepper controller
  72. */
  73. typedef struct {
  74. stepper_params_t p;
  75. } stepper_t;
  76. int stepper_init(stepper_t *dev, const stepper_params_t *params);
  77. int stepper_set_velocity(stepper_t *dev, float *velocity);
  78. int stepper_homing(stepper_t *dev);
  79. int stepper_get_angle(stepper_t *dev, float *angle);
  80. int stepper_step(stepper_t *dev, stepper_direction_t *direction);
  81. int stepper_write(stepper_t *dev, uint8_t reg, le_uint32_t *data);
  82. int stepper_read(stepper_t *dev, uint8_t reg);
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. #endif /* STEPPER_H */