-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
drivers/motor_driver: rework motor driver #20429
base: master
Are you sure you want to change the base?
Conversation
43ad42f
to
f975209
Compare
f975209
to
6c4dbb8
Compare
tests/drivers/motor_driver/main.c
Outdated
/* set interval to 20 milli-second */ | ||
#define INTERVAL (3000 * US_PER_MS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something does not match
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
tests/drivers/motor_driver/main.c
Outdated
puts("\nDisable motors"); | ||
motor_disable(&motor_driver, MOTOR_0_ID); | ||
motor_disable(&motor_driver, MOTOR_1_ID); | ||
xtimer_periodic_wakeup(&last_wakeup, INTERVAL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
port to ztimer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, done.
tests/drivers/motor_driver/main.c
Outdated
@@ -72,9 +88,9 @@ void motion_control(void) | |||
int8_t dir = 1; | |||
int ret = 0; | |||
xtimer_ticks32_t last_wakeup /*, start*/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xtimer_ticks32_t last_wakeup /*, start*/; | |
xtimer_ticks32_t last_wakeup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/motor_driver/motor_driver.c
Outdated
if ((gpio_is_valid(motor->gpio_dir0)) | ||
&& (gpio_is_valid(motor->gpio_dir1_or_brake))) { | ||
gpio_write(motor->gpio_dir0, direction); | ||
gpio_write(motor->gpio_dir1_or_brake, direction ^ 0x1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the XOR 0x01 seems a little non generic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure to understand, xor is not ok or 0x01
?
I replaced with: direction ^ 1
drivers/include/motor_driver.h
Outdated
@@ -145,59 +135,81 @@ typedef struct { | |||
gpio_t gpio_enable; /**< GPIO to enable/disable motor */ | |||
gpio_t gpio_dir0; /**< GPIO to control rotation direction */ | |||
gpio_t gpio_dir1_or_brake; /**< GPIO to control rotation direction */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gpio_t gpio_dir1_or_brake; /**< GPIO to control rotation direction */ | |
union { | |
gpio_t gpio_dir1; /**< GPIO to control rotation direction */ | |
gpio_t gpio_breake; /**< GPIO to break */ | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
tests/drivers/motor_driver/main.c
Outdated
#include "log.h" | ||
#include "motor_driver.h" | ||
#include "motor_driver_params.h" | ||
#include "test_utils/expect.h" | ||
#include "xtimer.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include "xtimer.h" | |
#include "ztimer.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you push your changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just pushed the fixes, was off for few days, sorry. ;)
drivers/motor_driver/motor_driver.c
Outdated
} | ||
} | ||
|
||
return 0; | ||
/* Set callbacks according to driver type */ | ||
switch(motor_driver_params->mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it make more sense do a switch when the brake or direction function is called instead of storing an extra function pointer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first idea was to make this two callbacks configurables with motor_driver_params_t
. However as the driver is able to support most of existing motor drivers, I did not find really relevant to expose this callbacks inside motor_driver_params_t
.
But I kept them internally, because it allows less lines of code at the price of 2 pointers, indeed.
As I do not have a real opinion about this, I follow your review. 😉
@kfessel thx for review, I added 2 fixup commits related to your comments and few other small things. 😉 |
tests/drivers/motor_driver/Makefile
Outdated
@@ -6,7 +6,8 @@ include ../Makefile.drivers_common | |||
|
|||
USEMODULE += motor_driver | |||
USEMODULE += shell_cmds_default | |||
USEMODULE += xtimer | |||
USEMODULE += ztimer | |||
USEMODULE += ztimer_sec |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
msec might be a better choice - every timer might be off by one count for second -> 3-4 seconds for msec 3000 - 3001 milliseconds
( usually it is a good choice to not wait single digits if you care about the time you wait)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some nitpicks by our static_tests
tests/drivers/motor_driver/main.c
Outdated
if (ret) { | ||
LOG_ERROR("motor_driver_init failed with error code %d\n", ret); | ||
} | ||
expect(ret == 0); | ||
|
||
for (;;) { | ||
while(1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while(1) { | |
while (1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
tests/drivers/motor_driver/main.c
Outdated
|
||
/* set interval to 20 milli-second */ | ||
#define INTERVAL (3000 * US_PER_MS) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/motor_driver/motor_driver.c
Outdated
int state = irq_disable(); | ||
|
||
/* Set direction */ | ||
switch(motor_driver->params->mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch(motor_driver->params->mode) { | |
switch (motor_driver->params->mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/motor_driver/motor_driver.c
Outdated
if (cb) { | ||
cb(motor_driver, motor_id, pwm_duty_cycle); | ||
/* Remove brake */ | ||
switch(motor_driver->params->mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch(motor_driver->params->mode) { | |
switch (motor_driver->params->mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/motor_driver/motor_driver.c
Outdated
int state = irq_disable(); | ||
|
||
/* Apply brake */ | ||
switch(motor_driver->params->mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch(motor_driver->params->mode) { | |
switch (motor_driver->params->mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/motor_driver/motor_driver.c
Outdated
pwm_mode_t mode = motor_driver_conf->pwm_mode; | ||
uint32_t freq = motor_driver_conf->pwm_frequency; | ||
uint16_t resol = motor_driver_conf->pwm_resolution; | ||
int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *motor_driver_params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *motor_driver_params) | |
int motor_driver_init(motor_driver_t *motor_driver, | |
const motor_driver_params_t *motor_driver_params) |
or may be you can shorten one of the params names
int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *motor_driver_params) | |
int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/include/motor_driver.h
Outdated
*/ | ||
int motor_driver_init(const motor_driver_t motor_driver); | ||
int motor_driver_init(motor_driver_t *motor_driver, const motor_driver_params_t *motor_driver_params); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you don't need > 100 chars for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
this has minor API changes |
The motor_driver device driver is developed as a periph driver and it should not. Make this driver compliant with RIOT device driver development guide [1]. Also make some cleanups and fix some typos. [1] https://doc.riot-os.org/driver-guide.html Signed-off-by: Gilles DOFFE <[email protected]>
The motor_driver module has been reworked in a previous commit to be compliant with RIOT device driver guide. Thus declaration in board.h is no more needed and should not work anymore. Moreover the driver test was calling a callback specific to the native architecture to simulate the native QDEC periph driver according to motors speed. This is not relevant as a test should only test the feature it has been developed for. Signed-off-by: Gilles DOFFE <[email protected]>
The motor_driver module has been reworked in a previous commit to be compliant with RIOT device driver guide. Thus declaration in board.h is no more needed and should not work anymore. Signed-off-by: Gilles DOFFE <[email protected]>
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]>
6fff369
to
3e5dec2
Compare
Contribution description
The motor_driver device driver is developed as a periph driver and it should not.
Make this driver compliant with RIOT device driver development guide [1].
Also make some cleanups and fix some typos.
[1] https://doc.riot-os.org/driver-guide.html
Testing procedure
Build for native architecture:
Run the test: