-
Notifications
You must be signed in to change notification settings - Fork 4
/
encoder.c
168 lines (146 loc) · 4.08 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "encoder.h"
/* Difference between the current count and the latest count */
static volatile int32_t left_diff_count;
static volatile int32_t right_diff_count;
/* Total number of counts */
static volatile int32_t left_total_count;
static volatile int32_t right_total_count;
/* Total travelled distance, in micrometers */
static volatile int32_t left_micrometers;
static volatile int32_t right_micrometers;
/* Speed, in meters per second */
static volatile float left_speed;
static volatile float right_speed;
/**
* @brief Read left motor encoder counter difference.
*
* The difference is with respect to the last motor encoder count read.
*/
int32_t get_encoder_left_diff_count(void)
{
return left_diff_count;
}
/**
* @brief Read right motor encoder counter difference.
*
* The difference is with respect to the last motor encoder count read.
*/
int32_t get_encoder_right_diff_count(void)
{
return right_diff_count;
}
/**
* @brief Read left motor encoder total count.
*
* The total count is simply the sum of all encoder counter differences.
*/
int32_t get_encoder_left_total_count(void)
{
return left_total_count;
}
/**
* @brief Read right motor encoder total count.
*
* The total count is simply the sum of all encoder counter differences.
*/
int32_t get_encoder_right_total_count(void)
{
return right_total_count;
}
/**
* @brief Read left motor encoder travelled distance in micrometers.
*/
int32_t get_encoder_left_micrometers(void)
{
return left_micrometers;
}
/**
* @brief Read right motor encoder travelled distance in micrometers.
*/
int32_t get_encoder_right_micrometers(void)
{
return right_micrometers;
}
/**
* @brief Read the average travelled distance in micrometers.
*/
int32_t get_encoder_average_micrometers(void)
{
return (left_micrometers + right_micrometers) / 2;
}
/**
* @brief Read left motor speed in meters per second.
*/
float get_encoder_left_speed(void)
{
return left_speed;
}
/**
* @brief Read right motor speed in meters per second.
*/
float get_encoder_right_speed(void)
{
return right_speed;
}
/**
* @brief Return the most likely counter difference.
*
* When reading an increasing or decreasing counter caution must be taken to:
*
* - Account for counter overflow.
* - Account for counter direction (increasing or decreasing).
*
* This function assumes the most likely situation is to read the counter fast
* enough to ensure that the direction is defined by the minimal difference
* between the two reads (i.e.: readings are much faster than counter
* overflows).
*
* @param[in] now Counter reading now.
* @param[in] before Counter reading before.
*/
int32_t max_likelihood_counter_diff(uint16_t now, uint16_t before)
{
uint16_t forward_diff;
uint16_t backward_diff;
forward_diff = (uint16_t)(now - before);
backward_diff = (uint16_t)(before - now);
if (forward_diff > backward_diff)
return -(int32_t)backward_diff;
return (int32_t)forward_diff;
}
/**
* @brief Update encoder readings.
*
* - Read raw encoder counters.
* - Update the count differences (with respect to latest reading).
* - Calculate distance travelled.
* - Calculate speed.
*/
void update_encoder_readings(void)
{
static uint16_t last_left_count;
static uint16_t last_right_count;
uint16_t left_count;
uint16_t right_count;
float micrometers_per_count;
left_count = read_encoder_left();
right_count = read_encoder_right();
micrometers_per_count = get_micrometers_per_count();
left_diff_count =
max_likelihood_counter_diff(left_count, last_left_count);
right_diff_count =
max_likelihood_counter_diff(right_count, last_right_count);
left_total_count += left_diff_count;
right_total_count += right_diff_count;
left_micrometers = (int32_t)(left_total_count * micrometers_per_count);
right_micrometers =
(int32_t)(right_total_count * micrometers_per_count);
left_speed = left_diff_count *
(micrometers_per_count / MICROMETERS_PER_METER) *
SYSTICK_FREQUENCY_HZ;
right_speed = right_diff_count *
(micrometers_per_count / MICROMETERS_PER_METER) *
SYSTICK_FREQUENCY_HZ;
last_left_count = left_count;
last_right_count = right_count;
}