amiro-os / unittests / periphery-lld / src / ut_alld_a3906.c @ 47680f67
History | View | Annotate | Download (12.2 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2018 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
#include <ut_alld_a3906.h> |
20 |
|
21 |
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_A3906)) || defined(__DOXYGEN__) |
22 |
|
23 |
#include <aos_debug.h> |
24 |
#include <chprintf.h> |
25 |
#include <alld_a3906.h> |
26 |
#include <aos_thread.h> |
27 |
#include <stdlib.h> |
28 |
#include <math.h> |
29 |
|
30 |
/**
|
31 |
* @brief Interval to poll QEI in certain tests.
|
32 |
*/
|
33 |
#define QEI_POLL_INTERVAL_MS 10 |
34 |
|
35 |
/**
|
36 |
* @brief Threshold for QEI differences.
|
37 |
* @details Differences smaller tahn or equal to this value are neglected (interpreted as zero).
|
38 |
*/
|
39 |
#define QEI_DIFF_THRESHOLD 4 |
40 |
|
41 |
/**
|
42 |
* @brief Enumerator to distinguish between left and right wheel.
|
43 |
*/
|
44 |
typedef enum { |
45 |
WHEEL_LEFT = 0, /**< left wheel identifier */ |
46 |
WHEEL_RIGHT = 1, /**< right wheel identifier */ |
47 |
} wheel_t; |
48 |
|
49 |
/**
|
50 |
* @brief Enumerator to distinguish directions.
|
51 |
*/
|
52 |
typedef enum { |
53 |
DIRECTION_FORWARD = 0, /**< forward direction identifier */ |
54 |
DIRECTION_BACKWARD = 1, /**< backward direction identifier */ |
55 |
} direction_t; |
56 |
|
57 |
/**
|
58 |
* @brief helper function to test each wheel and direction separately.
|
59 |
*
|
60 |
* @param[in] stream Stream for input/output.
|
61 |
* @param[in] data Unit test meta data.
|
62 |
* @param[in] wheel Wheel to test.
|
63 |
* @param[in] direction Direction to test.
|
64 |
* @param[in,out] result Result variable to modify.
|
65 |
*/
|
66 |
void _wheelDirectionTest(BaseSequentialStream* stream, ut_a3906data_t* data, wheel_t wheel, direction_t direction, aos_utresult_t* result)
|
67 |
{ |
68 |
// local variables
|
69 |
uint32_t status; |
70 |
bool qei_valid;
|
71 |
apalQEICount_t qei_count[2];
|
72 |
apalQEIDirection_t qei_direction; |
73 |
uint32_t timeout_counter; |
74 |
|
75 |
chprintf(stream, "%s wheel %s...\n", (wheel == WHEEL_LEFT) ? "left" : "right", (direction == DIRECTION_FORWARD) ? "forward" : "backward"); |
76 |
qei_valid = false;
|
77 |
status = apalQEIGetPosition((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_count[0]);
|
78 |
// increase PWM incrementally and read QEI data
|
79 |
for (apalPWMwidth_t pwm_width = APAL_PWM_WIDTH_MIN; pwm_width < APAL_PWM_WIDTH_MAX; ++pwm_width) {
|
80 |
status |= a3906_lld_set_pwm(data->pwm.driver, (wheel == WHEEL_LEFT) ? |
81 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.left_forward : data->pwm.channel.left_backward) : |
82 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.right_forward : data->pwm.channel.right_backward), |
83 |
pwm_width); |
84 |
status |= apalQEIGetPosition((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_count[1]);
|
85 |
qei_valid = qei_valid || (qei_count[0] != qei_count[1]); |
86 |
aosThdUSleep(5 * MICROSECONDS_PER_SECOND / (APAL_PWM_WIDTH_MAX - APAL_PWM_WIDTH_MIN));
|
87 |
qei_count[0] = qei_count[1]; |
88 |
} |
89 |
status |= qei_valid ? 0x00 : 0x10; |
90 |
status |= apalQEIGetDirection((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_direction); |
91 |
status |= (qei_direction == ((direction == DIRECTION_FORWARD) ? APAL_QEI_DIRECTION_UP : APAL_QEI_DIRECTION_DOWN)) ? 0x00 : 0x20; |
92 |
|
93 |
// let the wheel spin free until it stops
|
94 |
status |= a3906_lld_set_pwm(data->pwm.driver, (wheel == WHEEL_LEFT) ? |
95 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.left_forward : data->pwm.channel.left_backward) : |
96 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.right_forward : data->pwm.channel.right_backward), |
97 |
0);
|
98 |
qei_count[0] = 0; |
99 |
qei_count[1] = 0; |
100 |
timeout_counter = 0;
|
101 |
do {
|
102 |
status |= apalQEIGetPosition((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_count[0]);
|
103 |
aosThdMSleep(1);
|
104 |
status |= apalQEIGetPosition((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_count[1]);
|
105 |
++timeout_counter; |
106 |
} while ((qei_count[0] != qei_count[1]) && (timeout_counter * MICROSECONDS_PER_MILLISECOND <= data->timeout)); |
107 |
status |= (timeout_counter * MICROSECONDS_PER_MILLISECOND > data->timeout) ? APAL_STATUS_TIMEOUT : 0x00;
|
108 |
|
109 |
// report result
|
110 |
if (status == APAL_STATUS_SUCCESS) {
|
111 |
aosUtPassed(stream, result); |
112 |
} else {
|
113 |
aosUtFailedMsg(stream, result, "0x%08X\n", status);
|
114 |
} |
115 |
|
116 |
return;
|
117 |
} |
118 |
|
119 |
void _wheelSpeedTest(BaseSequentialStream* stream, ut_a3906data_t* data, wheel_t wheel, direction_t direction, aos_utresult_t* result)
|
120 |
{ |
121 |
// local variables
|
122 |
uint32_t status; |
123 |
apalQEICount_t qei_range; |
124 |
apalQEICount_t qei_count[2] = {0}; |
125 |
apalQEICount_t qei_increments[2] = {0}; |
126 |
apalQEICount_t qei_increments_diff = 0;
|
127 |
uint32_t timeout_counter = 0;
|
128 |
uint32_t stable_counter = 0;
|
129 |
|
130 |
chprintf(stream, "%s wheel full speed %s...\n", (wheel == WHEEL_LEFT) ? "left" : "right", (direction == DIRECTION_FORWARD) ? "forward" : "backward"); |
131 |
// spin up the wheel with full speed
|
132 |
status = apalQEIGetRange((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_range); |
133 |
status |= a3906_lld_set_pwm(data->pwm.driver, (wheel == WHEEL_LEFT) ? |
134 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.left_forward : data->pwm.channel.left_backward) : |
135 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.right_forward : data->pwm.channel.right_backward), |
136 |
APAL_PWM_WIDTH_MAX); |
137 |
aosThdMSleep(100);
|
138 |
do {
|
139 |
// read QEI data to determine speed
|
140 |
status |= apalQEIGetPosition((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_count[0]);
|
141 |
aosThdMSleep(QEI_POLL_INTERVAL_MS); |
142 |
status |= apalQEIGetPosition((wheel == WHEEL_LEFT) ? data->qei.left : data->qei.right, &qei_count[1]);
|
143 |
timeout_counter += QEI_POLL_INTERVAL_MS; |
144 |
qei_increments[0] = qei_increments[1]; |
145 |
qei_increments[1] = (direction == DIRECTION_FORWARD) ?
|
146 |
((qei_count[1] > qei_count[0]) ? (qei_count[1] - qei_count[0]) : (qei_count[1] + (qei_range - qei_count[0]))) : |
147 |
((qei_count[0] > qei_count[1]) ? (qei_count[0] - qei_count[1]) : (qei_count[0] + (qei_range - qei_count[1]))); |
148 |
qei_increments_diff = abs((int32_t)qei_increments[0] - (int32_t)qei_increments[1]); |
149 |
stable_counter = (qei_increments[1] != 0 && qei_increments_diff <= QEI_DIFF_THRESHOLD) ? stable_counter+1 : 0; |
150 |
} while ((stable_counter* QEI_POLL_INTERVAL_MS < MILLISECONDS_PER_SECOND) && (timeout_counter * MICROSECONDS_PER_MILLISECOND <= data->timeout));
|
151 |
status |= a3906_lld_set_pwm(data->pwm.driver, (wheel == WHEEL_LEFT) ? |
152 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.left_forward : data->pwm.channel.left_backward) : |
153 |
((direction == DIRECTION_FORWARD) ? data->pwm.channel.right_forward : data->pwm.channel.right_backward), |
154 |
APAL_PWM_WIDTH_OFF); |
155 |
status |= (timeout_counter * MICROSECONDS_PER_MILLISECOND > data->timeout) ? APAL_STATUS_TIMEOUT : 0x00;
|
156 |
|
157 |
// report results
|
158 |
if (status == APAL_STATUS_SUCCESS) {
|
159 |
aosUtPassed(stream, result); |
160 |
const float tps = qei_increments[1] * ((float)MILLISECONDS_PER_SECOND / (float)QEI_POLL_INTERVAL_MS); |
161 |
const float rpm = tps * SECONDS_PER_MINUTE / (float)data->qei.increments_per_revolution; |
162 |
const float velocity = tps / (float)data->qei.increments_per_revolution * ((wheel == WHEEL_LEFT) ? data->wheel_diameter.left : data->wheel_diameter.right) * acos(-1); |
163 |
chprintf(stream, "\t%f tps\n", tps);
|
164 |
chprintf(stream, "\t%f RPM\n", rpm);
|
165 |
chprintf(stream, "\t%f m/s\n", velocity);
|
166 |
chprintf(stream, "\n");
|
167 |
} |
168 |
else {
|
169 |
aosUtFailedMsg(stream, result, "0x%08X\n", status);
|
170 |
} |
171 |
} |
172 |
|
173 |
/**
|
174 |
* @brief A3905 unit test function.
|
175 |
*
|
176 |
* @param[in] stream Stream for input/output.
|
177 |
* @param[in] ut Unit test object.
|
178 |
*
|
179 |
* @return Unit test result value.
|
180 |
*/
|
181 |
aos_utresult_t utAlldA3906Func(BaseSequentialStream* stream, aos_unittest_t* ut) |
182 |
{ |
183 |
aosDbgCheck((ut->data != NULL) &&
|
184 |
(((ut_a3906data_t*)ut->data)->driver != NULL) &&
|
185 |
(((ut_a3906data_t*)ut->data)->pwm.driver != NULL) &&
|
186 |
(((ut_a3906data_t*)ut->data)->qei.left != NULL) &&
|
187 |
(((ut_a3906data_t*)ut->data)->qei.right != NULL));
|
188 |
|
189 |
|
190 |
|
191 |
// local variables
|
192 |
aos_utresult_t result = {0, 0}; |
193 |
uint32_t status = 0;
|
194 |
a3906_lld_power_t power_state; |
195 |
apalQEICount_t qei_count[2][2]; |
196 |
uint32_t timeout_counter; |
197 |
uint32_t stable_counter; |
198 |
|
199 |
chprintf(stream, "enable power...\n");
|
200 |
power_state = A3906_LLD_POWER_ON; |
201 |
status = a3906_lld_set_power(((ut_a3906data_t*)ut->data)->driver, power_state); |
202 |
status |= a3906_lld_get_power(((ut_a3906data_t*)ut->data)->driver, &power_state); |
203 |
status |= (power_state != A3906_LLD_POWER_ON) ? 0x10 : 0x00; |
204 |
if (status == APAL_STATUS_SUCCESS) {
|
205 |
aosUtPassed(stream, &result); |
206 |
} else {
|
207 |
aosUtFailedMsg(stream, &result, "0x%08X\n", status);
|
208 |
} |
209 |
|
210 |
_wheelDirectionTest(stream, (ut_a3906data_t*)ut->data, WHEEL_LEFT, DIRECTION_FORWARD, &result); |
211 |
|
212 |
_wheelDirectionTest(stream, (ut_a3906data_t*)ut->data, WHEEL_RIGHT, DIRECTION_FORWARD, &result); |
213 |
|
214 |
_wheelDirectionTest(stream, (ut_a3906data_t*)ut->data, WHEEL_LEFT, DIRECTION_BACKWARD, &result); |
215 |
|
216 |
_wheelDirectionTest(stream, (ut_a3906data_t*)ut->data, WHEEL_RIGHT, DIRECTION_BACKWARD, &result); |
217 |
|
218 |
_wheelSpeedTest(stream, (ut_a3906data_t*)ut->data, WHEEL_LEFT, DIRECTION_FORWARD, &result); |
219 |
|
220 |
_wheelSpeedTest(stream, (ut_a3906data_t*)ut->data, WHEEL_RIGHT, DIRECTION_FORWARD, &result); |
221 |
|
222 |
_wheelSpeedTest(stream, (ut_a3906data_t*)ut->data, WHEEL_LEFT, DIRECTION_BACKWARD, &result); |
223 |
|
224 |
_wheelSpeedTest(stream, (ut_a3906data_t*)ut->data, WHEEL_RIGHT, DIRECTION_BACKWARD, &result); |
225 |
|
226 |
chprintf(stream, "disable power... \n");
|
227 |
power_state = A3906_LLD_POWER_OFF; |
228 |
status = a3906_lld_set_power(((ut_a3906data_t*)ut->data)->driver, power_state); |
229 |
status |= a3906_lld_get_power(((ut_a3906data_t*)ut->data)->driver, &power_state); |
230 |
status |= (power_state != A3906_LLD_POWER_OFF) ? 0x10 : 0x00; |
231 |
qei_count[WHEEL_LEFT][0] = 0; |
232 |
qei_count[WHEEL_LEFT][1] = 0; |
233 |
qei_count[WHEEL_RIGHT][0] = 0; |
234 |
qei_count[WHEEL_RIGHT][1] = 0; |
235 |
timeout_counter = 0;
|
236 |
stable_counter = 0;
|
237 |
do {
|
238 |
status |= apalQEIGetPosition(((ut_a3906data_t*)ut->data)->qei.left, &qei_count[WHEEL_LEFT][0]);
|
239 |
status |= apalQEIGetPosition(((ut_a3906data_t*)ut->data)->qei.right, &qei_count[WHEEL_RIGHT][0]);
|
240 |
aosThdMSleep(1);
|
241 |
status |= apalQEIGetPosition(((ut_a3906data_t*)ut->data)->qei.left, &qei_count[WHEEL_LEFT][1]);
|
242 |
status |= apalQEIGetPosition(((ut_a3906data_t*)ut->data)->qei.right, &qei_count[WHEEL_RIGHT][1]);
|
243 |
++timeout_counter; |
244 |
stable_counter = (qei_count[WHEEL_LEFT][0] == qei_count[WHEEL_LEFT][1] && qei_count[WHEEL_RIGHT][0] == qei_count[WHEEL_RIGHT][1]) ? stable_counter+1 : 0; |
245 |
} while((stable_counter < 100) && (timeout_counter * MICROSECONDS_PER_MILLISECOND <= ((ut_a3906data_t*)ut->data)->timeout)); |
246 |
status |= (timeout_counter * MICROSECONDS_PER_MILLISECOND > ((ut_a3906data_t*)ut->data)->timeout) ? APAL_STATUS_TIMEOUT : 0x00;
|
247 |
if (status == APAL_STATUS_SUCCESS) {
|
248 |
aosUtPassed(stream, &result); |
249 |
} else {
|
250 |
aosUtFailedMsg(stream, &result, "0x%08X\n", status);
|
251 |
} |
252 |
|
253 |
// stop the PWM
|
254 |
a3906_lld_set_pwm(((ut_a3906data_t*)ut->data)->pwm.driver, ((ut_a3906data_t*)ut->data)->pwm.channel.left_forward, APAL_PWM_WIDTH_OFF); |
255 |
a3906_lld_set_pwm(((ut_a3906data_t*)ut->data)->pwm.driver, ((ut_a3906data_t*)ut->data)->pwm.channel.left_backward, APAL_PWM_WIDTH_OFF); |
256 |
a3906_lld_set_pwm(((ut_a3906data_t*)ut->data)->pwm.driver, ((ut_a3906data_t*)ut->data)->pwm.channel.right_forward, APAL_PWM_WIDTH_OFF); |
257 |
a3906_lld_set_pwm(((ut_a3906data_t*)ut->data)->pwm.driver, ((ut_a3906data_t*)ut->data)->pwm.channel.right_backward, APAL_PWM_WIDTH_OFF); |
258 |
|
259 |
aosUtInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(A3906Driver)); |
260 |
|
261 |
return result;
|
262 |
} |
263 |
|
264 |
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_A3906) */ |