Skip to content

Add support for Arduino Due and Arduino Zero #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
- https://raw.githubusercontent.com/ROBOTIS-GIT/OpenCR/master/arduino/opencr_release/package_opencr_index.json''' > arduino-cli.yaml
export PATH=$PATH:/github/workspace/bin:/__w/micro_ros_arduino/micro_ros_arduino/bin
arduino-cli core install OpenCR:OpenCR -v
arduino-cli core install arduino:samd -v
wget https://downloads.arduino.cc/arduino-1.8.13-linux64.tar.xz
tar -xf arduino-1.8.13-linux64.tar.xz
wget https://www.pjrc.com/teensy/td_153/TeensyduinoInstall.linux64
Expand All @@ -46,3 +47,4 @@ jobs:
arduino-cli compile --fqbn OpenCR:OpenCR:OpenCR /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
arduino-cli compile --fqbn teensy:avr:teensy31 /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
arduino-cli compile --fqbn teensy:avr:teensy41 /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
arduino-cli compile --fqbn arduino:samd:arduino_zero_native /github/home/Arduino/libraries/micro_ros_arduino-0.0.1/examples/micro-ros_publisher -v
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI tests are missing here as the upstream version of Arduino Due core doesn't support static libraries. Here is a relevant PR:
arduino/ArduinoCore-sam#115

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm curious to know why they allow precompiled linking and dont have to support in their own boards

50 changes: 50 additions & 0 deletions examples/micro-ros_publisher/transport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <Arduino.h>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to be able to change the transport in the project instead of the library. MCUs usually have multiple UARTs that users may switch to, or to other protocols, SPI, I2C, and CAN. Also, we can benefit from Arduino's libraries to interact with those protocols. Defining transport in the project would be super handy.

However, in that case, we need to include the same transport file in all examples which is not the best solution. Let me know if you have any suggestions.


extern "C"
{
#include <stdio.h>
#include <stdbool.h>
#include <sys/time.h>

int clock_gettime(clockid_t unused, struct timespec *tp)
{
(void)unused;
uint64_t m = micros();
tp->tv_sec = m / 1000000;
tp->tv_nsec = (m % 1000000) * 1000;
return 0;
}

bool arduino_serial_platform_open()
{
// Place here your initialization platform code
// Return true if success
SerialUSB.begin(115200);
return true;
}

bool arduino_serial_platform_close()
{
// Place here your closing platform code
// Return true if success
return true;
}

size_t arduino_serial_platform_write(uint8_t *buf, size_t len, uint8_t *errcode)
{
// Place here your writing bytes platform code
// Return number of bytes written
(void)errcode;
size_t sent = SerialUSB.write(buf, len);
return sent;
}

size_t arduino_serial_platform_read(uint8_t *buf, size_t len, int timeout, uint8_t *errcode)
{
// Place here your reading bytes platform code
// Return number of bytes read (max bytes: len)
(void)errcode;
SerialUSB.setTimeout(timeout);
return SerialUSB.readBytes((char *)buf, len);
}
}
5 changes: 4 additions & 1 deletion extras/library_generation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ RUN git clone https://github.com/micro-ROS/micro-ros-build.git src/micro-ros-bui
&& rm -rf gcc-arm-none-eabi-5_4-2016q2-20160622-linux.tar.bz2 gcc-arm-none-eabi-5_4-2016q2/share/doc \
&& 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 \
&& tar -xvf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 \
&& rm -rf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 gcc-arm-none-eabi-5_4-2016q3/share/doc
&& rm -rf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 gcc-arm-none-eabi-5_4-2016q3/share/doc \
&& 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 \
&& tar -xvf gcc-arm-none-eabi-4_8-2014q1-20140314-linux.tar.bz2 \
&& rm -rf gcc-arm-none-eabi-4_8-2014q1-20140314-linux.tar.bz2 gcc-arm-none-eabi-4_8-2014q1/share/doc
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't test, but probably we can recycle the same compiler for multiple platforms. I picked a safe route and took the same compiler that Arduino uses for the corresponding cores.


COPY ./entrypoint.sh /entrypoint.sh

