Skip to content

Commit ddb8494

Browse files
authored
Team F Wiki Entry - ROS Arduino Interface (#89)
* Update dji-drone-breakdown-for-technical-projects.md * Deleted empty entry * Adding ros arduino interface template * Finished ROS Arduino Interface Article * Added Ros Arduino Interface to navigation.yml Also reordered ROS Introduction to be with the rest of the ROS entries.
1 parent b03a19f commit ddb8494

File tree

3 files changed

+212
-5
lines changed

3 files changed

+212
-5
lines changed

_data/navigation.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ wiki:
1313
- title: System Design & Development
1414
url: /wiki/system-design-development/
1515
children:
16-
- title: Drone Flight Permissions
17-
url: /wiki/system-design-development/drone-flight-permissions/
1816
- title: System Engineering
1917
url: /wiki/system-design-development/system-engineering/
2018
- title: Mechanical Design
@@ -43,12 +41,14 @@ wiki:
4341
url: /wiki/common-platforms/dji-sdk/
4442
- title: Pixhawk
4543
url: /wiki/common-platforms/pixhawk/
46-
- title: ROS Introduction
47-
url: /wiki/common-platforms/ros/ros-intro/
4844
- title: Asctec Pelican UAV Setup Guide
4945
url: /wiki/common-platforms/asctec-uav-setup-guide/
5046
- title: Husky Interfacing Procedure
5147
url: /wiki/common-platforms/husky_interfacing_and_communication/
48+
- title: ROS Introduction
49+
url: /wiki/common-platforms/ros/ros-intro/
50+
- title: ROS Arduino Interface
51+
url: /wiki/common-platforms/ros/ros-arduino-interface/
5252
- title: ROS Motion Server Framework
5353
url: /wiki/common-platforms/ros-motion-server-framework/
5454
- title: ROS Cost Maps

wiki/common-platforms/dji-drone-breakdown-for-technical-projects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ date: 2020-04-06
33
title: DJI Drone Breakdown for Technical Projects
44
published: true
55
---
6-
DJI is one of the best selling drone companies in the world. Considering also the fact that a DJI Matrice M100 costs $3500 and a M210 costs $12000, compared to a couple hundred dollars for most other manufacturers, it is obvious that they have some of the best drones in the market. DJI’s hardware is one of the best if not the best at the writing of this article (2020).
6+
DJI is one of the best selling drone companies in the world. Considering also the fact that a DJI Matrice M100 costs \$3500 and a M210 costs \$12000, compared to a couple hundred dollars for most other manufacturers, it is obvious that they have some of the best drones in the market. DJI’s hardware is one of the best if not the best at the writing of this article (2020).
77

88
It’s a good choice for many projects where one would prefer not to build their own drone from scratch; however, it comes with important caveats especially for professional and research projects. Below we will look at its main drawback, and then give an introduction to its flight modes as well as general tips for drone projects.
99

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
---
2+
date: 2020-04-06
3+
title: ROS Arduino Interface
4+
published: true
5+
---
6+
The Arduino Microcontroller is a versatile piece of hardware that can be used in various applications to integrate hardware and software components in a short span of time. Fortunately, It can also be used with Robot Operating System (ROS) without much effort. The following tutorial will guide you through the process.
7+
8+
## Setup
9+
The connection is based on serial communication and there is one big advantage:
10+
you don’t need to install the Arduino IDE in the host computer when running it. In other words, you don’t even need a GUI to enable it. However, there are quite a few items to install to import the ros library onto arduino to code.
11+
12+
Type the following code in the terminal : (Replace Indigo with your current version of ROS)
13+
```
14+
sudo apt-get install ros-indigo-rosserial-arduino
15+
sudo apt-get install ros-indigo-rosserial
16+
```
17+
18+
The preceding installation steps created the necessary libraries, now the following will create the ros_lib folder that the Arduino build environment needs to enable Arduino program s to interact with ROS.
19+
20+
`ros_lib` has to be added in the arduino libraries directory. By default, it will be typically located in
21+
`~/sketchbook/libraries` or in `my documents`.
22+
If there is a `ros_lib` in that location already, delete it and run the following commands.
23+
```
24+
cd ~/sketchbook/libraries
25+
rm -rf ros_lib
26+
rosrun rosserial_arduino make_libraries.py .
27+
```
28+
(DO NOT FORGET THE PERIOD AT THE END)
29+
30+
## Example Code - Publisher
31+
Now you have everything set up to start writing your publisher and subscriber in Arduino IDE.
32+
After doing the above steps, you should be able to see `ros_lib` under File -> Examples.
33+
34+
I’ll step you through an example program and help you run it as well at the end as it might be tricky at first.
35+
36+
Let’s talk about a basic publisher subscriber program. If you are confused or want to refresh your memory on Ros Publishers and Subscribers, checkout [Ros Pub-Sub](http://wiki.ros.org/action/fullsearch/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29?action=fullsearch&context=180&value=linkto%3A%22ROS%2FTutorials%2FWritingPublisherSubscriber%28c%2B%2B%29%22). Once you understand that, you can head back and continue from here.
37+
38+
Here is an example from the official repository and also an example of my own which uses a Boolean data type rather than a String.
39+
40+
Basic Hello world program:
41+
```
42+
#include <ros.h>
43+
#include <std_msgs/String.h>
44+
45+
ros::NodeHandle nh; //Node Handler ()
46+
47+
std_msgs::String str_msg; //initialise variable
48+
ros::Publisher chatter("chatter", &str_msg);
49+
50+
char hello[13] = "Hi, This is my first Arduino ROS program!";
51+
52+
void setup()
53+
{
54+
nh.initNode();
55+
nh.advertise(chatter);
56+
}
57+
58+
void loop()
59+
{
60+
str_msg.data = hello;
61+
chatter.publish( &str_msg );
62+
nh.spinOnce();
63+
delay(1000);
64+
}
65+
```
66+
67+
Explanation:
68+
- [Node Handler](https://answers.ros.org/question/68182/what-is-nodehandle/)
69+
- [Initialise Node (InitNode)](http://wiki.ros.org/rospy/Overview/Initialization%20and%20Shutdown)
70+
- [spinOnce](https://answers.ros.org/question/11887/significance-of-rosspinonce/)
71+
72+
The above program can be used to send some messages to ros through serial communication. But if you are working on a real project, sending characters or String has very minimum use as you may know. So, let’s also go through the same example, but with some modifications to use a different data type, like a Boolean. Boolean, in my opinion, is the most used data type to flag different aspects of a program and to make decisions based on that to enable or disable various items.
73+
```
74+
#include <ros.h>
75+
#include <std_msgs/Bool.h>
76+
77+
ros::NodeHandle nh; //Node Handler ()
78+
79+
std_msgs::Bool bool_flag; //initialise variable
80+
ros::Publisher chatter("chatter", &bool_flag); #
81+
82+
Bool flag = true;
83+
84+
void setup()
85+
{
86+
nh.initNode();
87+
nh.advertise(chatter);
88+
}
89+
90+
void loop()
91+
{
92+
bool_flag.data = flag;
93+
chatter.publish( &bool_flag );
94+
nh.spinOnce();
95+
delay(1000);
96+
}
97+
```
98+
The only difference between the code above and the one before is the change in datatypes and the initialized values. You could use this to process information and send true or false information based on the condition. An example would be to monitor the temperature in a room with a sensor and when it's greater than a threshold, send out a warning message or turn on the Air conditioner.
99+
100+
This can be achieved directly by connecting the sensors to Arduino itself, but in more complex applications it is often desirable for ROS to handle all communications between devices. In that case, this can be achieved by subscribing to a topic which is published on ROS by something else. So, let’s see how to write a subscriber in Arduino.
101+
102+
## Example Code - Subscriber
103+
```
104+
#include <ros.h>
105+
#include <std_msgs/Empty.h>
106+
107+
ros::NodeHandle nh;
108+
109+
void messageCb(const std_msgs::Empty& toggle_msg){
110+
digitalWrite(13, HIGH-digitalRead(13)); // blink the led
111+
}
112+
113+
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );
114+
115+
void setup()
116+
{
117+
pinMode(13, OUTPUT);
118+
nh.initNode();
119+
nh.subscribe(sub);
120+
}
121+
122+
void loop()
123+
{
124+
nh.spinOnce();
125+
delay(1);
126+
}
127+
```
128+
Explanation for the above code:
129+
130+
It looks pretty similar to a generic non-arduino ROS subscriber. The topic being subscribed to `toggle_led` doesn’t have any data being published through it but the availability of the topic itself triggers a call back which would turn on the LED on pin 13 of the arduino. This is different from a usual subscriber wherein the subscriber subscribes for some data from the topic, say a boolean or integer, but it is `std_msgs::Empty` in this case. This is probably not known by many of us and if that’s the case, you learned a new thing today! You can subscribe to an Empty Data type and have a call back based on that.
131+
132+
As an addition, let’s also have a look at an example which actually subscribes to a topic with some data unlike the previous case. Specifically, let’s look at subscribing to a topic which has boolean data.
133+
The following example is to toggle the magnet on and off based on the bool value subscribed under the Ros Topic - `magnet_state`
134+
```
135+
#include <ros.h>
136+
#include <std_msgs/Empty.h>
137+
138+
ros::NodeHandle nh;
139+
140+
int flag;
141+
bool magnet_state;
142+
143+
void call_back( const std_msgs::Bool& msg){
144+
magnet_state = msg.data;
145+
flag = 1;
146+
}
147+
148+
ros::Subscriber<std_msgs::Bool> sub(“magnet_state", call_back );
149+
void setup() {
150+
nh.initNode();
151+
nh.subscribe(sub);
152+
}
153+
154+
void loop() {
155+
if ((magnet_state== false) && flag)
156+
{
157+
//Turn magnet on;
158+
}
159+
else if ((magnet_state == true) && flag)
160+
{
161+
//Turn magnet off;
162+
}
163+
164+
nh.spinOnce();
165+
}
166+
```
167+
168+
## Running the code
169+
Now, since we are done with all of the basic ways and examples to write an Arduino Pub Sub code, let’s see how to run it.
170+
171+
First and foremost, Upload the code to Arduino and make sure all the relevant connections are established.
172+
173+
Now, launch the roscore in a new terminal window by typing:
174+
175+
`roscore`
176+
177+
Next, run the rosserial client application that forwards your Arduino messages to the rest of ROS. Make sure to use the correct serial port:
178+
179+
`rosrun rosserial_python serial_node.py /dev/ttyUSB0`
180+
181+
In the above line, `/dev/ttyUSB0` is the default port. It might be `/dev/ttyACM0` depending on your local machine. The number at the end may vary depending on the COM port the arduino is connected to.
182+
183+
Once the publisher and the subscriber are turned on, and the above code is executed, check with the rostopic list command to see if it is working. You should see the names of the topic that is being published and the topic that is being subscribed to.
184+
185+
Note: Once the Arduino has the code, it can be used in any other machine with ROS. The arduino IDE and the `ros_lib` library are not necessary to run rosserial. (No GUI as such is needed.)
186+
187+
Run the following after connecting the required items.
188+
```
189+
sudo apt-get install ros-indigo-rosserial-arduino
190+
sudo apt-get install ros-indigo-rosserial
191+
192+
rosrun rosserial_python serial_node.py /dev/ttyUSB0
193+
```
194+
Note in many cases user permission can be an issue. If the connection is denied when trying to establish the rosserial connection, type the following command.
195+
```
196+
sudo chmod +x /dev/ttyUSB0
197+
```
198+
for example should open the port.
199+
200+
Alternatively, try
201+
```
202+
sudo chmod a+rw /dev/ttyUSB0
203+
```
204+
if the above command doesn’t work.
205+
206+
## See Also:
207+
- [ROS Introduction](https://roboticsknowledgebase.com/wiki/common-platforms/ros/ros-intro/)

0 commit comments

Comments
 (0)