Arduino development with Eclipse – A step by step tutorial to the basic setup

If you want to build some electronic stuff that needs some programming, then Arduino is certainly the best choice. It just works, you only need to plug it in, write your code and press on the deploy button to have your board flashed and working.
And this is great, it saves you from tons of troubles, if you worked with microchip PICs in the past you probably know what I’m talking about.

The only thing that I don’t like about Arduino is it’s IDE, which is a very simple code editor written in Java with very few features and many annoying problems. The default IDE is very good to start learning Arduino programming, but the more you use it, the more you hate it, at least I do.

However until now I always used the default IDE, but for my last project (Sir Biss, an Arduino based snake robot) I figured out how to setup Eclipse for Arduino, and, after a bit of headache, it works great.

If you Google something like “Arduino development with eclipse” you’ll find some tutorials, some of them are very inaccurate and incomplete, others are pretty well explained but they simply don’t work, because some fundamental step is missing.

These tutorials, however, gave me the start to setup my own Eclipse, here they are:

So first of all, thanks to the people who wrote these tutorials.

What follows is a step by step guide of my personal way on how to setup a development environment for Arduino, I’ve done it on a Windows 7 32 bit PC, and my Arduino hardware is the Arduino Duemilanove board.
I do not own other boards so I cannot test this guide for other types of Arduino (Arduino Mega, Arduino Mini, …) if someone reading this guide is able to test it on different boards, please tell me if and what changes must be done I’ll be glad to update this tutorial, thank you!

Install Eclipse

First thing to do is download the latest Eclipse IDE for C/C++ developers from the Eclipse website, as I’m writing this the latest Eclipse version is Indigo.

To run Eclipse you need the Java Runtime Environment (not the JDK, because we are not going to develop in Java, so the JRE is fine), you can download the latest JRE from the Oracle website, click on the “Download” button under JRE, then choose the right JRE for your system, I downloaded the Win 32 x86 version, for offline installation (jre-7u3-windows-i586.exe).

Start Eclipse and choose a workspace, where all your Arduino projects will be saved.

Install the Eclipse AVR Plugin

From Eclipse, go to “Help” and then “Install New Software”.

In the “Work with:” box write this url:

http://avr-eclipse.sourceforge.net/updatesite

Then click “Add”.
Set the “Name:” to “Eclipse AVR Plugin”, leave the “Location” as is.

Click “OK” and wait until the installable components are loaded, then select all and click “Next”, a dependencies check will start for the selected components and it should take some minutes.

After the depencencies check completes you will be asked to review the selected components, click “Next”, then accept the license terms and hit “Finish”. The components installation will start. An alert dialog will appear saying that you are installing unsigned software, say “OK”.

When the installation completes you will be asked to restart Eclipse.

Download the Arduino SDK

Download the latest Arduino SDK, at the time I am writing this the latest available SDK is marked as Arduino 1.0. Nedless to say, download the right one for your machine, I downloaded arduino-1.0-windows.zip.

Choose a place on your file system for the Arduino SDK and then unzip the downloaded file, I unzipped mine in D:\arduino so from now on remember to replace this base path with the one you choose.

Setup the Eclipse AVR Plugin

From Eclipse go to “Window” and then “Preferences”, open the “AVR” section on the left, then select “Paths”

Select AVR-GCC, click “Edit”, in the window that appears set “Path source:” to “Custom”, click “Browse” and point it to this path <arduino_base_path>\hardware\tools\avr\bin which is the path to the AVR compiler (avr-gcc.exe) inside the arduino SDK directory. I pointed mine to D:\arduino\hardware\tools\avr\bin.

Select GNU Make, click “Edit”, in the window that appears set “Path source:” to “Custom”, click “Browse” and point it to this path <arduino_base_path>\hardware\tools\avr\utils\bin which is the path to the make executable inside the arduino SDK directory. I pointed mine to D:\arduino\hardware\tools\avr\bin.

Select AVR Header Files, click “Edit”, in the window that appears set “Path source:” to “Custom”, click “Browse” and point it to this path <arduino_base_path>\hardware\tools\avr\avr\include which is the path to the AVR toolchain headers files inside the arduino SDK directory. I pointed mine to D:\arduino\hardware\tools\avr\avr\include.