Expand Down
51 changes: 51 additions & 0 deletions extras/library_generation/colcon_verylowmem.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"names": {
"tracetools": {
"cmake-args": [
"-DTRACETOOLS_DISABLED=ON",
"-DTRACETOOLS_STATUS_CHECKING_TOOL=OFF"
]
},
"rosidl_typesupport": {
"cmake-args": [
"-DROSIDL_TYPESUPPORT_SINGLE_TYPESUPPORT=ON"
]
},
"rcl": {
"cmake-args": [
"-DBUILD_TESTING=OFF",
"-DRCL_COMMAND_LINE_ENABLED=OFF",
"-DRCL_LOGGING_ENABLED=OFF"
]
},
"rcutils": {
"cmake-args": [
"-DENABLE_TESTING=OFF",
"-DRCUTILS_NO_FILESYSTEM=ON",
"-DRCUTILS_NO_THREAD_SUPPORT=ON",
"-DRCUTILS_NO_64_ATOMIC=ON",
"-DRCUTILS_AVOID_DYNAMIC_ALLOCATION=ON"
]
},
"microxrcedds_client": {
"cmake-args": [
"-DUCLIENT_PIC=OFF",
"-DUCLIENT_PROFILE_UDP=OFF",
"-DUCLIENT_PROFILE_DISCOVERY=OFF",
"-DUCLIENT_PROFILE_SERIAL=ON",
"-DUCLIENT_EXTERNAL_SERIAL=ON"
]
},
"rmw_microxrcedds": {
"cmake-args": [
"-DRMW_UXRCE_MAX_NODES=1",
"-DRMW_UXRCE_MAX_PUBLISHERS=2",
"-DRMW_UXRCE_MAX_SUBSCRIPTIONS=1",
"-DRMW_UXRCE_MAX_SERVICES=0",
"-DRMW_UXRCE_MAX_CLIENTS=1",
Comment on lines +42 to +45
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without these changes, I wasn't able to fit the library into Arduino Zero. Ideally, we would also be able to change these parameters from an Arduino project (without changing and recompiling the library), but I didn't know how to solve it.

"-DRMW_UXRCE_MAX_HISTORY=1",
"-DRMW_UXRCE_TRANSPORT=custom_serial"
]
}
}
}
16 changes: 16 additions & 0 deletions extras/library_generation/cortex_m0_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SET(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CROSSCOMPILING 1)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_C_COMPILER $ENV{TOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER $ENV{TOOLCHAIN_PREFIX}g++)

SET(CMAKE_C_COMPILER_WORKS 1 CACHE INTERNAL "")
SET(CMAKE_CXX_COMPILER_WORKS 1 CACHE INTERNAL "")

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)

set(CMAKE_C_FLAGS_INIT "-std=c11 ${FLAGS} -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_INIT "-std=c++14 ${FLAGS} -fno-rtti -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)

set(__BIG_ENDIAN__ 0)
16 changes: 16 additions & 0 deletions extras/library_generation/cortex_m3_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SET(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CROSSCOMPILING 1)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_C_COMPILER $ENV{TOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER $ENV{TOOLCHAIN_PREFIX}g++)

SET(CMAKE_C_COMPILER_WORKS 1 CACHE INTERNAL "")
SET(CMAKE_CXX_COMPILER_WORKS 1 CACHE INTERNAL "")

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)

set(CMAKE_C_FLAGS_INIT "-std=c11 ${FLAGS} -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_INIT "-std=c++11 ${FLAGS} -fno-rtti -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)

set(__BIG_ENDIAN__ 0)
30 changes: 30 additions & 0 deletions extras/library_generation/library_generation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ if [ $OPTIND -eq 1 ]; then
PLATFORMS+=("opencr1")
PLATFORMS+=("teensy4")
PLATFORMS+=("teensy3")
PLATFORMS+=("cortex_m0")
PLATFORMS+=("cortex_m3")
fi

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

######## Build for SAMD (e.g. Arduino Zero) ########
if [[ " ${PLATFORMS[@]} " =~ " cortex_m0 " ]]; then
rm -rf firmware/build

export TOOLCHAIN_PREFIX=/uros_ws/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-
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

find firmware/build/include/ -name "*.c" -delete
cp -R firmware/build/include/* /arduino_project/src/

mkdir -p /arduino_project/src/cortex-m0plus
cp -R firmware/build/libmicroros.a /arduino_project/src/cortex-m0plus/libmicroros.a
fi

######## Build for SAM (e.g. Arduino Due) ########
if [[ " ${PLATFORMS[@]} " =~ " cortex_m3 " ]]; then
rm -rf firmware/build

export TOOLCHAIN_PREFIX=/uros_ws/gcc-arm-none-eabi-4_8-2014q1/bin/arm-none-eabi-
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

find firmware/build/include/ -name "*.c" -delete
cp -R firmware/build/include/* /arduino_project/src/

mkdir -p /arduino_project/src/cortex-m3
cp -R firmware/build/libmicroros.a /arduino_project/src/cortex-m3/libmicroros.a
fi

######## Generate extra files ########
find firmware/mcu_ws/ros2 \( -name "*.srv" -o -name "*.msg" -o -name "*.action" \) | awk -F"/" '{print $(NF-2)"/"$NF}' > /arduino_project/available_ros2_types
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
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=micro-ROS Arduino library
url=https://github.com/micro-ROS
precompiled=true
category=Other
architectures=OpenCR,Teensyduino
architectures=OpenCR,Teensyduino,samd,sam
3 changes: 0 additions & 3 deletions src/arduino_transports.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
#include "teensy_transports.c.in"
#elif defined(ARDUINO_ARCH_OPENCR)
#include "opencr_transports.c.in"
#else
#error micro-ROS Library not supported for this platform
#endif
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transport is defined in the Arduino project and we don't need these files here. Should we move the OpenCR and Teensy transport files?



// TODO: This should be fixed
#if defined(ARDUINO_TEENSY32)

Expand Down
Binary file added src/cortex-m0plus/libmicroros.a
Binary file not shown.
Binary file added src/cortex-m3/libmicroros.a
Binary file not shown.