Examples
Prerequisites
pip install protobuf
protoc --python_out=. proto/asimov_command.proto
Scripts
Script |
What it does |
|---|---|
stand_up.py |
Make the robot stand |
walk_forward.py |
Walk in a direction |
read_telemetry.py |
Print sensor data |
Running
# Change ROBOT_IP in the script, then:
python3 stand_up.py
Common Pattern
All scripts follow the same structure:
import socket
import time
from asimov_command_pb2 import RobotCommand, ControlMode
ROBOT_IP = '192.168.1.100'
ROBOT_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(mode, vx=0, vy=0, vyaw=0):
cmd = RobotCommand()
cmd.mode = mode
cmd.vx, cmd.vy, cmd.vyaw = vx, vy, vyaw
cmd.enable = True
cmd.timestamp_us = int(time.time() * 1e6)
sock.sendto(cmd.SerializeToString(), (ROBOT_IP, ROBOT_PORT))
# Your control logic here
send(ControlMode.CONTROL_MODE_STAND)
Stand Up
#!/usr/bin/env python3
"""Make the robot stand up."""
import socket
import time
from asimov_command_pb2 import RobotCommand, ControlMode
ROBOT_IP = '192.168.1.100'
ROBOT_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(mode, vx=0, vy=0, vyaw=0):
cmd = RobotCommand()
cmd.mode = mode
cmd.vx, cmd.vy, cmd.vyaw = vx, vy, vyaw
cmd.enable = True
cmd.timestamp_us = int(time.time() * 1e6)
sock.sendto(cmd.SerializeToString(), (ROBOT_IP, ROBOT_PORT))
# Enable motors (soft)
print("Enabling motors...")
send(ControlMode.CONTROL_MODE_DAMP)
time.sleep(0.5)
# Stand up
print("Standing up...")
send(ControlMode.CONTROL_MODE_STAND)
time.sleep(2.5)
print("Done!")
Walk Forward
#!/usr/bin/env python3
"""Walk forward for 5 seconds."""
import socket
import time
from asimov_command_pb2 import RobotCommand, ControlMode
ROBOT_IP = '192.168.1.100'
ROBOT_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(mode, vx=0, vy=0, vyaw=0):
cmd = RobotCommand()
cmd.mode = mode
cmd.vx, cmd.vy, cmd.vyaw = vx, vy, vyaw
cmd.enable = True
cmd.timestamp_us = int(time.time() * 1e6)
sock.sendto(cmd.SerializeToString(), (ROBOT_IP, ROBOT_PORT))
# Stand up sequence
print("Standing up...")
send(ControlMode.CONTROL_MODE_STAND)
time.sleep(2.5)
print("Starting balance...")
send(ControlMode.CONTROL_MODE_START)
time.sleep(0.5)
# Walk forward at 0.3 m/s for 5 seconds
print("Walking forward...")
for _ in range(250): # 5 seconds at 50 Hz
send(ControlMode.CONTROL_MODE_MOVE, vx=0.3)
time.sleep(0.02)
# Stop
print("Stopping...")
send(ControlMode.CONTROL_MODE_DAMP)
Read Telemetry
#!/usr/bin/env python3
"""Print telemetry data."""
import socket
from asimov_command_pb2 import RobotState
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 9999))
sock.settimeout(5.0)
print("Waiting for telemetry...")
try:
while True:
data, addr = sock.recvfrom(512)
state = RobotState()
state.ParseFromString(data)
print(f"Mode: {state.current_mode} | "
f"Battery: {state.battery_percent}% | "
f"Motors: {'ON' if state.motors_enabled else 'OFF'}")
except KeyboardInterrupt:
print("\nDone")
except socket.timeout:
print("No telemetry received - is the robot running?")