Select AVRDude, click “Edit”, in the window that appears set “Path source:” to “Custom”, click “Browse” and point it to this path <arduino_base_path>\hardware\tools\avr\bin which is the path to the AVRDude executable (used to deploy the binary on the Arduino board) inside the arduino SDK directory. I pointed mine to D:\arduino\hardware\tools\avr\bin.

Now the AVR plugin knows where to find the toolchain to build and deply code for the Arduino board.
Click “Apply” and then “OK”

Create the ArduinoCore library

The ArduinoCore library will contain all the basic functionalities to deal with the Arduino board, this library can be seen as the standard c library equivalent for Arduino. If you write some code for Arduino in the Arduino IDE, this library is silently linked to your code, but if you plan to use Eclipse you have to manually link the library to each of our projects before deploying them on the Arduino board.
Here are the steps to create the ArduinoCore library (which is just the name I gave it):

From Eclipse go to “New” then “C++ Project” then “AVR Cross Target Static Library” and select “Empty Project”.

Set the “Project Name:” to “ArduinoCore” and click “Next”.
This library will contain code distributed with the Arduino SDK which is considered to be stable, so deselect the “Debug” configuration and click “Next”.
Set the “MCU Type” and “MCU Frequency”, this depends on the specific board, my Arduino Duemilanove is equipped with an ATmega328P chip clocked at 16MHz (16000000 Hz). If you are not sure which values to use here, go to the arduino website in the “Hardware” section and click on your specific board to see the tech specs, here’s the link.
Once done hit “Finish”.

Now you have an empty library project, what we are going to do now is import the Arduino core files from the Arduino SDK folder.

In Eclipse right click on the “ArduinoCore” project, select “Properties” and then “C/C++ Build”.
Open the “AVR Compiler” section and select “Directories”.
Add the include path to the arduino core header files in the Arduino SDK, they are located inside “<arduino_base_path>\hardware\arduino\cores\arduino”
Add the include path to the pins_arduino.h header file, this again depends on the specific board you are working with.
Many version of the pins_arduino.h header file can be found, they are located inside specific folders under “<arduino_base_path>\hardware\arduino\variants\”:

  • <arduino_base_path>\hardware\arduino\variants\standard: I used this one for my Arduino Duemilanove
  • <arduino_base_path>\hardware\arduino\variants\mega: I guess you should use this one if you are working on an Arduino Mega board
  • <arduino_base_path>\hardware\arduino\variants\leonardo: maybe to be used with the new Arduino Leonardo board
  • <arduino_base_path>\hardware\arduino\variants\micro: absolutely dunno

Do the same for the “AVR C++ Compiler” section, then click “OK”.

From Eclipse right click on the “ArduinoCore” project, select “Import” and then “File System”. Point the file browser to the arduino core files directory inside the Arduino SDK which are located in “<arduino_base_path>\hardware\arduino\cores\arduino”.

Select all the files except main.cpp.

Click on “Advanced” and select “Create links in workspace” so the files are not copied into the library but only weak-referenced. It’s better to only reference these files so if you download a newer version of the Arduino SDK you can simply overwrite the existing one and the ArduinoCore library is automatically updated.
Click “Finish”.

If everything went well you should now be able to build the ArduinoCore library, right click on the “ArduinoCore” project and do a “Build Project”.

Create a test project

Let’s now create a test project to check that everything’s fine.
Remember that these steps must be done with every new project that you create.

In Eclipse go to “New” then “C++ Project” and then “AVR Cross Target Application” and select “Empty Project”.
Set the “Project Name:” to “TestProject” and click “Next”.
Deselect the “Debug” configuration and click “Next”.
Set the “MCU Type” and “MCU Frequency” to the same values you set in the ArduinoCore library project and hit “Finish”.

Right click on the “ArduinoCore” project, go to “Properties” and then “C/C++ Build” and in both “AVR Compiler” and “AVR C++ Compiler” add the same include paths that were added in the ArduinoCore library project plus the include path for the test project itself which is not added by default and can be added with this magic string: “${workspace_loc:/${ProjName}}” which simply expands to your workspace directory followed by the test project directory.

