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.

303 lines
9.4KB

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "ik.h"
  4. #include "dynamic_sequencer.h"
  5. #define MAX_ENTRIES 100
  6. void sequencer_walk_callback(sequencer_entry *, bot *);
  7. static sequencer_entry sequencer_list[MAX_ENTRIES];
  8. void
  9. sequencer_init() {
  10. int i;
  11. for(i=0;i<MAX_ENTRIES;i++) {
  12. sequencer_list[i].command=seq_nop;
  13. }
  14. }
  15. void
  16. sequencer_dump() {
  17. int i;
  18. for(i=0;i<MAX_ENTRIES;i++) {
  19. printf("i: %d frame_delay: %d frame_t: %d frame_duration: %d cmd:",i,sequencer_list[i].frame_delay, sequencer_list[i].frame_t, sequencer_list[i].frame_duration);
  20. switch(sequencer_list[i].command) {
  21. case seq_nop: printf("nop\n"); break;
  22. case seq_move_body: printf("move body\n"); break;
  23. case seq_move_leg: printf("move leg %d %f %f %f\n",sequencer_list[i].arg_int,sequencer_list[i].arg_vector3d.x,sequencer_list[i].arg_vector3d.y,sequencer_list[i].arg_vector3d.z); break;
  24. }
  25. }
  26. }
  27. void
  28. sequencer_run_frame(bot *b) {
  29. int i;
  30. double t;
  31. vector3d temp;
  32. int leg;
  33. for(i=0;i<MAX_ENTRIES;i++) {
  34. if(sequencer_list[i].command!=seq_nop) {
  35. if(sequencer_list[i].frame_delay) {
  36. sequencer_list[i].frame_delay--;
  37. } else {
  38. if(sequencer_list[i].frame_t>sequencer_list[i].frame_duration) sequencer_list[i].command=seq_nop;
  39. switch(sequencer_list[i].command) {
  40. case seq_walk:
  41. sequencer_walk_callback(&sequencer_list[i], b);
  42. break;
  43. case seq_move_body:
  44. add_vector3d(&b->body_position,&sequencer_list[i].arg_vector3d,&b->body_position);
  45. break;
  46. case seq_rotate_body:
  47. add_vector3d(&b->body_orientation,&sequencer_list[i].arg_vector3d,&b->body_orientation);
  48. break;
  49. case seq_move_leg:
  50. add_vector3d(&b->leg[sequencer_list[i].arg_int].position,&sequencer_list[i].arg_vector3d,&b->leg[sequencer_list[i].arg_int].position);
  51. break;
  52. case seq_move_leg_parabolic:
  53. t = (2.0*sequencer_list[i].frame_t/sequencer_list[i].frame_duration)- 1.0;
  54. t = 1.0 - (t*t);
  55. scalar_mult_vector3d(&sequencer_list[i].arg_vector3d,t-sequencer_list[i].arg_double,&temp);
  56. sequencer_list[i].arg_double=t;
  57. add_vector3d(&b->leg[sequencer_list[i].arg_int].position,&temp,&b->leg[sequencer_list[i].arg_int].position);
  58. break;
  59. case seq_rotate_leg:
  60. if(sequencer_list[i].frame_t==0) {
  61. rot_x_vector3d(&b->leg[sequencer_list[i].arg_int].position,sequencer_list[i].arg_vector3d.x,&temp);
  62. rot_y_vector3d(&temp,sequencer_list[i].arg_vector3d.y,&temp);
  63. rot_z_vector3d(&temp,-sequencer_list[i].arg_vector3d.z,&temp);
  64. sub_vector3d(&temp,&b->leg[sequencer_list[i].arg_int].position,&temp);
  65. scalar_mult_vector3d(&temp,1.0/sequencer_list[i].frame_duration,&sequencer_list[i].arg_vector3d);
  66. printf("x: %f y: %f z: %f\n",temp.x, temp.y, temp.z);
  67. }
  68. add_vector3d(&b->leg[sequencer_list[i].arg_int].position,&sequencer_list[i].arg_vector3d,&b->leg[sequencer_list[i].arg_int].position);
  69. break;
  70. }
  71. sequencer_list[i].frame_t++;
  72. }
  73. }
  74. }
  75. }
  76. sequencer_entry *
  77. sequencer_new_entry() {
  78. int i=0;
  79. for(i=0;i<MAX_ENTRIES;i++) {
  80. if(sequencer_list[i].command==seq_nop) {
  81. sequencer_list[i].frame_t=0;
  82. return &sequencer_list[i];
  83. }
  84. }
  85. return 0;
  86. }
  87. void
  88. sequencer_move_body(int frame_delay, int frame_duration, vector3d *d) {
  89. sequencer_entry *e;
  90. e=sequencer_new_entry();
  91. if(e) {
  92. e->command=seq_move_body;
  93. e->frame_delay=frame_delay;
  94. e->frame_duration=frame_duration;
  95. scalar_mult_vector3d(d,1.0/frame_duration,&e->arg_vector3d);
  96. }
  97. }
  98. void
  99. sequencer_rotate_body(int frame_delay, int frame_duration, vector3d *d) {
  100. sequencer_entry *e;
  101. e=sequencer_new_entry();
  102. if(e) {
  103. e->command=seq_rotate_body;
  104. e->frame_delay=frame_delay;
  105. e->frame_duration=frame_duration;
  106. scalar_mult_vector3d(d,1.0/frame_duration,&e->arg_vector3d);
  107. }
  108. }
  109. void
  110. sequencer_rotate_deg_body(int frame_delay, int frame_duration, vector3d *d) {
  111. sequencer_entry *e;
  112. e=sequencer_new_entry();
  113. if(e) {
  114. e->command=seq_rotate_body;
  115. e->frame_delay=frame_delay;
  116. e->frame_duration=frame_duration;
  117. scalar_mult_vector3d(d,(M_PI/180.0)/frame_duration,&e->arg_vector3d);
  118. }
  119. }
  120. void
  121. sequencer_move_leg(int frame_delay, int frame_duration, int i, vector3d *d) {
  122. sequencer_entry *e;
  123. e=sequencer_new_entry();
  124. if(e) {
  125. e->command=seq_move_leg;
  126. e->frame_delay=frame_delay;
  127. e->frame_duration=frame_duration;
  128. e->arg_int=i;
  129. scalar_mult_vector3d(d,1.0/frame_duration,&e->arg_vector3d);
  130. }
  131. }
  132. void
  133. sequencer_rotate_leg(int frame_delay, int frame_duration, int i, vector3d *d) {
  134. sequencer_entry *e;
  135. e=sequencer_new_entry();
  136. if(e) {
  137. e->command=seq_rotate_leg;
  138. e->frame_delay=frame_delay;
  139. e->frame_duration=frame_duration;
  140. e->arg_int=i;
  141. e->arg_double=0;
  142. e->arg_vector3d.x=d->x;
  143. e->arg_vector3d.y=d->y;
  144. e->arg_vector3d.z=d->z;
  145. }
  146. }
  147. void
  148. sequencer_move_leg_parabolic(int frame_delay, int frame_duration, int i, vector3d *d) {
  149. sequencer_entry *e;
  150. e=sequencer_new_entry();
  151. if(e) {
  152. e->command=seq_move_leg_parabolic;
  153. e->frame_delay=frame_delay;
  154. e->frame_duration=frame_duration;
  155. e->arg_int=i;
  156. e->arg_double=0;
  157. e->arg_vector3d.x=d->x;
  158. e->arg_vector3d.y=d->y;
  159. e->arg_vector3d.z=d->z;
  160. }
  161. }
  162. void
  163. sequencer_walk(sequencer_walk_parameters *p) {
  164. sequencer_entry *e;
  165. if(e=sequencer_new_entry()) {
  166. e->command=seq_walk;
  167. e->arg_pointer=p;
  168. e->arg_int=0;
  169. e->frame_delay=0;
  170. e->frame_t=0;
  171. e->frame_duration=1;
  172. }
  173. }
  174. void
  175. sequencer_walk_callback(sequencer_entry *e, bot *b) {
  176. sequencer_walk_parameters *p;
  177. p=e->arg_pointer;
  178. vector3d v;
  179. int l;
  180. e->frame_t=-1;
  181. e->frame_duration=1;
  182. e->frame_delay=p->step_duration;
  183. switch(e->arg_int) {
  184. case 0:
  185. if(((p->step_direction.x*p->step_direction.x)+(p->step_direction.y*p->step_direction.y))>=1.0||(abs(p->step_rotation)>0.01)) {
  186. e->arg_int=1;
  187. for(l=0;l<NUM_LEGS;l++) {
  188. if(l&1) {
  189. rot_z_vector3d(&b->leg[l].position,p->step_rotation,&v);
  190. add_vector3d(&v,&p->step_direction,&v);
  191. } else {
  192. rot_z_vector3d(&b->leg[l].position,-p->step_rotation,&v);
  193. sub_vector3d(&v,&p->step_direction,&v);
  194. }
  195. sub_vector3d(&v,&b->leg[l].position,&v);
  196. sequencer_move_leg(0,p->step_duration,l,&v);
  197. if(l&1) {
  198. v.x=0;
  199. v.y=0;
  200. v.z=-p->step_height;
  201. sequencer_move_leg_parabolic(0,p->step_duration,l,&v);
  202. }
  203. }
  204. }
  205. break;
  206. case 1:
  207. for(l=0;l<NUM_LEGS;l++) {
  208. if(l&1) {
  209. sub_vector3d(&b->leg[l].position,&p->last_step_direction,&v);
  210. rot_z_vector3d(&v,-(p->last_step_rotation+p->step_rotation),&v);
  211. sub_vector3d(&v,&p->step_direction,&v);
  212. } else {
  213. add_vector3d(&b->leg[l].position,&p->last_step_direction,&v);
  214. rot_z_vector3d(&v,(p->last_step_rotation+p->step_rotation),&v);
  215. add_vector3d(&v,&p->step_direction,&v);
  216. }
  217. sub_vector3d(&v,&b->leg[l].position,&v);
  218. sequencer_move_leg(0,p->step_duration,l,&v);
  219. if(!(l&1)) {
  220. v.x=0;
  221. v.y=0;
  222. v.z=-p->step_height;
  223. sequencer_move_leg_parabolic(0,p->step_duration,l,&v);
  224. }
  225. }
  226. if(((p->step_direction.x*p->step_direction.x)+(p->step_direction.y*p->step_direction.y))>=1.0||(abs(p->step_rotation)>0.01)) {
  227. e->arg_int=2;
  228. } else {
  229. e->arg_int=0;
  230. }
  231. break;
  232. case 2:
  233. for(l=0;l<NUM_LEGS;l++) {
  234. if(l&1) {
  235. add_vector3d(&b->leg[l].position,&p->last_step_direction,&v);
  236. rot_z_vector3d(&v,(p->last_step_rotation+p->step_rotation),&v);
  237. add_vector3d(&v,&p->step_direction,&v);
  238. } else {
  239. sub_vector3d(&b->leg[l].position,&p->last_step_direction,&v);
  240. rot_z_vector3d(&v,-(p->step_rotation+p->last_step_rotation),&v);
  241. sub_vector3d(&v,&p->step_direction,&v);
  242. }
  243. sub_vector3d(&v,&b->leg[l].position,&v);
  244. sequencer_move_leg(0,p->step_duration,l,&v);
  245. if(l&1) {
  246. v.x=0;
  247. v.y=0;
  248. v.z=-p->step_height;
  249. sequencer_move_leg_parabolic(0,p->step_duration,l,&v);
  250. }
  251. }
  252. if((((p->step_direction.x*p->step_direction.x)+(p->step_direction.y*p->step_direction.y))>=1.0)||(abs(p->step_rotation)>0.01)) {
  253. e->arg_int=1;
  254. } else {
  255. e->arg_int=0;
  256. }
  257. break;
  258. }
  259. copy_vector3d(&p->step_direction,&p->last_step_direction);
  260. p->last_step_rotation=p->step_rotation;
  261. }