diff --git a/tests/drivers/motor_driver/Makefile b/tests/drivers/motor_driver/Makefile index ae186a3e580a..2399a6706b24 100644 --- a/tests/drivers/motor_driver/Makefile +++ b/tests/drivers/motor_driver/Makefile @@ -1,13 +1,13 @@ BOARD ?= native +INCLUDES += -I$(APPDIR) + include ../Makefile.drivers_common USEMODULE += motor_driver USEMODULE += shell_cmds_default -USEMODULE += xtimer - -FEATURES_REQUIRED += periph_qdec -FEATURES_REQUIRED += motor_driver +USEMODULE += ztimer +USEMODULE += ztimer_msec CFLAGS += -DLOG_LEVEL=LOG_DEBUG CFLAGS += -DDEBUG_ASSERT_VERBOSE diff --git a/tests/drivers/motor_driver/init_dev.h b/tests/drivers/motor_driver/init_dev.h new file mode 100644 index 000000000000..19cecafccb48 --- /dev/null +++ b/tests/drivers/motor_driver/init_dev.h @@ -0,0 +1,51 @@ + /* + * Copyright (C) 2023 COGIP Robotics association + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Motor driver test header file. + * + * @author Gilles DOFFE + * + */ + +#ifndef INIT_DEV_H +#define INIT_DEV_H + +#include + +#ifndef MOTOR_DRIVER_PARAM_MOTOR_SET_POST_CALLBACK +/** Default callback called at end of motor_set() */ +#define MOTOR_DRIVER_PARAM_MOTOR_SET_POST_CALLBACK motor_driver_callback_example +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Simple motor_set post callback example. + * Just print current PWM value. + * + * @param[in] motor_driver motor driver to which motor is attached + * @param[in] motor_id motor ID on driver + * @param[in] pwm_duty_cycle Signed PWM duty_cycle to set motor speed and direction + */ +void motor_driver_callback_example( + const motor_driver_t *motor_driver, uint8_t motor_id, + int32_t pwm_duty_cycle); + +#ifdef __cplusplus +} +#endif + +#endif /* INIT_DEV_H */ +/** @} */ diff --git a/tests/drivers/motor_driver/main.c b/tests/drivers/motor_driver/main.c index 5f47bd97bd18..c9079341642f 100644 --- a/tests/drivers/motor_driver/main.c +++ b/tests/drivers/motor_driver/main.c @@ -21,20 +21,36 @@ #include /* RIOT includes */ +#include "init_dev.h" #include "log.h" #include "motor_driver.h" +#include "motor_driver_params.h" #include "test_utils/expect.h" -#include "xtimer.h" +#include "time_units.h" +#include "ztimer.h" -/* set interval to 20 milli-second */ -#define INTERVAL (3000 * US_PER_MS) +void motor_driver_callback_example( + const motor_driver_t *motor_driver, uint8_t motor_id, + int32_t pwm_duty_cycle) +{ + LOG_DEBUG("MOTOR-DRIVER=%p" \ + " MOTOR_ID = %"PRIu8 \ + " PWM_VALUE = %"PRIi32"\n", \ + (void*)motor_driver, motor_id, + pwm_duty_cycle); +} + +/* Set interval to 3 seconds */ +#define INTERVAL (3 * MS_PER_SEC) #define MOTOR_0_ID 0 #define MOTOR_1_ID 1 +static motor_driver_t motor_driver; + void motors_control(int32_t duty_cycle) { - char str[6]; + char str[4]; if (duty_cycle >= 0) { strncpy(str, "CW", 3); @@ -43,26 +59,26 @@ void motors_control(int32_t duty_cycle) strncpy(str, "CCW", 4); } - printf("Duty cycle = %" PRId32 " Direction = %s\n", duty_cycle, str); + puts("\nActuate Motors"); - 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, MOTOR_0_ID, duty_cycle)) { + printf("Cannot set PWM duty cycle for motor %" PRIu8 "\n", \ + 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); + if (motor_set(&motor_driver, MOTOR_1_ID, duty_cycle)) { + printf("Cannot set PWM duty cycle for motor %" PRIu8 "\n", \ + MOTOR_1_ID); } } void motors_brake(void) { - puts("Brake motors !!!"); + puts("\nBrake motors"); - if (motor_brake(MOTOR_DRIVER_DEV(0), MOTOR_0_ID)) { + if (motor_brake(&motor_driver, MOTOR_0_ID)) { printf("Cannot brake motor %" PRIu32 "\n", (uint32_t)MOTOR_0_ID); } - if (motor_brake(MOTOR_DRIVER_DEV(0), MOTOR_1_ID)) { + if (motor_brake(&motor_driver, MOTOR_1_ID)) { printf("Cannot brake motor %" PRIu32 "\n", (uint32_t)MOTOR_1_ID); } } @@ -71,42 +87,40 @@ 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; + int32_t pwm_res = motor_driver_params->pwm_resolution; - ret = motor_driver_init(MOTOR_DRIVER_DEV(0)); + ret = motor_driver_init(&motor_driver, &motor_driver_params[0]); if (ret) { LOG_ERROR("motor_driver_init failed with error code %d\n", ret); } expect(ret == 0); - for (;;) { + while (1) { /* BRAKE - duty cycle 100% */ - last_wakeup = xtimer_now(); motors_brake(); - xtimer_periodic_wakeup(&last_wakeup, INTERVAL); + ztimer_sleep(ZTIMER_MSEC, INTERVAL); - /* CW - duty cycle 50% */ - last_wakeup = xtimer_now(); + ///* CW - duty cycle 50% */ motors_control(dir * pwm_res / 2); - xtimer_periodic_wakeup(&last_wakeup, INTERVAL); + ztimer_sleep(ZTIMER_MSEC, 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); + //feature */ + puts("\nDisable motors"); + motor_disable(&motor_driver, MOTOR_0_ID); + motor_disable(&motor_driver, MOTOR_1_ID); + ztimer_sleep(ZTIMER_MSEC, INTERVAL); + puts("\nEnable motors"); + motor_enable(&motor_driver, MOTOR_0_ID); + motor_enable(&motor_driver, MOTOR_1_ID); + ztimer_sleep(ZTIMER_MSEC, INTERVAL); /* CW - duty cycle 100% */ - last_wakeup = xtimer_now(); motors_control(dir * pwm_res); - xtimer_periodic_wakeup(&last_wakeup, INTERVAL); + ztimer_sleep(ZTIMER_MSEC, INTERVAL); /* Reverse direction */ - dir = dir * -1; + dir *= -1; } }