From the “C/C++ Build” section in the “Properties” window go to “AVR C++ Linker”.
Set the command line pattern to:

${COMMAND} --cref -s -Os ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lm ${FLAGS}

Now click on the “Libraries” subsection.

In the “Libraries path” box add the path to the ArduinoCore library by clicking on the “Add” button on the top right corner of the box, then select “Workspace” and point it to the “Release” folder inside the ArduinoCore project. You can also directly insert this magic string: “${workspace_loc:/ArduinoCore/Release}” which is the same thing.

In the “Libraries” box add the ArduinoCore library by clicking on the “Add” button on the top right corner of the box and simply type “ArduinoCore”, this instructs the linker to link against “libArduinoCore.a” when linking the final executable.

In the project properties window, click on “Project references” in the left menu and check “ArduinoCore”, so the library will be automatically built (and re-built if needed) when building the test project.

In the test project create a new main.cpp file and paste the following code into it.
This is the classic Blink code sample that blinks the led linked on pin 13 of the Arduino board once every second:

 
#include <Arduino.h>
 
int ledPin =  13;    // LED connected to digital pin 13
 
// The setup() method runs once, when the sketch starts
void setup()   {
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);
}
 
// the loop() method runs over and over again,
// as long as the Arduino has power
 
void loop()
{
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);                  // wait for a second
}
 
 
int main(void) {
 
  init();
  setup();
 
  while(true) {
    loop();
  }
}

You’ll notice that there are some difference between this code and the usual code that you write for Arduino, the next post will focus on these differences, for now just take it as it comes. No questions. Or this post will never end.

Deploy on the Arduino board

After compiling and linking the binary a tool named AVRDude will deploy it on the Arduino board.
AVRDude must be configured to work with Arduino.
What we are going to do now is stealing the AVRDude configuration from the Arduino SDK.

From Eclipse go to “Window” then “Preferences” and select the “AVR” section on the left, then select “AVRDude”.
Check “Use custom configuration file for AVRDude” and set “AVRDude config file” to the one provided with the Arduino SDK: it can be found under <arduino_base_path>\hardware\tools\avr\etc\avrdude.conf.
Click “Apply” and then “OK”.

From Eclipse select the test project and open the project properties once more (right click->“Properties”).
Select the “AVR” section and the click on “AVRDude”
You now have to create a new programmer configuration for your board, this must be done only once.
Click “New”, in the window that appears insert these data:

  • “Configuration Name” : “Arduino”
  • “Programmer Hardware” : “Arduino”
  • “Override default baudrate” : 57600

And set “Override default port” to the COM port that you use to work with the Arduino board. If you do not know what this value is, plug your Arduino board in and start the Default IDE, the COM port is showed at the bottom of the IDE window, my Arduino IDE says:

Arduino Duemilanove w/ ATMega328 on COM10

My COM port is COM10 so my “Override default port” field is set to:

//./COM10

Don’t ask me what the “//./” prefix means, I don’t know.
Click “OK”

Back in the “AVRDude” window select the “Advanced” tab.
Check “Disable Device signature check” and click “OK”.

First deploy

It’s time. Plug your Arduino board in.

Select the test project and build it, then right click on it and select “AVR” and then “Upload project to target device”, the deploy will start and if everything’s fine you should have your program running on the Arduino board after a few seconds. Look at the led at PIN 13 and it should be blinking. Mine blinks.
That’s all, hope this guide is useful.
Again, let me know any problem you had so I can update the tutorial with your info.

Sir Biss – An Arduino based snake robot

This post is part 1 of 1 in the series Building an Arduino based snake robot

Yep! There’s a robot maker inside all of us. Mine is waking up now.

My last robot was a classical obstacle-avoiding wheeled robot, you can find tons of them on YouTube, they are very common because they are simple to build and program and yet they can do intresting things like mapping a room or following walls or lines drawn on the ground. I built one of them two years ago, the intention was mainly to test a new emerging framework named Arduino which was composed by a small and very cheap programmable board and a ready to use development environment with libraries, code samples and everything else. Among all of these, Arduino was completely open-source, even the board hardware was open!

