Planet Earth II

Ten years after the original, the BBC produced a sequel to their nature series Planet Earth. While the first series was 11 episodes, this second iteration only has 6, but it’s still phenomenal. We live on an amazing planet. In the United States you can watch Planet Earth II on the Discovery Channel.

Some of the moments that left me in awe:

  • Penguins being able to find their family and distinguish the call of their mate in a group of over 1 million others.
  • Ibex effortlessly moving up and down steep mountain terrain.
  • Big cats like the snow leopard and jaguar.
  • The plague of locusts.
  • Stallions fighting.
  • A fox diving in the snow.
  • Langur monkeys running and jumping across rooftops.
  • Feeding the hyenas.
  • Singapore.

Throughout the series I kept wondering how much work went into catching some of the scenes. Thankfully they produced a bonus “Making Of”episode as well, which shows off some of the camera equipment and the challenges faced by the camera crews.

HC-SR04 as a Motion Sensor

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. Its operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.
Complete Guide for Ultrasonic Sensor HC-SR04

You can get the HC-SR04 from Amazon or various electronics shops for $3-5 or even under $2 if you buy packs of them. I got 2 of them in a parts kit I bought on Amazon and used one for Blog in a Box Paparazzi.

I was using the sensor to sort of detect motion, or more specifically when someone walked into a room. My prototype was set on a desk at about chest height about 1-2 feet after the doorway. While working on the project I ran into several challenges:

  • Accuracy of readings.
  • Other activity on the Raspberry Pi.
  • Sampling over multiple readings.
  • Rogue readings vs actual motion.
  • Timing between readings.
  • Bailing if an echo takes too long.

I wrote my code in Python and heavily based it on ModMyPi’s blog post HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi. Here are the important pieces…

def read_ultrasonic() :
	# Make sure the trigger pin is clean
	GPIO.output( ULTRASONIC_TRIG_PIN, GPIO.LOW )
	# Recommended resample time is 50ms
	time.sleep( 0.05 )
	# The trigger pin needs to be HIGH for at least 10ms
	GPIO.output( ULTRASONIC_TRIG_PIN, GPIO.HIGH )
	time.sleep( 0.02 )
	GPIO.output( ULTRASONIC_TRIG_PIN, GPIO.LOW )

	# Read the sensor
	while ( True ) :
		start = time.clock()
		if ( GPIO.input( ULTRASONIC_ECHO_PIN ) == GPIO.HIGH ) :
			break
	while ( True ) :
		diff = time.clock() - start
		if ( GPIO.input( ULTRASONIC_ECHO_PIN ) == GPIO.LOW ) :
			break
		if ( diff > 0.02 ) :
			return -1

	return int( round( diff * 17150 ) )

def is_ultrasonic_triggered() :
	global prev_ultrasonic

	# Take 6 readings
	for i in range( 6 ):
		ultrasonic = read_ultrasonic()
		#Shift readings
		prev_ultrasonic = ( prev_ultrasonic[1], prev_ultrasonic[2], prev_ultrasonic[3], prev_ultrasonic[4], prev_ultrasonic[5], ultrasonic )

		if ( is_light_enough()
				and prev_ultrasonic[0] != -1
				and prev_ultrasonic[3] < ULTRASONIC_DIST and prev_ultrasonic[4] < ULTRASONIC_DIST and prev_ultrasonic[5]  ULTRASONIC_DIST and prev_ultrasonic[1] > ULTRASONIC_DIST and prev_ultrasonic[2] > ULTRASONIC_DIST ) :
			#print 'Ultrasonic: {0}'.format( prev_ultrasonic )
			return True

	return False

while ( True ) :
	if ( is_ultrasonic_triggered() ) :
		take_picture()

It worked alright, but triggered a little too often. About a week later I came across the Python package gpiozero, which makes it easy to work with a bunch of common Raspberry Pi GPIO components. I wrote an alternate version of BIAB Paparazzi using this package, which worked a bit better. It was so much simpler with gpiozero because it has built-in support for the HC-SR04. All I had to do was initialize the sensor and tell it what code to run when something in range was detected.

ultrasonic = DistanceSensor(
	echo = ULTRASONIC_ECHO_PIN,
	trigger = ULTRASONIC_TRIG_PIN,
	max_distance = ULTRASONIC_MAX,
	threshold_distance = ULTRASONIC_DIST )

ultrasonic.when_in_range = take_picture

The neat thing about the gpiozero package is when you initialize a sensor it automatically starts taking readings, keeps the values in a queue, and does comparisons against an average. My code attempted to do something along those lines, but was much more rudimentary. As nice as this version sounds, it still triggered too often. You can find the complete code for both versions in the BIAB Paparazzi repo on GitHub.

I think I was pushing the limits of what the HC-SR04 is meant for. Most of the examples I’ve seen are people using these to detect approaching walls on a robot. The biggest issue I ran into was the inaccurate readings. For example I’d be getting readings of about 160cm and then out of nowhere it would return a distance of 80-90 cm, even several in a row at times.

At the end of the day there are reasons it’s such a cheap sensor. 😉 For a couple of dollars, what do you expect? I’m curious to try my code on a more powerful Raspberry Pi 3 and see if it works any better. Was the less powerful Pi Zero causing problems?

Link Dump – 2017/03/26

Make Stranded Wire Easier to Work With

If you have stranded wires add a bit of solder to the ends and it’ll be much easier to work with. This is a USB cable I cut in half so I can add circuitry in between the connectors, which will control the flow of electricity. By soldering the ends I can easily insert the wires into a breadboard and continue prototyping my project.

Zero Reps On 17.5… For Now

My 17.5 will have to wait. I tweaked my back 6 reps into a 17.4 redo on Sunday and it has turned out to be the worst one I’ve experienced. Walking and basic human function has been challenging most of the week. Thankfully I finally started to see some improvements this afternoon. I made it to the gym tonight to watch some of the crew crush the workout and then got to enjoy beer and pizza with everyone.

I’ll battle with 17.5 in a few weeks when I’m healed up. I’m going to finally find a PT and hopefully correct whatever keeps causing these back problems.

AdaBox003

AdaBox003 arrived! It’s my first box as an official subscriber. I bought 001 and 002 through the Adafruit store, but as a subscriber you get:

  1. The new box automatically each quarter.
  2. A cheaper price – $60 with free shipping.
  3. Extras!

Each AdaBox has a theme and this one is right up my alley…

THE WORLD OF IoT – CURATED BY DIGI-KEY

IoT is short for “Internet of Things” and covers a wide range of devices, including home automation. I had actually saved several of these items on my wishlist and planned to buy them when I’d finished some other projects, so it’s a very cool surprise.