Skip to content

Commit

Permalink
tests/drivers/motor_driver: rework test
Browse files Browse the repository at this point in the history
Rework the test as the motor_driver module has been reworked to be
compliant with RIOT device driver development guide.

Mainly keep same behavior and use a simpler callback to illustrate post
motor_set() callback use.

Signed-off-by: Gilles DOFFE <[email protected]>
  • Loading branch information
gdoffe committed Apr 5, 2024
1 parent 5d957ee commit 3e5dec2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 37 deletions.
8 changes: 4 additions & 4 deletions tests/drivers/motor_driver/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
51 changes: 51 additions & 0 deletions tests/drivers/motor_driver/init_dev.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
*/

#ifndef INIT_DEV_H
#define INIT_DEV_H

#include <motor_driver.h>

#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 */
/** @} */
80 changes: 47 additions & 33 deletions tests/drivers/motor_driver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,36 @@
#include <string.h>

/* 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);
Expand All @@ -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);
}
}
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 3e5dec2

Please sign in to comment.