Raspberry Pi and Grove Sensor Board
2 x Light Sensors (Red) : Sense car position at start/finish
2 x DC Current Sensors (Green): Sense current draw from cars
Running Node-RED, to handle connectivity to MQTT
Running Python, to handle incoming sensor data, and threshold triggering
Power Electronics (see inset)
convert low current digital output from PWM output, to high current 16v output for running DC Motors.
MQTT
Mosquitto MQTT broker running on Heroku service
GitHub Pages
Static site server, running on GitHub infrastructure
Monitor
Shows QR code, which contains URL to GitHub Pages, Single Page Web Application
Parameters show URL of MQTT server, username / password for the service, UUID of RPi installation.
Mobile Phones
Running recent smartphone OS - camera will automatically scan the QR code, and download SPWA from GitHub Pages.
SPWA
Single Page Web App, served from Github Pages, connects with MQTT using URL parameters.
1up
Instance of SPWA on player 1's smart phone
2up
Instance of SPWA on player 2's smart phone
Internet connectivity
supplied to RPi via ethernet / Wifi
supplied to phones via Wifi / mobile data
Usage:
RPi, boots, and reads a configuration file, containing URL + credentials of MQTT broker, UUID of previous run.
RPi checks connection with MQTT broker.
If a previous run UUID is available, RPi removes it from the MQTT server (security)
RPi generates run UUID, and saves in config file
If all is well, generates QR code containing URL, credentials and run UUID and displays on-screen.
RPi runs Node-RED service.
Node-RED service check configuration file for
- URL + credentials of MQTT server.
- UUID of current run
- Resource definitions
- Sensor threshold definitions
Node-RED publishes resource definitions under a known topic structure, beginning with the run UUID.
Node-RED subscribes to throttle and resource demand topics for each supported channel (track)
Node-RED starts Python scripts to monitor sensor values against thresholds.
Players connect with the game using their smart phone to scan the QR code.
Their phone will request load of the SPWA.
SPWA is loaded, and connects with the MQTT broker.
SPWA is able to publish and subscribe to all values on the MQTT tree.
Using the value PiState, it is able to check that the Pi is ready for interaction
Using the value GameState, it is able to communicate with any other instances of the SPWA, to read and write global game variables, such as whether channels (tracks) are in use.
Once the SPWA instance has access to the track, it restricts its throttle demand to a single channel, and can publish this as a percentage. The SPWA subscribes to the throttle value, so that it can react if the demand has not been acknowledged by the Pi.
Resources
The SPWA may deploy 'special weapons' as resources. These are published as a list, which specify id, name, description, and icon URLs. The resources can be found in the tree. Publishing a value to a resource id causes it to be deployed. Subscribing delivers the number of resources left.
The deployment of a resource optionally requires parameters:
- the target channel
Some resources require a target channel to be specified; others do not.
Subscribing to the resource when deployed will give one of 3 values:
- Ready
- Requested
- Busy
Events
A SPWA may subscribe to the Events on its channel (track).
When a resource deployed to with the channel as a target, the Event is reported, with the id of the resource.
Events are reported to all channels if a resource is deployed without a target.
Sensors
A SPWA may subscribe to any one of the sensor channels. Subscribers are updated whenever a sensor is triggered past a threshold.
The following sensors are supported:
- Current sensor: reports when current has dropped below a threshold.
- Light sensor: reports when light has dropped below a threshold.
To reset the trigger, the senor value must pass the threshold in the opposite direction.
Sensor can be used in conjunction with GameState and Throttle to sense when a car is in trouble - for instance in race mode, if there is a demand on the throttle, and the current sensor is triggered: if the race is about to begin, and cars are on their way to the start line.