The result was excellent, I named my obstacle-avoiding robot “Ignazzius” (don’t ask me why).

This time I wanted to build something very different, to be more specific, I wanted something that could perform more complex movements and assume different configurations so to be able to overcome obstacles such as stairs and even to climb not too high obstacles.

If you search “Arduino robot” on YouTube you will find all sort of robots, the most common, apart from simple obstacle avoiders are:

  • Humanoid robots
  • Biped robots, such as BRATs or SCOUTs
  • Hexapod robots
  • Tetrapod robots
  • Self-balancing robots
  • And finally: Snake robots

Between all of them, snake robots are what fashinated me the most so in the last weekend I searched into my unused electronic parts “repository” and I found what I needed to build a simple snake robot:

  • An Arduino Duemilanove board
  • Six Hitec HS-422 Servos
  • A servo controller by Pololu which can drive up to 8 independent servos
  • Some mechanics to link the servos together in a snake-like fashion
  • Three 2xAA battery holders to power up the whole thing

With the help of some beer and some good music I build what you can see in the picture below.

As you can see this is the development version, I’m not going to build a final version for now because I want to better investigate what can be done with this thing, that I named Sir Biss.

The robot is fairly simple: the Arduino board communicates with the Pololu SSC03A servo controller using a standard serial line.
The communication protocol is defined by Pololu and is described in detail in the SSC03A user guide. This servo controller allows to set different parameters for each servo such as the speed, the central position and the rotation direction so it’s fairly simple to configure the six servos to act like the different segments of a snake-like body.

However it turns out it’s not that easy to program an efficient set of movements to make the snake go forward and backward.

I found some docs online about snakes locomotions tecniques, here’s a link to an article on this topic: Snake Locomotion

After many tries the solution resulted in a simple sine function that pilots each servo to propagate a sinusoidal wave up or down through the snake body depending on the direction I want it to move:

  • Forward progression: The sine wave propagates from the snake’s tail up to the snake’s head
  • Backward progression: The sine wave propagates from the snake’s head down to the snake’s tail

This is similar to what the doc above defines as “Rectilinear locomotion”.

The result is better than I expected, the snake moves forward and backward pretty fast and I can also set different speeds by changing the sine wave amplitude, so for now the locomotion algorithm will stay as is, until I find a better way to do it.

The software that pilots the snake has been developed in C++, I did not use the default Arduino IDE because it sucks.
The default Arduino IDE that can be downloaded from the Arduino website is good for small projects because it works like magic, out of the box with no setup needed. However the editor itself is very limited and poor.

That’s why I spent a whole day configuring an Eclipse installation to work with the Arduino board and then used the Eclipse IDE to write the software and burn it on the board.
I found some tutorials online on how to do that, they gave me the start, however I had to do some additional steps in order for it to work properly, maybe the tutorial I followed was old.

So, before I start talking about the robot itself, the next post will be a tutorial on how to setup Eclipse to develop on Arduino.

See you soon! Meanwhile here’s a video of Sir Biss in action, the speed has been slowed down for the video:

How to write code that doesn’t fall apart two days later

The title of this article has been inspired by a brilliant essay from Philip K. Dick’s “How to build a universe that doesn’t fall apart two days later”.
For those who don’t know him, Philip K. Dick (PKD – 1928-1982) was one of the most appreciated sci-fi authors of the last century, his novels inspired great movies such as “Blade Runner” and “Minority Report” and still today new movies are created from his books.

Why this post?
Recently I had to dig into some old code (actually not too old, 1 or 2 years old) in order to fix some bugs and add a bunch of new functionalities. I’ve been shocked about what I found: it wasn’t code, it was voodoo.

I spent an entire week just to understand where I was and then another week trying to understand how to do what I had to do without compromising any other functionality…
Simply put: people who wrote that code didn’t realize that nobody would never have been able to understand it.

But, you say, this may be an exception: you’d normally expect the code to be linear, well-written, easily understandable and easily editable if someone needs to add something.

