Rev 30 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 30 | Rev 46 | ||
|---|---|---|---|
| Line 7... | Line 7... | ||
| 7 | */ |
7 | */ |
| 8 | #include <math.h> |
8 | #include <math.h> |
| 9 | #include "stm32l1xx_hal.h" |
9 | #include "stm32l1xx_hal.h" |
| 10 | #include "ap_math.h" |
10 | #include "ap_math.h" |
| 11 | /* this is an approximate maths library where all the answers are accurate enough */ |
11 | /* this is an approximate maths library where all the answers are accurate enough */ |
| 12 | static uint8_t sintab[90]; |
12 | static uint8_t sintab[SINE_STEPS]; |
| 13 | void ap_init(void) |
13 | void ap_init(void) |
| 14 | { |
14 | { |
| 15 | uint8_t i; |
15 | uint16_t i; |
| 16 | for(i=0;i<90;i++) |
16 | for(i=0;i<SINE_STEPS;i++) |
| 17 | { |
17 | { |
| 18 | sintab[i]=sin(i/180.0*3.14159)*AP_K; |
18 | sintab[i]=sin(i/(SINE_STEPS*2.0)*3.14159)*AP_K; |
| 19 | } |
19 | } |
| 20 | } |
20 | } |
| 21 | 21 | ||
| 22 | /* returns 255 * sin(ang) where ang is in degrees */ |
22 | /* returns 255 * sin(ang) where ang is in degrees */ |
| 23 | int ap_sin(int ang) |
23 | int ap_sin(int ang) |
| 24 | { |
24 | { |
| 25 | /* wrap into range */ |
25 | /* wrap into range */ |
| 26 | while(ang<0) |
26 | while(ang<0) |
| 27 | { |
27 | { |
| 28 | ang+= 360; |
28 | ang+= SINE_STEPS * 4; |
| 29 | } |
29 | } |
| 30 | while(ang>=360) |
30 | while(ang>=SINE_STEPS * 4) |
| 31 | { |
31 | { |
| 32 | ang-= 360; |
32 | ang-= SINE_STEPS * 4; |
| 33 | } |
33 | } |
| 34 | 34 | ||
| 35 | if(ang>=0 && ang<90) |
35 | if(ang>=0 && ang<SINE_STEPS) |
| 36 | { |
36 | { |
| 37 | return(sintab[ang]); |
37 | return(sintab[ang]); |
| 38 | } |
38 | } |
| 39 | else |
39 | else |
| 40 | if(ang==90) |
40 | if(ang==SINE_STEPS) |
| 41 | { |
41 | { |
| 42 | return AP_K; |
42 | return AP_K; |
| 43 | } |
43 | } |
| 44 | else |
44 | else |
| 45 | if(ang>90 && ang<=180) |
45 | if(ang>SINE_STEPS && ang<=SINE_STEPS*2) |
| 46 | { |
46 | { |
| 47 | return(sintab[180-ang]); |
47 | return(sintab[SINE_STEPS*2-ang]); |
| 48 | } |
48 | } |
| 49 | else |
49 | else |
| 50 | if(ang==270) |
50 | if(ang==3*SINE_STEPS) |
| 51 | { |
51 | { |
| 52 | return -AP_K; |
52 | return -AP_K; |
| 53 | } |
53 | } |
| 54 | if(ang>180 && ang< 270) |
54 | if(ang>SINE_STEPS *2 && ang< SINE_STEPS*3) |
| 55 | { |
55 | { |
| 56 | return(-sintab[ang-180]); |
56 | return(-sintab[ang-SINE_STEPS*2]); |
| 57 | } |
57 | } |
| 58 | else |
58 | else |
| 59 | /* ang > 270 and ang < 360 */ |
59 | /* ang > 270 and ang < 360 */ |
| 60 | { |
60 | { |
| 61 | return(-sintab[360-ang]); |
61 | return(-sintab[SINE_STEPS*4-ang]); |
| 62 | } |
62 | } |
| 63 | } |
63 | } |
| 64 | 64 | ||
| 65 | /* returns 255 * cos(ang) where ang is in degrees */ |
65 | /* returns 255 * cos(ang) where ang is in degrees */ |
| 66 | int ap_cos(int ang) |
66 | int ap_cos(int ang) |
| 67 | { |
67 | { |
| 68 | return ap_sin(90-ang); |
68 | return ap_sin(SINE_STEPS-ang); |
| 69 | } |
69 | } |
| 70 | 70 | ||
| 71 | 71 | ||