From 37cfd17de6d0735e7f2a130f671e102daa8adbe1 Mon Sep 17 00:00:00 2001 From: Gilles DOFFE Date: Tue, 16 Oct 2018 15:09:13 +0200 Subject: [PATCH] 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 --- tests/driver_motor_driver/Makefile | 16 +++++ tests/driver_motor_driver/README.md | 22 ++++++ tests/driver_motor_driver/main.c | 101 ++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 tests/driver_motor_driver/Makefile create mode 100644 tests/driver_motor_driver/README.md create mode 100644 tests/driver_motor_driver/main.c diff --git a/tests/driver_motor_driver/Makefile b/tests/driver_motor_driver/Makefile new file mode 100644 index 0000000000..649264d831 --- /dev/null +++ b/tests/driver_motor_driver/Makefile @@ -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 diff --git a/tests/driver_motor_driver/README.md b/tests/driver_motor_driver/README.md new file mode 100644 index 0000000000..e5fc5fd434 --- /dev/null +++ b/tests/driver_motor_driver/README.md @@ -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. diff --git a/tests/driver_motor_driver/main.c b/tests/driver_motor_driver/main.c new file mode 100644 index 0000000000..82f80a4898 --- /dev/null +++ b/tests/driver_motor_driver/main.c @@ -0,0 +1,101 @@ +#include +#include + +/* RIOT includes */ +#include +#include +#include + +/* 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; +}