Skip to content

Commit 01ee38e

Browse files
authored
Fix RP2040 WiFi (#7)
* Update Signed-off-by: Pablo Garrido <[email protected]> * Update Signed-off-by: Pablo Garrido <[email protected]> * Update Signed-off-by: Pablo Garrido <[email protected]>
1 parent 154ab16 commit 01ee38e

File tree

6 files changed

+115
-28
lines changed

6 files changed

+115
-28
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
platform: [teensy41, teensy40, teensy36, teensy35, teensy31, due, zero, olimex_e407, esp32dev, nanorp2040connect, portenta_h7_m7, teensy41_eth, nanorp2040connect_eth, portenta_h7_m7_wifi, esp32dev_wifi]
17+
platform: [teensy41, teensy40, teensy36, teensy35, teensy31, due, zero, olimex_e407, esp32dev, nanorp2040connect, portenta_h7_m7, teensy41_eth, nanorp2040connect_wifi, portenta_h7_m7_wifi, esp32dev_wifi]
1818

1919
steps:
2020
- uses: actions/checkout@v3

ci/platformio.ini

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ lib_deps =
100100
arduino-libraries/Ethernet@^2.0.0
101101
SPI
102102

103-
[env:nanorp2040connect_eth]
104-
platform = raspberrypi
105-
board = nanorp2040connect
106-
framework = arduino
107-
microros_transport = native_ethernet
108-
lib_deps =
109-
../
110-
arduino-libraries/Ethernet@^2.0.0
111-
SPI
112-
113103
; WiFi platforms
114104

115105
[env:portenta_h7_m7_wifi]
@@ -132,4 +122,14 @@ microros_transport = wifi
132122
lib_deps =
133123
../
134124
arduino-libraries/WiFi@^1.2.7
125+
SPI
126+
127+
[env:nanorp2040connect_wifi]
128+
platform = raspberrypi
129+
board = nanorp2040connect
130+
framework = arduino
131+
microros_transport = wifi_nina
132+
lib_deps =
133+
../
134+
arduino-libraries/WiFiNINA@^1.8.13
135135
SPI

ci/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void setup() {
5252
size_t agent_port = 8888;
5353

5454
set_microros_native_ethernet_transports(local_mac, local_ip, agent_ip, agent_port);
55-
#elif defined(MICRO_ROS_TRANSPORT_ARDUINO_WIFI)
55+
#elif defined(MICRO_ROS_TRANSPORT_ARDUINO_WIFI) || defined(MICRO_ROS_TRANSPORT_ARDUINO_WIFI_NINA)
5656
IPAddress agent_ip(192, 168, 1, 113);
5757
size_t agent_port = 8888;
5858

platform_code/arduino/wifi/micro_ros_transport.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22
#include <WiFiUdp.h>
33

44
struct micro_ros_agent_locator {
5-
IPAddress address;
6-
int port;
5+
IPAddress address;
6+
int port;
77
};
88

99
static inline void set_microros_wifi_transports(char * ssid, char * pass, IPAddress agent_ip, uint16_t agent_port){
10-
WiFi.begin(ssid, pass);
11-
12-
while (WiFi.status() != WL_CONNECTED) {
10+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
1311
delay(500);
1412
}
1513

16-
static struct micro_ros_agent_locator locator;
17-
locator.address = agent_ip;
18-
locator.port = agent_port;
14+
static struct micro_ros_agent_locator locator;
15+
locator.address = agent_ip;
16+
locator.port = agent_port;
1917

20-
rmw_uros_set_custom_transport(
21-
false,
22-
(void *) &locator,
23-
platformio_transport_open,
24-
platformio_transport_close,
25-
platformio_transport_write,
26-
platformio_transport_read
27-
);
18+
rmw_uros_set_custom_transport(
19+
false,
20+
(void *) &locator,
21+
platformio_transport_open,
22+
platformio_transport_close,
23+
platformio_transport_write,
24+
platformio_transport_read
25+
);
2826
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <Arduino.h>
2+
3+
#include <micro_ros_platformio.h>
4+
5+
#include <WiFi.h>
6+
#include <WiFiUdp.h>
7+
8+
#include <uxr/client/util/time.h>
9+
#include <uxr/client/profile/transport/custom/custom_transport.h>
10+
11+
extern "C" {
12+
13+
static WiFiUDP udp_client;
14+
15+
bool platformio_transport_open(struct uxrCustomTransport * transport)
16+
{
17+
struct micro_ros_agent_locator * locator = (struct micro_ros_agent_locator *) transport->args;
18+
return true == udp_client.begin(locator->port);
19+
}
20+
21+
bool platformio_transport_close(struct uxrCustomTransport * transport)
22+
{
23+
udp_client.stop();
24+
return true;
25+
}
26+
27+
size_t platformio_transport_write(struct uxrCustomTransport * transport, const uint8_t *buf, size_t len, uint8_t *errcode)
28+
{
29+
(void)errcode;
30+
31+
struct micro_ros_agent_locator *locator = (struct micro_ros_agent_locator *)transport->args;
32+
33+
size_t sent = 0;
34+
if(true == udp_client.beginPacket(locator->address, locator->port)){
35+
sent = udp_client.write(buf, len);
36+
sent = true == udp_client.endPacket() ? sent : 0;
37+
}
38+
39+
udp_client.flush();
40+
41+
return sent;
42+
}
43+
44+
size_t platformio_transport_read(struct uxrCustomTransport * transport, uint8_t *buf, size_t len, int timeout, uint8_t *errcode)
45+
{
46+
(void)errcode;
47+
48+
int64_t start_time = uxr_millis();
49+
50+
while ((uxr_millis() - start_time) < ((int64_t)timeout) && udp_client.parsePacket() == 0) {
51+
delay(1);
52+
}
53+
54+
size_t available = 0;
55+
if(udp_client.available()){
56+
available = udp_client.read(buf, len);
57+
}
58+
59+
return (available < 0) ? 0 : available;
60+
}
61+
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <SPI.h>
2+
#include <WiFiNINA.h>
3+
#include <WiFiUdp.h>
4+
5+
struct micro_ros_agent_locator {
6+
IPAddress address;
7+
int port;
8+
};
9+
10+
static inline void set_microros_wifi_transports(char * ssid, char * pass, IPAddress agent_ip, uint16_t agent_port){
11+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
12+
delay(500);
13+
}
14+
15+
static struct micro_ros_agent_locator locator;
16+
locator.address = agent_ip;
17+
locator.port = agent_port;
18+
19+
rmw_uros_set_custom_transport(
20+
false,
21+
(void *) &locator,
22+
platformio_transport_open,
23+
platformio_transport_close,
24+
platformio_transport_write,
25+
platformio_transport_read
26+
);
27+
}

0 commit comments

Comments
 (0)