Bittle()
High level class that represents your Bittle.
Attributes
id (UUID): Bittle's unique id.
bluetoothManager (BluetoothManager): Manager for sending messages to Bittle through Bluetooth.
wifiManager (WifiManager): Manager for sending messages to Bittle through WiFi.
serialManager (SerialManager): Manager for sending messages to Bittle through Serial communication.
gait (Gait): Current gait (WALK by default); used to send movement commands.
commands ({Command : str}): Map with avaliable commands and its Serial translation.
Methods
connect_bluetooth(get_first_bittle=True): Connects to Bittle through Bluetooth connection. Returns True if connected, False otherwise. If get_first_bittle is enabled, searchs for BitleSPP among paired devices and connect to it.
send_command_bluetooth(command): Sends a command to Bittle through Bluetooth connection.
send_msg_bluetooth(message): Sends a custom message to Bittle through Bluetooth connection.
receive_msg_bluetooth(buffer_size=1014): Returns received message from Bittle through Bluetooth connection (bytes).
disconnect_bluetooth(): Closes Bluetooth connection with Bittle.
has_wifi_connection(): Checks wether there is connection with REST API. Returns True if there is connection, False otherwise.
send_command_wifi(command): Sends a command to Bittle through WiFi connection. Returns REST API response code (int), -1 if there is no connection.
send_msg_wifi(message): Sends a custom message to Bittle through WiFi connection. Returns REST API response code (int), -1 if there is no connection.
send_movement_bluetooth(direction): Sends a movement command to Bittle through Bluetooth connection. This method sends current gait plus direction, only WALK gait is avaliable for backward directions.
send_movement_wifi(direction): Sends a movement command to Bittle through WiFi connection. This method sends current gait plus direction, only WALK gait is avaliable for backward directions.
connect_serial(discover_port=True): Connects to Bittle through Serial. Return True if connected, False otherwise. If discover_port is enabled, search among avaliable communication ports the one associated to CH340 USB driver, which is used by Bittle and connect to it.
send_command_serial(command): Sends a command to Bittle through Serial connection.
send_msg_serial(message): Sends a custom message to Bittle through Serial connection.
receive_msg_serial(buffer_size): Returns received message from Bittle through Serial connection (bytes).
send_movement_serial(direction): Sends a movement command to Bittle through Serial connection. Thid method sends current gait plus direction, only WALK gait is avaliable for backward directions.
disconnect_serial(): Closes Serial connection with Bittle.
"""Example: Making Bittle trot forward-left for 5 seconds and stop afterward.
"""
import pyBittle
bittle = pyBittle.Bittle()
bittle.gait = pyBittle.Gait.TROT
bittle.send_movement_wifi(pyBittle.Direction.FORWARDLEFT)
time.sleep(5)
bittle.send_command_wifi(pyBittle.Command.BALANCE) # BALANCE is Command type!
Examples
The following example shows the complete process of creating a Bittle instance and communicating with it through Bluetooth. If connection is successful, send GREETING and REST commands to check whether Bittle receives and replies to them:
import pyBittle
import time
greet_command = pyBittle.Command.GREETING
rest_command = pyBittle.Command.REST
bittle = pyBittle.Bittle() # Create Bittle instance
isConnected = bittle.connect_bluetooth()
if isConnected:
bittle.send_command_bluetooth(greet_command) # Send 'khi' 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: k")
time.sleep(6)
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()
else:
print("Can't connect to Bittle!")
The following example shows the complete process of creating a Bittle instance and communicating with it through WiFi. If connection is successful, send GREETING and REST commands to check whether Bittle receives and replies to them:
import pyBittle
import time
greet_command = pyBittle.Command.GREETING
rest_command = pyBittle.Command.REST
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(greet_command) # Send 'khi' message
print(f"Received response: {response}, expected: 200")
time.sleep(6)
response = bittle.send_command_wifi(rest_command) # Send 'd' message
print(f"Received response: {response}, expected: 200")
time.sleep(5)
else:
print("Can't connect to Bittle!")
The following example shows the complete process of creating a Bittle instance and communicating with it through Serial. If connection is successful, send GREETING and REST commands to check wether Bittle receives and replies to them:
import pyBittle
import time
sit_command = pyBittle.Command.SIT
rest_command = pyBittle.Command.REST
bittle = pyBittle.Bittle() # Create Bittle instance
is_connected = bittle.connect_serial()
if is_connected:
bittle.send_command_serial(sit_command) # Send 'ksit' message
received = bittle.receive_msg_serial()
decoded_msg = received.decode('utf-8')
decoded_msg = decoded_msg.replace('\r\n', '')
print(f"Received message: {decoded_msg}, expected: 'k'")
time.sleep(5)
bittle.send_command_serial(rest_command) # Send 'd' message
received = bittle.receive_msg_serial()
decoded_msg = received.decode('utf-8')
decoded_msg = decoded_msg.replace('\r\n', '')
print(f"Received message: {decoded_msg}, expected: 'd'")
time.sleep(5)
bittle.disconnect_serial()
else:
print("Bittle not found!")
Last updated
Was this helpful?