Happy New Year

January 1, 2014

I’ve been playing a lot with Neopixels during the holidays. I made a bracelet, a pendant, and earrings for my wife, and also had a 4m 240 LED strip on display over my window.

The NeoPixo library

All of these projects share the same animations, just with slightly different parameters: the bracelet has 30 LEDs, the earrings/pendant have 16, and the strip has 237 (some of the Neopixels didn’t quite make it, sadly). Also timings have to be adapted to the form factor. But still most of the code is naturally sharable, so I made a little library I call NeoPixo which handles all the animations.

Here is the library itself: http://github.com/robidouille/robidouille/tree/master/Arduino/libraries/NeoPixo

And examples using it: http://github.com/robidouille/robidouille/tree/master/Arduino/neopixel

Hardware

Wait this is all nice and everything, but how do I get the hardware to use this on?

Well Adafruit has a bunch of tutorials for this, here are some links:

Adding a Raspberry Pi

ArduinoPi

While the bracelet, pendant, and earrings, just cycle through the different programs, the strip is very different. It’s connected to a Raspberry Pi, which can program the Arduino using Ino. Ino is a command-line version of the Arduino-IDE, but much better suited for the Raspberry Pi. I have a tutorial covering  Ino here.

The christmahanukwanzaa-tree example above used an Arduino Yun for this, I chose a Raspberry Pi instead. Having a Raspberry Pi opens a lot more possibilities. The Arduino can listen for commands on its serial port, and the Pi can schedule the programs now, and also play sounds through an attached speaker.

Webserver

One way is through a web-page, allowing users to click and select the program they want. PHP scripts turn the button clicks into serial port commands, which in turn drive the Arduino.

It’s important to grant Apache permission to use the serial port, for things to work properly. To do this run “sudo usermod -a -G dialout www-data” on the Raspberry Pi itself, and then restart Apache. More details on this are available in these pages.

The code for the web pages I use for this is here: http://github.com/robidouille/robidouille/tree/master/WebSites/html/neopixels

New-Year countdown

Since the Raspberry Pi has an NTP synchronized clock, it’s perfect for a precise countdown until the exact New Year moment.

I have a python script, exactly for that purpose: http://github.com/robidouille/robidouille/tree/master/Tools/python/NewYear

It wakes up 13 seconds before the target time, opens the serial port, and starts sequencing events every second. It also uses to the Raspberry Pi audio out jack to play sounds.

Advertisements

Installing Ino on a Raspberry Pi

December 26, 2013

I’ve been buying a lot of Adafruit Neopixels lately, and particularly a 4 meter long 240 LED strip which sits next to the Christmas tree. The setup is pretty standard, with an Arduino Mega setting up the various light patterns and movement. But now of course, you want to be able to control and program the Arduino from your network. I’ve seen people use the Arduino Yun for this, and it’s probably the easiest and most elegant solution for this.

I don’t have an Arduino Yun unfortunately, but I do have a Raspberry Pi, so I’ll use that instead. We have two options:

  • configure a setup similar to the Yun, where we forward the serial port over the network. This requires a separate host computer where we’d run the Aduino IDE, and compile the sketches. The Raspberry Pi would just be a dumb proxy in this case.
  • install the Arduino IDE on the Raspberry Pi itself, and have it compile and upload sketches directly. This is the solution we’ll be pursuing in the remainder of this article.

Here is a picture of my setup, I simply connect the Arduino to the Raspberry Pi USB port, just as you would with a regular computer:

ArduinoPi

Running the full Arduino IDE is unbearably slow when you run it on a Raspberry Pi, and through a remote ssh connection. We are trying to do things remotely after all, and the Raspberry Pi is somewhere close to the LEDs, and not very accessible. A command line solution is much better suited here, and fortunately Ino is just such a tool.

Well, what are we waiting for, let’s install it!

I started off with instructions from here. But ended up modifying them, mostly setting up symlinks instead of copying the files. This way they stay up to date when the OS gets updated in the future. I also found I needed to replace many more files for the Ino installation to work. Here is the precise sequence of commands I used to install and run Ino on my Raspberry Pi:

First install dependencies we’ll need:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install avr-libc libftdi1 avrdude librxtx-java openjdk-6-jre

Install the Arduino IDE:
mkdir ~/arduino
cd ~/arduino
wget http://arduino.googlecode.com/files/arduino-1.0.5-linux64.tgz
tar xfz arduino-1.0.5-linux64.tgz

The Arduino IDE comes with a bunch of libraries meant for x86/x64 computers, which are useless for the Pi. We need to replace them with Arm versions we already have installed, but are located in various system directories:
cd ~/arduino/arduino-1.0.5/lib/
ln -sf /usr/lib/jni/librxtxSerial.so librxtxSerial.so
ln -sf /usr/share/java/RXTXcomm.jar RXTXcomm.jar

