RaspberryPi + NodeJS + EasyDriver
wanted to control a Nema 17 stepper motor using EasyDriver board with Raspberry Pi B+ over the web with NodeJS. Did not go as planed.
I’m working on a personal project that requires the control of stepper motors. There are a bunch of EasyDrivers around the Lab and a bunch of Raspberry Pi’s which were never used for a project as they are way removed from my preferred hardware to program such projects as two silicon chips can be, so why not try to use them I thought.
My first instinct was to use Atmel or Espressif chips but wanted to do something new and chose Raspberry Pi B+, one of the first Raspberry boards. One issue is that RasPi operating system is Raspbian which is not generally a real-time OS. Raspbian is a distribution built on top of Debian Linux, a general-purpose operating system. For systems like CNC or industrial applications, a real-time OS is necessary as any skipped steps or hiccups could lead to disaster. There are ways to make real-time OS out of Linux with the use of the PREEMPT_RT patch for the kernel, or just by using proper RTOS. Still, I wanted to try it.
First off I was looking at python for controlling the GPIO pins on the board, but I also wanted to create a web-based control system and wanted to go out of my comfort zone by finally using NodeJS. Been lurking around it for a long time but never really needed it. On microprocessors the sheer size of NodeJS is prohibitive so mostly I used vanilla JS served to the browser and the browser did all the stuff that usually backend would do.
All of these decisions are suboptimal for the current task but it’s challenging and something new, and at first I thought it would be faster and simpler than going the microprocessor route.
So in short I wanted to control a Nema 17 stepper motor using an EasyDriver board with Raspberry Pi B+ over the web directly from NodeJS without a microprocessor in the mix.
Installing NodeJS on RasPi is simple but the highest version available is v12. Just use sudo apt install nodejs. Most online tutorials make it seem complicated but I found no faults in using this method. Remember to update sudo apt-get update before installing Nodejs.
Most of the examples and code online are using the WiringPi node library which is a GPIO access library written in C for the BCM2835, BCM2836, and BCM2837 SoC devices used in all Raspberry Pi. versions. But there is a problem, it does not work. At first, I thought that because I’m using the oldest RasPi there was an issue and I needed to recompile it for it but even its official git is old or non-accessible with a message
git.drogon.net is currently unavailable. Please look for alternatives for wiringPi, etc.
As a replacement node-rpio is available and is “a high-performance Node.js addon that provides access to the Raspberry Pi and SunXi (Allwinner V40) GPIO interfaces, supporting regular GPIO as well as i²c, PWM, and SPI.”
So, as a basis for this testing already available Node.js module stepper-wiringpi was modified to be used with node-rpio. The modification was simple with the major modification being the change from sleep() to msleep() as sleep() function in node-rpio is in seconds and we need microseconds.
The modified EasyDriver stepper module for NodeJS is on GitHub as is a small set of test scripts.
There are issues: timings look not right and there’s an occasional stutter in stepper movement, which is really bad. Need more testing, especially with timings but I think that I should rethink the approach. I will update as I’m not willing to give up just yet.