-
Notifications
You must be signed in to change notification settings - Fork 12
/
encoder.c
executable file
·91 lines (84 loc) · 1.95 KB
/
encoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "encoder.h"
#include "comm_usb.h"
#include "ch.h"
#include "hal.h"
#include "as5x45.h"
#include "as5x47.h"
#include <math.h>
#include "config.h"
static volatile Config *config;
void encoder_init(void)
{
config = config_get_configuration();
switch(config->encoderType) {
case AS5045B:
case AS5145B:
as5x45_init(config->encoderType);
break;
case AS5x47P:
as5x47_init();
break;
case HALL:
break;
case SENSORLESS:
break;
default:
break;
}
}
/* Returns rotor electrical degrees */
float encoder_get_angle(void)
{
float edeg = 0;
float deg = 0;
switch(config->encoderType) {
case AS5045B:
case AS5145B:
deg = as5x45_get_angle() - config->encoderZero;
if (deg < 0)
{
deg += 360.0;
}
if (config->encoderInverted)
deg = 360 - deg;
edeg = fmod(deg, 360.0f / config->polePairs) * config->polePairs;
break;
case AS5x47P:
deg = as5x47_get_angle() - config->encoderZero;
if (deg < 0)
{
deg += 360.0;
}
if (config->encoderInverted)
deg = 360 - deg;
edeg = fmod(deg, 360.0f / config->polePairs) * config->polePairs;
break;
case HALL:
break;
case SENSORLESS:
break;
default:
break;
}
return edeg;
}
float encoder_get_raw_angle(void)
{
float deg = 0;
switch(config->encoderType) {
case AS5045B:
case AS5145B:
deg = as5x45_get_raw_position();
break;
case AS5x47P:
deg = as5x47_get_last_angle();
break;
case HALL:
break;
case SENSORLESS:
break;
default:
break;
}
return deg;
}