Hello, I have developed this Python library that allows connecting to Bittle through Bluetooth and WiFi (and Serial communication soon) and sending commands and custom messages.
pyBittle objective is keeping it simple to connect to and manage Bittle while allowing more complex operations.
The library repository can be found here: github/pyBittle
You can read in-depth documentation and usage examples here: gitbook/pyBittle
pyBittle can be installed using the following comand:
pip install pyBittle
Connecting to your Bittle and sending commands to it is as simple as shown below:
bittle = pyBittle.Bittle() # Create Bittle instance
# Connecting through Bluetooth
is_connected = bittle.connect_bluetooth()
if is_connected:
bittle.send_command_bluetooth(pyBittle.Command.GREETING)
# Connecting through WiFi
bittle.wifiManager.ip = '192.168.1.132' # Set Bittle IP address
has_connection = bittle.has_wifi_connection()
if has_connection:
bittle.send_command_wifi(pyBittle.Command.SIT)
pyBittle has been tested on Raspbian and Windows10. WiFi and Bluetooth connection are supported on Raspbian, only WiFi connection is supported on Windows10 due to pyBluez (dependency) limitations.
pyBittle structure
pyBittle is composed of three modules:
bittleManager: High level module that allows connecting to Bittle through Bluetooth and WiFi and sending and receiving messages to control it. bittleManager contains the following classes:
Bittle: This class represents your Bittle. Has a bluetoothManager and a wifiManager that allow sending messages and receive their responses from your Bittle.
Command: Enum that defines avaliable commands that can be sent to Bittle (greeting, sit, pushup, balance...).
Direction: Enum that defines avaliable movement directions (forward, forward left, backward...).
Gait: Enum that defines avaliable gaits (walk, crawl, trot, run).
bluetoothManager: Module that manages Bluetooth connection; allows finding your Bittle among paired devices, connecting to it and sending and receiving messages.
wifiManager: Module that manages WiFi connection through REST API hosted in providen WiFi dongle; allows sending and receiving messages to Bittle.
bittleManager is the main module, instanciating Bittle class is enough to manage and control your Bittle.
Usage examples
The following examples show the full process of creating a Bittle instance and communicating with it through Bluetooth and WiFi.
Sending 'khi' (greet) and 'd' (rest) commands through Bluetooth:
import pyBittle
import time
greet_command = pyBittle.Command.GREETING # 'khi' message
rest_command = pyBittle.Command.REST # 'd' message
bittle = pyBittle.Bittle() # Create Bittle instance
isConnected = bittle.connect_bluetooth() # Search for your Bittle among paired devices and connect to it
if isConnected:
bittle.send_command_bluetooth(greet_command) # Send 'khi' message
received_msg = bittle.receive_msg_bluetooth() # Get response from Bittle
decoded_msg = received_msg.decode('utf-8') # received_msg is byte type
decoded_msg = decoded_msg.replace('\r\n', '') # Remove new line from decoded_msg
print(f"Received message: {decoded_msg}, expected: k")
time.sleep(5) # Give Bittle few seconds to finish the sent action
bittle.send_command_bluetooth(rest_command) # Send 'd' message
received_msg = bittle.receive_msg_bluetooth()
decoded_msg = received_msg.decode('utf-8')
decoded_msg = decoded_msg.replace('\r\n', '')
print(f"Received message: {decoded_msg}, expected: d")
time.sleep(5)
bittle.disconnect_bluetooth() # Close Bluetooth connection
else:
print("Can't connect to Bittle!")
Sending 'kstr' (stretch) and 'ksit' (sit) commands through WiFi:
import pyBittle
import time
stretch_command = pyBittle.Command.STRETCH # 'kstr' message
sit_command = pyBittle.Command.SIT # 'ksit' message
bittle = pyBittle.Bittle() # Create Bittle instance
bittle.wifiManager.ip = '192.168.1.131' # Set REST API IP address
if bittle.has_wifi_connection():
response = bittle.send_command_wifi(stretch_command) # Send 'kstr' message
print(f"Received response: {response}, expected: 200")
time.sleep(5) # Give Bittle few seconds to finish the sent action
response = bittle.send_command_wifi(sit_command) # Send 'ksit' message
print(f"Received response: {response}, expected: 200")
time.sleep(5)
else:
print("Can't connect to Bittle!")
Changing gait and sending movement commands:
import pyBittle
import time
forward = pyBittle.Direction.FORWARD
forward_right = pyBittle.Direction.FORWARDRIGHT
stop = pyBittle.Command.BALANCE # BALANCE is Command type, not Direction type!
crawl = pyBittle.Gait.CRAWL
trot = pyBittle.Gait.TROT
bittle = pyBittle.Bittle() # Create Bittle instance
bittle.wifiManager.ip = '192.168.1.132' # Set Bittle IP address
has_connection = bittle.has_wifi_connection()
if has_connection:
bittle.gait = crawl # Change current gait (by default, WALK) to CRAWL
bittle.send_movement_wifi(forward)
time.sleep(5)
bittle.send_movement_wifi(forward_right)
time.sleep(3)
bittle.gait = trot # Change current gait (WALK) to TROT
bittle.send_movement_wifi(forward)
time.sleep(5)
bittle.send_command_wifi(stop) # Send command, not movement (as BALANCE is Command type)
time.sleep(2)
else:
print("Can't connect to Bittle!")
Project made with pyBittle
One of the Bittle-related projects I have made is BittleXboxController, which makes use of pyBittle. This project makes full use of command sending and movement management.
pyBittle has been my most ambitious and serious project so far, I hope you all find it useful.
Anyone ever crack Bluetooth on Mac as yet?? For Python module??? Thanks
has anyone tried a PS4controller? I have a Bittle X and would like to know if it is possible. Thanks.
@Struzck seems like you are not updating it for a while, if you dont mind, i'd takeover the improvements and add state reporting code for NN development
Hi, my kid can't read wery well (4yo), so I wanted to change text commands in android app to be pictures instead. Can I ask for a source code for android app?
hello I am bittle newbie. I want to run it with Python. What programs should I use and what files should I download? Thank you for letting me know from a beginner's point of view.
Hi, interesting idea, install crashed when tried on Mac OS.
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
error in PyBluez setup command: use_2to3 is invalid.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
Great! Mark it.
Thanks, but from screen shot you can see that the serial port is ttyS0 (though the last digit 0 is cut off from the picture)
@Rongzhong Li please advise if you have met this issue as for previous message screenshot. Furthermore I found out that by replacing the USB cable the WIFI adapter and USB loader (programmer) operate properly. I apologize for bothering. Finally while testing WIFI adapter I realized that the IP address indicated in the manual https://docs.petoi.com/modules/wifi-esp8266 192.168.4.1 did not work. I connected the serial port of USB loader by cable to the Ubuntu *with WIFI adapter connected to USB loader as for instructions) and opened the Arduino IDE serial port and read the correct address 192.168.88.33
In this way the control panel commands on mobile phone work smoothy.
Test result: the full cycle is executed by Bittle with Battery ON. Response is still empty or with incorrect characters, as for below picture
Try use logger.info(f"Response is: {response.decode('ISO-8859-1')}") in the example.py
@Struzck @Rongzhong Li I found the problem: Serial master folder was not located in the correct location, that is not into pyBittle-Master folder. Now it works, but there is still a problem as you can see from following screenshot, after running python3 exampley.py The top part is run with RPi powered and Bittle connected but with battery OFF. The bottom part is with battery ON. Whereas with battery OFF the full script is executed, when Bittle actually moves and carry out the instruction, it stops after the 7. step.
Additionally on top you can see the 'Response' in full, whereas on the bottom 'Response' is empty. Any hint ?
@Struzck @Rongzhong Li I followed pyBittle installation instruction on RPi (Buster OS) as for https://enriquemoran95.gitbook.io/pybittle/user-guide/installation, However when running last command 'pip install . ' I receive following error: AttributeError: 'PosixPath' object has no attribute 'read_text'. Is there anything I am missing ?
Hello! Thanks for the reply, I really enjoy your project, my Bittle is coming and I'm currently developing some code on computer vision and object following for my new pet :) can you please tell me, are there any standard python libraries with movement commands (methods) for Bittle? Like move_forward() or turn_left() or something live that? I couldn't find them on opencat's GitHub.
Hey everyone! I'm new to this robotic stuff.
My question is: Is it possible to make Bittle as interactive and "Alive" as Vector Robot by Anki? And if yes, what do I need to do that? Maybe there are some standard libraries for Bittle or something like that?