Subversion Repositories EngineBay2

Rev

Details | Last modification | View Log | RSS feed

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