cd ~/arduino/arduino-1.0.5/hardware/tools/
ln -sf /usr/bin/avrdude avrdude
ln -sf /etc/avrdude.conf avrdude.conf

cd ~/arduino/arduino-1.0.5/hardware/tools/avr/bin
ln -sf /usr/lib/avr/bin/ar avr-ar
ln -sf /usr/lib/avr/bin/as avr-as
ln -sf /usr/lib/avr/bin/ld avr-ld
ln -sf /usr/lib/avr/bin/nm avr-nm
ln -sf /usr/lib/avr/bin/objcopy avr-objcopy
ln -sf /usr/lib/avr/bin/objdump avr-objdump
ln -sf /usr/lib/avr/bin/ranlib avr-ranlib
ln -sf /usr/lib/avr/bin/strip avr-strip
ln -sf /usr/bin/avr-cpp avr-cpp
ln -sf /usr/bin/avr-g++ avr-g++
ln -sf /usr/bin/avr-gcc avr-gcc

We don’t need the bin.gcc directory anymore:
rm -rf ~/arduino/arduino-1.0.5/hardware/tools/avr/bin.gcc

We need setuptools so easy_install can work:
cd ~
sudo apt-get install python
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

And finally we can install Ino itself:
mkdir ~/git/amperka
cd ~/git/amperka
git clone git://github.com/amperka/ino.git
sudo easy_install ino
sudo ln -s ~/arduino/arduino-1.0.5 /usr/share/arduino

Once you’re done head on to the inotool Quick start guide to compile your first sketch.


Raspberry Pi Camera with OpenCV

October 19, 2013

I have OpenCV code using cvCreateCameraCapture and cvQueryFrame. Can I use it with the Raspberry Pi Camera? Well yes, read on to know how.

The Raspberry Pi Camera is widely available now, but there aren’t any open source video4linux (v4l) for it yet. Among other things, this makes it harder to use OpenCV with the camera.

When searching for a solution, I found Pierre Raufast’s blog, where he solves this problem. He took the existing userland libraries, which provide the source for the raspistill and raspivid tools. He modified them to produce OpenCV images instead.

Excellent news! So I now I can use this in my own OpenCV projects, and bypass the v4l drivers issue entirely. But wait, my code already calls cvCreateCameraCapture, cvQueryFrame in its own main loop, how do I do this?

I took Pierre Raufast’s modified camcv_vid0.c file and converted it into a library.

Instead of calling cvCreateCameraCapture and cvQueryFrame, you call the new raspi cam equivalents: raspiCamCvCreateCameraCapture and raspiCamCvQueryFrame. Instead of using a CvCapture, you use a RaspiCamCvCapture.

It’s a plain and simple search and replace operation in your code.

My source is here, it creates a library called libraspicamcv.a, you need to link with it and  #include “RaspiCamCV.h”

A more detailed README on how to compile and use it is the code repository itself.

This is a work in progress, I’ve done limited testing on the library. Contact me if you find bugs.

There is a raspiCamCvSetCaptureProperty method as well, but it doesn’t do anything yet. I plan to update it in the future,  to change parameters like: state->width, state->height, and state->graymode

For now, they are hardcoded in RaspiCamCV.c, so you need to change the code, and recompile the library to change resolution, or switch to grayscale mode (which is much faster).


Hawi Details

November 21, 2009

After making good progress on my robot arm, it became obvious that to be useful the arm could not remain confined to a table. Instead it had to be mobile. So the next logical step is to build a platform that would be able to move around and eventually have an arm (or maybe even two of them) attached.

So I started experimenting with wheels and motors, which led to Hawi: a holonomic drive platform, which instead of using omni-wheels, simply rotates all four wheels completely independently.

This is achieved by mounting each wheel on a servo.

Of course Hawi is a work in progress, I expect to upgrade different parts as needed, just like I did with my robot arm.

Here is a video showing Hawi in action:

Main components

An Asus Eee PC Netbook is the main brains. It runs the software that commands the microcontrollers and servos. It’s pretty basic for the moment as just uses the keyboard for commands. It’s not very pratical to chase Hawi and type on his keyboard while it’s moving. So I use VNC and control it from another laptop.

Under the belly:

  • 2 Arduino Duemilanove controllers are used to drive the h-bridges and handle back-emf. Each Arduino handles 2 DC-motors.
  • A PhidgetAdvancedServo 8-Motor is used to handle the 4 servos that control wheel direction.
  • 4 L298 H-bridges from Solarbotics: one per DC-motor. Although each L298 can handle 2 DC-motors, the way I wired back-emf only allows me to control a single DC-motor, as explained here.
  • Power is provided by a 12V sealed lead-acid battery.

