I've been asked to look into "enchanting" some objects for a workshop up in Leeds at the end of the month so I thought I'd do some research into different tools to use.
Software
I've already used Python and that has good support for the GPIO with libraries such as Ben Nuttal's GPIO Zero. GPIO Zero: Developing a new friendly Python API for Physical Computing - Ben Nuttall
I had a play with that and it is simple and intuitive. There's lots going on under the surface but the API is clean and simple.
I looked at using Scratch and was rather disappointed. The implementation for interacting with the GPIO is via a external service and "broadcast" block with "magic" strings. This seems a horrid cludge, I was expecting to see at best some motor / LED control blocks or at worst some low level pin blocks. https://www.raspberrypi.org/documentation/usage/scratch/gpio/README.md
I was also aware of Node-Red, I'd never tried it out but knew it was good for controlling dinosaurs.
Hardware
I knew that I was also going to need some hardware. Given that the event I was going on was only a day long it made sense to look for some off the shelf components. I wanted something that had screw terminals and would be easy for people to use.
I looked at a couple firstly the PiFace, this seems a capable board but was out of stock at the time I was ordering.
There was also the RasPiRobot board from Simon Monk (of Maker's Guide to the Zombie Apocalypse fame). I got one of these ordered to try out and wired it up to an old motor / gearbox.
Getting started with Node-Red
I did experiment with running Node-Red on my Windows Laptop and it is possible to get it to work. However it's pre-installed on the latest Raspbian image so you can just fire it up from the console with node-red. I was running my Pi headerless so I did that by using SSH.
Once you've set it going, wait a few seconds and then you can open up a web browser to connect on the default port of 1880.
The flow based approach is a bit different if you are a coder but if you've an electronics background then it might actually seem more intuitive and it did not take me long to get used to how it worked.
LED Example
My first experiment was to turn the 2 LEDs on and off. I created a couple of "inject" nodes as those come with buttons to press on the left. One was configured to send a 1 and the other a 0. I then added some GPIO nodes and a function node which was coded to invert the signal. These were wired up as follows. The only "issue" I found was that Simon's pins referenced the BCM (broadcom) numbering not the pins on the header. Once I'd fixed that the flow worked fine.
Source - Library - Node-RED
PWM Control
The first issue I had when I tried to create a new flow was that the pins were already used on the previous flow. I saved that off and deleted it. I restarted node-red and was able to add the new flow.
The flow has 5 switches that pass the values 0 to 100 to the pin. The Pin is configured for PWM control. I found that the brightness did not seem linear so I selected specific values for the inputs to compensate for that.
Source - Library - Node-RED
Motor Control
The motor control is a combination of these two approaches. However, because of the control requirements for the H-Bridge chip I needed to create a complex flow to convert from on/off/back/forward into the pin values for the chip. This was handled by a sub flow and some flow specific variables.
The fast and slow injectors have a message type of "Speed", this is detected by the split message note and passed straight to output 3 to control the speed pin.
Go and Stop have a message type of "On", this is also picked up by the split message and sent to the on and off nodes. Off turns off both 1 and 2 outputs, On passes to the get direction function which reads the direction variable and then generates a H/L or L/H depending on the value.
The forwards and backwards node have a type of "Direction" so are split out to the flow at the bottom. These save the value of the direction to a flow specific variable. This flows on to get direction node and handles the direction in the same way as the On flow.
Source - Library - Node-RED
Top Comments