Lesson 2 of 5Robotics with fischertechnik
Robotics with fischertechnik · Lesson 2 · 30 minutes
Controlling XS motors
What you'll learn
- Run an XS motor forwards and backwards with
setSpeed() - Drive an exact distance in encoder pulses with
setDistance() - Wait with
finished()until a movement is complete
The code in this lesson comes from the book “Python for Robots”, Chapter 3.2, and runs unchanged in the simulator. German edition on Amazon ↗
Power for conveyor belts and barriers
Actuators are all parts that do something: turn, move, shine. On the TXT 4.0 these are mainly the XS motors on M1 to M4 and the lamps on O1 to O8.
Setting the speed
With setSpeed(...) you give a number from -512 (full power backwards) to 512 (full power forwards). 0 stops the motor. Watch the wheel in the control panel when you run it:
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
# Create a motor on output M1
conveyor_motor = txt.motor(1)
# Run the motor forward at half power
conveyor_motor.setSpeed(256)
sleep(2)
# Stop the motor
conveyor_motor.setSpeed(0)
# Run the motor backward
conveyor_motor.setSpeed(-256)
sleep(2)
conveyor_motor.setSpeed(0)Driving an exact distance
The M outputs have encoders. They count how far the motor has turned – in pulses. That’s why you can set an exact distance. First the speed with setSpeed(...), then the target with setDistance(...). This is how the switch in the sorting machine project from the book (chapter 3.6) swings out and back again:
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
switch_motor = txt.motor(2) # Motor on M2
def deflect_switch():
# Briefly swing the switch out and then reset it
switch_motor.setSpeed(400)
switch_motor.setDistance(90)
while not switch_motor.finished():
pass
sleep(0.3)
switch_motor.setSpeed(-400)
switch_motor.setDistance(90)
while not switch_motor.finished():
pass
deflect_switch()
print("The switch is back.")The loop while not ....finished(): pass waits until the motor has arrived. pass means: “Nothing happens here – just check again.”
Watch the order:
setDistance(...)uses the speed you last set withsetSpeed(...). WithoutsetSpeed(...)the speed is 0 – and the motor doesn’t move at all. Thenfinished()waits forever. Try it: delete the first line withsetSpeed(400)in the example above and then press Stop.
Your task
Your turn
A barrier on M2 should open and close again. Drive the motor at speed 400 exactly 180 pulses and wait until it's finished. Then pause for one second. After that, drive 180 pulses back at -400 – and wait again. At the end, print Barrier done.
import ftrobopy
from time import sleep
txt = ftrobopy.ftrobopy("localhost", 65000)
barrier = txt.motor(2)
# 1. Open: 180 pulses at speed 400
# 2. Pause for one second
# 3. Close: 180 pulses at speed -400- Something to think about
First
barrier.setSpeed(400), thenbarrier.setDistance(180)– and the same with -400 for the way back. - Which command helps?
After every
setDistance():whilenotbarrier.finished(): pass – andsleep(1)in between. - Gap code – fill in the ___
import ftrobopy from time import sleep txt = ftrobopy.ftrobopy("localhost", 65000) barrier = txt.motor(2) barrier.setSpeed(___) barrier.setDistance(___) while ___: pass sleep(___) barrier.setSpeed(___) barrier.setDistance(___) while ___: pass 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)
barrier = txt.motor(2)
barrier.setSpeed(400)
barrier.setDistance(180)
while not barrier.finished():
pass
sleep(1)
barrier.setSpeed(-400)
barrier.setDistance(180)
while not barrier.finished():
pass
print("Barrier done")