Pi Node-Red Streetcar

For our streetcar design we wanted to :

  • manually control things with a Web interface on a smart phone, and
  • automatically have the streetcar stop at station, wait, then go in the reverse direction.

For this project we built everything in Lego, with Lego Mindstorm components. The programming was done in Node-Red which was running on a Raspberry Pi.

The Lego Mindstorms EV3 and NXT components can be wired into your Pi (or Arduino) projects by using some custom adapters. If you are brave you could cut one end off of the connector cables. We unfortunately didn’t have any extra cables so went the safe route with the adapters. There are a couple of different adapters that are available we used the breadboard version from Dexter Industries.

Using the following parts we were able to make an automated streetcar :

  • 1 Lego Mindstorms motor
  • 1 Lego Mindstorms touch sensors
  • 3 Lego Mindstorms connector cables
  • 3 Dexter Lego Mindstorms bread board adapter
  • 1 small breadboard
  • 1 Pi 3 (or Pi 2 with a network connection)
  • 1 Pimoroni ExplorerPro Hat
  • 2 long pieces of wire
  • lots of Lego blocks

Streetcar Design

Our goal for the streetcar project was to have two stations, then have the streetcar automatically stop and switch directions at each station.

full_street

Station 1 had the motor, a touch sensor, and the Raspberry Pi.

station1

Station 2 had a pulley wheel, a touch sensor and a small breadboard that wired back to the Pi at station 1. Our pulley string went above the streetcar. We needed to keep the string quite tight so the pulley would not slip.

station2_top1

The Wiring

All of the Lego Mindstorms wiring used the same pins on the left side of the connector. and they are labeled “ANG” and “GND”. We used an ExplorerHat Pro to connect the Lego Mindstorms Motor and touch sensors to the Raspberry Pi. As we mentioned earlier the station 2 touch sensor was wired into a small breadboard and then from there two wires connected it to the ExplorerPro. For our setup station 1 touch sensor was wired on Explorer Pro input 1, and station 2 was on input 2.

circuit

Node-Red Installation

To check that Node-Red is installed and working, go to a Terminal window and enter:

node-red-start &

If you are able to run Node-Red, the next step is to install the Pimoroni ExplorerHat node and a web dashboard node. Depending on your installation you might need to load the Node Package Manager, npm :

sudo apt-get update
sudo apt-get install npm

Then to install the added libraries:

\curl -sS get.pimoroni.com/explorerhat | bash
 cd ~/.node-red
 npm install node-red-contrib-explorerhat
 npm install node-red-dashboard

After you install these library nodes you will need to restart your Pi. Once Node-RED restarts, you use a web browser to build applications. If you are working directly on your Pi, enter 127.0.0.1:1880 in the URL address box of your browser.

Node-Red Manual Web Control

To make a web based manual control program, drag three button nodes from the left node panel onto the center panel. Then wire the button nodes to an ExplorerHat output node.

nr2

Double click on a button node to open an edit window. The edit window allows you to configure the dashboard, labels and button actions. To control the first ExplorerHat motor the topic is motor.one. The payload is between -100 and 100, and this corresponds to full reverse and full forward, with 0 being stopped.

After you have finished all your Node-Red configure click the Deploy button at the top right of your browser window. Deploying will run your program and enable the web dashboards. To access the web dashboards enter: http://your-ip-address:1880/ui. If you are unsure of your IP address go to a terminal window and enter : ifconfig.

screenshot

Node-Red Automatic Control Program

We found the automatic control logic to be a little tricker than the manual logic. Our first step was to see if we could read the Lego Mindstorms touch sensors. For this we connected an ExplorerHAT input node to a debug node. We manually pushed each touch sensor and we used the debug tab to check the results.

debug

To catch when the streetcar hits the touch sensor at station 1 we needed to create a function that looked at the topic explorerhat/input.1. If this topic’s playload is 1 or pushed then the ExplorerHat motor can be stopped by sending a message with the topic of motor.one and the payload=0. A similar function is created for the touch sensor at station 2 except the topic explorerhat/input.2 is monitored.

button1

After the streetcar hits a touch sensor we wanted it to pause, and then move in the opposite direction. A delay node was used for the pause. Following this a function node is used to send a message with a payload of the new speed and direction. For our project station 1 restarted with a speed of -70, and station 2 restarted with a speed of 70 (or +70).

reverse1

Our complete logic with both manual and automatic control is shown below:

full_logic

Lego Electric Guitar

guitar_pic

Enchanting is a great free open source program that allows you to program Lego Mindstorms NXT robots in Scratch. Many kids are familiar with Scatch, and for us we found that we were able to start programming our robots almost right away.

The Enchanting application can be downloaded from: https://launchpad.net/enchanting .

The first time that you run Enchanting it will need to reload the NXT firmware with LeJOS ( a Java based Lego NXT firmware). There is a warning message about possible problems with reloading the firmware. We loaded and unloaded the firmware many times without any issues.

We were short some Lego pieces so we made a simple guitar. For step-by-step building directions to make a more complex guitar see:  http://www.nxtprograms.com/guitar/index.html.