Sorry to say, but this is not an exception, this is the rule: the code is usually non-linear, not well-written, absolutely not understandable and almost impossible to edit without breaking something somewhere else. At least in my experience.

Read more »

How to read sensors data on a Bada device – Receiving and displaying sensor data

This post is part 2 of 2 in the series Read sensors data on a Bada device

This post will complete the previous one, in which we learned how to register a form to receive the data from the different sensors mounted inside a Bada device. If you missed the previous post please go back and read it before this one.

Remember that the complete sample app is available on GitHub, I suggest you (if you didn’t yet) to git clone or download it and keep the code at hand while reading this post.

The last post stopped right after the initialization and the termination code for the sample application, so let’s start from there and move on to reading sensors data.

Receiving sensors data

With the initialization code and the termination code both set up and working we now must implement the only method required by the ISensorEventListener interface, this method will be called every time interval with the updates data from a specific sensor:

void Osp::Uix::ISensorEventListener::OnDataReceived(SensorType sensorType,  
                                                    SensorData & sensorData,  
                                                    result r);

This method is passed the SensorType, the corresponding SensorData that we are going to learn how to read, and an error code that we can check against to see if some error occurred:

void DataForm::OnDataReceived(SensorType sensorType, 
                              SensorData &sensorData, 
                              result r) {
 
  if (IsFailed(r)) {
    AppLog("OnDataReceived() Error: %s", GetErrorMessage(r));
    return;
  }

Read more »

How to read sensors data on a Bada device – Theory and setup

This post is part 1 of 2 in the series Read sensors data on a Bada device


Nowadays almost every mobile phone is equipped with a large variety of different sensors that developers can use to know a little more about the surrounding environment, and thus improve the user experience for their applications.
In this tutorial I’m going to show you how you can retrieve different types of sensors data from your Bada phone.
Since the whole tutorial was too long for a single post I sliced it down into two parts, welcome to part 1 “Theory and setup”.

As always a sample app is available on GitHub, I suggest you to git clone or download it and keep it at hand while reading this post.

To introduce this topic let’s make a list of the sensors that you can normally find on a Bada device, maybe other sensors will be available in the future, but now we have these:

The Acceleration sensor is a 3 axis (x,y,z) accelerometer sensor used to determine changes in velocity. It provides the three dimensional acceleration vector components expressed in g, which is the standard unit of measure for the acceleration. One g corresponds to the acceleration due to the Earth’s gravity (1 g = 9.8 m/s^2).
Note that since you’ll be using this sensor mostly on Earth (if not, please let me know!) a 1 g acceleration value is what you’ll get from this sensor if you lay down the device not moving it. The direction of the acceleration vector however depends on how the device is positioned.
If you need accurate acceleration measures you’ll have to take this default 1 g value into account and adjust your measures accordingly, it can be done by determining the phone orientation with the tilt sensor. However this is not what we are trying to do here, so let’s move on.

Read more »

How to use the Bada Event Injector

This post is part 9 of 9 in the series Bada from scratch

This is a very short post, just to show you the event injector tool which is fundamental to test your Bada application in every situation.
The event injector is only available when you run the application on the bada emulator, by using this tool you can simulate every kind of problem which you may not be able to simulate on a real device.
The event injector can also be used to simulate gps locations, sensor data, incoming calls or sms messages and so on.
This is only an introduction, it shows you how to access the event injector and how to simulate low battery and low memory conditions, just to try out the code we wrote in the last part.

Introducing the Event Injector

To access the event injector simply run the application in the Bada emulator, once the app is launched (and not before) right-click on the emulator and select “Event Injector” to launch the tool.
The event injector window has many tabs that allows you to send different kinds of events to the emulator, the one that we need now is the “Device” tab.
In the event injector window select the “Device” tab on the top-right corner:

By using the highlighted controls you can send both fake low memory alerts and fake battery level change notifications to the running emulator, just to see what happens.
Try pressing the “Low Memory” button and take a look at the emulator:

That’s our MessageBox, it works!

If you are intrested in seeing what happens when the battery level changes, just try changing the value of the slider and pressing “Send Battery Level”, the levels are:

  • Value 100 : BATTERY_FULL
  • Value 99 to 40: BATTERY_HIGH
  • Value 39 to 5 : BATTERY_LOW
  • Value 4 to 1 : BATTERY_CRITICAL
  • Value 0 : BATTERY_EMPTY

If you try sending a value of 0, the application will terminate and the emulator will shut down after showing an alert message.

This is it, the tool is very useful and we’ll probably use it again in this series, stay tuned!

Bada Applications Life Cycle – Part 2

This post is part 8 of 9 in the series Bada from scratch

When creating a Bada application with the “New application” wizard, like we did, the wizard creates a subclass of the Osp::App:Application class and overrides the basic methods with some default code, so you don’t have to write it from scratch. The Osp::App:Application subclass is named after your project and you can find it’s declaration inside the <your_project_name>.h header file while the class implementation is inside the <your_project_name>.cpp source file.

Another file is created automatically by the wizard, it is named <your_project_name>Entry.cpp. In this case it is FirstProjectEntry.cpp. This file contains a lot of strange C++ code inside a function named OspMain(), this is the entry point of the application. The entry point is the point inside the code where the execution of our application begins. Forget about it, the wizard handles this for us, and it is not easily understandable code at all. We can safely consider the OnAppInitializing() method as the entry point of our application.

Let’s move on and see what’s inside the FirstProject.h header file, double click on it inside the project explorer and you should see it appear inside the editor window.

Read more »

Bada Applications Life Cycle – Part 1

This post is part 7 of 9 in the series Bada from scratch

Hi all, and welcome to this new part of the Bada from scratch programming course.

Today we are going to understand the life cycle of a bada application. In order to do so, we have to look inside the sample project created back in part 3, and we also need the C++ knowledge exposed in the last 3 parts:

  1. Understanding C++ for Bada – The basics
  2. Understanding C++ for Bada – Interfaces and Event Listeners
  3. Understanding C++ for Bada – Idioms, Code conventions and Namespaces

This post will finally put together the knowledge aquired since now.
The initial version of this article was far too long for a single post, so I decided to split it up into two parts, the first part is focused on the theory while the second part is more about digging in the sample project to study how it’s composed.
Let’s now start with an overview of the life cycle of a Bada application.
Read more »

Understanding C++ for Bada – Idioms, Code conventions and Namespaces

This post is part 6 of 9 in the series Bada from scratch

Hi, welcome to this new part of the Bada from scratch programming course.
In this part we are going to understand four different topics: coding idioms used in Bada programming, code conventions used in Bada programming, how Bada APIs are splitter into C++ namespaces and how to identify which namespaces are available in every Bada header file. Each of these topics is really important but also pretty easy to expose, so I decided to make only one post.

Bada C++ coding conventions

Understanding coding conventions can dramatically improve your understanding of existing code, and helps you writing code that others will easily understand. Among this, it’s usually a good practice to adopt the same coding conventions of the platform you are developing on.
Last but not least, understanding code conventions can prevent your code from doing harmful things, you will see an example in a minute.
In the previous part, which was about interfaces and event listeners, you already saw one of the coding conventions adopted in Bada.
Read more »

Understanding C++ for Bada – Interfaces and Event Listeners

This post is part 5 of 9 in the series Bada from scratch

Hi, all and welcome to this new part in the Bada from scratch course.
In this part, we are going to talk more about the sample Application class which was created in the previous part, and we’ll also see how it can be used to represent a real Bada application that interacts with the operating system by introducing the idea of communication interface and event listener interface.
In order to understand what follows I recommend you to make sure you understood what was exposed in the previous part of this tutorial.
Let’s begin with a short recap of what was said in the last part.

One class interface, one class implementation

In the previous part I explained how a C++ class can be splitted down into two distinct components:

  • The class interface: defines how the class interacts with the outside world, that is, the methods and the properties that the code outside the class can access and use.
  • The class implementation: provides the code for the methods defined in the interface and possibly some other code that is private, not exposed to the outside world through the interface.

As a practical example I created a sample class to describe a generic application, this is the class interface:
Read more »

Related Posts Plugin for WordPress, Blogger...