Goodbye Joe

Last night I attended one last Detroit Red Wings game at Joe Louis Arena with some friends. Next year the team will be moving to the brand new Little Caesars Arena. They lost 5-4 to Toronto, but the team is rebuilding and it’s been a down year, so it wasn’t that surprising. It’s just a little bittersweet that the final year at “The Joe” is the first year since the 1989-90 season that the Wings will miss the playoffs. I don’t really know a spring without them playing after the regular season; I was 10 years old the last time this happened!

I’ve been to several games over the years and have posted about some of them on this blog:

My Mom is a big Wings fan, so I took her to a playoff game in 2015…

mom-wings.jpg

The NHL game has changed a lot throughout this Red Wings playoff streak. A former player, Kris Draper, recently published an excellent article titled There Will be Blood to The Players’ Tribune, which tells stories that contributed to many of the changes.

There have also been some strikes and lockouts during the playoff streak. Due to the lockout during the 2004-05 season I was able to experience one of my favorite professional sports related memory. On New Year’s Eve that year, my Dad and I found ourselves playing poker with Darren McCarty in a St. Ignace casino. If it hadn’t been for the lockout he never would have been there.

So there have been a lot of great Red Wings memories and visits to Joe Louis Arena. It was fun to get in one last trip. Detroit hockey is long overdue for a new home as the building is quite old and outdated. Every guy who has been there can tell you how weird it is to piss in the community trough urinals. Compared to any arena these days, it really is a shit hole.

DIY Spliced USB Cable

Even better than soldering the ends of a spliced USB cable is tearing apart an old cable to salvage the connectors and soldering your own wires. I only need power so skipped the 2 data wires. Hopefully using my own 22 AWG instead of the cheap stranded wires will be less resistant to a voltage drop as well.

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

Design in Tech Report 2017 Sorry, Kellyanne Conway. Microwaves Can’t Spy On You Michigan’s higher speed limits will kill people Donald Trump’s Official 2017 NCAA Tournament Bracket Risk Is Not as Simple as It Appears Sales soar: Raspberry Pi British board beats Commodore 64, world’s third best-selling computer Florida Won’t Charge Prison Guards Who Allegedly […]