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.

202 lines
6.2 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. /* 需要注意别传入ENCODER_NUMBER,否则数组越界 */
  36. m_encoders[encoder].count = 0;
  37. }
  38. void encoder_read_with_encoder(encoder_usage_t encoder, uint32_t *encoder_value)
  39. {
  40. switch (encoder)
  41. {
  42. case CAMERA_ENCODER:
  43. *encoder_value = m_encoders[CAMERA_ENCODER].count;
  44. break;
  45. case DRIVEN_ENCODER_GEAR:
  46. *encoder_value = m_encoders[DRIVEN_ENCODER_GEAR].count;
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. void encoder_light_schedule(bool netif_link_status, bool *camera_encoder_flag, bool *driven_encoder_gear_flag)
  53. {
  54. static uint8_t camera_encoder_light_flicker_count = 0;
  55. static uint8_t driven_encoder_gear_light_flicker_count = 0;
  56. if (netif_link_status)
  57. {
  58. if (*camera_encoder_flag)
  59. {
  60. if (sys_haspassedms(camera_encoder_lastprocess) > 100)
  61. {
  62. camera_encoder_lastprocess = HAL_GetTick();
  63. HAL_GPIO_TogglePin(DIS_G1_GPIO_Port, DIS_G1_Pin);
  64. camera_encoder_light_flicker_count += 1;
  65. }
  66. if (camera_encoder_light_flicker_count > 6)
  67. {
  68. encoder_light_switch_set_color(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_GREEN);
  69. camera_encoder_light_flicker_count = 0;
  70. *camera_encoder_flag = false;
  71. }
  72. }
  73. if (*driven_encoder_gear_flag)
  74. {
  75. if (sys_haspassedms(driven_encoder_gear_lastprocess) > 100)
  76. {
  77. driven_encoder_gear_lastprocess = HAL_GetTick();
  78. HAL_GPIO_TogglePin(DIS_G2_GPIO_Port, DIS_G2_Pin);
  79. driven_encoder_gear_light_flicker_count += 1;
  80. }
  81. if (driven_encoder_gear_light_flicker_count > 6)
  82. {
  83. encoder_light_switch_set_color(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_GREEN);
  84. driven_encoder_gear_light_flicker_count = 0;
  85. *driven_encoder_gear_flag = false;
  86. }
  87. }
  88. }
  89. }
  90. /*
  91. R1:tim2-->TIM_CHANNEL_4
  92. G1:tim2-->TIM_CHANNEL_1
  93. B1:tim3-->TIM_CHANNEL_1
  94. R2:tim2-->TIM_CHANNEL_3
  95. G2:tim4-->TIM_CHANNEL_3
  96. B2:tim4-->TIM_CHANNEL_4
  97. */
  98. void encoder_light_switch_set_color(encoder_usage_t encoder, encoder_light_color_table_t color)
  99. {
  100. if (encoder == CAMERA_ENCODER)
  101. {
  102. switch (color)
  103. {
  104. case ENCODER_LIGHT_COLOR_RED:
  105. /* 红色 */
  106. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_RESET);
  107. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
  108. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
  109. break;
  110. case ENCODER_LIGHT_COLOR_GREEN:
  111. /* 绿色 */
  112. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
  113. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_RESET);
  114. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
  115. break;
  116. case ENCODER_LIGHT_COLOR_BLUE:
  117. /* 蓝色 */
  118. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
  119. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
  120. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_RESET);
  121. break;
  122. default:
  123. /* not find color */
  124. break;
  125. }
  126. }
  127. else if (encoder == DRIVEN_ENCODER_GEAR)
  128. {
  129. switch (color)
  130. {
  131. case ENCODER_LIGHT_COLOR_RED:
  132. /* 红色 */
  133. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_RESET);
  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. break;
  137. case ENCODER_LIGHT_COLOR_GREEN:
  138. /* 绿色 */
  139. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
  140. HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_RESET);
  141. HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
  142. break;
  143. case ENCODER_LIGHT_COLOR_BLUE:
  144. /* 蓝色 */
  145. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
  146. HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
  147. HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_RESET);
  148. break;
  149. default:
  150. /* not find color */
  151. break;
  152. }
  153. }
  154. else
  155. {
  156. // error
  157. }
  158. }
  159. void encoder_switch_close_light(encoder_usage_t encoder)
  160. {
  161. if (encoder == CAMERA_ENCODER)
  162. {
  163. HAL_GPIO_WritePin(DIS_R1_GPIO_Port, DIS_R1_Pin, GPIO_PIN_SET);
  164. HAL_GPIO_WritePin(DIS_G1_GPIO_Port, DIS_G1_Pin, GPIO_PIN_SET);
  165. HAL_GPIO_WritePin(DIS_B1_GPIO_Port, DIS_B1_Pin, GPIO_PIN_SET);
  166. }
  167. else if (encoder == DRIVEN_ENCODER_GEAR)
  168. {
  169. HAL_GPIO_WritePin(DIS_R2_GPIO_Port, DIS_R2_Pin, GPIO_PIN_SET);
  170. HAL_GPIO_WritePin(DIS_G2_GPIO_Port, DIS_G2_Pin, GPIO_PIN_SET);
  171. HAL_GPIO_WritePin(DIS_B2_GPIO_Port, DIS_B2_Pin, GPIO_PIN_SET);
  172. }
  173. else
  174. {
  175. // error
  176. }
  177. }
  178. encoder_t *encoder_get_camera_encoder_structer(void)
  179. {
  180. return &m_encoders[CAMERA_ENCODER];
  181. }
  182. encoder_t *encoder_get_driven_encoder_gear_structer(void)
  183. {
  184. return &m_encoders[DRIVEN_ENCODER_GEAR];
  185. }