ReflexBall Rally
 All Data Structures Files Functions Variables Macros
math.c
Go to the documentation of this file.
1 #include <eZ8.h> // special encore constants, macros and flash routines
2 #include <sio.h> // special encore serial i/o routines
3 #include "math.h"
4 #include "lut.h"
5 
6 long expand(long input) { // Convert an 18.14 to 16.16
7  return input << 2;
8 }
9 
10 void printNumber(long input, unsigned char decimalBits, unsigned char decimal) { // Print a fixed point number in any format
11  int i;
12  unsigned long pow = 10, mask = 0xFFFFFFFF, result, output;
13 
14  if ((input & 0x80000000) != 0) {
15  printf("-");
16  input = -input;
17  }
18 
19  if (decimal > 5)
20  decimal = 5;
21 
22  for (i=1;i<decimal;i++)
23  pow *= 10;
24 
25  mask >>= (32 - decimalBits);
26  result = pow * (unsigned long)(input & mask);
27  output = result >> decimalBits;
28  output += (result >> (decimalBits-1)) & 0x1; // Round correctly
29 
30  printf("%ld.%0*ld", input >> decimalBits, decimal, output);
31 }
32 
33 void printFix(long input, unsigned char decimal) { // Used to print an 18.14 fixed point number
34  printNumber(input,FIX14_SHIFT,decimal);
35 }
36 
37 long sin(int val) { // sin()
38  return SIN[val & 0x1FF];
39 }
40 
41 long cos(int val) { // cos()
42  return sin(val + 128);
43 }
44 
45 void initVector(TVector* v, long x, long y) { // Initialize a vector
46  v->x = x << FIX14_SHIFT;
47  v->y = y << FIX14_SHIFT;
48 }
49 
50 void rotate(TVector* v, int val) { // Rotate a vector from 512-0 equal to 360-0 degrees
51  long sinVal, cosVal, tempX;
52  sinVal = sin(val);
53  cosVal = cos(val);
54  tempX = v->x;
55 
56  v->x = FIX14_MULT(v->x,cosVal) - FIX14_MULT(v->y,sinVal);
57  v->y = FIX14_MULT(tempX,sinVal) + FIX14_MULT(v->y,cosVal);
58 }
59 
60 void printVector(TVector* v) { // Print a vector
61  printf("(");
62  printFix(v->x,4);
63  printf(",");
64  printFix(v->y,4);
65  printf(")\n");
66 }