Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- /*
- global constants
- */
- static unsigned FRAMEWAITTIME = 10;
- static unsigned POLYWAITTIME = 20; // time in ms must be unsigned to be passed to sleep()
- static double MAXPOLYDEV = 30; // angle in degrees
- /*
- angle tolerances in degrees used when there should be no movement in that direction
- */
- static double ANGLETOLHEAD = 15;
- static double ANGLETOLROLL = 15;
- static double ANGLETOLPITCH = 15;
- /*
- multiplier used when movement exists in that direction
- */
- static double ANGLETOLMULTIPLIER = 0.25; // y = a*(1+x)
- static double MINDRAWLEN = 1.8; // in meters
- static double MINDRAWLENLIGHT = 0.3; // in meters
- static double POLYANGLETRI = 60;
- static double POLYANGLELIGHT = 150;
- static double POLYANGLESQUARE = 90;
- /*
- values of each type
- 0 = earth
- 1 = fire
- 2 = lightning
- 3 = water
- 4 = wind
- */
- static short EARTH = 0;
- static short FIRE = 1;
- static short LIGHTNING = 2;
- static short WATER = 3;
- static short WIND = 4;
- static short NOTCIRCLE = 5;
- static short ERRORVAL = -1;
- /*
- struct
- */
- typedef struct runeClassStruct
- {
- double init_heading;
- double init_roll;
- double init_pitch;
- bool isCircle;
- bool isLightning;
- } runeClassDataAlias;
- typedef struct spellQueueStruct
- {
- struct spellQueueStruct *next;
- /*
- spellType
- 0 = earth
- 1 = fire
- 2 = lightning
- 3 = water
- 4 = wind
- other = various errors
- */
- short spellType;
- } spellQueueAlias;
- /*
- global variables
- */
- /*
- stores current spell type
- 0 = earth
- 1 = fire
- 2 = lightning
- 3 = water
- 4 = wind
- other = various errors
- */
- extern double currTotalLength;
- extern short currSpellType;
- extern bool isCasting;
- /*
- we can use pointers for these as they start as NULL
- */
- extern spellQueueAlias *spellQueueStart; // other files read spells from this guy
- extern spellQueueAlias *spellQueueEnd; // end of queue
- /*
- we can't use pointers here because we
- */
- extern runeClassDataAlias runeClassData; // other files access this global struct
- extern struct bnoeul initEulStruct; // stores initial orientation
- extern struct bnoeul currEulStruct; // stores current orientation
- extern struct bnolin currLinStruct; // stores current velocity
- /*
- function declarations
- */
- extern int main();
- extern short checkCircle(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
- extern bool checkLightning(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
- extern short classifyShape(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
- extern double getVelocity(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
- extern void calibrateImu();
- extern bool isValidSpell(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr);
- extern void enqueueSpell(short spell);
- extern short dequeueSpell(); // returns -1 on error
- extern void initQueue(); // initialize spellQueueStart
- /*
- source code
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <time.h>
- #include <unistd.h>
- #include <getbno055.h>
- #include <imumodule.h>
- #include <errno.h>
- int main()
- {
- bool isCircle = false;
- bool isLightning = false;
- int retval = 0;
- /*
- get initial hrp values
- passes to global pointer
- */
- calibrateImu();
- /*
- main loop
- */
- while (isCasting)
- {
- sleep(FRAMEWAITTIME);
- retval = get_eul(&currEulStruct);
- // error getting euler orientation
- if (retval < 0)
- {
- return retval;
- }
- retval = get_lin(&currLinStruct);
- // error getting linear acceleration
- if (retval < 0)
- {
- return retval;
- }
- currSpellType = classifyShape(&currEulStruct, &currLinStruct);
- }
- return retval;
- }
- /*
- checks if rune is a Horizontal or Vertical circle
- must pass pointers bnoeulptr and bnolinptr that are already filled
- waits POLYWAITTIME; if angle change is > MAXPOLYWAITTIME
- returns WIND if wind (horizontal circle)
- returns WATER if water (vertical circle)
- returns NOTCIRCLE otherwise
- */
- short checkCircle(struct bnoeul *starteulptr, struct bnolin *bnolinptr)
- {
- int errval;
- double final_h;
- double final_r;
- double final_p;
- struct bnoeul *finaleulptr;
- short retval = NOTCIRCLE;
- double init_h = initEulStruct.eul_head;
- double init_r = initEulStruct.eul_roll;
- double init_p = initEulStruct.eul_pitc;
- double start_h = starteulptr->eul_head;
- double start_r = starteulptr->eul_roll;
- double start_p = starteulptr->eul_pitc;
- clock_t time_start = clock();
- // allocate memory for struct
- finaleulptr = (struct bnoeul *) malloc(sizeof(struct bnoeul));
- // wait POLYWAITTIME
- sleep(POLYWAITTIME);
- errval = get_eul(finaleulptr);
- if (errval < 0)
- {
- retval = ERRORVAL;
- }
- else if (final_h - start_h > MAXPOLYDEV)
- {
- retval = WIND;
- }
- else if (final_r - start_r > MAXPOLYDEV)
- {
- retval = WATER;
- }
- // free memory after casting to void*
- free((void *)finaleulptr);
- return retval;
- }
- /*
- checks if rune is for lightning
- */
- bool checkLightning(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr)
- {
- double init_h = initEulStruct.eul_head;
- double init_r = initEulStruct.eul_roll;
- double init_p = initEulStruct.eul_pitc;
- double h = bnoeulptr->eul_head;
- double r = bnoeulptr->eul_roll;
- double p = bnoeulptr->eul_pitc;
- if (init_r - r < ANGLETOLROLL)
- {
- return false;
- }
- else if (init_h - h < ANGLETOLHEAD)
- {
- return false;
- }
- else if (init_p - p < ANGLETOLPITCH)
- {
- return false;
- }
- }
- short classifyShape(struct bnoeul *bnoeulptr, struct bnolin *bnolinptr)
- {
- short circleType = 0;
- bool isLightning;
- circleType = checkCircle(bnoeulptr, bnolinptr);
- isLightning = checkLightning(bnoeulptr, bnolinptr);
- if (circleType == WATER)
- {
- currSpellType = WATER;
- return currSpellType;
- }
- else if (circleType == WIND)
- {
- currSpellType = WIND;
- return currSpellType;
- }
- else if (isLightning)
- {
- currSpellType = LIGHTNING;
- return currSpellType;
- }
- return currSpellType;
- }
- double getDistance(clock_t start, clock_t end, struct bnoeul *bnoeulptr, struct bnolin *bnolinptr)
- {
- double acc_x = bnolinptr->linacc_x;
- double acc_y = bnolinptr->linacc_y;
- double acc_z = bnolinptr->linacc_z;
- }
- void calibrateImu()
- {
- int retval = 0;
- bno_reset();
- retval = get_eul(&initEulStruct);
- }
- /*
- add spell to end of list: FIFO
- */
- void enqueueSpell(short spell)
- {
- /*
- allocate memory
- */
- struct spellQueueStruct *newNode = (struct spellQueueStruct*) malloc(sizeof(struct spellQueueStruct));
- /*
- fill in data of new struct
- */
- newNode->next = NULL;
- newNode->spellType = spell;
- /*
- if adding to empty list set values
- */
- if (spellQueueStart == NULL)
- {
- spellQueueStart = newNode;
- spellQueueEnd = newNode;
- }
- else
- {
- /*
- update next pointer of last element that existed before
- */
- spellQueueEnd->next = newNode;
- /*
- update last pointer
- */
- spellQueueEnd = newNode;
- }
- }
- /*
- remove spell from start of list: FIFO
- returns -1 on error
- 0 = earth
- 1 = fire
- 2 = lightning
- 3 = water
- 4 = wind
- */
- short dequeueSpell()
- {
- short spell;
- struct spellQueue *start;
- if (spellQueueStart == NULL)
- {
- return -1;
- }
- spell = spellQueueStart->spellType;
- spellQueueStart = spellQueueStart->next;
- /*
- free memory
- */
- free((void *) start);
- return spell;
- }
- // initialize spellQueue & spellQueueStart
- void initQueue()
- {
- spellQueueStart = NULL;
- spellQueueEnd = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement