Skip to content

Commit d785b53

Browse files
Add support for Arduino Due and Arduino Zero (#35)
* Add Arduino Due support * Add binary * Add support * Improve arduino_serial_platform_read * Add working example * Revert example * Revert example * New line * Add CI * Updates Co-authored-by: Darko Lukic <lukicdarkoo@gmail.com>
1 parent e5c9d15 commit d785b53

16 files changed

+445
-11
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
with:
1717
path: checkout/
1818

19-
- name: Build OpenCR and Teensy 3.2/4.1
19+
- name: Build
2020
run: |
2121
apt update
2222
apt install -y git curl lib32z1 wget libfontconfig libxft2 xz-utils rsync
@@ -28,21 +28,34 @@ jobs:
2828
- https://raw.githubusercontent.com/ROBOTIS-GIT/OpenCR/master/arduino/opencr_release/package_opencr_index.json''' > arduino-cli.yaml
2929
export PATH=$PATH:/github/workspace/bin:/__w/micro_ros_arduino/micro_ros_arduino/bin
3030
arduino-cli core install OpenCR:OpenCR -v
31+
arduino-cli core install arduino:samd -v
32+
arduino-cli core install arduino:sam -v
33+
#
34+
# INSTALLING TEENSY SUPPORT
3135
wget https://downloads.arduino.cc/arduino-1.8.13-linux64.tar.xz
3236
tar -xf arduino-1.8.13-linux64.tar.xz
3337
wget https://www.pjrc.com/teensy/td_153/TeensyduinoInstall.linux64
3438
chmod 755 TeensyduinoInstall.linux64
3539
./TeensyduinoInstall.linux64 --dir=arduino-1.8.13
36-
# Faking loader
40+
# Faking Teensy loader
3741
TRUE_PATH=$(which true)
3842
rm -rf arduino-1.8.13/hardware/tools/teensy_post_compile
3943
cp $TRUE_PATH arduino-1.8.13/hardware/tools/teensy_post_compile
4044
cp -R arduino-1.8.13/hardware/teensy/ /github/home/.arduino15/packages/
4145
rsync -a arduino-1.8.13/hardware/tools/ /github/home/.arduino15/packages/tools/
42-
rm -rf arduino-1.8.13
43-
curl https://raw.githubusercontent.com/micro-ROS/micro_ros_arduino/foxy/extras/library_generation/platform_teensy.txt > /github/home/.arduino15/packages/teensy/avr/platform.txt
46+
rm -rf arduino-1.8.13 arduino-1.8.13-linux64.tar.xz
47+
#
48+
# PATCHING TEENSY AND SAM
49+
cat checkout/extras/patching_boards/platform_teensy.txt > /github/home/.arduino15/packages/teensy/avr/platform.txt
50+
# remove when https://github.com/arduino/ArduinoCore-sam/pull/115 merged
51+
cat checkout/extras/patching_boards/platform_arduinocore_sam.txt > /github/home/.arduino15/packages/arduino/hardware/sam/1.6.12/platform.txt
52+
#
53+
# BUILDING EXAPLE FOR EACH PLATFORM
4454
arduino-cli core update-index
4555
arduino-cli lib update-index
4656
arduino-cli compile --fqbn OpenCR:OpenCR:OpenCR /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
4757
arduino-cli compile --fqbn teensy:avr:teensy31 /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
4858
arduino-cli compile --fqbn teensy:avr:teensy41 /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
59+
arduino-cli compile --fqbn arduino:samd:arduino_zero_native /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
60+
arduino-cli compile --fqbn arduino:sam:arduino_due_x /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
61+
arduino-cli compile --fqbn arduino:sam:arduino_due_x /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_custom_transport -v

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ Supported boards are:
1212
| [Teensy 4.1](https://www.pjrc.com/store/teensy41.html) | v1.8.5 | Supported | [Based on Teensyduino](https://www.pjrc.com/teensy/td_download.html) | `colcon.meta` |
1313
| [Teensy 3.2/3.1](https://www.pjrc.com/store/teensy32.html) | v1.8.5 | Supported | [Based on Teensyduino](https://www.pjrc.com/teensy/td_download.html) | `colcon_lowmem.meta` |
1414

15+
Community contributed boards are:
16+
17+
| Board | Min version | Contributor | Details | Prebuild meta |
18+
| ----------------------------------------------------- | ----------- | ------------ | ------- | ------------------------ |
19+
| [Arduino Due](https://store.arduino.cc/arduino-due) | - | @lukicdarkoo | | `colcon_verylowmem.meta` |
20+
| [Arduino Zero](https://store.arduino.cc/arduino-zero) | - | @lukicdarkoo | | `colcon_verylowmem.meta` |
21+
22+
1523
You can find the available precompiled ROS 2 types for messages and services in [available_ros2_types](available_ros2_types).
1624

1725
## How to use the precompiled library
@@ -42,14 +50,25 @@ docker run -it --rm -v $(pwd):/arduino_project microros/micro_ros_arduino_builde
4250
```
4351
Note that folders added to `extras/library_generation/extra_packages` and entries added to `extras/library_generation/extra_packages/extra_packages.repos` will be taken into account by this build system.
4452

45-
## Patch Teensyduino
53+
## Patch Arduino board for support precompiled libraries
54+
### Patch Teensyduino
4655

4756
Go inside your Arduino + Teensyduino installation and replace `platform.txt`:
4857

4958
```bash
5059
export ARDUINO_PATH=[Your Arduino + Teensiduino path]
5160
cd $ARDUINO_PATH/hardware/teensy/avr/
52-
curl https://raw.githubusercontent.com/micro-ROS/micro_ros_arduino/foxy/extras/library_generation/platform_teensy.txt > platform.txt
61+
curl https://raw.githubusercontent.com/micro-ROS/micro_ros_arduino/foxy/extras/patching_boards/platform_teensy.txt > platform.txt
62+
```
63+
64+
### Patch SAMD
65+
66+
Go inside your Arduino + Teensyduino installation and replace `platform.txt`:
67+
68+
```bash
69+
export ARDUINO_PATH=[Your Arduino path]
70+
cd $ARDUINO_PATH/hardware/sam/1.6.12/
71+
curl https://raw.githubusercontent.com/micro-ROS/micro_ros_arduino/foxy/extras/patching_boards/platform_arduinocore_sam.txt > platform.txt
5372
```
5473

5574
## Purpose of the Project
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <micro_ros_arduino.h>
2+
3+
#include <stdio.h>
4+
#include <rcl/rcl.h>
5+
#include <rcl/error_handling.h>
6+
#include <rclc/rclc.h>
7+
#include <rclc/executor.h>
8+
9+
#include <std_msgs/msg/int32.h>
10+
11+
rcl_publisher_t publisher;
12+
std_msgs__msg__Int32 msg;
13+
rclc_executor_t executor;
14+
rclc_support_t support;
15+
rcl_allocator_t allocator;
16+
rcl_node_t node;
17+
rcl_timer_t timer;
18+
19+
#define LED_PIN 13
20+
21+
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
22+
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
23+
24+
25+
void error_loop(){
26+
while(1){
27+
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
28+
delay(100);
29+
}
30+
}
31+
32+
void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
33+
{
34+
RCLC_UNUSED(last_call_time);
35+
if (timer != NULL) {
36+
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
37+
msg.data++;
38+
}
39+
}
40+
41+
void setup() {
42+
43+
pinMode(LED_PIN, OUTPUT);
44+
digitalWrite(LED_PIN, HIGH);
45+
46+
delay(2000);
47+
48+
allocator = rcl_get_default_allocator();
49+
50+
//create init_options
51+
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
52+
53+
// create node
54+
node = rcl_get_zero_initialized_node();
55+
RCCHECK(rclc_node_init_default(&node, "micro_ros_arduino_node", "", &support));
56+
57+
// create publisher
58+
RCCHECK(rclc_publisher_init_default(
59+
&publisher,
60+
&node,
61+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
62+
"micro_ros_arduino_node_publisher"));
63+
64+
// create timer,
65+
timer = rcl_get_zero_initialized_timer();
66+
const unsigned int timer_timeout = 1000;
67+
RCCHECK(rclc_timer_init_default(
68+
&timer,
69+
&support,
70+
RCL_MS_TO_NS(timer_timeout),
71+
timer_callback));
72+
73+
// create executor
74+
executor = rclc_executor_get_zero_initialized_executor();
75+
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
76+
77+
unsigned int rcl_wait_timeout = 100; // in ms
78+
RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));
79+
RCCHECK(rclc_executor_add_timer(&executor, &timer));
80+
81+
msg.data = 0;
82+
}
83+
84+
void loop() {
85+
delay(100);
86+
RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
87+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <Arduino.h>
2+
3+
// This is a working example of how to use micro-ROS with user defined transports.
4+
// These functions should be filled with custom transport code and they will replace
5+
// the weak functions defined in the custom transport in the library.
6+
7+
extern "C"
8+
{
9+
#include <stdio.h>
10+
#include <stdbool.h>
11+
#include <sys/time.h>
12+
13+
bool arduino_serial_platform_open()
14+
{
15+
// Place here your initialization platform code
16+
// Return true if success
17+
Serial2.begin(115200);
18+
return true;
19+
}
20+
21+
bool arduino_serial_platform_close()
22+
{
23+
// Place here your closing platform code
24+
// Return true if success
25+
return true;
26+
}
27+
28+
size_t arduino_serial_platform_write(uint8_t *buf, size_t len, uint8_t *errcode)
29+
{
30+
// Place here your writing bytes platform code
31+
// Return number of bytes written
32+
(void)errcode;
33+
size_t sent = Serial2.write(buf, len);
34+
return sent;
35+
}
36+
37+
size_t arduino_serial_platform_read(uint8_t *buf, size_t len, int timeout, uint8_t *errcode)
38+
{
39+
// Place here your reading bytes platform code
40+
// Return number of bytes read (max bytes: len)
41+
(void)errcode;
42+
Serial2.setTimeout(timeout);
43+
return Serial2.readBytes((char *)buf, len);
44+
}
45+
}

extras/library_generation/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ RUN git clone https://github.com/micro-ROS/micro-ros-build.git src/micro-ros-bui
1818
&& rm -rf gcc-arm-none-eabi-5_4-2016q2-20160622-linux.tar.bz2 gcc-arm-none-eabi-5_4-2016q2/share/doc \
1919
&& wget https://launchpad.net/gcc-arm-embedded/5.0/5-2016-q3-update/+download/gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 \
2020
&& tar -xvf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 \
21-
&& rm -rf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 gcc-arm-none-eabi-5_4-2016q3/share/doc
21+
&& rm -rf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 gcc-arm-none-eabi-5_4-2016q3/share/doc \
22+
&& wget https://launchpad.net/gcc-arm-embedded/4.8/4.8-2014-q1-update/+download/gcc-arm-none-eabi-4_8-2014q1-20140314-linux.tar.bz2 \
23+
&& tar -xvf gcc-arm-none-eabi-4_8-2014q1-20140314-linux.tar.bz2 \
24+
&& rm -rf gcc-arm-none-eabi-4_8-2014q1-20140314-linux.tar.bz2 gcc-arm-none-eabi-4_8-2014q1/share/doc
2225

2326
COPY ./entrypoint.sh /entrypoint.sh
2427

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"names": {
3+
"tracetools": {
4+
"cmake-args": [
5+
"-DTRACETOOLS_DISABLED=ON",
6+
"-DTRACETOOLS_STATUS_CHECKING_TOOL=OFF"
7+
]
8+
},
9+
"rosidl_typesupport": {
10+
"cmake-args": [
11+
"-DROSIDL_TYPESUPPORT_SINGLE_TYPESUPPORT=ON"
12+
]
13+
},
14+
"rcl": {
15+
"cmake-args": [
16+
"-DBUILD_TESTING=OFF",
17+
"-DRCL_COMMAND_LINE_ENABLED=OFF",
18+
"-DRCL_LOGGING_ENABLED=OFF"
19+
]
20+
},
21+
"rcutils": {
22+
"cmake-args": [
23+
"-DENABLE_TESTING=OFF",
24+
"-DRCUTILS_NO_FILESYSTEM=ON",
25+
"-DRCUTILS_NO_THREAD_SUPPORT=ON",
26+
"-DRCUTILS_NO_64_ATOMIC=ON",
27+
"-DRCUTILS_AVOID_DYNAMIC_ALLOCATION=ON"
28+
]
29+
},
30+
"microxrcedds_client": {
31+
"cmake-args": [
32+
"-DUCLIENT_PIC=OFF",
33+
"-DUCLIENT_PROFILE_UDP=OFF",
34+
"-DUCLIENT_PROFILE_DISCOVERY=OFF",
35+
"-DUCLIENT_PROFILE_SERIAL=ON",
36+
"-DUCLIENT_EXTERNAL_SERIAL=ON"
37+
]
38+
},
39+
"rmw_microxrcedds": {
40+
"cmake-args": [
41+
"-DRMW_UXRCE_MAX_NODES=1",
42+
"-DRMW_UXRCE_MAX_PUBLISHERS=2",
43+
"-DRMW_UXRCE_MAX_SUBSCRIPTIONS=1",
44+
"-DRMW_UXRCE_MAX_SERVICES=0",
45+
"-DRMW_UXRCE_MAX_CLIENTS=1",
46+
"-DRMW_UXRCE_MAX_HISTORY=1",
47+
"-DRMW_UXRCE_TRANSPORT=custom_serial"
48+
]
49+
}
50+
}
51+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SET(CMAKE_SYSTEM_NAME Generic)
2+
set(CMAKE_CROSSCOMPILING 1)
3+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
4+
5+
set(CMAKE_C_COMPILER $ENV{TOOLCHAIN_PREFIX}gcc)
6+
set(CMAKE_CXX_COMPILER $ENV{TOOLCHAIN_PREFIX}g++)
7+
8+
SET(CMAKE_C_COMPILER_WORKS 1 CACHE INTERNAL "")
9+
SET(CMAKE_CXX_COMPILER_WORKS 1 CACHE INTERNAL "")
10+
11+
set(FLAGS "-O2 -fsingle-precision-constant -ffunction-sections -fdata-sections -fno-exceptions -mcpu=cortex-m0 -nostdlib -mthumb -D'RCUTILS_LOG_MIN_SEVERITY=RCUTILS_LOG_MIN_SEVERITY_NONE'" CACHE STRING "" FORCE)
12+
13+
set(CMAKE_C_FLAGS_INIT "-std=c11 ${FLAGS} -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
14+
set(CMAKE_CXX_FLAGS_INIT "-std=c++14 ${FLAGS} -fno-rtti -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
15+
16+
set(__BIG_ENDIAN__ 0)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SET(CMAKE_SYSTEM_NAME Generic)
2+
set(CMAKE_CROSSCOMPILING 1)
3+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
4+
5+
set(CMAKE_C_COMPILER $ENV{TOOLCHAIN_PREFIX}gcc)
6+
set(CMAKE_CXX_COMPILER $ENV{TOOLCHAIN_PREFIX}g++)
7+
8+
SET(CMAKE_C_COMPILER_WORKS 1 CACHE INTERNAL "")
9+
SET(CMAKE_CXX_COMPILER_WORKS 1 CACHE INTERNAL "")
10+
11+
set(FLAGS "-O2 -ffunction-sections -fdata-sections -fno-exceptions -mcpu=cortex-m3 -nostdlib -DARDUINO=10813 -mthumb --param max-inline-insns-single=500 -DF_CPU=84000000L -D'RCUTILS_LOG_MIN_SEVERITY=RCUTILS_LOG_MIN_SEVERITY_NONE'" CACHE STRING "" FORCE)
12+
13+
set(CMAKE_C_FLAGS_INIT "-std=c11 ${FLAGS} -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
14+
set(CMAKE_CXX_FLAGS_INIT "-std=c++11 ${FLAGS} -fno-rtti -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
15+
16+
set(__BIG_ENDIAN__ 0)

extras/library_generation/library_generation.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ if [ $OPTIND -eq 1 ]; then
1313
PLATFORMS+=("opencr1")
1414
PLATFORMS+=("teensy4")
1515
PLATFORMS+=("teensy3")
16+
PLATFORMS+=("cortex_m0")
17+
PLATFORMS+=("cortex_m3")
1618
fi
1719

1820
shift $((OPTIND-1))
@@ -94,6 +96,34 @@ if [[ " ${PLATFORMS[@]} " =~ " teensy4 " ]]; then
9496
cp -R firmware/build/libmicroros.a /arduino_project/src/imxrt1062/fpv5-d16-hard/libmicroros.a
9597
fi
9698

99+
######## Build for SAMD (e.g. Arduino Zero) ########
100+
if [[ " ${PLATFORMS[@]} " =~ " cortex_m0 " ]]; then
101+
rm -rf firmware/build
102+
103+
export TOOLCHAIN_PREFIX=/uros_ws/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-
104+
ros2 run micro_ros_setup build_firmware.sh /arduino_project/extras/library_generation/cortex_m0_toolchain.cmake /arduino_project/extras/library_generation/colcon_verylowmem.meta
105+
106+
find firmware/build/include/ -name "*.c" -delete
107+
cp -R firmware/build/include/* /arduino_project/src/
108+
109+
mkdir -p /arduino_project/src/cortex-m0plus
110+
cp -R firmware/build/libmicroros.a /arduino_project/src/cortex-m0plus/libmicroros.a
111+
fi
112+
113+
######## Build for SAM (e.g. Arduino Due) ########
114+
if [[ " ${PLATFORMS[@]} " =~ " cortex_m3 " ]]; then
115+
rm -rf firmware/build
116+
117+
export TOOLCHAIN_PREFIX=/uros_ws/gcc-arm-none-eabi-4_8-2014q1/bin/arm-none-eabi-
118+
ros2 run micro_ros_setup build_firmware.sh /arduino_project/extras/library_generation/cortex_m3_toolchain.cmake /arduino_project/extras/library_generation/colcon_lowmem.meta
119+
120+
find firmware/build/include/ -name "*.c" -delete
121+
cp -R firmware/build/include/* /arduino_project/src/
122+
123+
mkdir -p /arduino_project/src/cortex-m3
124+
cp -R firmware/build/libmicroros.a /arduino_project/src/cortex-m3/libmicroros.a
125+
fi
126+
97127
######## Generate extra files ########
98128
find firmware/mcu_ws/ros2 \( -name "*.srv" -o -name "*.msg" -o -name "*.action" \) | awk -F"/" '{print $(NF-2)"/"$NF}' > /arduino_project/available_ros2_types
99129
find firmware/mcu_ws/extra_packages \( -name "*.srv" -o -name "*.msg" -o -name "*.action" \) | awk -F"/" '{print $(NF-2)"/"$NF}' >> /arduino_project/available_ros2_types

0 commit comments

Comments
 (0)