mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
tests: add motor_driver test
Test motor_driver API by making 2 motors turn clockwise and counter-clockwise alternatively. Speed varies from 0 to max PWM duty cycle. Signed-off-by: Gilles DOFFE <g.doffe@gmail.com>
This commit is contained in:
parent
3fd3eea5bc
commit
37cfd17de6
16
tests/driver_motor_driver/Makefile
Normal file
16
tests/driver_motor_driver/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
BOARD ?= native
|
||||
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += motor_driver
|
||||
USEMODULE += shell
|
||||
USEMODULE += shell_commands
|
||||
USEMODULE += xtimer
|
||||
|
||||
FEATURES_REQUIRED += periph_qdec
|
||||
FEATURES_REQUIRED += motor_driver
|
||||
|
||||
CFLAGS += -DLOG_LEVEL=LOG_DEBUG
|
||||
CFLAGS += -DDEBUG_ASSERT_VERBOSE
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
22
tests/driver_motor_driver/README.md
Normal file
22
tests/driver_motor_driver/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
Motor driver test
|
||||
======
|
||||
|
||||
Background
|
||||
------
|
||||
|
||||
Test for the high level `motor_driver` driver.
|
||||
|
||||
Expected result
|
||||
------
|
||||
|
||||
Should do an infinite loop :
|
||||
1) Both motors should turn at half pwm duty cycle speed in clowkwise
|
||||
direction.
|
||||
2) Both motors should turn at full pwm duty cycle speed in clowkwise
|
||||
direction.
|
||||
3) Both motors should brake.
|
||||
4) Both motors should turn at half pwm duty cycle speed in counter
|
||||
clowkwise direction.
|
||||
5) Both motors should turn at full pwm duty cycle speed in counter
|
||||
clowkwise direction.
|
||||
6) Both motors should brake.
|
101
tests/driver_motor_driver/main.c
Normal file
101
tests/driver_motor_driver/main.c
Normal file
@ -0,0 +1,101 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* RIOT includes */
|
||||
#include <log.h>
|
||||
#include <motor_driver.h>
|
||||
#include <xtimer.h>
|
||||
|
||||
/* set interval to 20 milli-second */
|
||||
#define INTERVAL (3000 * US_PER_MS)
|
||||
|
||||
#define MOTOR_0_ID 0
|
||||
#define MOTOR_1_ID 1
|
||||
|
||||
|
||||
void motors_control(int32_t duty_cycle)
|
||||
{
|
||||
char str[6];
|
||||
|
||||
if (duty_cycle >= 0) {
|
||||
strncpy(str, "CW", 3);
|
||||
}
|
||||
else {
|
||||
strncpy(str, "CCW", 4);
|
||||
}
|
||||
|
||||
printf("Duty cycle = %" PRId32 " Direction = %s\n", duty_cycle, str);
|
||||
|
||||
if (motor_set(MOTOR_DRIVER_DEV(0), MOTOR_0_ID, duty_cycle)) {
|
||||
printf("Cannot set PWM duty cycle for motor %" PRIu32 "\n", \
|
||||
(uint32_t)MOTOR_0_ID);
|
||||
}
|
||||
if (motor_set(MOTOR_DRIVER_DEV(0), MOTOR_1_ID, duty_cycle)) {
|
||||
printf("Cannot set PWM duty cycle for motor %" PRIu32 "\n", \
|
||||
(uint32_t)MOTOR_1_ID);
|
||||
}
|
||||
}
|
||||
|
||||
void motors_brake(void)
|
||||
{
|
||||
puts("Brake motors !!!");
|
||||
|
||||
if (motor_brake(MOTOR_DRIVER_DEV(0), MOTOR_0_ID)) {
|
||||
printf("Cannot brake motor %" PRIu32 "\n", (uint32_t)MOTOR_0_ID);
|
||||
}
|
||||
if (motor_brake(MOTOR_DRIVER_DEV(0), MOTOR_1_ID)) {
|
||||
printf("Cannot brake motor %" PRIu32 "\n", (uint32_t)MOTOR_1_ID);
|
||||
}
|
||||
}
|
||||
|
||||
void motion_control(void)
|
||||
{
|
||||
int8_t dir = 1;
|
||||
int ret = 0;
|
||||
xtimer_ticks32_t last_wakeup /*, start*/;
|
||||
int32_t pwm_res = motor_driver_config[MOTOR_DRIVER_DEV(0)].pwm_resolution;
|
||||
|
||||
ret = motor_driver_init(MOTOR_DRIVER_DEV(0));
|
||||
if (ret) {
|
||||
LOG_ERROR("motor_driver_init failed with error code %d\n", ret);
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
for (;;) {
|
||||
/* BRAKE - duty cycle 100% */
|
||||
last_wakeup = xtimer_now();
|
||||
motors_brake();
|
||||
xtimer_periodic_wakeup(&last_wakeup, INTERVAL);
|
||||
|
||||
/* CW - duty cycle 50% */
|
||||
last_wakeup = xtimer_now();
|
||||
motors_control(dir * pwm_res / 2);
|
||||
xtimer_periodic_wakeup(&last_wakeup, INTERVAL);
|
||||
|
||||
/* Disable motor during INTERVAL µs (motor driver must have enable
|
||||
feature */
|
||||
last_wakeup = xtimer_now();
|
||||
motor_disable(MOTOR_DRIVER_DEV(0), MOTOR_0_ID);
|
||||
motor_disable(MOTOR_DRIVER_DEV(0), MOTOR_1_ID);
|
||||
xtimer_periodic_wakeup(&last_wakeup, INTERVAL);
|
||||
motor_enable(MOTOR_DRIVER_DEV(0), MOTOR_0_ID);
|
||||
motor_enable(MOTOR_DRIVER_DEV(0), MOTOR_1_ID);
|
||||
|
||||
/* CW - duty cycle 100% */
|
||||
last_wakeup = xtimer_now();
|
||||
motors_control(dir * pwm_res);
|
||||
xtimer_periodic_wakeup(&last_wakeup, INTERVAL);
|
||||
|
||||
/* Reverse direction */
|
||||
dir = dir * -1;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
xtimer_init();
|
||||
|
||||
motion_control();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user