A pythonic implementation of ftrobopy using asyncio.
Warning!
Even when I think this program should work, I didn't test it duo to my broken accu. If you want to use it now, do it at your own risk. If you find any error, please open a new GitHub Issue. I will try to test the program as fast as possible.
TXTControl is an asynchronous high-level alternative to ftrobopy. It allows easy integration into modern existing applications.
When I started programming my TXT robot, I had to use ftrobopy as there were no alternatives. After a lot of frustrations about this very unpythonic library, which looked as if it was the first Python program by a C programmer, I finally brought by robot to life. But my code looked like C, not Python. Then, a few years later, I wrote this program to help people with the same problem.
New to asyncio? Check out the official Python asyncio tutorial: https://docs.python.org/3/library/asyncio.html
The following example will play a sound if the button is pressed otherwise, the motor will run. The program ends when the terminator button is hit.
from txtcontrol import TXT, Motor, Button, Speaker, Sound
async def main():
with TXT() as txt:
motor = await Motor.create(txt, 0)
button = await Button.create(txt, 0)
terminator_button = await Button.create(txt, 1)
speaker = Speaker(txt)
while not terminator_button.state:
if button.state:
motor.speed = 512
speaker.stop_sound()
else:
motor.speed = 0
if speaker.finished:
speaker.play_sound(Sound.CAR_HORN_SHORT)
await txt.wait() # Wait for the next update intervall
Base class for TXT-related errors.
TXT-related ConnectionError.
The class representing the TXT connection.
If host
is not given, TXTControl tries to find a TXT using the following ports:
192.168.7.2
USB (Ethernet)192.168.8.2
WLAN192.168.9.2
Bluetooth
from txtcontrol import TXT
async with TXT() as txt:
print("TXT connected!")
Query the current status of the TXT.
Returns: device name, device version
Transfer the current configuration to the TXT.
This function is automatically called when using the create
method to create inputs or outputs.
Wait until the next time the TXT sends new input values.
Contextmanager that syncs multiple commands without sending the current state to the TXT.
Warnings: This method just blocks the connection. Long-running operations will result in a deadlock or in a TXTConnectionError!
Class representing a TXT motor.
Create a new Motor class. Automatically updates the config of the TXT.
Args:
- txt: The base TXT class object.
- port: the port of the motor. 0–3 for M1–M4
The current motor speed.
The current target distance.
The current value of the motor counter (C1-C4).
If the motor reached its goal position.
Stop the motor.
Class for running two synced motors.
The current motor speed.
The current target distance.
The current value of the motor counter (C1-C4).
If the motors reached their goal position.
Stop the motors.
Class representing a simple output like a LED.
Create a new Output class. Automatically updates the config of the TXT.
Args:
- txt: The base TXT class object.
- port: The output port. 0-7 for port 1-8.
The current output level (0–512)
Base class for input. Should not be used directly!
Create a new input class instance. Automatically updates the TXT config.
Args:
- txt: the base TXT class.
- port: the input port. 0–7 for port I1-I8
The current input value.
The current input value (pressed or not).
Resistor input.
Converts the current input to a temperature
Ultrasonic input.
Voltage input.
Values:
- WHITE = 0
- RED = 1
- BLUE = 2
Color sensor input.
The current color.
Trail follower input.
If a trail is recognized or not.
The raw input value.
The sounds that the TXT can play. The sound names are not the original ones!
Values:
- EMPTY = 0
- AIRPLANE = 1
- ALARM = 2
- BELL = 3
- BRAKES = 4
- CAR_HORN_SHORT = 5
- CAR_HORN_LONG = 6
- BREAKING_WOOD = 7
- EXCAVATOR = 8
- FANTASY_1 = 9
- FANTASY_2 = 10
- FANTASY_3 = 11
- FANTASY_4 = 12
- FARM = 13
- FIRE_SIREN = 14
- CAMPFIRE = 15
- FORMULA1_CAR = 16
- HELICOPTER = 17
- HYDRAULIC = 18
- RUNNING_ENGINE = 19
- STARTING_ENGINE = 20
- PROPELLER_PLANE = 21
- ROLLER_COASTER = 22
- SHIP_HORN = 23
- TRACTOR = 24
- TRUCK = 25
- WINK = 26
- DRIVING_NOISE = 27
- RAISE_HEAD = 28
- TILT_HEAD = 29
Class for playing sounds on the TXT.
Play the given sound on the TXT.
Stop the current playing sound.
If the current sound has finished playing.
Wait for the current sound to finish playing.
Start the camera.
Stop the camera.
The last frame the camera took.
The current frame size.