bala.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "bala.h"
  2. #include "M5Stack.h"
  3. Bala::Bala() {
  4. wheel_left_encoder = 0;
  5. wheel_right_encoder = 0;
  6. i2c_mutex = NULL;
  7. }
  8. void Bala::ClearEncoder() {
  9. SetEncoder(0, 0);
  10. wheel_left_encoder = 0;
  11. wheel_right_encoder = 0;
  12. }
  13. void Bala::GetEncoder(int32_t* wheel_left, int32_t* wheel_right) {
  14. UpdateEncoder();
  15. *wheel_left = wheel_left_encoder;
  16. *wheel_right = wheel_right_encoder;
  17. }
  18. void Bala::SetEncoder(int32_t wheel_left, int32_t wheel_right) {
  19. uint8_t data_out[8];
  20. data_out[0] = (uint8_t)(wheel_left >> 24);
  21. data_out[1] = (uint8_t)(wheel_left >> 16);
  22. data_out[2] = (uint8_t)(wheel_left >> 8);
  23. data_out[3] = (uint8_t)(wheel_left >> 0);
  24. data_out[4] = (uint8_t)(wheel_right >> 24);
  25. data_out[5] = (uint8_t)(wheel_right >> 16);
  26. data_out[6] = (uint8_t)(wheel_right >> 8);
  27. data_out[7] = (uint8_t)(wheel_right >> 0);
  28. if (i2c_mutex != NULL) {
  29. xSemaphoreTake(i2c_mutex, portMAX_DELAY);
  30. }
  31. M5.I2C.writeBytes(0x3A, 0x10, data_out, 8);
  32. if (i2c_mutex != NULL) {
  33. xSemaphoreGive(i2c_mutex);
  34. }
  35. }
  36. void Bala::UpdateEncoder() {
  37. uint8_t data_in[8];
  38. if (i2c_mutex != NULL) {
  39. xSemaphoreTake(i2c_mutex, portMAX_DELAY);
  40. }
  41. M5.I2C.readBytes(0x3A, 0x10, 8, data_in);
  42. if (i2c_mutex != NULL) {
  43. xSemaphoreGive(i2c_mutex);
  44. }
  45. wheel_left_encoder = (data_in[0] << 24) | (data_in[1] << 16) |
  46. (data_in[2] << 8) | data_in[3];
  47. wheel_right_encoder = (data_in[4] << 24) | (data_in[5] << 16) |
  48. (data_in[6] << 8) | data_in[7];
  49. }
  50. void Bala::SetSpeed(int16_t wheel_left, int16_t wheel_right) {
  51. uint8_t data_out[4];
  52. data_out[0] = (int8_t)(wheel_left >> 8);
  53. data_out[1] = (int8_t)(wheel_left >> 0);
  54. data_out[2] = (int8_t)(wheel_right >> 8);
  55. data_out[3] = (int8_t)(wheel_right >> 0);
  56. if (i2c_mutex != NULL) {
  57. xSemaphoreTake(i2c_mutex, portMAX_DELAY);
  58. }
  59. M5.I2C.writeBytes(0x3A, 0x00, data_out, 4);
  60. if (i2c_mutex != NULL) {
  61. xSemaphoreGive(i2c_mutex);
  62. }
  63. }
  64. void Bala::SetMutex(SemaphoreHandle_t* mutex) { i2c_mutex = *mutex; }
  65. void Bala::SetServoAngle(uint8_t pos, uint8_t angle) {
  66. if (pos < 1) {
  67. pos = 1;
  68. } else if (pos > 8) {
  69. pos = 8;
  70. }
  71. pos = pos - 1;
  72. if (i2c_mutex != NULL) {
  73. xSemaphoreTake(i2c_mutex, portMAX_DELAY);
  74. }
  75. M5.I2C.writeBytes(0x3A, 0x20 | pos, &angle, 1);
  76. if (i2c_mutex != NULL) {
  77. xSemaphoreGive(i2c_mutex);
  78. }
  79. }
  80. void Bala::SetServoPulse(uint8_t pos, uint16_t width) {
  81. if (pos < 1) {
  82. pos = 1;
  83. } else if (pos > 8) {
  84. pos = 8;
  85. }
  86. pos = (pos - 1) << 1;
  87. uint8_t buff_out[2];
  88. buff_out[0] = width >> 8;
  89. buff_out[1] = width & 0xff;
  90. if (i2c_mutex != NULL) {
  91. xSemaphoreTake(i2c_mutex, portMAX_DELAY);
  92. }
  93. M5.I2C.writeBytes(0x3A, 0x30 | pos, buff_out, 2);
  94. if (i2c_mutex != NULL) {
  95. xSemaphoreGive(i2c_mutex);
  96. }
  97. }