基质喷涂
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.

82 lines
2.1 KiB

  1. //
  2. // Created by iflyt on 2025/3/3.
  3. //
  4. #include "motor_manager.h"
  5. // 初始化单例实例指针
  6. MotorManager* MotorManager::instance = nullptr;
  7. MotorManager::MotorManager() {}
  8. // 获取单例实例
  9. MotorManager* MotorManager::ins() {
  10. if (instance == nullptr) {
  11. instance = new MotorManager();
  12. }
  13. return instance;
  14. }
  15. // 获取某个电机的位置
  16. int32_t MotorManager::getMotorPosition(int motorIndex) {
  17. if (motorIndex < 0 || motorIndex >= 3) {
  18. return 0;
  19. }
  20. return motors[motorIndex].getPosition();
  21. }
  22. // 设置某个电机的目标位置
  23. void MotorManager::setMotorTargetPosition(int motorIndex, int32_t targetPosition) {
  24. if (motorIndex < 0 || motorIndex >= 3) {
  25. return;
  26. }
  27. motors[motorIndex].setTargetPosition(targetPosition);
  28. }
  29. // 设置某个电机的速度
  30. void MotorManager::setMotorSpeed(int motorIndex, int32_t speed) {
  31. if (motorIndex < 0 || motorIndex >= 3) {
  32. return;
  33. }
  34. motors[motorIndex].setSpeed(speed);
  35. }
  36. // 获取某个电机的速度
  37. int32_t MotorManager::getMotorSpeed(int motorIndex) {
  38. if (motorIndex < 0 || motorIndex >= 3) {
  39. return 0;
  40. }
  41. return motors[motorIndex].getRTSpeed();
  42. }
  43. // 获取某个电机的运动状态
  44. uint8_t MotorManager::getMotorMovingStatus(int motorIndex) {
  45. if (motorIndex < 0 || motorIndex >= 3) {
  46. return 0;
  47. }
  48. return motors[motorIndex].isMoving();
  49. }
  50. // 获取某个电机的编码器位置
  51. int32_t MotorManager::getMotorEncoderPosition(int motorIndex) {
  52. if (motorIndex < 0 || motorIndex >= 3) {
  53. return 0;
  54. }
  55. return motors[motorIndex].getEncoderPosition();
  56. }
  57. // 设置某个电机的编码器位置
  58. void MotorManager::setMotorEncoderPosition(int motorIndex, int32_t encoderPos) {
  59. if (motorIndex < 0 || motorIndex >= 3) {
  60. return;
  61. }
  62. motors[motorIndex].setEncoderPosition(encoderPos);
  63. }
  64. // 新增设置电机是否运动的接口
  65. void MotorManager::setMotorMoving(int motorIndex, bool moving) {
  66. if (motorIndex < 0 || motorIndex >= 3) {
  67. return;
  68. }
  69. motors[motorIndex].setMoving(moving);
  70. }