I’m primarily a software guy; not sure why since for most of my life I’ve played with electronics. I was soldering long before consumer computers even existed and I could start programming. And before I was soldering I was playing with a Radio Shack 100 in 1 electronic project kit (http://www.samstoybox.com/toys/ElectronicProjectKits.html). But this lifelong interest in electronics has served me well while working in the robotics field by giving me an appreciation and basic understanding of all the hardware and firmware that is required in order for my code to move a vehicle. Since I’m not the guy you want designing and building the electronics on large unmanned platforms, I feed my hardware interests by periodically pursuing electronics projects. The projects I pursue are often motivated by some need I have or see, or occasionally just for fun. I took on the project described below to prove to myself just how easy it could be, and to provide some entertainment for a bored cat.
There are many automated cat toys available (http://mashable.com/2012/09/26/cat-tech-toys/), but I was interested in something a bit fancier. Before investing too much time in the project, I wanted get some idea of whether the cat would be at all interested. To that end, I designed a simple system that could move a cat toy up and down in response to a simple lunging motion of the cat. The system is composed of a single servo, an Arduino to drive the servo, a camera to view the area near the toy, and a laptop to process the camera imagery to detect sudden lunging motion and send commands to the Arduino.
The Arduino sketch uses the Servo and Serial libraries. The servo library allows the sketch to set the angle of the servo with a simple “write” command. The serial library provides communication to the laptop. After initializing the servo and serial communication, the main loop monitors the serial line for a string that contains a command from the laptop to move the servo. Commands include “move the toy up”, “move the toy down”, and “move the toy to a specific angle”. If the last command was “move the toy down”, then the sketch loop jiggles the toy by changing the servo angle a small amount every 400 or 800 milliseconds.
The code on the laptop, written in C++, uses OpenCV to do all camera capture, image display, and image processing. In order to detect when there is movement by the cat, it simply subtracts two frames that are half a second apart, detects blobs in the resulting difference image, and if there is a sufficiently large change blob, assumes that it must be a moving cat. When a movement is detected, a command is sent to the Arduino to move the cat toy up. The program on the laptop then waits four seconds before commanding the Arduino to lower the toy. Parameters are tuned such that the movement of the toy by itself does not trigger an “up command”. In order to perform the video processing while also issuing the servo commands to the Arduino, the Arduino control code runs in a separate thread. The commands are written to the Arduino using the LibSerial library.
The first video below shows the setup. The second video shows the toy in action, with the cat showing only mild interest. Early on, the cat showed more interest, but has shown less interest after the toy was unexpectedly dropped on her head. This head bopping incident is related to one of the two main problems with this prototype. First, the image processing is way too simple. Background appearance modeling would allow the system to know that the cat is still sitting there after lunging or moving and not drop the toy back down until the cat has moved away. Further, when a person uses a cat toy to play with a cat, the person continually adjusts the toy movement based on the cat’s movements, demeanor, pose, etc. The algorithms required to do this are at or beyond the state of the art in computer vision. Even if the computer vision processing were enhanced, the limited toy movement provided by a single servo would likely become another limiting factor.
I probably won’t continue on with this project since I suspect that the cat will never appreciate the effort that I would need to put into it to make it a truly effective cat toy. But by using an Arduino and OpenCV, I was able to quickly build and evaluate my idea and in the process, provide a fix for my electronic tinkering urges.