otto_movements.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef __OTTO_MOVEMENTS_H__
  2. #define __OTTO_MOVEMENTS_H__
  3. #include "driver/gpio.h"
  4. #include "esp_log.h"
  5. #include "esp_timer.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "oscillator.h"
  9. //-- Constants
  10. #define FORWARD 1
  11. #define BACKWARD -1
  12. #define LEFT 1
  13. #define RIGHT -1
  14. #define BOTH 0
  15. #define SMALL 5
  16. #define MEDIUM 15
  17. #define BIG 30
  18. // -- Servo delta limit default. degree / sec
  19. #define SERVO_LIMIT_DEFAULT 240
  20. // -- Servo indexes for easy access
  21. #define LEFT_LEG 0
  22. #define RIGHT_LEG 1
  23. #define LEFT_FOOT 2
  24. #define RIGHT_FOOT 3
  25. #define LEFT_HAND 4
  26. #define RIGHT_HAND 5
  27. #define SERVO_COUNT 6
  28. class Otto {
  29. public:
  30. Otto();
  31. ~Otto();
  32. //-- Otto initialization
  33. void Init(int left_leg, int right_leg, int left_foot, int right_foot, int left_hand = -1,
  34. int right_hand = -1);
  35. //-- Attach & detach functions
  36. void AttachServos();
  37. void DetachServos();
  38. //-- Oscillator Trims
  39. void SetTrims(int left_leg, int right_leg, int left_foot, int right_foot, int left_hand = 0,
  40. int right_hand = 0);
  41. //-- Predetermined Motion Functions
  42. void MoveServos(int time, int servo_target[]);
  43. void MoveSingle(int position, int servo_number);
  44. void OscillateServos(int amplitude[SERVO_COUNT], int offset[SERVO_COUNT], int period,
  45. double phase_diff[SERVO_COUNT], float cycle);
  46. //-- HOME = Otto at rest position
  47. void Home(bool hands_down = true);
  48. bool GetRestState();
  49. void SetRestState(bool state);
  50. //-- Predetermined Motion Functions
  51. void Jump(float steps = 1, int period = 2000);
  52. void Walk(float steps = 4, int period = 1000, int dir = FORWARD, int amount = 0);
  53. void Turn(float steps = 4, int period = 2000, int dir = LEFT, int amount = 0);
  54. void Bend(int steps = 1, int period = 1400, int dir = LEFT);
  55. void ShakeLeg(int steps = 1, int period = 2000, int dir = RIGHT);
  56. void UpDown(float steps = 1, int period = 1000, int height = 20);
  57. void Swing(float steps = 1, int period = 1000, int height = 20);
  58. void TiptoeSwing(float steps = 1, int period = 900, int height = 20);
  59. void Jitter(float steps = 1, int period = 500, int height = 20);
  60. void AscendingTurn(float steps = 1, int period = 900, int height = 20);
  61. void Moonwalker(float steps = 1, int period = 900, int height = 20, int dir = LEFT);
  62. void Crusaito(float steps = 1, int period = 900, int height = 20, int dir = FORWARD);
  63. void Flapping(float steps = 1, int period = 1000, int height = 20, int dir = FORWARD);
  64. // -- 手部动作
  65. void HandsUp(int period = 1000, int dir = 0); // 双手举起
  66. void HandsDown(int period = 1000, int dir = 0); // 双手放下
  67. void HandWave(int period = 1000, int dir = LEFT); // 挥手
  68. void HandWaveBoth(int period = 1000); // 双手同时挥手
  69. // -- Servo limiter
  70. void EnableServoLimit(int speed_limit_degree_per_sec = SERVO_LIMIT_DEFAULT);
  71. void DisableServoLimit();
  72. private:
  73. Oscillator servo_[SERVO_COUNT];
  74. int servo_pins_[SERVO_COUNT];
  75. int servo_trim_[SERVO_COUNT];
  76. unsigned long final_time_;
  77. unsigned long partial_time_;
  78. float increment_[SERVO_COUNT];
  79. bool is_otto_resting_;
  80. bool has_hands_; // 是否有手部舵机
  81. void Execute(int amplitude[SERVO_COUNT], int offset[SERVO_COUNT], int period,
  82. double phase_diff[SERVO_COUNT], float steps);
  83. };
  84. #endif // __OTTO_MOVEMENTS_H__