BaseX.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * @Author: Sorzn
  3. * @Date: 2019-12-12 14:33:45
  4. * @LastEditTime: 2019-12-13 15:58:32
  5. * @Description: M5Stack project
  6. * @FilePath: /M5Stack/examples/Modules/BaseX/BaseX.cpp
  7. */
  8. #include "BaseX.h"
  9. #include "M5Stack.h"
  10. #define MAX(in, max) (((in) > (max)) ? (in) : (max))
  11. #define MIN(in, min) (((in) < (min)) ? (in) : (min))
  12. #define CONSTRAIN(in, min, max) MAX(min, MIN(in, max))
  13. uint8_t BASE_X::CheckPos(uint8_t pos) {
  14. pos = pos - 1;
  15. pos = CONSTRAIN(pos, 0, 3);
  16. return pos;
  17. }
  18. /**
  19. * @description:
  20. * @param pos: 1 ~ 4
  21. * @param mode: NORMAL, POSITION LOCK, SPEED LOCK
  22. * @return: None
  23. */
  24. void BASE_X::SetMode(uint8_t pos, uint8_t mode) {
  25. pos = CheckPos(pos);
  26. M5.I2C.writeByte(BASE_X_ADDR, BASE_X_CONFIG_ADDR + (0x10 * pos), mode);
  27. }
  28. /**
  29. * @description:
  30. * @param pos: 1 ~ 4
  31. * @return: encoder value
  32. */
  33. int32_t BASE_X::GetEncoderValue(uint8_t pos) {
  34. uint8_t addr;
  35. uint8_t read_buf[4] = {0, 0, 0, 0};
  36. pos = CheckPos(pos);
  37. addr = BASE_X_ENCODER_ADDR + 4 * pos;
  38. M5.I2C.readBytes(BASE_X_ADDR, addr, 4, read_buf);
  39. return (read_buf[0] << 24) | (read_buf[1] << 16) | (read_buf[2] << 8) |
  40. read_buf[3];
  41. }
  42. /**
  43. * @description:
  44. * @param pos: 1 ~ 4
  45. * @param encoder: INT32_MIN ~ INT32_MAX
  46. * @return: None
  47. */
  48. void BASE_X::SetEncoderValue(uint8_t pos, int32_t encoder) {
  49. uint8_t addr;
  50. uint8_t write_buf[4] = {0, 0, 0, 0};
  51. pos = CheckPos(pos);
  52. addr = BASE_X_ENCODER_ADDR + 4 * pos;
  53. write_buf[0] = encoder >> 24;
  54. write_buf[1] = encoder >> 16;
  55. write_buf[2] = encoder >> 8;
  56. write_buf[3] = encoder & 0xff;
  57. M5.I2C.writeBytes(BASE_X_ADDR, addr, write_buf, 4);
  58. }
  59. /**
  60. * @description:
  61. * @param pos: 1 ~ 4
  62. * @param duty: Set motor speed, -127 ~ 127
  63. * @return:
  64. */
  65. void BASE_X::SetMotorSpeed(uint8_t pos, int8_t duty) {
  66. uint8_t addr;
  67. pos = CheckPos(pos);
  68. addr = BASE_X_PWM_DUTY_ADDR + pos;
  69. M5.I2C.writeByte(BASE_X_ADDR, addr, duty);
  70. }
  71. /**
  72. * @description:
  73. * @param pos: 1 ~ 4
  74. * @return Motor run speed, -127 ~ 127:
  75. */
  76. int8_t BASE_X::GetMotorSpeed(uint8_t pos) {
  77. uint8_t read_data;
  78. uint8_t addr;
  79. pos = CheckPos(pos);
  80. addr = BASE_X_PWM_DUTY_ADDR + pos;
  81. M5.I2C.readByte(BASE_X_ADDR, addr, &read_data);
  82. return read_data;
  83. }
  84. /**
  85. * @description:
  86. * @param pos: 1 ~ 4
  87. * @return: Motor encoder increments every 20 ms
  88. */
  89. int8_t BASE_X::GetMotorSpeed20MS(uint8_t pos) {
  90. uint8_t read_data;
  91. uint8_t addr;
  92. pos = CheckPos(pos);
  93. addr = BASE_X_SPEED_ADDR + pos;
  94. M5.I2C.readByte(BASE_X_ADDR, addr, &read_data);
  95. return read_data;
  96. }
  97. void BASE_X::SetPositionPID(uint8_t pos, uint8_t kp, uint8_t ki, uint8_t kd) {
  98. uint8_t write_buf[3] = {0, 0, 0};
  99. uint8_t addr;
  100. pos = CheckPos(pos);
  101. addr = BASE_X_CONFIG_ADDR + pos * 0x10 + 0x01;
  102. write_buf[0] = kp;
  103. write_buf[1] = ki;
  104. write_buf[2] = kd;
  105. M5.I2C.writeBytes(BASE_X_ADDR, addr, write_buf, 3);
  106. }
  107. /**
  108. * @description:
  109. * @param pos: 1 ~ 4
  110. * @param position_point: in POSITION mode, motor will lock in this value,
  111. * INT32_MIN ~ INT32_MAX
  112. * @return: None
  113. */
  114. void BASE_X::SetPositionPoint(uint8_t pos, int32_t position_point) {
  115. uint8_t addr;
  116. uint8_t write_buf[4] = {0, 0, 0, 0};
  117. pos = CheckPos(pos);
  118. addr = BASE_X_CONFIG_ADDR + pos * 0x10 + 0x04;
  119. write_buf[0] = position_point & 0xff;
  120. write_buf[1] = position_point >> 8;
  121. write_buf[2] = position_point >> 16;
  122. write_buf[3] = position_point >> 24;
  123. // Serial.printf(" %x %x %x %x \r\n", write_buf[0], write_buf[1],
  124. // write_buf[2], write_buf[3]);
  125. M5.I2C.writeBytes(BASE_X_ADDR, addr, write_buf, 4);
  126. }
  127. /**
  128. * @description:
  129. * @param pos: 1 ~ 4
  130. * @param max_pwm: 0 ~ 127, POSITION mode, max speed
  131. * @return:
  132. */
  133. void BASE_X::SetPostionPIDMaxSpeed(uint8_t pos, uint8_t max_pwm) {
  134. uint8_t addr;
  135. pos = CheckPos(pos);
  136. addr = BASE_X_CONFIG_ADDR + pos * 0x10 + 0x08;
  137. M5.I2C.writeByte(BASE_X_ADDR, addr, max_pwm);
  138. }
  139. void BASE_X::SetSpeedPID(uint8_t pos, uint8_t kp, uint8_t ki, uint8_t kd) {
  140. uint8_t write_buf[3] = {0, 0, 0};
  141. uint8_t addr;
  142. pos = CheckPos(pos);
  143. addr = BASE_X_CONFIG_ADDR + pos * 0x10 + 0x09;
  144. write_buf[0] = kp;
  145. write_buf[1] = ki;
  146. write_buf[2] = kd;
  147. M5.I2C.writeBytes(BASE_X_ADDR, addr, write_buf, 3);
  148. }
  149. /**
  150. * @description:
  151. * @param pos: 1 ~ 4
  152. * @param speed_point: speed_point is lock GetMotorSpeed20MS(), not
  153. * GetMotorSpeed(), maybe -20 ~ 20
  154. * @return: None
  155. */
  156. void BASE_X::SetSpeedPoint(uint8_t pos, int8_t speed_point) {
  157. uint8_t addr;
  158. pos = CheckPos(pos);
  159. addr = BASE_X_CONFIG_ADDR + pos * 0x10 + 0x0c;
  160. M5.I2C.writeByte(BASE_X_ADDR, addr, (uint8_t)speed_point);
  161. }
  162. /**
  163. * @description:
  164. * @param pos: 1 ~ 2
  165. * @param angle: 0 ~ 180
  166. * @return: None
  167. */
  168. void BASE_X::SetServoAngle(uint8_t pos, uint8_t angle) {
  169. uint8_t addr;
  170. pos = CheckPos(pos);
  171. addr = BASE_X_SERVO_ANGLE_ADDR + pos;
  172. M5.I2C.writeByte(BASE_X_ADDR, addr, angle);
  173. }
  174. /**
  175. * @description:
  176. * @param pos: 1 ~ 2
  177. * @param width: 500 ~ 2500
  178. * @return: None
  179. */
  180. void BASE_X::SetServoPulseWidth(uint8_t pos, uint16_t width) {
  181. uint8_t addr;
  182. uint8_t write_buf[2] = {0, 0};
  183. pos = CheckPos(pos);
  184. addr = BASE_X_SERVO_PULSE_ADDR + pos * 0x02;
  185. write_buf[0] = width >> 8;
  186. write_buf[1] = width & 0xff;
  187. M5.I2C.writeBytes(BASE_X_ADDR, addr, write_buf, 2);
  188. }