Tuesday, February 3, 2015

Practical Applications of C++11 and Why You Should Use It

A couple years ago I began looking at the experimental support of some of the newer C++11 features. At the time, compilers only offered subsets of the functionality. My first exposure was the 'auto' keyword. I didn't like. I want a strongly typed language when I use C++. `auto` seemed so lazy and sooo un-explicit. After listening to several cppcon talks on youtube, including one by Herb Sutter, I realized that auto can be a useful tool when used appropriately.

My experience with `auto` illustrates one of the major barriers for established C++ developers using newer C++ features. I don't know if you know this, but C++ has been around for a long time. And by long time, I mean it is ancient as far as languages are concerned. Sure, it is newer than Fortran and COBOL, but at the rate at which programming languages have exploded, C++ is a grandparent with many grandchildren. Many senior developers have spent their entire careers developing in C++ with minimal change. Sure, they lived through the wild west of the STL. Sometimes they had to use assembly when C++ just wasn't giving them the performance they needed. C++ finally provided a language that was good enough to solve most programming problems. C++ accumulated significant technical debt when compared to other more modern languages. As such, when it came time, releasing a new standard took a long time and had snowballed into a huge feature set.

Fast forward to today. Several compilers actually comply to the full standard today, and will comply to future standards within months of new standards being released. How amazing is that? That means that you can use these awesome features without worrying about library compatibility or linking in 3rd party libraries. Plus, the built in features mean more cross platform compatible primitives. That... is huge.

Ok, so a cross platform range based for loop isn't a big deal, how about cross platform threads? Some of the new planned features may include networking, 2d graphics. How about web services? I'm betting that some of these planned features will rely heavily on the C++11 features. If you want these new features, you'd better start learning the foundation. So, a promise of new features sometime down the line is not really a good reason to learn the new standards. Let me move to my main point

C++11 features will make your life easier by reducing complexity, eliminating boiler plate code, and creating easily maintained code

Use these features. Please please use them

In the following posts I will go through some of the new features and actually talk about real world situations where these features will improve your code.

C++11 Features that I will cover with practical applications

  • auto
  • Range based for loops
  • Lambda functions and std::function
  • C++11 additions to std::algorithm
  • std::thread (and this_thread)
  • mutex, condition_variable, and locks
  • Smart pointers
  • Variadic templates
  • std::chrono
  • And other miscellaneous goodies...

Monday, January 21, 2013

Hacking a Cheap HobbyKing 6 Channel Receiver - Summed Signals

RC Hobby Receivers

Any standard modern hobby RC receiver works the same way. The position of joysticks is converted to a digital value, which is then encoded, modulated, and broadcast on some radio frequency. On the other end, a receiver demodulates and decodes the values, converts them to PWM (Pulse Width Modulation), and outputs each of the PWM signals on a separate output pin. Typically, you would plug in a servo or motor controller to each output.

The signal on each output looks like the following picture

And now let us zoom in on one of those pulses

The value of the PWM is the width of the pulse. Standard hobby electronics have a pulse width between 1000us and 2000us, or between 1ms and 2ms. A pulse of 1000us is equivalent to 0% and a pulse of 2000us equals 100%. In the picture above, you can see that the pulse is about 1.46ms wide, which translates to just under 50%, or the joystick is centered. There is no official standard for the time between pulses, but typically a pulse occurs around every 20ms for use with average analog servos.

The Problem

Often in robotics or, for example, fancy multirotor aircraft, there may be addition smarts on the vehicle that needs to do additional computation on the inputs. RC radio transmitters are great. They usually have 2 nice joysticks, a miriad of switches, excellent range,

Tuesday, December 11, 2012

Using GStreamer to Stream All Audio Captured from Soundcard from One PC to Another

Jump to the solution

The Problem

I often work on multiple computers at a time. Additionally, I use headphones the majority of the time. Sometimes I would like to be able to stream all the sound (including system notifications) from one computer to another, allowing me to hear the system notifications from both machines on my headphones. The old-school way to accomplish this means running an audio cable from the output of one PC to the line in on the other. Although this works, the line-in device is usually low quality and noisy. Plus, what if the computers are far away? How about sending the audio digitally over the network? No need for a cable, assuming the computers are already networked!

The Solution - GStreamer

GStreamer is actually an amazing framework that allows you to create all sorts of media pipelines. In my opinion, it is easier to use than ffmpeg once you become familiar with the concepts of how to use it.

What is GStreamer and how does it work?

GStreamer uses a simple concept. You create pipes. Pipes have a source and a sink. A source generates content, and a sink consumes it. You can do fun things with pipes. Imagine that you have a soda fountain that takes in soda water for one source, soda flavoring from another, and spits out into cup sized increments. This process is very similar to the process of taking a video source and an audio source, combining them into a media stream, and then breaking it up in to packets to send it out over the network. Keep the idea of a pipe, with sources and sinks that you can chain together, in mind.

The trickiest part of GStreamer is that each source and each sink has its own set of input types or output types that it can produce or consume. The trick is matching up sinks to sources. If an mp3 encoding element takes in raw audio and spits out an mp3, you must ensure that something that can source raw audio is plugged in one side and something that sinks mp3 audio is plugged in the other. Makes sense, right? It is a little trickier in practice, just because there are so many different formats media can take.

Sunday, September 2, 2012

Winning "Jet" Powered Pinewood Derby Car

