Cats and Lasers

I finally am a full member of the internet – I am the proud servant of two cats! To keep them entertained when me and my wife are at work, I built a web-controlled laser-turret:

At its heart is an ESP2866 on an nodemcu amica v2, a cheap servo pan/tilt kit with small 9g servos and some no-brand laser-module rated for 5v and 40mA.

In this build-log I will write about the process of creating it.

The code and all files I used can be found in my github-repo: https://github.com/TobiasWeis/cats_and_lasers.

Here is the machine in action with one of the test subjects:

As always, in the beginning was a rough idea, a breadboard and a mess of jumper-wires, with the usual quirks and problems: loose or sketchy contacts, wires in the way, a confusing colorfull mess.

I created a python-script to simulate the movements of the turret (can be seen on the screen in the background) using some sine/cosine-math.

 

 

However, when everything was working, I wanted a more permanent solution, and quickly created the schematic in EAGLE – there even is a part-library for the NodeMCU-Controller I was using. (I know,  the interfaces for the contacts of the servos are messed up, in my case I just switched the pins inside their casing. As I own one of those very small CNC machines, I took the board-file from eagle to http://chilipeppr.com/jpadie, an online CAM processor that is able to read eagle board-files and output gcode, which can be sent to the CNCs controller. I mirrored the board along the X-axis in jpadie, as it will be milled on the bottom-layer of the board.

First I fitted a very sharp, 0.1mm and 10° engraving bit into the CNC, then I fastened the bare PCB using 3d-printed holders. Using the autolevel-feature of jpadie and connected probe-wires, I measured the real heights of the PCB at over 100 evenly spaced points. Milling took less than thirty minutes, and then I quickly soldered some pins and headers onto the board, and inserted the components.

 

All that was left to do was to cut out some holes (using a Dremel with a cutting-disc and a regular drill) from a casing for the servo and the cables, wrap the cables nicely, and polish the software.

 

 

 

 

The code is straight forward and based on standard libraries. I was using the arduino IDE to create and compile the code for the ESP2866, as there are easy-to-use libraries to quickly connect to a WiFi-network, and also handle webserver-tasks.

I gave the device a static IP in my router and assigned a hostname to it. That way, the website can simply be called by inserting http://katzenlaser in the browsers URL field. With a little bit of bootstrap and css, the website looks like this:

The Wifi is set up to connect to our access-point, and the webserver „hosts“ a page to display and is able to handle post-requests sent by buttons. The arduino-code below handles said tasks, and calls functions to control the servos and the laser.

 

The frontend – a simple website with some buttons, input-fields and jquery to handle the ajax-requests – is saved as a progmem-variable in the included header-file (index.h) – there are two reasons for this, the first is readability, and the second is that the arduino-IDE will create function-prototypes when it sees the „function“-keyword – which you need to define javascript-functions. It does not care if it is inside a string or not, it will just mess up the resulting string if you try.
I wanted a nice image on the website but did not want to bother with the filesystem-plugins, so I just base64-encoded jpeg and directly inserted this string in the src-field of the img-tag.

I calculated/simulated the servo-movements using a jupyter-notebook, as I could quickly plug in some formulas and see the resulting curves without having to flash and test on the servos each time:

And finally, the parts I used:

Which means a total cost of something like 20 EUR, of course not counting in the reusable parts like breadboard, jumper-wires or the CNC…

TobiasWeis | 12. Oktober 2019
  • Pedro 26. Oktober 2019 at 20:59
    Is there any danger of the cats looking into the lasers or the beam reflecting into their eyes?
    • TobiasWeis 27. Oktober 2019 at 1:49
      Those lasers are not really lasers as far as I understand, and are not as tightly bundled or nearly as strong.

Leave a Reply