Wheels, motors and gearbox are all from banebots:

  • BaneBots Wheel, 2-7/8″ x 0.8″
  • 64:1, 36mm Planetary Gearmotor
  • RS-540 Motor

The wheels are mounted to the chassis through a 3″ lazy susan, and you can almost see the HS-322HD servo under the wheel.

Going forward

Mechanically Hawi can handle pretty heavy weights. The wheels are mounted to the platform through 3″ lazy susan bearings, and both ends of my motor shafts are supported by bearing blocks.

I’m turning the wheels using cheapo HS-322HDs, they work for now and were even able to turn the wheels on carpet. I may have to move to something with more torque in the future though, or maybe get rid of the servos entirely (but that’s a whole other subject).

As far as electronics are concerned, my h-bridges are going to be my first limiting factor. I’m currently using L298s, which are limited to 2 amps. This is far below the stall current of my motors. So far I’ve been able to get away with it by adding heat sinks. The good part is that they are cheap to replace if I smoke them 🙂

But for now software is main area that needs to progress. Using VNC to remote login into the netbook was a quick but limited solution. Hawi’s software needs to have its own client-server code.

And of course Hawi needs to become more autonomous, by receiving high-level tasks and figuring out how to accomplish them, instead of the forward/backward commands it handles now.

PhidgetAdvancedServo 8-Motor

Back EMF

September 20, 2009

As promised, here are some more details on how I implemented back-emf to control the wheel’s rotational speed.

First, what is back-emf exactly?

When you apply voltage to a DC motor, it turns. But also when the DC motor is turning, voltage appears on its pins, like a dynamo. This voltage is the back-emf I’m using here.

The back-emf voltage is proportional to the speed at which the motor turns, so it can be used to measure the motor speed. But this can only be measured when the pins are not already supplied with voltage from the h-bridge. So we need to “cut power” for short time intervals to do the measurement. For more details you can visit this site.

The controller I used is an Arduino Duemilanove and an L298 based H-bridge from solarbotics.

Here is a schematic of the circuit I used:

BackEMF schematic

You’ll notice the h-bridge has one enable pin (E1/E2) for each input pin (I1/I2). I need separate enable pins when I measure the back-emf, because one output has to be set to ground, while the other is on hi-Z.

On most h-bridges I1 and I2 share the same enable pin. So to get around this limitation, I only use of the h-bridge. Then combine two halves to make a full h-bridge with separate E1/E2 pins. Luckily the L298 has 2 full h-bridges on the same chip. So I one L298, with its two h-bridges, controls a single DC motor.

Both motor wires are connected to analog input 0 through identical resistors. But since one of those 2 wires will always be set to the ground (exactly which one depends on motor direction), the other wire will receive the back-emf voltage from the motor. So effectively the three resistors are a voltage divider by 3. Since the theoretical maximum back-emf voltage is 12V, the analog would see a maximum of 4V, which is fine for its 5V range.

When the motor is running, the arduino will run the motor for 100ms, by setting one output to the ground and the other to a PWM signal.

When the 100ms are up, the PWM output will switch to hi-Z for 4ms: first 2ms to allow transient currents to flow out, then another 2ms to measure. The average voltage taken during those last 2ms is then compared to our target voltage. Then PWM signal driving the motor speed is increased or decreased depending on the difference between that average and the target voltage.

Arduino code I used for this project is available at http://code.google.com/p/robidouille/

There is room for improvement in the algorithm.

  • the 2ms wait for transient voltage to dissipate is a maximum value. If we autodetected the end of the transient spike, we could start sampling the input voltage sooner
  • the 2ms measuring and averaging voltages could also be shortened
  • shortening the 4ms during which the motor is not driven, but back-emf voltage is instead measured would allow us to measure more frequently, and react more quickly to changing conditions.

While these 3 points are valid, I’ll wait for the motors to be mounted on an actual platform to make tweaks that are really necessary.

Here is the video showing back-emf in action.

measure for

Visit the Robidouille website for the latest updates to this project


Time to start something new

September 6, 2009

After working on my robot arm, it became obvious I needed to attach it to a mobile platform that would be able to move around and do things. So I decided to build my own platform.

Why not take an existing platform? Well for one, it takes half the fun away! But also it allows me to build something tailored specifically to my needs, or that can be upgraded should those needs change.

As a first step, it seems logical to start with the wheels. To keep things as upgradable as possible, I built separate modules for the motor and the wheel. This allows me to upgrade one module while affecting the others as little as possible.

Here is the motor module:

Motor

Motor module

And the wheel module:

Wheel module

Wheel module

They are designed to be assembled together like this:

Motor assembly

Motor assembly

As you can see, I don’t have any encoders. I’ll use back EMF, to control the rotation speed of the wheels, like this:

I’ll post more details on back EMF next time.

For more details on my projects visit: http://www.robidouille.com