To configure the ultrasonic sensor click the “Sensing” button, then select the “configure sensors” button. Next drag the ultrasonic sensor block to the port you are using,

enchanting_sensors_fig

For our guitar project we used an octave (8 notes) and each note was 3 cm. on the guitar’s neck. Our code had 8 “if” blocks where we defined the distance from the ultrasonic sensor and the note to play in that range. When you click on the note in the “play note” block a little keyboard pops up to show you the key.

guitarcode

if_statement

 

Lego/Pi Crittercam

Create a crittercam  with a Lego Mindstorms NXT set, a Raspberry Pi and  a USB Webcam.

crittercam_dalia

To install the Python NXT libraries:

wget https://nxt-python.googlecode.com/files/nxt-python-2.2.2.tar.gz
tar -zxvf nxt-python-2.2.2.tar.gz
cd nxt*
sudo python setup.py install

The Python NXT libraries will work with either USB or Bluetooth.

We used a simple USB Webcam connected to the Raspberry Pi to take pictures, and the ultrasonic sensor on the Lego NXT to turn on the camera when an object was within range.

For the Webcam software there were a lot of options available, we find that fswebcam is pretty easy to install by:

sudo apt-get install fswebcam

If the Webcam is working then the following Python code should create a picture:

fswebcam -r 384x288 picture1.jpg

The next step is to have the Pi communicate with the Lego NXT ultrasonic sensor. For this we use PORT 4 on the NXT brick. The NXT sensor will return a value of 0-255 cm. For our setup we put the CritterCam next to our cat feeder. You will need to set a distance that triggers the camera, we used 50 cm. For our pictures we name them 1.jpg, 2.jpg and so on. Listing below shows our full CritterCam code:

#!/usr/bin/env python
import os
import time
import nxt
from nxt.sensor import *

print 'looking for NXT ... could take 15 seconds'
b = nxt.locator.find_one_brick()

filecnt=0
while True:
	distance = Ultrasonic(b, PORT_4,check_compatible=False).get_sample()

	if distance < 50 :
		print 'You are close Iam going to take your picture'
		filecnt = filecnt + 1
		filename = str(filecnt) + '.jpg'
		os.system('fswebcam -r 352x288 ' + filename)
	time.sleep(1)

Wii Controlled Lego Rover

Use your Raspberry Pi (or a Linux PC) to talk to a Lego NXT rover and then use a Wii remote to drive the rover.

Python Libraries

There are 2 sets of libraries that we used:

  • nxt-python : to talk to the Lego Mindstorm NXT
  • cwiid : to talk to Wii remotes

To install these libraries:

wget https://nxt-python.googlecode.com/files/nxt-python-2.2.2.tar.gz
tar -zxvf nxt-python-2.2.2.tar.gz
cd nxt*
sudo python setup.py install
sudo apt-get install python-cwiid

The Lego NXT brick needs to have Bluetooth turned on. Check the top left corner of the Brick’s screen to see if the Bluetooth symbol is showing. If it is not showing go into the Bluetooth options and turn it on.

nxt_bt

The next step is to ensure that the Raspberry Pi can see the Lego NXT. The Pi can scan Bluetooth devices with the command:

hcitool scan

If the Bluetooth is working then you will see the Bluetooth address of the NXT., (for example: 00:16:53:04:23:3D). Using the NXT’s Bluetooth address you can pair the Pi and the Lego NXT by:

sudo bluez-simple-agent hci0 00:16:53:04:23:3D

For the pin code use the default of: 1234
The NXT will beep and prompt you for the pairing passkey.

nxt_pair

After the Raspberry Pi is paired with the Lego NXT brick you are able to use Python to read the NXT sensors and control the motors. No NXT coding is required. In the Python NXT directory there are some examples, mary.py  is a good test example because it does not require any sensors or motors.

Our full Python code to control the NXT Rover with a Wii remote is below:

import cwiid
import time
import nxt.locator
from nxt.motor import *

print 'Looking for a Lego NXT ... may take up to 15 seconds...'
b = nxt.locator.find_one_brick()
print 'Lego NXT Connected'

left = Motor(b, PORT_B)
right = Motor(b, PORT_C)

print 'Press 1+2 on your Wiimote now...'
wii = cwiid.Wiimote()
time.sleep(1)
wii.rpt_mode = cwiid.RPT_BTN 
print 'WII Remote Connected'

while True:
 buttons = wii.state['buttons']
 if (buttons & cwiid.BTN_LEFT):
 right.brake()
 left.run(power = 100,regulated=False)
 time.sleep(0.1)

if (buttons & cwiid.BTN_RIGHT):
 left.brake()
 right.run(power = 100,regulated=False)
 time.sleep(0.1)

if (buttons & cwiid.BTN_UP):
 right.run(power = 100,regulated=False)
 left.run(power = 100,regulated=False)
 time.sleep(0.1)

if (buttons & cwiid.BTN_DOWN):
 left.brake()
 right.brake()
 time.sleep(0.1)