|
import gradio as gr |
|
from PIL import Image |
|
from client import CobotController |
|
from utils import get_credentials |
|
import json |
|
import uuid |
|
|
|
user, pwd, host, endpoint, port = get_credentials(False) |
|
client = CobotController(user, pwd, host, port, endpoint) |
|
|
|
def handle_request( |
|
selected_command, angle1=0.0, angle2=0.0, angle3=0.0, angle4=0.0, |
|
angle5=0.0, angle6=0.0, coord1=0.0, coord2=0.0, coord3=0.0, coord4=0.0, |
|
coord5=0.0, coord6=0.0, gripper_value=0.0, speed_angles=0.0, speed_coords=0.0, |
|
speed_gripper=0.0 |
|
): |
|
img = None |
|
if selected_command == "query/angles": |
|
response = client.get_angles() |
|
elif selected_command == "query/coords": |
|
response = client.get_coords() |
|
elif selected_command == "query/gripper": |
|
response = client.get_gripper_value() |
|
elif selected_command == "query/camera": |
|
response = client.get_camera() |
|
img = response.pop("image") |
|
elif selected_command == "control/angles": |
|
response = client.send_angles( |
|
angle_list = [angle1, angle2, angle3, angle4, angle5, angle6], |
|
speed = speed_angles |
|
) |
|
elif selected_command == "control/coords": |
|
response = client.send_coords( |
|
angle_list = [coord1, coord2, coord3, coord4, coord5, coord6], |
|
speed = speed_coords |
|
) |
|
elif selected_command == "control/gripper": |
|
response = client.send_gripper_value( |
|
value = gripper_value, |
|
speed = speed_gripper |
|
) |
|
|
|
text_output = json.dumps(response, indent=4) |
|
if img is not None: |
|
return text_output, gr.update(value=img, visible=True) |
|
return text_output, None |
|
|
|
options = [ |
|
"query/angles", "query/coords", "query/gripper", "query/camera", |
|
"control/angles", "control/coords", "control/gripper" |
|
] |
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("# MyCobot 280pi MQTT Control demo") |
|
gr.Markdown("This is a public demo of the MyCobot 280pi...") |
|
|
|
selected_command = gr.Dropdown(choices=options, label="Select command") |
|
|
|
with gr.Group(visible=False) as query_angles_group: |
|
pass |
|
|
|
with gr.Group(visible=False) as query_coords_group: |
|
pass |
|
|
|
with gr.Group(visible=False) as query_camera_group: |
|
pass |
|
|
|
with gr.Group(visible=False) as query_gripper_group: |
|
pass |
|
|
|
with gr.Group(visible=False) as control_angles_group: |
|
angle1 = gr.Number(value=0.0, label="Angle 1", step=5.0) |
|
angle2 = gr.Number(value=0.0, label="Angle 2", step=5.0) |
|
angle3 = gr.Number(value=0.0, label="Angle 3", step=5.0) |
|
angle4 = gr.Number(value=0.0, label="Angle 4", step=5.0) |
|
angle5 = gr.Number(value=0.0, label="Angle 5", step=5.0) |
|
angle6 = gr.Number(value=0.0, label="Angle 6", step=5.0) |
|
speed_angles = gr.Slider(value=50.0, minimum=0.0, maximum=100.0, step=1.0, label="Speed") |
|
|
|
with gr.Group(visible=False) as control_coords_group: |
|
coord1 = gr.Number(value=0.0, label="X coordinate", step=5.0) |
|
coord2 = gr.Number(value=0.0, label="Y coordinate", step=5.0) |
|
coord3 = gr.Number(value=0.0, label="Z coordinate", step=5.0) |
|
coord4 = gr.Number(value=0.0, label="Rotation X", step=5.0) |
|
coord5 = gr.Number(value=0.0, label="Rotation Y", step=5.0) |
|
coord6 = gr.Number(value=0.0, label="Rotation Z", step=5.0) |
|
speed_coords = gr.Slider(value=50.0, minimum=0.0, maximum=100.0, step=1.0, label="Speed") |
|
|
|
with gr.Group(visible=False) as control_gripper_group: |
|
gripper_value = gr.Slider(minimum=0.0, maximum=100.0, step=1.0, label="Gripper value") |
|
speed_gripper = gr.Slider(value=50.0, minimum=0.0, maximum=100.0, step=1.0, label="Speed") |
|
|
|
def toggle_visibility(command): |
|
return ( |
|
gr.update(visible=command == "query/angles"), |
|
gr.update(visible=command == "query/coords"), |
|
gr.update(visible=command == "query/camera"), |
|
gr.update(visible=command == "query/gripper"), |
|
gr.update(visible=command == "control/angles"), |
|
gr.update(visible=command == "control/coords"), |
|
gr.update(visible=command == "control/gripper"), |
|
gr.update(visible=False), |
|
gr.update(value="") |
|
) |
|
|
|
submit_button = gr.Button("Send Command") |
|
output_text = gr.Textbox(label="") |
|
output_image = gr.Image(visible=False) |
|
|
|
selected_command.change( |
|
toggle_visibility, |
|
selected_command, |
|
[ |
|
query_angles_group, query_coords_group, query_camera_group, query_gripper_group, |
|
control_angles_group, control_coords_group, control_gripper_group, output_image, |
|
output_text |
|
] |
|
) |
|
|
|
submit_button.click( |
|
handle_request, |
|
inputs=[selected_command, angle1, angle2, angle3, angle4, angle5, angle6, |
|
coord1, coord2, coord3, coord4, coord5, coord6, gripper_value, speed_angles, |
|
speed_coords, speed_gripper], |
|
outputs=[output_text, output_image] |
|
) |
|
|
|
app.launch() |
|
|