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.

138 lines
3.9 KiB

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. static encoder_light_state_t camera_encoder_state = STANDBY;
  6. static encoder_light_state_t driven_encoder_gear_state = STANDBY;
  7. static uint32_t camera_encoder_lastprocess = 0;
  8. static uint32_t driven_encoder_gear_lastprocess = 0;
  9. static encoder_t m_uarts[] = {
  10. {&camera_encoder, TIM_CHANNEL_1 | TIM_CHANNEL_2}, // 相机编码器
  11. {&driven_encoder_gear, TIM_CHANNEL_1 | TIM_CHANNEL_2} // 从动编码器
  12. };
  13. void encoder_all_start(void)
  14. {
  15. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  16. {
  17. HAL_TIM_Encoder_Start(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
  18. }
  19. }
  20. void encoder_all_stop(void)
  21. {
  22. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  23. {
  24. HAL_TIM_Encoder_Stop(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
  25. }
  26. }
  27. bool encoder_clear_counter(encoder_usage_t encoder)
  28. {
  29. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  30. {
  31. if (encoder == i)
  32. {
  33. __HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0;
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. void encoder_all_clear_counter(void)
  40. {
  41. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  42. {
  43. __HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0; // 计数器值重新置位
  44. }
  45. }
  46. bool encoder_read_with_encoder(encoder_usage_t encoder, uint32_t *encoder_value)
  47. {
  48. bool get_encoder_value_flag = false;
  49. switch (encoder)
  50. {
  51. case CAMERA_ENCODER:
  52. *encoder_value = __HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
  53. get_encoder_value_flag = true;
  54. break;
  55. case DRIVEN_ENCODER_GEAR:
  56. *encoder_value = __HAL_TIM_GET_COUNTER(m_uarts[encoder].tim_handler);
  57. get_encoder_value_flag = true;
  58. break;
  59. default:
  60. break;
  61. }
  62. return get_encoder_value_flag;
  63. }
  64. void encoder_light_schedule(void)
  65. {
  66. if (camera_encoder_state == STANDBY)
  67. {
  68. if (sys_haspassedms(camera_encoder_lastprocess) > 500)
  69. {
  70. camera_encoder_lastprocess = HAL_GetTick();
  71. HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
  72. }
  73. }
  74. if (driven_encoder_gear_state == STANDBY)
  75. {
  76. if (sys_haspassedms(camera_encoder_lastprocess) > 500)
  77. {
  78. driven_encoder_gear_lastprocess = HAL_GetTick();
  79. // HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
  80. }
  81. }
  82. }
  83. void encoder_set_state(encoder_usage_t encoder, encoder_light_state_t state)
  84. {
  85. /* 主动上报的时候常亮,接收到触发指令直接翻转led,待机中0.5s周期闪烁 */
  86. if (encoder == CAMERA_ENCODER)
  87. {
  88. camera_encoder_state = state;
  89. if (state == STANDBY)
  90. {
  91. camera_encoder_lastprocess = HAL_GetTick();
  92. HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
  93. }
  94. else if (state == WORKING)
  95. {
  96. /* 如果工作状态的时候触发了genlock等,灯的状态不应该变成待机状态,只有同为待机的时候闪烁,上层写的时候需要加判断 */
  97. HAL_GPIO_WritePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin, GPIO_PIN_RESET);
  98. }
  99. }
  100. else if (encoder == DRIVEN_ENCODER_GEAR)
  101. {
  102. driven_encoder_gear_state = state;
  103. if (state == STANDBY)
  104. {
  105. driven_encoder_gear_lastprocess = HAL_GetTick();
  106. // HAL_GPIO_TogglePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin);
  107. }
  108. else if (state == WORKING)
  109. {
  110. // HAL_GPIO_WritePin(ENCODER_LIGHT_GPIO_Port, ENCODER_LIGHT_Pin, GPIO_PIN_SET);
  111. }
  112. }
  113. }
  114. encoder_light_state_t encoder_get_state(encoder_usage_t encoder)
  115. {
  116. if (encoder == CAMERA_ENCODER)
  117. {
  118. return camera_encoder_state;
  119. }
  120. else if (encoder == DRIVEN_ENCODER_GEAR)
  121. {
  122. return driven_encoder_gear_state;
  123. }
  124. return ENCODER_STATE_NUMBER_AND_ERR_STATE;
  125. }