M5Bala.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2018 M5Stack
  4. // Author: bin@m5stack.com (0x1abin)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #ifndef _M5BALA_H
  24. #define _M5BALA_H
  25. #include "Arduino.h"
  26. #include "Wire.h"
  27. #include "imuCalibration.h"
  28. #define MOTOR_RPM 150
  29. #define MAX_PWM 255
  30. #define DEAD_ZONE 20
  31. #define M5GO_WHEEL_ADDR 0x56
  32. #define MOTOR_CTRL_ADDR 0x00
  33. #define ENCODER_ADDR 0x04
  34. class M5Bala {
  35. public:
  36. M5Bala();
  37. M5Bala(TwoWire &w);
  38. void begin();
  39. void run();
  40. void PIDCompute();
  41. void setMotor(int16_t pwm0, int16_t pwm1);
  42. void readEncder();
  43. uint8_t i2c_readByte(uint8_t address, uint8_t subAddress);
  44. void setAngleOffset(float offset) { angle_offset = offset; };
  45. float getAngle() { return pitch; };
  46. int16_t getSpeed0() { return speed_input0; };
  47. int16_t getSpeed1() { return speed_input1; };
  48. int16_t getOut0() { return pwm_out0; };
  49. int16_t getOut1() { return pwm_out1; };
  50. int16_t left_offset, right_offset;
  51. int16_t forward_offset;
  52. // control
  53. void stop();
  54. void move(int16_t speed, uint16_t duration = 0);
  55. void turn(int16_t speed, uint16_t duration = 0);
  56. void rotate(int16_t speed, uint16_t duration = 0);
  57. private:
  58. TwoWire *wire;
  59. float yaw, pitch, roll;
  60. int16_t speed_input0, speed_input1;
  61. int16_t pwm_out0, pwm_out1;
  62. int8_t angle_offset;
  63. uint32_t loop_interval;
  64. float K1, K2, K3, K4, K5;
  65. uint8_t imu_id;
  66. };
  67. #endif