Wednesday, December 24, 2014

Modifying the original BrickPi case to fit a Raspberry Pi model B+

The standard acrylic plates that come with a BrickPi do not accommodate a Raspberry Pi model B+. The screws don't fit and the holes are in the wrong locations. You need to do some modding to make it work. But then you have 4 USB ports on your lego creation, and a nice and compact micro SD!

What you'll need

  • A 3mm drill
  • An M3 screw with a small head

Step 1: The extra hole

The first thing to do is drilling a 3mm hole in the corner of the bottom acrylic plate, the one without the BrickPi logo. It doesn't have to be super accurate, as we'll be using a large hole for the other screw. Just put your B+ on the acrylic plate to mark the hole.

Step 2: Enlarge the holes on your Raspberry Pi

Somehow the new model has smaller holes than the old B+. Carefully enlarge the holes on the circuit board with a 3mm drill.

Step 3: Mount the RPi on the acrylic plate

In the top left corner you'll need that smaller M3 screw. The screws that come with the BrickPi are too large and cover the micro USB port and the black 4R7 thingy, so you can't tighten them.
The bottom right screw goes into a larger hole in the acrylic plate that is meant for a Lego peg. So you have some play there.

Step 4: Slide on the BrickPi and assemble the rest

Here's a completed assembly in an unfinished Lego robot.

Optional: Add a bevel to the holes in the acrylic plates

It's hard to insert Lego pegs in the acrylic plates that com with the BrickPi. Using a large drill you can manually add a little bevel so they go in more smoothly. You can also use a slow turning Dremel tool.

Thursday, December 11, 2014

Writing blog posts on blogger with code snippets made easy

In my mindstorms hacking projects I need quite some code. And I want to blog about it, but the default editor in Blogger.com doesn’t have a button to mark text as code. You can abuse the blockquote, or add the <code> tag by hand, but it’s quite a hassle. The solution proved the be the brilliant StackEdit web app! It’s so amazing, I’m going to pay the little fee they ask.

With it you can write your blog post using markdown. And with the code blocks like GitHub has them in the readme.md file. Actually I got the idea to use markdown for blogging when I was writing a readme. Markdown is so much easier to use than a wysiwyg editor or typing all the HTML tags manually.

Here’s how to use it on your blogger.com blog.

1. Edit the html of your blogger blog

In the blogger dashboard go to ‘template’ and click the ‘Edit HTML’ button. Then, just above the closing <\HEAD> tag insert this:

<link href='http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/zenburn.min.css' rel='stylesheet'/>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js'/>
<script>hljs.initHighlightingOnLoad();</script>

2. Go to StackEdit.io and write something interesting about code

StackEdit is mostly self-explanatory. So start writing and then click the hash icon on the top left. Choose: Publish > Blogger and there you are! A great blogpost with minimal typing and layout effort!

Realtime video stream with a Raspberry Pi and PiCam

I want to build remote controlled Lego robots with an onboard camera so I can drive around with them without having to see them. I did a lot of research to get a lagless video stream from the Raspberry Pi to my computer. It proved to be quite a challenge. But I found a way, it works!
Actually there are two methods that work: gstreamer and netcat. Both are detailed below. VLC and Mjpeg player are alternative methods that I didn’t get to work, at least not lagless. My favorite method is gstreamer.

Gstreamer

This solution proved the most stable, lag-free and flexible solution to me. The reasons for this being that gstreamer has nice python bindings and the order in which you start the sender or the receiver doesn’t matter. Gstreamer installation should be really easy, both on the RPi and on your mac. I will assume you installed and enabled the PiCamera already. On your RPi just do:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install gstreamer1.0
On the mac, the easiest way to install gstreamer is using homebrew. I prefer it over macports. Just do:
$ brew install gstreamer gst-plugins-base gst-plugins-good
Easy as Pi. On windows I wasn’t able to get gstreamer to work. If you know a good installation tutorial, let me know.
Now it’s time to stream. These are the commands you need.
On Raspberry Pi do (change the IP address to the address of your target computer):
$ raspivid -t 999999 -b 2000000 -o - | gst-launch-1.0 -e -vvv fdsrc ! h264parse ! rtph264pay pt=96 config-interval=5 ! udpsink host=192.168.99.61 port=5000
On your mac do:
$ gst-launch-1.0 udpsrc port=5001 ! application/x-rtp, payload=96 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! fpsdisplaysink sync=false text-overlay=false
On my setup I had a near realtime stream over wifi. I didn’t measure it exactly but the lag was below 300ms.

Netcat/mplayer

The alternative to gstreamer is using netcat to dump the camera data over a network pipe. This requires installing mplayer on your mac/pc. Again, it’s easy with brew. The trick is to read at a higher framerate than Pi is sending. This way the buffer stays empty and the video is real-time.
Here the order in which you execute the commands is important. First do this on the mac:
$ nc -l 5001 | mplayer -fps 31 -cache 1024 -
Then, do this on the RPi - insert the correct IP address, of course.
$ raspivid -t 999999 -w 640 -h 480 -fps 20 -o - | nc 192.168.178.22 5001
It’s also possible to do this on Windows. For this you have to download netcat and mplayer and put them in the same directory. Go to that directory using the command prompt and execute this:
> nc -l -p 5001 | mplayer -cache 32 -demuxer lavf -

VLC

Streaming with VLC from the raspberry Pi is fairly straightforward. I was unable to do it lagless, but the cool thing is that you can pick up the stream on an iPad with vlc installed, or on a mac using just the VLC app. No need for brewing.
First install VLC on the RPi
$ sudo apt-get install vlc
Then start streaming on the RPi
$ raspivid -o - -t 0 -hf -b 1000000 -w 640 -h 480 -fps 24 |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8160}' :demux=h264
To pick up the stream, open the VLC app and pick up the stream with a URL like this: http://192.168.178.91:8160. Here insert the name or IP address of the RPi.

Mjpg-streamer

Mjpg-streamer is also cited as an alternative sometimes, but I haven’t gotten it to work. The installation instructions are arcane and require v4l drivers.
Written with StackEdit.