I finally had an opportunity to participate in a 'no holds barred' pinewood derby for grownups. The annual competition does have some standing by-laws, however.

  1. No open flame or explosions
  2. Any car leaving the track will be disqualified
  3. No sabotaging the other vehicles during the race

Being new to the area, and new to the competition, I set out to at least compete for the fastest car. After spending many hours finishing unpacking my garage to clear out a space to build a car, I had about 4 hours before the competition. I grabbed my tools and set to work.

The Design

Originally, I wanted to use the bell of an outrunner electric motor as the drive wheel, but I knew I did not have enough time to design and build such a pinewood derby car. So, I decided to salvage the parts from an old RC airboat (that didn't work so well) I had collecting dust in my garage.

The Chassis

The Materials

  1. 1/8" Scrap Plywood from the Homely Despot (they usually have scrap wood in the back of the lumber section that you can pick up for pennies on the dollar
  2. 1/2" Square Hardwood Dowel - Your local hardware store probably has 2ft lengths for about a dollar
  3. 3/8" Square Hardwood Dowel - same as the 1/2" dowel
  4. Small Self Tapping Wood Screws
  5. Pinewood derby Wheels and 'Axles' - You can substitute other wheels if you please
  6. 9x4x4mm ball bearing
  7. M4 bolt
  8. M4 nuts

Saturday, September 1, 2012

Cheap Source for Arduino Uno and Arduino Mega

Of all places, Hobbyking has a bunch of super cheap Arduino hardware. Probably the cheapest source you can find for these boards. Awesome! They even have a couple shields, if you know where to look.

Tuesday, May 8, 2012

Xbee Enabled RC Joystick Overview

Originally, I did this project to highlight how simple it can be to add a joystick to your robotics project using only:

  • Two Xbees (Series 1)
  • One Arduino
  • And one old joystick with a gameport connector

That is it. In this I also cover two things, how to hack those old joysticks all you electronics hoarders have (like myself) and demonstrate how to set up an Xbee as a data harvester and use some of the underutilized powers of the Xbee

The Overview

Here is a simple overview of the project. It doesn't get simpler than this.

Additionally, for the reasonable cost of an Arduino, two Xbees, and an old joystick you can find at a local thrift shop, you can have a multi-axis controller for you robotic project.

The Start - That Old Joystick

First you want to learn about that old joystick of yours. Check out Part I. I explain the process of modding your old Gameport joystick to work with a regular ADC you would find on a microcontroller.

Xbee Enabled Joystick, part I

Setting Up the Xbees

The next step in the project is setting up the Xbees. This explains how to set up the Xbee to automatically read values from the ADCs and digital inputs and send that information to a predetermined Xbee. The receiving Xbee is configured to receive that data and spit it out over the serial line using API mode

Xbee Enabled Joystick, part II

Finishing Up, Code for the Arduino and Schematics for the Xbee

In the conclusion I dust off my RC airboat, slap an Arduino to it, and demonstrate the final project. I include code samples for parsing the Xbee API packets and pulling out the ADC values from the joystick connected Xbee

Xbee Enabled Joystick, part III

Friday, April 27, 2012

Advanced Eagle Layout Tutorial - Building Better Library Parts - Variants and Technologies

Advanced Variants

Everybody knows that to create a device set you must have at least one variant of the device that binds the symbol to the package. The default variant is called ''. That is right. Double single quotes. It represents an empty string.

Naming Variants

You may have also noticed that if you name a variant, the name is appended to the device name. For example, if you have a device named


and a variant


the resulting name will appear as


Pretty cool, huh? Often manufacturers will have a package code embedded in the device part number. For example, if


is the base part number, and the manufacturer has package codes


Wednesday, April 25, 2012

Dirt Cheap USBasp AVR Programmer Available

Did you know that you can get a super cheap USBasp AVR programmer? $4.95 plus shipping (around $3 for International Unregistered). Where can I get this you might ask? An unlikely source, HobbyKing.com. Wow. I don't know of anywhere else that can sell it that cheap. Generally stuff takes at least one month to arrive to the US from China, that is if the item is in stock. Longer for backordered items. But that is super cheap! Next HobbyKing order I make I'll through one on and review the programmer.

Monday, April 23, 2012

Eagle's Library Part Creation Standard

Did you know that Eagle had a Library Part Creation Standard? I didn't either. Check it out. I've seen plenty of parts that violate some of these rules, and consequently make life a little more difficult.

Sunday, April 22, 2012

Advanced Eagle Layout Tutorial - Building Better Library Parts - Pin Swaplevel

Once you have pin direction down, we can move on to some of the other features of Eagle's pins.

How to Use Pinswap/Swaplevel

Swaplevel can come in handy on occasion. Its purpose is to allow you to be able to dynamically change which pin is assigned to a certain pad when using an instance of a part in your schematic/on your board. The end effect would be the same as physically swapping the pins' locations in the symbol while leaving the rest of the symbol intact. Let's look at an example using a simple resistor:

Now, because a resistor is the same forwards, backwards, and and any which way, it makes sense to be able to arbitrarily swap the pins. Look at the following board.

Oops, my nice row of resistors has one that is flipped! I could rotate the part 180 degrees, but then the text name would in the wrong place. I can simply select the 'Pinswap' tool, click on the first SMD pad and then the second. Voila! The pins have been swapped!

Youtube Channel