micro-ROS / micro-ROS/micro_ros_stm32cubemx_utils

What about adding UDP transport?

Open
#133 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
275
Forks
100
PR merge metrics
No merged PRs in 30d

Description

Hello Microros or ROS2 Community,
I recently tried deploying micro-ROS on STM32H743. The project requires me to establish a connection with the Agent using STM32 over the network. Unfortunately, I couldn't find relevant information online, so I attempted to use LWIP to write a custom transport.I imitated the writing style of the custom transport in this repository and referred to the micro-ROS tutorial to write my own `udp_transport.c`. **Surprisingly, it worked successfully.** I hope the developers will consider submitting UDP transport as an option.It's worth noting that when using UDP, modifications are required in the content of `simple_main.c`.
```
rmw_uros_set_custom_transport(
false, //Framing disable here. Udp should Use Packet-oriented mode.
"192.168.1.121", //your agent's ip address
cubemx_transport_open,
cubemx_transport_close,
cubemx_transport_write,
cubemx_transport_read);
```
Detail in `udp_transport.c`:
```
#include

#include

#include "main.h"
#include "cmsis_os.h"

#include
#include
#include
#include

// --- LWIP ---
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/api.h"
#include

#ifdef RMW_UXRCE_TRANSPORT_CUSTOM

// --- micro-ROS Transports ---
#define UDP_PORT 8888
static int sock_fd = -1;

bool cubemx_transport_open(struct uxrCustomTransport * transport){
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(UDP_PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
{
return false;
}

return true;
}

bool cubemx_transport_close(struct uxrCustomTransport * transport){
if (sock_fd != -1)
{
closesocket(sock_fd);
sock_fd = -1;
}
return true;
}

size_t cubemx_transport_write(struct uxrCustomTransport* transport, uint8_t * buf, size_t len, uint8_t * err){
if (sock_fd == -1)
{
return 0;
}
const char * ip_addr = (const char*) transport->args;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(UDP_PORT);
addr.sin_addr.s_addr = inet_addr(ip_addr);
int ret = 0;
ret = sendto(sock_fd, buf, len, 0, (struct sockaddr *)&addr, sizeof(addr));
size_t writed = ret>0? ret:0;

return writed;
}

size_t cubemx_transport_read(struct uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* err){

struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
int ret = 0;
//set timeout
struct timeval tv_out;
tv_out.tv_sec = 0;
tv_out.tv_usec = timeout * 1000;
setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO,&tv_out, sizeof(tv_out));
//Use non-block mode
ret = recv(sock_fd, buf, len, MSG_DONTWAIT);
size_t readed = ret>0? ret:0;
return readed;
}

#endif
```

Contributor guide

Open the contributing guide

Research direction

Start with the custom transport example in udp_transport.c and the simple_main.c call to rmw_uros_set_custom_transport, then compare them with the repository's existing STM32CubeMX transport setup. Check the micro-ROS custom transport tutorial and LWIP socket usage first. Done means UDP is available as a documented, supported transport option for the STM32CubeMX utility.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot, networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.