Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

データサイエンスに向いたPython用可視化ツールstreamlit その3:ロボットアームxArmと連動したサンプル

実際のところ

{
  "xarm_ip": "192.168.1.100",
  "max_value_x": 500,
  "min_value_x": -500,
  "max_value_y": 500,
  "min_value_y": -500,
  "max_value_z": 500,
  "min_value_z": -500,
  "speed": 100
}
import json
import streamlit as st
from xarm import version
from xarm.wrapper import XArmAPI

# Load configuration from config.json
with open("config.json") as f:
    config = json.load(f)

# Initialize xArm
xarm_ip = config["xarm_ip"]
arm = XArmAPI(xarm_ip)
arm.motion_enable(enable=True)
arm.set_mode(0)
arm.set_state(state=0)

# Get the initial position of xArm
_, init_pose = arm.get_position()
init_x, init_y, init_z, init_rx, init_ry, init_rz = init_pose

st.title("xArm Control using Streamlit")

# Create sliders for X, Y, Z
st.header("Position Control")
x = st.slider("X (mm)", min_value=config["min_value_x"], max_value=config["max_value_x"], value=int(init_x), step=1)
y = st.slider("Y (mm)", min_value=config["min_value_y"], max_value=config["max_value_y"], value=int(init_y), step=1)
z = st.slider("Z (mm)", min_value=config["min_value_z"], max_value=config["max_value_z"], value=int(init_z), step=1)

# Move xArm to the new position
new_pose = [x, y, z, init_rx, init_ry, init_rz]
arm.set_position(*new_pose, speed=config["speed"], wait=True)

# Create a reset button
if st.button("Reset"):
    arm.set_position(*init_pose, speed=config["speed"], wait=True)
    arm.set_state(0)