From a30877fd3d6b484bd15548eee3c3091bf510967f Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Fri, 10 Jul 2020 14:22:07 +0200 Subject: [PATCH] official_models/pup/trains_sensors: tilt project This adds a project that shows how you can combine LEGO trains with the Powered Up Tilt Sensor to detect slopes in a train track. --- .../trains_sensors/slope_detection/README.md | 32 +++++++++++++++++ .../trains_sensors/slope_detection/slope.py | 35 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 official_models/pup/trains_sensors/slope_detection/README.md create mode 100644 official_models/pup/trains_sensors/slope_detection/slope.py diff --git a/official_models/pup/trains_sensors/slope_detection/README.md b/official_models/pup/trains_sensors/slope_detection/README.md new file mode 100644 index 0000000..1effbf8 --- /dev/null +++ b/official_models/pup/trains_sensors/slope_detection/README.md @@ -0,0 +1,32 @@ +# Rail slope detection + +Made by: The Pybricks Authors + +## Introduction + +This project shows how you can integrate the +[Tilt Sensor](https://docs.pybricks.com/en/latest/pupdevices.html#tilt-sensor) +in your LEGO trains. You can use this sensor to detect a slope in the rail +track. + +## Demo + +This program makes the train drive until it detects a slope of 8 degrees or +more. Then it drives back down. You can watch a video +[here](https://www.youtube.com/watch?v=Wc0mRIBjhIU). + +## Requirements +- Generic LEGO train, such as 60197 or 60198 +- Powered Up [Tilt Sensor](https://docs.pybricks.com/en/latest/pupdevices.html#tilt-sensor) + +## Building instructions +Build the train using the standard instructions. Mount the Tilt Sensor anywhere +on the train. See the video for an example. Mount it horizontally. The side +with the cable should face towards the front: in the direction of driving. + +## Ports +- Train motor on port A +- Tilt sensor on port B + +## Programming and running instructions +The train must start on a horizontal track. Then run the program. diff --git a/official_models/pup/trains_sensors/slope_detection/slope.py b/official_models/pup/trains_sensors/slope_detection/slope.py new file mode 100644 index 0000000..4615d0a --- /dev/null +++ b/official_models/pup/trains_sensors/slope_detection/slope.py @@ -0,0 +1,35 @@ +from pybricks.pupdevices import TiltSensor, DCMotor +from pybricks.parameters import Port +from pybricks.tools import wait + +# Initialize the train motor. +# If you have a motor with encoders, use the Motor class instead. +train = DCMotor(Port.A) + +# Initialize the tilt sensor. +sensor = TiltSensor(Port.B) + +# Measure the tilt while the train is on a flat surface. +pitch_start, roll_start = sensor.tilt() + +# Start driving. +train.dc(-50) + +# We need to reach a constant speed before we can check tilt again, +# because acceleration affects tilt. So we wait a second. +wait(1000) + +# Wait for the train to sense the hill. +while True: + pitch_now, roll_now = sensor.tilt() + + wait(10) + + # If we reached an extra 3 degrees, exit/break the loop. + if pitch_now >= pitch_start + 8: + break + +# Drive backwards for a few seconds and then stop. +train.dc(40) +wait(3000) +train.dc(0)