Programming Tux Droid with Python
Note: This is the print view with all the tutorial pages on one page. The paginated version is available here, if you prefer that.
Tux Droid... How does it work?
Introduction
Tux Droid is a new intelligent computer gadget. It's your new clever buddy specially developed for Linux and the Open Source community. In short, Tux Droid is a wireless robot connected to your PC. Equipped with sensors, motors, a microphone and speaker he can easily interact with his environment. With Tux Droid you will be able to quickly program some funny stuff, even if you are just now getting into programming.Tux Droid, the robot
Depending on your interests or background, Tux Droid can be considered as a robot. He has three motors for actuating the eyes, beak and flippers, and to make him rotate. Furthermore he has two blue LED's in the eyes which can be controlled individually. Tux Droid also comes with a remote control to operate him from a distance. With the available source codes you can develop new programs and at at same time gain knowledge about robotics. But it would be wrong to simply view Tux Droid as a robot. The available software turns Tux Droid in a clever computer agent and is pretty straightforward to program.
The following table shows the main functions of Tux Droid:
|
Motors/output |
Sensors |
Audio |
Communication |
|
Shared motor for eyes and beak |
Light sensors |
Microphone |
Dongle USB |
|
Flipper motor |
Right flipper switch |
Speaker |
RF 2.4 Ghz link with Tux Droid |
|
Rotation motor |
Left flipper switch |
Flash audio memory |
I2C for the CPU's |
|
2 blue LED's |
Head button |
|
I2C for external sensors |
|
Infrared emitter |
Infrared receiver |
|
|
Tux Droid, your computer buddy
The graphic below explains the main components powering the Tux Droid software:
Don't panic! Knowing the software architecture in detail is not required to program Tux Droid. But it is useful to know that one daemon is responsible for the behavior of the robot (Tux Droid daemon) and another one for the voice synthesis (TTS resource daemon). Both daemons are accessible via TCP/IP sockets in server mode (S). The applications connect with these sockets to send and receive requests.
An interface, or what we normally call an API, is made available to aid you for programming Tux Droid. This API is written in Python, an object-oriented language known for its power and many libraries. But Python is by many probably most appreciated for its simplicity.
Before the fun starts
The following is required to start the "tux shell" and to connect it with the daemons:
Running the "tux shell" program
The program to control Tux Droid is called "tux shell". Now, to start it:- Open a terminal window in Linux
- Type the command "tuxsh"
$ tuxsh
If all goes well, the terminal window will display this:
---------------------------------------------------------------
TUXDROID PYTHON API 0.2.0
---------------------------------------------------------------
>>>
Connecting "tux shell" with Tux Droid
To control Tux Droid, messages need to be sent to the "daemons". These "daemons" are tiny programs that speak the language of Tux Droid. To be able to send them messages, you will need to know the following:
- The computer address where Tux Droid is located. The address of a computer is called an "IP address". In the graphic above we use vvv.xxx.yyy.zzz
- The number where the daemon listens for incoming messages. This number is what we call a "port". To send messages we use port 5000. Now, to specify we want to send messages to computer vvv.xxx.yyy.zzz trough port 5000, you type:
>>> tux.daemon.connect(5000,'192.168.0.1')
How to access documentation?
The Tux Droid function classes
The Tux Droid functions are divided into "classes" depending on the use of the functions. Classes help to structure the functions better. Three function classes are important for this tutorial:- <cmd> class: This function class contains the commands to control the Tux Droid motors. For example: with the function [mouth_open] you can open the beak. This function is located in the [cmd] class, which in its turn is part of the [tux] class.
Executing this function, goes like this:
tux.cmd.mouth_open()
- <status> class: Functions from this class will provide you with information from the sensors and the robot status. For example: the following command will display a parameter describing the condition of the light sensor:
tux.status.light_level()
- <tts> class: this one controls the communication with the voice synthesis daemon. With these functions you can let Tux Droid say a sentence, or specify the kind of voice, etc...
tux.tts.speak('I am Tux. Tux Droid !')
Documentation
The documentation is divided according to the different classes of the Tux Droid functions. It can be accessed in two ways:- By looking up the online function class documentation with tux-api-documentation
- to get the documentation on the <cmd> class, click cmd-class
- to get the documentation on the <status> class, click daemon-class
- to get the documentation on the <tts> class, click tts-class
- Or you can access the same documentation from the tux shell, just type:
>>> tux.misc.doc(tux)
And the complete <tux> class documentation will be displayed.
>>> tux.misc.doc(tux.cmd)
The above command will display all the functions from the <cmd> subclass within the <tux> class.
And next an example of a command to display the documentation of one single function:
>>> tux.misc.doc(tux.cmd.eyes_on)
A few words of Python
Once "tuxsh" is started, it's useful to know about some of the basic constructions in Python.
Assigning a variable
Here we will assign the variable [name] with the text string 'John'. Like this:>>> name = ’John’Of course you can try this with your own name.
>>> print name
John
>>>
Strings
The next code will display the variable in a more fluent way:>>> print ’My name is %s.’ %nameThe character [%s] indicates that the displayed variable value is a string.
My name is John.
>>>
Functions
The above line of text is not very convenient to retype every time. The solution is to make it a function. This is done like this:>>> def display(n):
... print ’My name is %s.’ %n
>>>
Note that [print] is more indented to the right than the first line of code. This means that the instruction [print] is located within the function [display]. This is done with the "tab" key. This approach is very important in Python because it is part of the basic grammar rules. With this method we have defined the function [display]. This function will use the variable [n] that needs to be given when executing the command:
>>> display('John')
My name is John.
>>> display('David')
My name is David.
>>>
Loops
An important construction in every programming language is the loop. For example:>>> for number in range(10) :
... print ’%d’ % number
You read the instructions like this:
"For the value of the variable [number] we count from 0 to 10, display the value in decimals." The result:
>>> for number in range(10) :
... print ’%d’ % number
0
1
2
3
4
5
6
7
8
9
Conditions
Another important construction is the condition. This will execute a code as long the condition is met. The following lines of code describe the function [test] with the variable [x] as a parameter. It will check if [x] has the value [5], and if so it will display a message.
>>> def test(x):
... if x == 5 :
... print ’x has value 5’
... else :
... print ’x has not value 5 !’
...
>>>
What happens if we try to execute this function with [test(4)]? Try it!
>>> test(4)
x has not value 5 !
>>>
Conclusion
If you have reached this point, you know some of the basic Python constructions and you can start to program Tux Droid.
Making Tux Droid move and talk
What ?
The following exercises will let you command the Tux Droid movements. The presented exercises can be solved by using the Tux Droid documentation and the Python basic constructions explained previously.Exercise 1:
- Make the robot rotate two times to the left.
Exercise 2:
- Make tux Droid blink the eyes 6 times.
Exercise 3:
- First open and then close the beak.
Exercise 4:
- Make Tux Droid say something in English with a female voice.
Exercise 5:
- Let tux Droid count with the voice synthesis from 0 to 9.
Exercise 6:
- Let Tux Droid count from 0 to 9 like in the above exercise. But
this time he needs to say more than just the numbers. For every number Tux needs to say: "The number is ", followed by the number. And if the number is 5 he has to say: "The magic number is " followed by the number.
The solutions
Exercise 1:
- Make the robot rotate two times to the left.
>>> tux.cmd.spinl_on(8,5)
Exercise 2:
- Make tux Droid blink the eyes 6 times.
>>> tux.cmd.eyes_on(12)
Exercise 3:
- First open and then close the beak.
>>> tux.cmd.mouth_open()
>>> tux.cmd.mouth_close()
Exercise 4:
- Make Tux Droid say something in English with a female voice.
>>> tux.tts.select_voice_us_female()
>>> tux.tts.speak('Hello there !')
Exercise 5:
- Let Tux Droid count with the voice synthesis from 0 to 9.
>>> for number in range(10):
... tux.tts.speak(number)
>>>
Exercise 6:
Let Tux Droid count from 0 to 9 like in the above exercise. But this time he needs to say more than just the numbers. For every number Tux needs to say: "The number is ", followed by the number. And if the number is 5 he has to say: "The magic number is " followed by the number.>>> for number in range(10):
... if number == 5:
... tux.tts.speak(’The magic number is %d’ % number)
... else :
... tux.tts.speak(’The number is %d’ % number)
>>>