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.

285 lines
8.9 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
  1. #include "encoder.h"
  2. #include "zboard.h"
  3. #include <stdio.h>
  4. #include "zport.h"
  5. #include "udpclient.h"
  6. static encoder_light_state_t camera_encoder_state = STANDBY;
  7. static encoder_light_state_t driven_encoder_gear_state = STANDBY;
  8. static uint32_t camera_encoder_lastprocess = 0;
  9. static uint32_t driven_encoder_gear_lastprocess = 0;
  10. static bool camera_encoder_light_state;
  11. static bool driven_encoder_light_state;
  12. static encoder_t m_uarts[] = {
  13. {&camera_encoder, TIM_CHANNEL_1 | TIM_CHANNEL_2}, // 相机编码器
  14. {&driven_encoder_gear, TIM_CHANNEL_1 | TIM_CHANNEL_2} // 从动编码器
  15. };
  16. void encoder_all_start(void)
  17. {
  18. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  19. {
  20. HAL_TIM_Encoder_Start(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
  21. }
  22. }
  23. void encoder_all_stop(void)
  24. {
  25. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  26. {
  27. HAL_TIM_Encoder_Stop(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
  28. }
  29. }
  30. bool encoder_clear_counter(encoder_usage_t encoder)
  31. {
  32. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  33. {
  34. if (encoder == i)
  35. {
  36. __HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0;
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. void encoder_all_clear_counter(void)
  43. {
  44. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  45. {
  46. __HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0; // 计数器值重新置位
  47. }
  48. }
  49. bool encoder_read_with_encoder(encoder_usage_t encoder, uint32_t *encoder_value)
  50. {
  51. bool get_encoder_value_flag = false;
  52. switch (encoder)
  53. {
  54. case CAMERA_ENCODER:
  55. *encoder_value += (short)__HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
  56. __HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler) = 0; /* 清零 */
  57. get_encoder_value_flag = true;
  58. break;
  59. case DRIVEN_ENCODER_GEAR:
  60. *encoder_value += (short)__HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
  61. __HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler) = 0; /* 清零 */
  62. get_encoder_value_flag = true;
  63. break;
  64. default:
  65. break;
  66. }
  67. return get_encoder_value_flag;
  68. }
  69. void encoder_light_schedule(void)
  70. {
  71. if (camera_encoder_state == STANDBY)
  72. {
  73. if (sys_haspassedms(camera_encoder_lastprocess) > 500)
  74. {
  75. camera_encoder_lastprocess = HAL_GetTick();
  76. if (camera_encoder_light_state)
  77. {
  78. encoder_light_switch_set_color_and_brightness(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_BLUE, encoder_light_max_brightness);
  79. }
  80. else
  81. {
  82. encoder_switch_close_light(CAMERA_ENCODER);
  83. }
  84. camera_encoder_light_state = !camera_encoder_light_state;
  85. }
  86. }
  87. if (driven_encoder_gear_state == STANDBY)
  88. {
  89. if (sys_haspassedms(driven_encoder_gear_lastprocess) > 500)
  90. {
  91. driven_encoder_gear_lastprocess = HAL_GetTick();
  92. if (driven_encoder_light_state)
  93. {
  94. encoder_light_switch_set_color_and_brightness(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_BLUE, encoder_light_max_brightness);
  95. }
  96. else
  97. {
  98. encoder_switch_close_light(DRIVEN_ENCODER_GEAR);
  99. }
  100. driven_encoder_light_state = !driven_encoder_light_state;
  101. }
  102. }
  103. }
  104. void encoder_set_state(encoder_usage_t encoder, encoder_light_state_t state)
  105. {
  106. /* 主动上报的时候常亮,接收到触发指令直接翻转led,待机中0.5s周期闪烁 */
  107. if (encoder == CAMERA_ENCODER)
  108. {
  109. camera_encoder_state = state;
  110. if (state == STANDBY)
  111. {
  112. camera_encoder_lastprocess = HAL_GetTick();
  113. // HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
  114. }
  115. else if (state == WORKING)
  116. {
  117. /* 如果工作状态的时候触发了genlock等,灯的状态不应该变成待机状态,只有同为待机的时候闪烁,上层写的时候需要加判断 */
  118. encoder_light_switch_set_color_and_brightness(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_BLUE, encoder_light_max_brightness);
  119. }
  120. }
  121. else if (encoder == DRIVEN_ENCODER_GEAR)
  122. {
  123. driven_encoder_gear_state = state;
  124. if (state == STANDBY)
  125. {
  126. driven_encoder_gear_lastprocess = HAL_GetTick();
  127. // HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
  128. }
  129. else if (state == WORKING)
  130. {
  131. encoder_light_switch_set_color_and_brightness(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_BLUE, encoder_light_max_brightness);
  132. }
  133. }
  134. }
  135. encoder_light_state_t encoder_get_state(encoder_usage_t encoder)
  136. {
  137. if (encoder == CAMERA_ENCODER)
  138. {
  139. return camera_encoder_state;
  140. }
  141. else if (encoder == DRIVEN_ENCODER_GEAR)
  142. {
  143. return driven_encoder_gear_state;
  144. }
  145. return ENCODER_STATE_NUMBER_AND_ERR_STATE;
  146. }
  147. void encoder_all_encoder_clear_counter_and_structer_count(void)
  148. {
  149. udp_client_get_active_report_data_structer()->encoder_1_count = 0;
  150. udp_client_get_active_report_data_structer()->encoder_2_count = 0;
  151. encoder_all_clear_counter();
  152. }
  153. void encoder_switch_encoder_clear_count_and_structer_count(encoder_usage_t encoder)
  154. {
  155. switch (encoder)
  156. {
  157. case CAMERA_ENCODER:
  158. udp_client_get_active_report_data_structer()->encoder_1_count = 0;
  159. encoder_clear_counter(CAMERA_ENCODER);
  160. break;
  161. case DRIVEN_ENCODER_GEAR:
  162. udp_client_get_active_report_data_structer()->encoder_2_count = 0;
  163. encoder_clear_counter(DRIVEN_ENCODER_GEAR);
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. /*
  170. R1:tim2-->TIM_CHANNEL_4
  171. G1:tim2-->TIM_CHANNEL_1
  172. B1:tim3-->TIM_CHANNEL_1
  173. R2:tim2-->TIM_CHANNEL_3
  174. G2:tim4-->TIM_CHANNEL_3
  175. B2:tim4-->TIM_CHANNEL_4
  176. */
  177. void encoder_light_switch_set_color_and_brightness(encoder_usage_t encoder, encoder_light_color_table_t color, uint16_t brightness)
  178. {
  179. uint16_t count;
  180. if (encoder == CAMERA_ENCODER)
  181. {
  182. switch (color)
  183. {
  184. case ENCODER_LIGHT_COLOR_RED:
  185. count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim2);
  186. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, count);
  187. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
  188. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
  189. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
  190. break;
  191. case ENCODER_LIGHT_COLOR_GREEN:
  192. count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim2);
  193. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, count);
  194. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
  195. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  196. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
  197. break;
  198. case ENCODER_LIGHT_COLOR_BLUE:
  199. count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim3);
  200. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, count);
  201. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
  202. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
  203. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
  204. break;
  205. default:
  206. /* not find color */
  207. break;
  208. }
  209. }
  210. else if (encoder == DRIVEN_ENCODER_GEAR)
  211. {
  212. switch (color)
  213. {
  214. case ENCODER_LIGHT_COLOR_RED:
  215. count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim2);
  216. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, count);
  217. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  218. HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
  219. HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
  220. break;
  221. case ENCODER_LIGHT_COLOR_GREEN:
  222. count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim4);
  223. __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, count);
  224. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
  225. HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
  226. HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
  227. break;
  228. case ENCODER_LIGHT_COLOR_BLUE:
  229. count = (1 - brightness / 1000.0) * __HAL_TIM_GET_AUTORELOAD(&htim4);
  230. __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_4, count);
  231. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
  232. HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
  233. HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
  234. break;
  235. default:
  236. /* not find color */
  237. break;
  238. }
  239. }
  240. else
  241. {
  242. // error
  243. }
  244. }
  245. void encoder_switch_close_light(encoder_usage_t encoder)
  246. {
  247. if (encoder == CAMERA_ENCODER)
  248. {
  249. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_4);
  250. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
  251. HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
  252. }
  253. else if (encoder == DRIVEN_ENCODER_GEAR)
  254. {
  255. HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
  256. HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
  257. HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
  258. }
  259. else
  260. {
  261. // error
  262. }
  263. }