Lesson 4 of 5Robotics with fischertechnik
Robotics with fischertechnik · Lesson 4 · 30 minutes
Line tracker and phototransistor
What you'll learn
- Detect with the line tracker whether a line is under the sensor
- Measure brightness as a voltage in mV with the phototransistor
- Collect readings in a list and analyse them
The code in this lesson comes from the book “Python for Robots”, Chapter 3.3, and runs unchanged in the simulator. German edition on Amazon ↗
Sensors: the robot’s senses
For your model to react to its surroundings, it needs sensors. The two most important ones for fischertechnik projects are the line tracker and the phototransistor. Both go on the inputs I1 to I8.
The line tracker
The line tracker detects whether there’s a dark line under it or a bright surface. trailfollower(...) gives you a clean value right away: 0 for the line, 1 for bright.
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
# Two line-tracking sensors, left and right, on I1 and I2
track_left = txt.trailfollower(1)
track_right = txt.trailfollower(2)
while True:
value_left = track_left.state() # 0 = black line, 1 = bright surface
value_right = track_right.state()
print(f"Left: {value_left}, Right: {value_right}")
if value_left == 0:
print("Left sensor is on the line.")
sleep(0.2)In the control panel you switch the two sensors between “bright” and “line”. Watch how the output changes.
The phototransistor
The phototransistor measures how much light falls on it. txt.voltage(...) gives you the voltage in millivolts (mV). With the slider in the control panel you change the light:
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
phototransistor = txt.voltage(3) # Sensor on I3
while True:
voltage_mv = phototransistor.voltage()
print(f"Measured voltage: {voltage_mv} mV")
sleep(0.3)Measure first, then set a threshold: depending on the wiring, more light means a higher or a lower voltage. So first hold something bright and something dark in front of the sensor and look at the values with
print(). In the simulator: bright = high voltage (about 800 mV), dark = low (about 200 mV).
For the task you need append(), min(), max() and sum() from the Python part.
Your task
Your turn
Exercise 3.3 from the book: write a sensor logger. Take 20 readings from the phototransistor on I3 one after the other (with a short pause, e.g. sleep(0.1)) and collect them in the list readings. At the end, print: Smallest value: …, Largest value: … and Average: … (the average may be a decimal number).
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
phototransistor = txt.voltage(3)
readings = []
# Take 20 readings and collect them in readings
# Print the smallest, largest and average value- Something to think about
A
forloop withrange(20)– inside itreadings.append(phototransistor.voltage()). - Which command helps?
min(readings),max(readings)andsum(readings)/len(readings)– and print exactly these words. - Gap code – fill in the ___
import ftrobopy from time import sleep txt = ftrobopy.ftrobopy("localhost", 65000) phototransistor = txt.voltage(3) readings = [] for _ in ___: readings.append(___) sleep(___) print(___) print(___) print(___)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
phototransistor = txt.voltage(3)
readings = []
for _ in range(20):
readings.append(phototransistor.voltage())
sleep(0.1)
print(f"Smallest value: {min(readings)}")
print(f"Largest value: {max(readings)}")
print(f"Average: {sum(readings) / len(readings)}")