You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "encoder.h"
  2. #include "zboard.h"
  3. #include <stdio.h>
  4. #include "zport.h"
  5. #include "udpclient.h"
  6. static uint32_t camera_encoder_lastprocess = 0;
  7. static uint32_t driven_encoder_gear_lastprocess = 0;
  8. static encoder_t m_encoders[] = {
  9. {&camera_encoder, TIM_CHANNEL_1 | TIM_CHANNEL_2, 0, 0}, // 相机编码器
  10. {&driven_encoder_gear, TIM_CHANNEL_1 | TIM_CHANNEL_2, 0, 0} // 从动编码器
  11. };
  12. void encoder_all_start(void)
  13. {
  14. for (uint8_t i = 0; i < (sizeof(m_encoders) / sizeof(encoder_t)); i++)
  15. {
  16. HAL_TIM_Encoder_Start(m_encoders[i].tim_handler, m_encoders[i].tim_channel);
  17. }
  18. }
  19. void encoder_all_stop(void)
  20. {
  21. for (uint8_t i = 0; i < (sizeof(m_encoders) / sizeof(encoder_t)); i++)
  22. {
  23. HAL_TIM_Encoder_Stop(m_encoders[i].tim_handler, m_encoders[i].tim_channel);
  24. }
  25. }
  26. void encoder_all_clear_counter(void)
  27. {
  28. for (uint8_t i = 0; i < (sizeof(m_encoders) / sizeof(encoder_t)); i++)
  29. {
  30. m_encoders[i].count = 0;
  31. }
  32. }
  33. void encoder_switch_clear_counter(encoder_usage_t encoder)
  34. {
  35. m_encoders[encoder].count = 0;
  36. }
  37. void encoder_read_with_encoder(encoder_usage_t encoder, uint32_t *encoder_value)
  38. {
  39. switch (encoder)
  40. {
  41. case CAMERA_ENCODER:
  42. *encoder_value = m_encoders[CAMERA_ENCODER].count;
  43. break;
  44. case DRIVEN_ENCODER_GEAR:
  45. *encoder_value = m_encoders[DRIVEN_ENCODER_GEAR].count;
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. void encoder_light_schedule(void)
  52. {
  53. }
  54. /*
  55. R1:tim2-->TIM_CHANNEL_4
  56. G1:tim2-->TIM_CHANNEL_1
  57. B1:tim3-->TIM_CHANNEL_1
  58. R2:tim2-->TIM_CHANNEL_3
  59. G2:tim4-->TIM_CHANNEL_3
  60. B2:tim4-->TIM_CHANNEL_4
  61. */
  62. void encoder_light_switch_set_color(encoder_usage_t encoder, encoder_light_color_table_t color)
  63. {
  64. if (encoder == CAMERA_ENCODER)
  65. {
  66. switch (color)
  67. {
  68. case ENCODER_LIGHT_COLOR_RED:
  69. /* 红色 */
  70. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_RESET);
  71. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
  72. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
  73. break;
  74. case ENCODER_LIGHT_COLOR_GREEN:
  75. /* 绿色 */
  76. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
  77. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_RESET);
  78. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
  79. break;
  80. case ENCODER_LIGHT_COLOR_BLUE:
  81. /* 蓝色 */
  82. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
  83. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
  84. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_RESET);
  85. break;
  86. default:
  87. /* not find color */
  88. break;
  89. }
  90. }
  91. else if (encoder == DRIVEN_ENCODER_GEAR)
  92. {
  93. switch (color)
  94. {
  95. case ENCODER_LIGHT_COLOR_RED:
  96. /* 红色 */
  97. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_RESET);
  98. HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
  99. HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
  100. break;
  101. case ENCODER_LIGHT_COLOR_GREEN:
  102. /* 绿色 */
  103. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
  104. HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_RESET);
  105. HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
  106. break;
  107. case ENCODER_LIGHT_COLOR_BLUE:
  108. /* 蓝色 */
  109. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
  110. HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
  111. HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_RESET);
  112. break;
  113. default:
  114. /* not find color */
  115. break;
  116. }
  117. }
  118. else
  119. {
  120. // error
  121. }
  122. }
  123. void encoder_switch_close_light(encoder_usage_t encoder)
  124. {
  125. if (encoder == CAMERA_ENCODER)
  126. {
  127. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
  128. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
  129. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
  130. }
  131. else if (encoder == DRIVEN_ENCODER_GEAR)
  132. {
  133. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
  134. HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
  135. HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
  136. }
  137. else
  138. {
  139. // error
  140. }
  141. }
  142. encoder_t *encoder_get_camera_encoder_structer(void)
  143. {
  144. return &m_encoders[CAMERA_ENCODER];
  145. }
  146. encoder_t *encoder_get_driven_encoder_gear_structer(void)
  147. {
  148. return &m_encoders[DRIVEN_ENCODER_GEAR];
  149. }