Skip to content

nonik0/CardputerLegoTrainControl

Repository files navigation

Overview

This is a simple program for the M5Stack Cardputer to control several LEGO trains or accessories with one device. All supported hubs/devices have functionality to control the motor speed with specific functionality described in the section for each device type. Supported devices are:

fullui

Thanks to the Legoino library for making this a lot easier to work with Power Functions and Powered Up, and giving me some initial boilerplate and design work for the SBrick and CircuitCubes BT libraries I wrote.

UI / Controls:

Two remote devices can be viewed on screen and controlled at the same time. The two active, on-screen remotes can be easily toggled between the four supported remote types using the controls and will be referenced as the left remote and the right remote. There is an indicator in the upper left that shows which types of the remote are active (left is red and right is blue). The button layout displayed on the screen roughly maps to the layout of the keys on the Cardputer's keyboard.

Function Left Remote Right Remote Description
Left Port Control e, s, d i, j, n motor control
Right Port Control r, d, x o, k, m motor control
Aux1 esc del/backspace BT connection toggle, channel toggle
Aux2 tab \ specific to remote device
Change Remote ctrl space change remote to show/control
Port Function Toggle 3, 4 8, 9 change function of port, specific to remote device
Function Shift fn ok/enter hold down to change function of certain keys, specific to remote device
Display Brightness control brightness of display

Powered Up Control

pu_0

Function Description
Aux1 Toggle BT connection
Aux2 Toggle LED color

The BT connection will autodisconnect if the hub is left connected and no motors are on.

with Color & Distance Sensor:

pu 1 pu 2

Function Description
Aux1 Toggle BT connection
Aux2 shows last color detected by sensor, resume after stop
Port Control Up toggle "speed up" action: increment speed, off
Port Control Stop toggle "stop" action: stop until interrupt (button on train or aux2 key), wait 2s/5s/9s and continue, off
Port Control Down toggle "speed down" action: decrement speed, off
Function Shift + Port Key change trigger color (defaults green, red, yellow)

When a color & distance sensor is plugged in, it will be auto-detected and the port function will change. The sensor should attached to the train facing down onto the track. The sensor can trigger three different auto-actions when the corresponding color on the port button is seen by the sensor. These trigger colors can be changed, see the table. The sensor distance is also measured and the train motor will turn off if the distance measured is too great, such as when the train tips over on the track.

Power Functions (IR) Control:

pf 0 pf 1 pf 2

Function Description
Aux1 change IR channel (1-4)
Aux2 change between modes
Port Function Toggle toggle between motor and switch mode

Aux1 simply changes the IR channel being controlled and needs to match IR receiver.

Aux2 controls the remote modes, which cycle between normal -> state -> state/broadcast -> broadcast -> normal.

  • State mode: tracks the speed of the motor on the Cardputer and sends specific speed commands to the remote device, as opposed to sending simple motor speed increment signals.
  • Broadcast mode: broadcasts all IR commands using ESP-NOW such that other ESP devices can pick up and rebroadcast the IR signal, extending the range of the Cardputer's weak IR. See the PowerFunctionsIRRepeater project for a simple implementation of a repeater using M5Atom. Can also listen to other broadcasts from other devices and show/update state locally.

Port Function Toggle will switch a port between motor and switch functionality. Switch functionality is for when a motor is used to control a track switch (like this example). Pressing the up/down port button will send a one second pulse to the motor to switch the track to that direction, and the stop/center port button will toggle the switch state.

SBrick Control

sb 0 sb 1 sb 0

Function Description
Aux1 toggle BT connection
Port Function Toggle change port controlled on device (A, B, C, D)

SBrick has 4 ports to control. Use the port function toggle to change control between ports A, B, C, and D. Battery voltage and temperature are shown. The WeDo motion and tilt sensors are also (potentially) auto-detected and configured. When detected, an indicator on the remote is shown as the sensoor readings on the corresponding port buttons. If any motion or tilt is detected, like when the train is derailed, all motors will be turned off. If a sensor is not functioning properly, the sensor can be recalibrated by pressing any port key for the sensor.

CircuitCubes Control

cc 0 cc 1

Function Description
Aux1 Toggle BT connection
Port Function Toggle change port controlled on device (A, B, C)

CircuitCubes has 3 ports to control motors (or other devices via PWM like LEDs, etc.). Use the port function toggle to change control between ports A, B, and C. Battery voltage is also shown.

Ideas

  • track speed to be able to keep constant as battery voltage falls
    • track speed with specific calibration tile colors? measure time between two tiles and track
    • track speed with color sensor and rate of change? (i.e. motor moving but no color change after X time -> bump speed)
  • support for using multiple remotes for one device type

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published