Advertisement
Guest User

code

a guest
Oct 26th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. #include <stdbool.h>
  2.  
  3. /*
  4. global constants
  5. */
  6. static unsigned FRAMEWAITTIME = 10;
  7. static unsigned POLYWAITTIME = 20; // time in ms must be unsigned to be passed to sleep()
  8. static double MAXPOLYDEV = 30; // angle in degrees
  9. /*
  10. angle tolerances in degrees used when there should be no movement in that direction
  11. */
  12. static double ANGLETOLHEAD = 15;
  13. static double ANGLETOLROLL = 15;
  14. static double ANGLETOLPITCH = 15;
  15. /*
  16. multiplier used when movement exists in that direction
  17. */
  18. static double ANGLETOLMULTIPLIER = 0.25; // y = a*(1+x)
  19. static double MINDRAWLEN = 1.8; // in meters
  20. static double MINDRAWLENLIGHT = 0.3; // in meters
  21. static double POLYANGLETRI = 60;
  22. static double POLYANGLELIGHT = 150;
  23. static double POLYANGLESQUARE = 90;
  24. /*
  25. values of each type
  26. 0 = earth
  27. 1 = fire
  28. 2 = lightning
  29. 3 = water
  30. 4 = wind
  31. */
  32. static short EARTH = 0;
  33. static short FIRE = 1;
  34. static short LIGHTNING = 2;
  35. static short WATER = 3;
  36. static short WIND = 4;
  37. static short NOTCIRCLE = 5;
  38. static short ERRORVAL = -1;
  39. /*
  40. struct
  41. */
  42. typedef struct runeClassStruct
  43. {
  44. double init_heading;
  45. double init_roll;
  46. double init_pitch;
  47. bool isCircle;
  48. bool isLightning;
  49. } runeClassDataAlias;
  50.  
  51. typedef struct spellQueueStruct
  52. {
  53. struct spellQueueStruct *next;
  54. /*
  55. spellType
  56. 0 = earth
  57. 1 = fire
  58. 2 = lightning
  59. 3 = water
  60. 4 = wind
  61. other = various errors
  62. */
  63. short spellType;
  64. } spellQueueAlias;
  65.  
  66. /*
  67. global variables
  68. */
  69. /*
  70. stores current spell type
  71. 0 = earth
  72. 1 = fire
  73. 2 = lightning
  74. 3 = water
  75. 4 = wind
  76. other = various errors
  77. */
  78. extern double currTotalLength;
  79. extern short currSpellType;
  80. extern bool isCasting;
  81. /*
  82. we can use pointers for these as they start as NULL
  83. */
  84. extern spellQueueAlias *spellQueueStart; // other files read spells from this guy
  85. extern spellQueueAlias *spellQueueEnd; // end of queue
  86. /*
  87. we can't use pointers here because we
  88. */
  89. extern runeClassDataAlias runeClassData; // other files access this global struct
  90. extern struct bnoeul initEulStruct; // stores initial orientation
  91. extern struct bnoeul currEulStruct; // stores current orientation
  92. extern struct bnolin currLinStruct; // stores current velocity
  93.  
  94. /*
  95. function declarations
  96. */
  97. extern int main();
  98. extern short checkCircle(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
  99. extern bool checkLightning(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
  100. extern short classifyShape(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
  101. extern double getVelocity(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
  102. extern void calibrateImu();
  103. extern bool isValidSpell(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
  104. extern void enqueueSpell(short spell);
  105. extern short dequeueSpell(); // returns -1 on error
  106. extern void initQueue(); // initialize spellQueueStart
  107.  
  108. /*
  109. source code
  110. */
  111.  
  112.  
  113. #include <stdlib.h>
  114. #include <stdio.h>
  115. #include <stdbool.h>
  116. #include <time.h>
  117. #include <unistd.h>
  118. #include <getbno055.h>
  119. #include <imumodule.h>
  120. #include <errno.h>
  121.  
  122. int main()
  123. {
  124. bool isCircle = false;
  125. bool isLightning = false;
  126. int retval = 0;
  127.  
  128. /*
  129. get initial hrp values
  130. passes to global pointer
  131. */
  132. calibrateImu();
  133. /*
  134. main loop
  135. */
  136. while (isCasting)
  137. {
  138. sleep(FRAMEWAITTIME);
  139. retval = get_eul(&currEulStruct);
  140. // error getting euler orientation
  141. if (retval < 0)
  142. {
  143. return retval;
  144. }
  145. retval = get_lin(&currLinStruct);
  146. // error getting linear acceleration
  147. if (retval < 0)
  148. {
  149. return retval;
  150. }
  151. currSpellType = classifyShape(&currEulStruct, &currLinStruct);
  152. }
  153. return retval;
  154. }
  155.  
  156. /*
  157. checks if rune is a Horizontal or Vertical circle
  158. must pass pointers bnoeulptr and bnolinptr that are already filled
  159. waits POLYWAITTIME; if angle change is > MAXPOLYWAITTIME
  160. returns WIND if wind (horizontal circle)
  161. returns WATER if water (vertical circle)
  162. returns NOTCIRCLE otherwise
  163. */
  164. short checkCircle(struct bnoeul *starteulptr, struct bnolin *bnolinptr)
  165. {
  166. int errval;
  167. double final_h;
  168. double final_r;
  169. double final_p;
  170. struct bnoeul *finaleulptr;
  171. short retval = NOTCIRCLE;
  172. double init_h = initEulStruct.eul_head;
  173. double init_r = initEulStruct.eul_roll;
  174. double init_p = initEulStruct.eul_pitc;
  175. double start_h = starteulptr->eul_head;
  176. double start_r = starteulptr->eul_roll;
  177. double start_p = starteulptr->eul_pitc;
  178. clock_t time_start = clock();
  179. // allocate memory for struct
  180. finaleulptr = (struct bnoeul *) malloc(sizeof(struct bnoeul));
  181.  
  182. // wait POLYWAITTIME
  183. sleep(POLYWAITTIME);
  184. errval = get_eul(finaleulptr);
  185. if (errval < 0)
  186. {
  187. retval = ERRORVAL;
  188. }
  189. else if (final_h - start_h > MAXPOLYDEV)
  190. {
  191. retval = WIND;
  192. }
  193. else if (final_r - start_r > MAXPOLYDEV)
  194. {
  195. retval = WATER;
  196. }
  197. // free memory after casting to void*
  198. free((void *)finaleulptr);
  199. return retval;
  200. }
  201.  
  202. /*
  203. checks if rune is for lightning
  204. */
  205. bool checkLightning(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr)
  206. {
  207. double init_h = initEulStruct.eul_head;
  208. double init_r = initEulStruct.eul_roll;
  209. double init_p = initEulStruct.eul_pitc;
  210. double h = bnoeulptr->eul_head;
  211. double r = bnoeulptr->eul_roll;
  212. double p = bnoeulptr->eul_pitc;
  213. if (init_r - r < ANGLETOLROLL)
  214. {
  215. return false;
  216. }
  217. else if (init_h - h < ANGLETOLHEAD)
  218. {
  219. return false;
  220. }
  221. else if (init_p - p < ANGLETOLPITCH)
  222. {
  223. return false;
  224. }
  225. }
  226.  
  227. short classifyShape(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr)
  228. {
  229. short circleType = 0;
  230. bool isLightning;
  231.  
  232. circleType = checkCircle(bnoeulptr, bnolinptr);
  233. isLightning = checkLightning(bnoeulptr, bnolinptr);
  234. if (circleType == WATER)
  235. {
  236. currSpellType = WATER;
  237. return currSpellType;
  238. }
  239. else if (circleType == WIND)
  240. {
  241. currSpellType = WIND;
  242. return currSpellType;
  243. }
  244. else if (isLightning)
  245. {
  246. currSpellType = LIGHTNING;
  247. return currSpellType;
  248. }
  249. return currSpellType;
  250. }
  251.  
  252. double getDistance(clock_t start, clock_t end, struct bnoeul *bnoeulptr, struct bnolin *bnolinptr)
  253. {
  254. double acc_x = bnolinptr->linacc_x;
  255. double acc_y = bnolinptr->linacc_y;
  256. double acc_z = bnolinptr->linacc_z;
  257. }
  258.  
  259. void calibrateImu()
  260. {
  261. int retval = 0;
  262. bno_reset();
  263. retval = get_eul(&initEulStruct);
  264. }
  265.  
  266. /*
  267. add spell to end of list: FIFO
  268. */
  269. void enqueueSpell(short spell)
  270. {
  271. /*
  272. allocate memory
  273. */
  274. struct spellQueueStruct *newNode = (struct spellQueueStruct*) malloc(sizeof(struct spellQueueStruct));
  275. /*
  276. fill in data of new struct
  277. */
  278. newNode->next = NULL;
  279. newNode->spellType = spell;
  280. /*
  281. if adding to empty list set values
  282. */
  283. if (spellQueueStart == NULL)
  284. {
  285. spellQueueStart = newNode;
  286. spellQueueEnd = newNode;
  287. }
  288. else
  289. {
  290. /*
  291. update next pointer of last element that existed before
  292. */
  293. spellQueueEnd->next = newNode;
  294. /*
  295. update last pointer
  296. */
  297. spellQueueEnd = newNode;
  298. }
  299.  
  300. }
  301.  
  302. /*
  303. remove spell from start of list: FIFO
  304. returns -1 on error
  305. 0 = earth
  306. 1 = fire
  307. 2 = lightning
  308. 3 = water
  309. 4 = wind
  310. */
  311. short dequeueSpell()
  312. {
  313. short spell;
  314. struct spellQueue *start;
  315. if (spellQueueStart == NULL)
  316. {
  317. return -1;
  318. }
  319. spell = spellQueueStart->spellType;
  320. spellQueueStart = spellQueueStart->next;
  321. /*
  322. free memory
  323. */
  324. free((void *) start);
  325. return spell;
  326. }
  327.  
  328. // initialize spellQueue & spellQueueStart
  329. void initQueue()
  330. {
  331. spellQueueStart = NULL;
  332. spellQueueEnd = NULL;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement