File size: 4,478 Bytes
2499126 7e745ee 2499126 7e745ee c5bd0e7 bd88e0f 7e745ee bd88e0f c5bd0e7 bd88e0f c5bd0e7 bd88e0f c5bd0e7 bd88e0f 2499126 bd88e0f 7e745ee bd88e0f 7e745ee bd88e0f 7e745ee bd88e0f 7e745ee bd88e0f b498103 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
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()
|