Interfacing Arduino with C++ and libSerial.

Standard

I know that there is a section at the Arduino Playground on interfacing the Arduino with C. However, I found it is much easier, almost trivial, to use libSerial with C++.

(libSerial works on POSIX systems. I guess Windows users could use it through Cygwin but I’m not sure)
(Update: No, they can’t)

First, you import your libraries and define the port you will be using:


#include &lt SerialStream.h &gt
#include &lt iostream &gt
#define PORT "/dev/ttyUSB0" //This is system-specific

SerialStream ardu;

using namespace std;
using namespace LibSerial;

The above code is assuming that the arduino is bound to /dev/ttyUSB0. Make sure to point the stream to the real device on you computer ;)

A new serial stream “ardu” is created.

Now let’s define a simple function to setup the Arduino for communication.

void open(){
    /*The arduino must be setup to use the same baud rate*/ 
    ardu.Open(PORT);    
    ardu.SetBaudRate(SerialStreamBuf::BAUD_9600);  
    ardu.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
    (....)

At least in my case, this simple function is enough to have a working serial communication with the microcontroller.

Now I’ll write another simple function that sends one byte through the serial port to the arduino and then reads a string, which gets interpreted as an integer..

int get(char out){    
    int res;
    char str[SOME_BIG_SIZE];
    ardu << out;
    ardu >> str;
    sscanf(str,"%d",&res);
    return res;
}


See? It’s easy! :)
If for some reason you must use standard POSIX libraries, here is a great article on POSIX serial programming.

If you have any doubts, post a comment.

Rotating cubes.

Standard

A couple of days ago I got 3 LIS3LV02DQ accelerometers from Sparkfun electronics.
Today I finished a working demo showing a cube that rotates as you rotate the device.
This accelerometer has some good documentation to learn how to use it. There is also a post in some blog with a specific example for the arduino.
That example didn’t work for me, but when I wasn’t sure about something, it did help to take a peek at it. Reading the datasheet carefully is extremely important.
The arduino has built-in SPI support, which helps a lot since the accelerometer uses SPI (or I^2C) to communicate. However, it is bound to 4 specific pins in the board, and if I’m going to use the 3 accelerometers at the same time without any additional hardware, I am going to have to write a software implementation, since I would be using 12 pins.
The interface to the arduino was written in C++. It uses libSerial, which makes communicating with the arduino almost as trivial as when using Python.


Arduino board.

Standard

(This devlog used to be on livejournal, but I moved all the entries to blogger using a program I found on the web.)

My new arduino board got delivered today =D. I spent a part of the day doing some electronics experiments. I assassinated my RC Helicopter and a personal fan in order to use their motors. Unfortunately they are not strong enough for my future plans (making an autonomous copter). Turning some LED’s on and off was fun for a while too =P.

It’s got a nice development environment (it uses a derivative of the C language), but I think I’ll be happier using Emacs.

Before it arrived I was starting to incorporate Framebuffer objects into pycave, my little python game. Turns out they’re pretty easy to use, and also elegant. The specification is available online and they’re a couple of pdf’s floating around the web. I’ll be using them to improve the shadow effects, and to make them faster.
A couple of days ago I made a model and texture for the ship that according to me looked pretty nice. That hasn’t been the general opinion =P
I am also aching to start writing shaders. However, I think that pyCave will stay with a fixed-function pipeline.

CUDA is another thing I would love to get my hands dirty with, but making killer robots is just a bit more interesting right now. Tomorrow I will order some motors (and actuators?) to start doing fun stuff!

I have left the ray tracer alone for now. I do plan to finish it. It’s a really basic ray tracer so it shouldn’t take long. I have also been gaming a bit too much, I got Call of Duty 4 and it’s a kick ass game.

These vacations haven’t been as productive as I hoped, since I went on an unexpected trip to the US. But I’m going to squeeze as much time as I can (and as much as COD4 does not take from me) to program =). I will also be managing my time better next semester in order to not be so stagnant.