bala.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) { xSemaphoreTake(i2c_mutex, portMAX_DELAY); }
  29. M5.I2C.writeBytes(0x3A, 0x10, data_out, 8);
  30. if(i2c_mutex != NULL) { xSemaphoreGive(i2c_mutex); }
  31. }
  32. void Bala::UpdateEncoder() {
  33. uint8_t data_in[8];
  34. if(i2c_mutex != NULL) { xSemaphoreTake(i2c_mutex, portMAX_DELAY); }
  35. M5.I2C.readBytes(0x3A, 0x10, 8, data_in);
  36. if(i2c_mutex != NULL) { xSemaphoreGive(i2c_mutex); }
  37. wheel_left_encoder = (data_in[0] << 24) | (data_in[1] << 16) | (data_in[2] << 8) | data_in[3];
  38. wheel_right_encoder = (data_in[4] << 24) | (data_in[5] << 16) | (data_in[6] << 8) | data_in[7];
  39. }
  40. void Bala::SetSpeed(int16_t wheel_left, int16_t wheel_right) {
  41. uint8_t data_out[4];
  42. data_out[0] = (int8_t)(wheel_left >> 8);
  43. data_out[1] = (int8_t)(wheel_left >> 0);
  44. data_out[2] = (int8_t)(wheel_right >> 8);
  45. data_out[3] = (int8_t)(wheel_right >> 0);
  46. if(i2c_mutex != NULL) { xSemaphoreTake(i2c_mutex, portMAX_DELAY); }
  47. M5.I2C.writeBytes(0x3A, 0x00, data_out, 4);
  48. if(i2c_mutex != NULL) { xSemaphoreGive(i2c_mutex); }
  49. }
  50. void Bala::SetMutex(SemaphoreHandle_t* mutex) {
  51. i2c_mutex = *mutex;
  52. }
  53. void Bala::SetServoAngle(uint8_t pos, uint8_t angle) {
  54. if (pos < 1) {
  55. pos = 1;
  56. } else if (pos > 8) {
  57. pos = 8;
  58. }
  59. pos = pos - 1;
  60. if(i2c_mutex != NULL) { xSemaphoreTake(i2c_mutex, portMAX_DELAY); }
  61. M5.I2C.writeBytes(0x3A, 0x20 | pos, &angle, 1);
  62. if(i2c_mutex != NULL) { xSemaphoreGive(i2c_mutex); }
  63. }
  64. void Bala::SetServoPulse(uint8_t pos, uint16_t width) {
  65. if (pos < 1) {
  66. pos = 1;
  67. } else if (pos > 8) {
  68. pos = 8;
  69. }
  70. pos = (pos - 1) << 1;
  71. uint8_t buff_out[2];
  72. buff_out[0] = width >> 8;
  73. buff_out[1] = width & 0xff;
  74. if(i2c_mutex != NULL) { xSemaphoreTake(i2c_mutex, portMAX_DELAY); }
  75. M5.I2C.writeBytes(0x3A, 0x30 | pos, buff_out, 2);
  76. if(i2c_mutex != NULL) { xSemaphoreGive(i2c_mutex); }
  77. }