aparently working, more test is needed

This commit is contained in:
Miguel I. 2025-09-08 22:07:54 +02:00
parent 33c5f4654c
commit ac9e564d6a
2 changed files with 16 additions and 11 deletions

View File

@ -5,8 +5,7 @@
#include <zephyr/logging/log.h>
#include <zephyr/device.h>
#include <zephyr/drivers/counter.h>
LOG_MODULE_REGISTER(actuator, LOG_LEVEL_INF);
#include <stdlib.h>
/* === GPIOs from devicetree === */
#define USER_NODE DT_PATH(zephyr_user)
@ -48,7 +47,7 @@ static void pwm_isr(const struct device *dev,
static int configure_gpio(void)
{
if (!device_is_ready(do1.port) || !device_is_ready(do2.port) || !device_is_ready(do_en.port)) {
LOG_ERR("GPIO device not ready");
printk("GPIO device not ready");
return -ENODEV;
}
@ -64,7 +63,7 @@ static void start_counter(uint32_t us)
int err = counter_get_value(counter_dev, &now_ticks);
if (err)
{
LOG_ERR("Failed to get counter value (%d)", err);
printk("Failed to get counter value (%d)", err);
return;
}
@ -76,7 +75,12 @@ static void start_counter(uint32_t us)
alarm_cfg.callback = pwm_isr;
alarm_cfg.user_data = NULL;
counter_set_channel_alarm(counter_dev, 0, &alarm_cfg);
err = counter_set_channel_alarm(counter_dev, 0, &alarm_cfg);
if (err)
{
printk("Failed to set channel alarm (%d)", err);
return;
}
}
/* === ISR callback === */
@ -146,7 +150,7 @@ int actuator_init(enum actuator_mode mode)
if (ret) return ret;
if (!device_is_ready(counter_dev)) {
LOG_ERR("Counter device not ready");
printk("Counter device not ready");
return -ENODEV;
}
@ -155,7 +159,7 @@ int actuator_init(enum actuator_mode mode)
current_mode = mode;
initialized = true;
active_phase = false;
LOG_INF("Actuator initialized in mode %d", mode);
printk("Actuator initialized in mode %d", mode);
return 0;
}
@ -174,7 +178,7 @@ int actuator_motor_set_speed(int speed_percent)
motor_speed = speed_percent;
active_phase = false;
start_counter(1); /* trigger ISR soon */
start_counter(100); /* trigger ISR soon */
return 0;
}
@ -186,6 +190,6 @@ int actuator_servo_set_angle(int angle_deg)
servo_angle = angle_deg;
active_phase = false;
start_counter(1); /* trigger ISR soon */
start_counter(100); /* trigger ISR soon */
return 0;
}

View File

@ -27,10 +27,11 @@ int main(void)
return 0;
}
actuator_init(ACTUATOR_MODE_MOTOR);
actuator_init(ACTUATOR_MODE_SERVO);
actuator_enable(1);
actuator_motor_set_speed(50); // -100..100 %
actuator_servo_set_angle(100);
//actuator_motor_set_speed(-50); // -100..100 %
/* Init modules */
led_init();
button_init();