movements.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "movements.h"
  2. #include <algorithm>
  3. #include <cstring>
  4. #include "oscillator.h"
  5. static const char* TAG = "Movements";
  6. Otto::Otto() {
  7. is_otto_resting_ = false;
  8. for (int i = 0; i < SERVO_COUNT; i++) {
  9. servo_pins_[i] = -1;
  10. servo_trim_[i] = 0;
  11. }
  12. }
  13. Otto::~Otto() {
  14. DetachServos();
  15. }
  16. unsigned long IRAM_ATTR millis() {
  17. return (unsigned long)(esp_timer_get_time() / 1000ULL);
  18. }
  19. void Otto::Init(int right_pitch, int right_roll, int left_pitch, int left_roll, int body,
  20. int head) {
  21. servo_pins_[RIGHT_PITCH] = right_pitch;
  22. servo_pins_[RIGHT_ROLL] = right_roll;
  23. servo_pins_[LEFT_PITCH] = left_pitch;
  24. servo_pins_[LEFT_ROLL] = left_roll;
  25. servo_pins_[BODY] = body;
  26. servo_pins_[HEAD] = head;
  27. AttachServos();
  28. is_otto_resting_ = false;
  29. }
  30. ///////////////////////////////////////////////////////////////////
  31. //-- ATTACH & DETACH FUNCTIONS ----------------------------------//
  32. ///////////////////////////////////////////////////////////////////
  33. void Otto::AttachServos() {
  34. for (int i = 0; i < SERVO_COUNT; i++) {
  35. if (servo_pins_[i] != -1) {
  36. servo_[i].Attach(servo_pins_[i]);
  37. }
  38. }
  39. }
  40. void Otto::DetachServos() {
  41. for (int i = 0; i < SERVO_COUNT; i++) {
  42. if (servo_pins_[i] != -1) {
  43. servo_[i].Detach();
  44. }
  45. }
  46. }
  47. ///////////////////////////////////////////////////////////////////
  48. //-- OSCILLATORS TRIMS ------------------------------------------//
  49. ///////////////////////////////////////////////////////////////////
  50. void Otto::SetTrims(int right_pitch, int right_roll, int left_pitch, int left_roll, int body,
  51. int head) {
  52. servo_trim_[RIGHT_PITCH] = right_pitch;
  53. servo_trim_[RIGHT_ROLL] = right_roll;
  54. servo_trim_[LEFT_PITCH] = left_pitch;
  55. servo_trim_[LEFT_ROLL] = left_roll;
  56. servo_trim_[BODY] = body;
  57. servo_trim_[HEAD] = head;
  58. for (int i = 0; i < SERVO_COUNT; i++) {
  59. if (servo_pins_[i] != -1) {
  60. servo_[i].SetTrim(servo_trim_[i]);
  61. }
  62. }
  63. }
  64. ///////////////////////////////////////////////////////////////////
  65. //-- BASIC MOTION FUNCTIONS -------------------------------------//
  66. ///////////////////////////////////////////////////////////////////
  67. void Otto::MoveServos(int time, int servo_target[]) {
  68. if (GetRestState() == true) {
  69. SetRestState(false);
  70. }
  71. final_time_ = millis() + time;
  72. if (time > 10) {
  73. for (int i = 0; i < SERVO_COUNT; i++) {
  74. if (servo_pins_[i] != -1) {
  75. increment_[i] = (servo_target[i] - servo_[i].GetPosition()) / (time / 10.0);
  76. }
  77. }
  78. for (int iteration = 1; millis() < final_time_; iteration++) {
  79. partial_time_ = millis() + 10;
  80. for (int i = 0; i < SERVO_COUNT; i++) {
  81. if (servo_pins_[i] != -1) {
  82. servo_[i].SetPosition(servo_[i].GetPosition() + increment_[i]);
  83. }
  84. }
  85. vTaskDelay(pdMS_TO_TICKS(10));
  86. }
  87. } else {
  88. for (int i = 0; i < SERVO_COUNT; i++) {
  89. if (servo_pins_[i] != -1) {
  90. servo_[i].SetPosition(servo_target[i]);
  91. }
  92. }
  93. vTaskDelay(pdMS_TO_TICKS(time));
  94. }
  95. // final adjustment to the target.
  96. bool f = true;
  97. int adjustment_count = 0;
  98. while (f && adjustment_count < 10) {
  99. f = false;
  100. for (int i = 0; i < SERVO_COUNT; i++) {
  101. if (servo_pins_[i] != -1 && servo_target[i] != servo_[i].GetPosition()) {
  102. f = true;
  103. break;
  104. }
  105. }
  106. if (f) {
  107. for (int i = 0; i < SERVO_COUNT; i++) {
  108. if (servo_pins_[i] != -1) {
  109. servo_[i].SetPosition(servo_target[i]);
  110. }
  111. }
  112. vTaskDelay(pdMS_TO_TICKS(10));
  113. adjustment_count++;
  114. }
  115. };
  116. }
  117. void Otto::MoveSingle(int position, int servo_number) {
  118. if (position > 180)
  119. position = 90;
  120. if (position < 0)
  121. position = 90;
  122. if (GetRestState() == true) {
  123. SetRestState(false);
  124. }
  125. if (servo_number >= 0 && servo_number < SERVO_COUNT && servo_pins_[servo_number] != -1) {
  126. servo_[servo_number].SetPosition(position);
  127. }
  128. }
  129. void Otto::OscillateServos(int amplitude[SERVO_COUNT], int offset[SERVO_COUNT], int period,
  130. double phase_diff[SERVO_COUNT], float cycle = 1) {
  131. for (int i = 0; i < SERVO_COUNT; i++) {
  132. if (servo_pins_[i] != -1) {
  133. servo_[i].SetO(offset[i]);
  134. servo_[i].SetA(amplitude[i]);
  135. servo_[i].SetT(period);
  136. servo_[i].SetPh(phase_diff[i]);
  137. }
  138. }
  139. double ref = millis();
  140. double end_time = period * cycle + ref;
  141. while (millis() < end_time) {
  142. for (int i = 0; i < SERVO_COUNT; i++) {
  143. if (servo_pins_[i] != -1) {
  144. servo_[i].Refresh();
  145. }
  146. }
  147. vTaskDelay(5);
  148. }
  149. vTaskDelay(pdMS_TO_TICKS(10));
  150. }
  151. void Otto::Execute(int amplitude[SERVO_COUNT], int offset[SERVO_COUNT], int period,
  152. double phase_diff[SERVO_COUNT], float steps = 1.0) {
  153. if (GetRestState() == true) {
  154. SetRestState(false);
  155. }
  156. int cycles = (int)steps;
  157. //-- Execute complete cycles
  158. if (cycles >= 1)
  159. for (int i = 0; i < cycles; i++)
  160. OscillateServos(amplitude, offset, period, phase_diff);
  161. //-- Execute the final not complete cycle
  162. OscillateServos(amplitude, offset, period, phase_diff, (float)steps - cycles);
  163. vTaskDelay(pdMS_TO_TICKS(10));
  164. }
  165. ///////////////////////////////////////////////////////////////////
  166. //-- HOME = Otto at rest position -------------------------------//
  167. ///////////////////////////////////////////////////////////////////
  168. void Otto::Home(bool hands_down) {
  169. if (is_otto_resting_ == false) { // Go to rest position only if necessary
  170. MoveServos(1000, servo_initial_);
  171. is_otto_resting_ = true;
  172. }
  173. vTaskDelay(pdMS_TO_TICKS(1000));
  174. }
  175. bool Otto::GetRestState() {
  176. return is_otto_resting_;
  177. }
  178. void Otto::SetRestState(bool state) {
  179. is_otto_resting_ = state;
  180. }
  181. ///////////////////////////////////////////////////////////////////
  182. //-- PREDETERMINED MOTION SEQUENCES -----------------------------//
  183. ///////////////////////////////////////////////////////////////////
  184. //---------------------------------------------------------
  185. //-- 统一手部动作函数
  186. //-- Parameters:
  187. //-- action: 动作类型 1=举左手, 2=举右手, 3=举双手, 4=放左手, 5=放右手, 6=放双手,
  188. //-- 7=挥左手, 8=挥右手, 9=挥双手, 10=拍打左手, 11=拍打右手, 12=拍打双手
  189. //-- times: 重复次数
  190. //-- amount: 动作幅度 (10-50)
  191. //-- period: 动作时间
  192. //---------------------------------------------------------
  193. void Otto::HandAction(int action, int times, int amount, int period) {
  194. // 限制参数范围
  195. times = 2 * std::max(3, std::min(100, times));
  196. amount = std::max(10, std::min(50, amount));
  197. period = std::max(100, std::min(1000, period));
  198. int current_positions[SERVO_COUNT];
  199. for (int i = 0; i < SERVO_COUNT; i++) {
  200. current_positions[i] = (servo_pins_[i] != -1) ? servo_[i].GetPosition() : servo_initial_[i];
  201. }
  202. switch (action) {
  203. case 1: // 举左手
  204. current_positions[LEFT_PITCH] = 180;
  205. MoveServos(period, current_positions);
  206. break;
  207. case 2: // 举右手
  208. current_positions[RIGHT_PITCH] = 0;
  209. MoveServos(period, current_positions);
  210. break;
  211. case 3: // 举双手
  212. current_positions[LEFT_PITCH] = 180;
  213. current_positions[RIGHT_PITCH] = 0;
  214. MoveServos(period, current_positions);
  215. break;
  216. case 4: // 放左手
  217. case 5: // 放右手
  218. case 6: // 放双手
  219. // 回到初始位置
  220. memcpy(current_positions, servo_initial_, sizeof(current_positions));
  221. MoveServos(period, current_positions);
  222. break;
  223. case 7: // 挥左手
  224. current_positions[LEFT_PITCH] = 150;
  225. MoveServos(period, current_positions);
  226. for (int i = 0; i < times; i++) {
  227. current_positions[LEFT_PITCH] = 150 + (i % 2 == 0 ? -30 : 30);
  228. MoveServos(period / 10, current_positions);
  229. vTaskDelay(pdMS_TO_TICKS(period / 10));
  230. }
  231. memcpy(current_positions, servo_initial_, sizeof(current_positions));
  232. MoveServos(period, current_positions);
  233. break;
  234. case 8: // 挥右手
  235. current_positions[RIGHT_PITCH] = 30;
  236. MoveServos(period, current_positions);
  237. for (int i = 0; i < times; i++) {
  238. current_positions[RIGHT_PITCH] = 30 + (i % 2 == 0 ? 30 : -30);
  239. MoveServos(period / 10, current_positions);
  240. vTaskDelay(pdMS_TO_TICKS(period / 10));
  241. }
  242. memcpy(current_positions, servo_initial_, sizeof(current_positions));
  243. MoveServos(period, current_positions);
  244. break;
  245. case 9: // 挥双手
  246. current_positions[LEFT_PITCH] = 150;
  247. current_positions[RIGHT_PITCH] = 30;
  248. MoveServos(period, current_positions);
  249. for (int i = 0; i < times; i++) {
  250. current_positions[LEFT_PITCH] = 150 + (i % 2 == 0 ? -30 : 30);
  251. current_positions[RIGHT_PITCH] = 30 + (i % 2 == 0 ? 30 : -30);
  252. MoveServos(period / 10, current_positions);
  253. vTaskDelay(pdMS_TO_TICKS(period / 10));
  254. }
  255. memcpy(current_positions, servo_initial_, sizeof(current_positions));
  256. MoveServos(period, current_positions);
  257. break;
  258. case 10: // 拍打左手
  259. current_positions[LEFT_ROLL] = 20;
  260. MoveServos(period, current_positions);
  261. for (int i = 0; i < times; i++) {
  262. current_positions[LEFT_ROLL] = 20 - amount;
  263. MoveServos(period / 10, current_positions);
  264. current_positions[LEFT_ROLL] = 20 + amount;
  265. MoveServos(period / 10, current_positions);
  266. }
  267. current_positions[LEFT_ROLL] = 0;
  268. MoveServos(period, current_positions);
  269. break;
  270. case 11: // 拍打右手
  271. current_positions[RIGHT_ROLL] = 160;
  272. MoveServos(period, current_positions);
  273. for (int i = 0; i < times; i++) {
  274. current_positions[RIGHT_ROLL] = 160 + amount;
  275. MoveServos(period / 10, current_positions);
  276. current_positions[RIGHT_ROLL] = 160 - amount;
  277. MoveServos(period / 10, current_positions);
  278. }
  279. current_positions[RIGHT_ROLL] = 180;
  280. MoveServos(period, current_positions);
  281. break;
  282. case 12: // 拍打双手
  283. current_positions[LEFT_ROLL] = 20;
  284. current_positions[RIGHT_ROLL] = 160;
  285. MoveServos(period, current_positions);
  286. for (int i = 0; i < times; i++) {
  287. current_positions[LEFT_ROLL] = 20 - amount;
  288. current_positions[RIGHT_ROLL] = 160 + amount;
  289. MoveServos(period / 10, current_positions);
  290. current_positions[LEFT_ROLL] = 20 + amount;
  291. current_positions[RIGHT_ROLL] = 160 - amount;
  292. MoveServos(period / 10, current_positions);
  293. }
  294. current_positions[LEFT_ROLL] = 0;
  295. current_positions[RIGHT_ROLL] = 180;
  296. MoveServos(period, current_positions);
  297. break;
  298. }
  299. }
  300. //---------------------------------------------------------
  301. //-- 统一身体动作函数
  302. //-- Parameters:
  303. //-- action: 动作类型 1=左转, 2=右转,3=回中心
  304. //-- times: 转动次数
  305. //-- amount: 旋转角度 (0-90度,以90度为中心左右旋转)
  306. //-- period: 动作时间
  307. //---------------------------------------------------------
  308. void Otto::BodyAction(int action, int times, int amount, int period) {
  309. // 限制参数范围
  310. times = std::max(1, std::min(10, times));
  311. amount = std::max(0, std::min(90, amount));
  312. period = std::max(500, std::min(3000, period));
  313. int current_positions[SERVO_COUNT];
  314. for (int i = 0; i < SERVO_COUNT; i++) {
  315. if (servo_pins_[i] != -1) {
  316. current_positions[i] = servo_[i].GetPosition();
  317. } else {
  318. current_positions[i] = servo_initial_[i];
  319. }
  320. }
  321. int body_center = servo_initial_[BODY];
  322. int target_angle = body_center;
  323. switch (action) {
  324. case 1: // 左转
  325. target_angle = body_center + amount;
  326. target_angle = std::min(180, target_angle);
  327. break;
  328. case 2: // 右转
  329. target_angle = body_center - amount;
  330. target_angle = std::max(0, target_angle);
  331. break;
  332. case 3: // 回中心
  333. target_angle = body_center;
  334. break;
  335. default:
  336. return; // 无效动作
  337. }
  338. current_positions[BODY] = target_angle;
  339. MoveServos(period, current_positions);
  340. vTaskDelay(pdMS_TO_TICKS(100));
  341. }
  342. //---------------------------------------------------------
  343. //-- 统一头部动作函数
  344. //-- Parameters:
  345. //-- action: 动作类型 1=抬头, 2=低头, 3=点头, 4=回中心, 5=连续点头
  346. //-- times: 重复次数 (仅对连续点头有效)
  347. //-- amount: 角度偏移 (1-15度范围内)
  348. //-- period: 动作时间
  349. //---------------------------------------------------------
  350. void Otto::HeadAction(int action, int times, int amount, int period) {
  351. // 限制参数范围
  352. times = std::max(1, std::min(10, times));
  353. amount = std::max(1, std::min(15, abs(amount)));
  354. period = std::max(300, std::min(3000, period));
  355. int current_positions[SERVO_COUNT];
  356. for (int i = 0; i < SERVO_COUNT; i++) {
  357. if (servo_pins_[i] != -1) {
  358. current_positions[i] = servo_[i].GetPosition();
  359. } else {
  360. current_positions[i] = servo_initial_[i];
  361. }
  362. }
  363. int head_center = 90; // 头部中心位置
  364. switch (action) {
  365. case 1: // 抬头
  366. current_positions[HEAD] = head_center + amount; // 抬头是增加角度
  367. MoveServos(period, current_positions);
  368. break;
  369. case 2: // 低头
  370. current_positions[HEAD] = head_center - amount; // 低头是减少角度
  371. MoveServos(period, current_positions);
  372. break;
  373. case 3: // 点头 (上下运动)
  374. // 先抬头
  375. current_positions[HEAD] = head_center + amount;
  376. MoveServos(period / 3, current_positions);
  377. vTaskDelay(pdMS_TO_TICKS(period / 6));
  378. // 再低头
  379. current_positions[HEAD] = head_center - amount;
  380. MoveServos(period / 3, current_positions);
  381. vTaskDelay(pdMS_TO_TICKS(period / 6));
  382. // 回到中心
  383. current_positions[HEAD] = head_center;
  384. MoveServos(period / 3, current_positions);
  385. break;
  386. case 4: // 回到中心位置
  387. current_positions[HEAD] = head_center;
  388. MoveServos(period, current_positions);
  389. break;
  390. case 5: // 连续点头
  391. for (int i = 0; i < times; i++) {
  392. // 抬头
  393. current_positions[HEAD] = head_center + amount;
  394. MoveServos(period / 2, current_positions);
  395. // 低头
  396. current_positions[HEAD] = head_center - amount;
  397. MoveServos(period / 2, current_positions);
  398. vTaskDelay(pdMS_TO_TICKS(50)); // 短暂停顿
  399. }
  400. // 回到中心
  401. current_positions[HEAD] = head_center;
  402. MoveServos(period / 2, current_positions);
  403. break;
  404. default:
  405. // 无效动作,回到中心
  406. current_positions[HEAD] = head_center;
  407. MoveServos(period, current_positions);
  408. break;
  409. }
  410. }