Create a streetcar with Lego Mindstorms components and a Raspberry Pi

Node-RED Automatic Control

We found the automatic control logic to be a little trickier than the manual logic. Our first step was to see if we could read the Lego Mindstorms touch sensors, so we connected an Explorer HAT input node to a debug node (Figure 7), manually pushed each touch sensor, and clicked the debug tab to check the results.

Figure 7: Reading the output of the touch sensors.

To catch when the streetcar hits the touch sensor at station 1, we needed to create a function (Figure 8) that looked at the topic explorerhat/input.1. If this topic's payload is 1 (i.e., touched), then the Explorer HAT motor can be stopped by sending a message with the topic motor.one and payload = 0. A similar function is created for the touch sensor at station 2, except the topic explorerhat/input.2 is monitored.

Figure 8: Function to stop the streetcar at station 1.

After the streetcar hits a touch sensor, we want it to pause and then move in the opposite direction. A delay node was used for the pause, followed by a function node to send a message payload with the new speed and direction (Figure 9). For this project, station 1 restarted with a speed of -70, and station 2 restarted with a speed of 70 (i.e., +70). The complete logic with both manual and automatic control is shown in Figure 10.

Figure 9: Function to reverse the direction of the streetcar at station 1.
Figure 10: Full streetcar logic.

Summary

The combination of Lego Mindstorms components, the Explorer HAT Pro, Node-RED, and a Raspberry Pi can be the starting point for many interesting projects. Here, we created a streetcar that runs its route automatically, but also allows manual intervention – such as for those ubiquitous summer street repair projects.

Some future ideas for this project are:

  • writing the code in Python
  • adding a speed adjustment
  • creating an elevator instead of a streetcar

The Author

Brooke and Leah Metcalfe are 14-year-old twins who live in Burlington, Canada. When they aren't doing computer projects with their dad, they like to swim, paddle board, and ski. Their blog site is https://funprojects.blog.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content