This commit is contained in:
2025-08-23 10:27:29 +02:00
parent 0bb1b5d026
commit 6c53932267
4 changed files with 74 additions and 55 deletions

View File

@@ -14,7 +14,7 @@
&uart4 { &uart4 {
status = "okay"; status = "okay";
current-speed = <115200>; current-speed = <921600>;
pinctrl-0 = <&uart4_tx_pa0 &uart4_rx_pd0>; pinctrl-0 = <&uart4_tx_pa0 &uart4_rx_pd0>;
pinctrl-names = "default"; pinctrl-names = "default";
}; };
@@ -22,3 +22,16 @@
&usart3 { &usart3 {
status = "okay"; status = "okay";
}; };
&timers1 {
status = "okay";
st,prescaler = <0>; /* Required */
pwm0: pwm {
compatible = "st,stm32-pwm";
status = "okay";
pinctrl-0 = <&tim1_ch1_pa8>;
pinctrl-names = "default";
};
};

View File

@@ -84,11 +84,13 @@ bool zephyr_transport_close(struct uxrCustomTransport * transport){
size_t zephyr_transport_write(struct uxrCustomTransport* transport, const uint8_t * buf, size_t len, uint8_t * err){ size_t zephyr_transport_write(struct uxrCustomTransport* transport, const uint8_t * buf, size_t len, uint8_t * err){
zephyr_transport_params_t * params = (zephyr_transport_params_t*) transport->args; zephyr_transport_params_t * params = (zephyr_transport_params_t*) transport->args;
/*
printf("TX: "); printf("TX: ");
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
printf("%02X ", buf[i]); printf("%02X ", buf[i]);
} }
printf("\n"); printf("\n");
*/
for (size_t i = 0; i < len; i++) for (size_t i = 0; i < len; i++)
{ {
@@ -113,11 +115,13 @@ size_t zephyr_transport_read(struct uxrCustomTransport* transport, uint8_t* buf,
read = ring_buf_get(&in_ringbuf, buf, len); read = ring_buf_get(&in_ringbuf, buf, len);
uart_irq_rx_enable(params->uart_dev); uart_irq_rx_enable(params->uart_dev);
/*
printf("RX: "); printf("RX: ");
for (size_t i = 0; i < read; i++) { for (size_t i = 0; i < read; i++) {
printf("%02X ", buf[i]); printf("%02X ", buf[i]);
} }
printf("\n"); printf("\n");
*/
return read; return read;
} }

View File

@@ -9,13 +9,20 @@ CONFIG_POSIX_API=y
# logging # logging
CONFIG_LOG=y CONFIG_LOG=y
CONFIG_STDOUT_CONSOLE=y
CONFIG_PRINTK=y
CONFIG_LOG_DEFAULT_LEVEL=3
# gpio support # gpio support
CONFIG_GPIO=y CONFIG_GPIO=y
CONFIG_LED=y
# usb # usb
CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_INTERRUPT_DRIVEN=y
# pwm
CONFIG_PWM=y
CONFIG_PWM_STM32=y
# micro ros # micro ros
CONFIG_MICROROS=y CONFIG_MICROROS=y

View File

@@ -5,18 +5,16 @@
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/devicetree.h> #include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio.h>
#include <zephyr/posix/time.h>
#else #else
#include <zephyr.h> #include <zephyr.h>
#include <device.h> #include <device.h>
#include <devicetree.h> #include <devicetree.h>
#include <drivers/gpio.h> #include <drivers/gpio.h>
#include <posix/time.h>
#endif #endif
#include <rcl/rcl.h> #include <rcl/rcl.h>
#include <rcl/error_handling.h> #include <rcl/error_handling.h>
#include <std_msgs/msg/int32.h> #include <std_msgs/msg/bool.h>
#include <rclc/rclc.h> #include <rclc/rclc.h>
#include <rclc/executor.h> #include <rclc/executor.h>
@@ -27,69 +25,66 @@
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc);for(;;){};}} #define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc);for(;;){};}}
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}} #define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}}
rcl_publisher_t publisher; rcl_subscription_t subscriber;
std_msgs__msg__Int32 msg;
void timer_callback(rcl_timer_t * timer, int64_t last_call_time) std_msgs__msg__Bool msg;
static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
void led0_callback(const void *msgin)
{ {
RCLC_UNUSED(last_call_time); const std_msgs__msg__Bool *msg = (const std_msgs__msg__Bool *)msgin;
if (timer != NULL) {
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL)); gpio_pin_configure_dt(&led0, msg->data ? GPIO_OUTPUT_ACTIVE : GPIO_OUTPUT_INACTIVE);
printk("msg: %d", msg.data++);
}
} }
int main(void) int main(void)
{ {
rmw_uros_set_custom_transport( if (!device_is_ready(led0.port)) {
MICRO_ROS_FRAMING_REQUIRED, printk("LED not ready\n");
(void *) &default_params, return -1;
zephyr_transport_open, }
zephyr_transport_close,
zephyr_transport_write,
zephyr_transport_read
);
rcl_allocator_t allocator = rcl_get_default_allocator(); /* micro-ROS transport setup */
rclc_support_t support; rmw_uros_set_custom_transport(
MICRO_ROS_FRAMING_REQUIRED,
(void *)&default_params,
zephyr_transport_open,
zephyr_transport_close,
zephyr_transport_write,
zephyr_transport_read
);
// create init_options rcl_allocator_t allocator = rcl_get_default_allocator();
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator)); rclc_support_t support;
// create node RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
rcl_node_t node;
RCCHECK(rclc_node_init_default(&node, "zephyr_int32_publisher", "", &support));
// create publisher rcl_node_t node;
RCCHECK(rclc_publisher_init_default( RCCHECK(rclc_node_init_default(&node, "zephyr_servo_node", "", &support));
&publisher,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
"zephyr_int32_publisher"));
// create timer, /* Create subscriber for /servo/position */
rcl_timer_t timer; RCCHECK(rclc_subscription_init_default(
const unsigned int timer_timeout = 1000; &subscriber,
RCCHECK(rclc_timer_init_default( &node,
&timer, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Bool),
&support, "/nucleo/led0"
RCL_MS_TO_NS(timer_timeout), ));
timer_callback));
// create executor /* Create executor */
rclc_executor_t executor; rclc_executor_t executor;
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator)); RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
RCCHECK(rclc_executor_add_timer(&executor, &timer)); RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &msg, &led0_callback, ON_NEW_DATA));
msg.data = 0; /* Spin executor loop */
while (1) {
rclc_executor_spin_some(&executor, 100);
k_sleep(K_MSEC(10));
}
while(1){ /* Free resources (unreachable in infinite loop) */
rclc_executor_spin_some(&executor, 100); RCCHECK(rcl_subscription_fini(&subscriber, &node))
usleep(100000); RCCHECK(rcl_node_fini(&node))
}
// free resources return 0;
RCCHECK(rcl_publisher_fini(&publisher, &node))
RCCHECK(rcl_node_fini(&node))
return 0;
} }