From fc8086f1ab030a61015ac20f852d18861b1a3a4b Mon Sep 17 00:00:00 2001 From: Angar0Os <122360705+Angar0Os@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:34:32 +0100 Subject: [PATCH 1/4] Add AppState + notified AUTOMATIC DANCE --- app/main.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index bc28f41..7b9ba2d 100644 --- a/app/main.py +++ b/app/main.py @@ -5,24 +5,27 @@ from statistics import median import math from OrbitalCam import OrbitalController +from enum import Enum str_error = "The robot is not connected" -# helpers +class AppState(Enum): + PHYSICAL_INTERACTION = 1 + UI_INTERACTION = 2 + AUTOMATIC_DANCE = 3 +current_state = AppState.AUTOMATIC_DANCE +# helpers def clamp(v, v_min, v_max): return max(v_min, min(v, v_max)) - def rangeadjust_clamp(k, a, b, u, v): return rangeadjust(clamp(k, a, b), a, b, u, v) - def rangeadjust(k, a, b, u, v): return (k - a) / (b - a) * (v - u) + u - def lerp(k, a, b): return a + (b - a) * k @@ -56,7 +59,7 @@ def lerp(k, a, b): hg.RenderReset(res_x, res_y, hg.RF_MSAA8X | hg.RF_FlipAfterRender | hg.RF_FlushAfterRender | hg.RF_MaxAnisotropy) -hg.AddAssetsFolder("resources_compiled") +hg.AddAssetsFolder("../resources_compiled") # AAA render params aaa_config = hg.ForwardPipelineAAAConfig() @@ -179,6 +182,7 @@ def lerp(k, a, b): app_status = "dancing" +#AUTOMATIC DANCE def toggle_button(label, value, x, y): global has_switched mat = hg.TransformationMat4( @@ -233,9 +237,11 @@ def toggle_button(label, value, x, y): return value +#AUTOMATIC DANCE buttonlist = [[100, res_y - 80]] +#AUTOMATIC DANCE / Sert a quoi ? Impression qu'il essaye de faire en sorte de modifier les valeurs des moteurs def is_switching(): for i in buttonlist: mat = hg.TransformationMat4( @@ -318,6 +324,8 @@ def get_v_from_dancing(id_robot): send_dt = 1/10 # send information to the poppy every send_dt # get values from the real robot + + #AUTOMATIC DANCE : requests that give robot new positions if compliance_mode: timer_requests_not_overload += hg.time_to_sec_f(dt) if timer_requests_not_overload > send_dt: @@ -366,6 +374,7 @@ def get_v_from_dancing(id_robot): (hg_m["v"] - v) / (hg.time_to_sec_f(dt)**2), -9999, 9999)) hg_m["v"] = v + #AUTOMATIC DANCE if compliance_mode and compliance_lerp: adjusted_time = rangeadjust(hg.GetClock( ), hg_motors_previous[id][1], hg_motors_previous[id][1] + hg.time_from_sec_f(send_dt), 0, 1) From 046b42cf45d50786536620b9d4407a1423eb7d41 Mon Sep 17 00:00:00 2001 From: Angaros Date: Wed, 11 Dec 2024 17:58:48 +0100 Subject: [PATCH 2/4] Added comments on main and add ui_inteaction_button --- app/main.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/app/main.py b/app/main.py index 7b9ba2d..89b873a 100644 --- a/app/main.py +++ b/app/main.py @@ -9,13 +9,6 @@ str_error = "The robot is not connected" -class AppState(Enum): - PHYSICAL_INTERACTION = 1 - UI_INTERACTION = 2 - AUTOMATIC_DANCE = 3 - -current_state = AppState.AUTOMATIC_DANCE - # helpers def clamp(v, v_min, v_max): return max(v_min, min(v, v_max)) @@ -52,6 +45,7 @@ def lerp(k, a, b): has_switched = False compliance_mode = False compliance_lerp = True +ui_interaction_mode = False win = hg.NewWindow("Harfang - Poppy", res_x, res_y, 32) # , hg.WV_Fullscreen) @@ -181,16 +175,17 @@ def lerp(k, a, b): app_clock = 0 app_status = "dancing" - -#AUTOMATIC DANCE def toggle_button(label, value, x, y): global has_switched + + #Target for user interaction mat = hg.TransformationMat4( hg.Vec3(x, y, 1), hg.Vec3(0, 0, 0), hg.Vec3(1, 1, 1)) pos = hg.GetT(mat) axis_x = hg.GetX(mat) * 56 axis_y = hg.GetY(mat) * 24 + #Rectangular zone drawn on scren who depends on var "value" toggle_vtx = hg.Vertices(vtx_layout, 4) toggle_vtx.Begin(0).SetPos( pos - axis_x - axis_y).SetTexCoord0(hg.Vec2(0, 1)).End() @@ -203,8 +198,9 @@ def toggle_button(label, value, x, y): toggle_idx = [0, 3, 2, 0, 2, 1] hg.DrawTriangles(view_id, toggle_idx, toggle_vtx, shader_for_plane, [], [ - texture_on if value else texture_off], render_state_quad) + texture_on if value else texture_off], render_state_quad) + #if player click on interactive zone if mouse.Down(hg.MB_0): mousexlist.append(mouse.X()) mouseylist.append(mouse.Y()) @@ -213,6 +209,7 @@ def toggle_button(label, value, x, y): mouseylist.clear() has_switched = False + #Then code verify if player clicked many time to get median value from mouse positions if len(mousexlist) > 20: mousexlist.pop(0) if len(mouseylist) > 20: @@ -221,12 +218,15 @@ def toggle_button(label, value, x, y): if len(mouseylist) > 0: mouse_x = median(mousexlist) mouse_y = median(mouseylist) + + #if mouse is in rectancle zone defined by our vertices if mouse_x > pos.x - axis_x.x and mouse_x < pos.x + axis_x.x and mouse_y > pos.y - axis_y.y and mouse_y < pos.y + axis_y.y and not has_switched: value = True if not value else False has_switched = True mousexlist.clear() mouseylist.clear() + #This is the text asociated to the interactive zone next to it mat = hg.TranslationMat4(hg.Vec3(pos.x + axis_x.x + 10, y - 10, 1)) hg.SetS(mat, hg.Vec3(1, -1, 1)) hg.DrawText(view_id, @@ -236,12 +236,8 @@ def toggle_button(label, value, x, y): [font_color_white], [], text_render_state) return value - -#AUTOMATIC DANCE buttonlist = [[100, res_y - 80]] - -#AUTOMATIC DANCE / Sert a quoi ? Impression qu'il essaye de faire en sorte de modifier les valeurs des moteurs def is_switching(): for i in buttonlist: mat = hg.TransformationMat4( @@ -325,7 +321,6 @@ def get_v_from_dancing(id_robot): # get values from the real robot - #AUTOMATIC DANCE : requests that give robot new positions if compliance_mode: timer_requests_not_overload += hg.time_to_sec_f(dt) if timer_requests_not_overload > send_dt: @@ -374,7 +369,6 @@ def get_v_from_dancing(id_robot): (hg_m["v"] - v) / (hg.time_to_sec_f(dt)**2), -9999, 9999)) hg_m["v"] = v - #AUTOMATIC DANCE if compliance_mode and compliance_lerp: adjusted_time = rangeadjust(hg.GetClock( ), hg_motors_previous[id][1], hg_motors_previous[id][1] + hg.time_from_sec_f(send_dt), 0, 1) @@ -582,6 +576,8 @@ def get_v_from_dancing(id_robot): if compliance_mode: compliance_lerp = toggle_button( "Motion Interpolation ON" if compliance_lerp else "Motion Interpolation OFF", compliance_lerp, 100, res_y - 180) + ui_interaction_mode = toggle_button( + "Interaction Mode ON" if ui_interaction_mode else "Interaction Mode OFF", ui_interaction_mode, 100, res_y - 180) view_id += 1 From 6cc3a274b409bbb2865e823acb142fc57598f314 Mon Sep 17 00:00:00 2001 From: Angaros Date: Fri, 13 Dec 2024 15:58:25 +0100 Subject: [PATCH 3/4] Added ui_interaction_mode Need to desactivate camera when player is using ui_interaction_mode --- app/main.py | 105 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 26 deletions(-) diff --git a/app/main.py b/app/main.py index 89b873a..52a7815 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,6 @@ import harfang as hg import requests import socket -from math import pi from statistics import median import math from OrbitalCam import OrbitalController @@ -22,7 +21,6 @@ def rangeadjust(k, a, b, u, v): def lerp(k, a, b): return a + (b - a) * k - # look for the poppy on the network url = "" try: @@ -38,7 +36,7 @@ def lerp(k, a, b): mouse = hg.Mouse() res_x, res_y = 1920, 1080 - +WIDTH, HEIGHT = res_x, res_y # initialize lists and variables for toggle button (swipe style) mousexlist = [] mouseylist = [] @@ -321,32 +319,87 @@ def get_v_from_dancing(id_robot): # get values from the real robot + if ui_interaction_mode: + compliance_mode = False + app_status = "None" + + if mouse.Down(hg.MB_0): + mousexlist.append(mouse.X()) + mouseylist.append(mouse.Y()) + else: + mousexlist.clear() + mouseylist.clear() + has_switched = False + + if len(mousexlist) > 20: + mousexlist.pop(0) + if len(mouseylist) > 20: + mouseylist.pop(0) + + if len(mouseylist) > 0: + mouse_x = median(mousexlist) + mouse_y = median(mouseylist) + + Sx = 0.0 + Sy = 0.0 + + Ex = WIDTH + + interactable_rectangles = [] + + for i in range(1, 7): + + Ey = (HEIGHT / 6) * i + + rectangle = hg.Rect(Sx, Sy, Ex, Ey) + interactable_rectangles.append(rectangle) + + Sy = Ey + + + for i in range(len(interactable_rectangles)): + inter_sx = interactable_rectangles[i].sx + inter_sy = interactable_rectangles[i].sy + inter_ex = interactable_rectangles[i].ex + inter_ey = interactable_rectangles[i].ey + + if mouse.Down(hg.MB_0): + if mouse_x > inter_sx and mouse_x < inter_ex and mouse_y > inter_sy and mouse_y < inter_ey: + hg_m = hg_motors[i] + mouse_pos = rangeadjust(mouse_x, 0, WIDTH, hg_m["lower_limit"], hg_m["upper_limit"]) #rename + hg_m["v"] = mouse_pos + + if not compliance_mode and not ui_interaction_mode: + app_status = "dancing" if compliance_mode: timer_requests_not_overload += hg.time_to_sec_f(dt) - if timer_requests_not_overload > send_dt: - timer_requests_not_overload = 0 + ui_interaction_mode = False + if timer_requests_not_overload > send_dt: + timer_requests_not_overload = 0 - if url != "": - try: - r = requests.get(url + "/motors/get/positions").text - for id, m in enumerate(hg_motors): - hg_m = hg_motors[id] - hg_motors_previous[id][0] = hg_m["v"] - hg_motors_previous[id][1] = hg.GetClock() - hg_m["v"] = float(r.split(';')[id]) - if id == 5: - # send negative value for claw motor angle () - hg_m["v"] = -float(r.split(';')[id]) + if url != "": + try: + r = requests.get(url + "/motors/get/positions").text + for id, m in enumerate(hg_motors): + hg_m = hg_motors[id] + hg_motors_previous[id][0] = hg_m["v"] + hg_motors_previous[id][1] = hg.GetClock() + hg_m["v"] = float(r.split(';')[id]) + if id == 5: + # send negative value for claw motor angle () + hg_m["v"] = -float(r.split(';')[id]) - except: - print("Robot not connected " + url) + except: + print("Robot not connected " + url) # set 3D mesh with the current motor pos + + v = 0 for id, m in enumerate(hg_motors): hg_m = hg_motors[id] # check if we are getting value from the real root - if compliance_mode: + if compliance_mode or ui_interaction_mode: v = hg_m["v"] else: # sending value to the real robot if app_status == "dancing": @@ -378,22 +431,22 @@ def get_v_from_dancing(id_robot): # set the position to the virtual robot rot = hg.Vec3(0, 0, 0) if hg_m["axis"] == "X": - rot = hg.Vec3(-new_v*pi/180.0, 0, 0) + rot = hg.Vec3(-new_v*math.pi/180.0, 0, 0) elif hg_m["axis"] == "Y": - rot = hg.Vec3(0, -new_v*pi/180.0, 0) + rot = hg.Vec3(0, -new_v*math.pi/180.0, 0) elif hg_m["axis"] == "Z": - rot = hg.Vec3(-1.57, 0, -new_v*pi/180.0) + rot = hg.Vec3(-1.57, 0, -new_v*math.pi/180.0) hg_m["n"].GetTransform().SetRot(rot) else: rot = hg.Vec3(0, 0, 0) if hg_m["axis"] == "X": - rot = hg.Vec3(-v*pi/180.0, 0, 0) + rot = hg.Vec3(-v*math.pi/180.0, 0, 0) elif hg_m["axis"] == "Y": - rot = hg.Vec3(0, -v*pi/180.0, 0) + rot = hg.Vec3(0, -v*math.pi/180.0, 0) elif hg_m["axis"] == "Z": - rot = hg.Vec3(-1.57, 0, -v*pi/180.0) + rot = hg.Vec3(-1.57, 0, -v*math.pi/180.0) hg_m["n"].GetTransform().SetRot(rot) @@ -577,7 +630,7 @@ def get_v_from_dancing(id_robot): compliance_lerp = toggle_button( "Motion Interpolation ON" if compliance_lerp else "Motion Interpolation OFF", compliance_lerp, 100, res_y - 180) ui_interaction_mode = toggle_button( - "Interaction Mode ON" if ui_interaction_mode else "Interaction Mode OFF", ui_interaction_mode, 100, res_y - 180) + "Interaction Mode ON" if ui_interaction_mode else "Interaction Mode OFF", ui_interaction_mode, 100, res_y - 280) view_id += 1 From 63051e52b91081145da450d6c8cc86b9c3840102 Mon Sep 17 00:00:00 2001 From: Angaros Date: Fri, 13 Dec 2024 16:22:44 +0100 Subject: [PATCH 4/4] Renamed variables --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 52a7815..84963a2 100644 --- a/app/main.py +++ b/app/main.py @@ -366,8 +366,8 @@ def get_v_from_dancing(id_robot): if mouse.Down(hg.MB_0): if mouse_x > inter_sx and mouse_x < inter_ex and mouse_y > inter_sy and mouse_y < inter_ey: hg_m = hg_motors[i] - mouse_pos = rangeadjust(mouse_x, 0, WIDTH, hg_m["lower_limit"], hg_m["upper_limit"]) #rename - hg_m["v"] = mouse_pos + new_motor_angle = rangeadjust(mouse_x, 0, WIDTH, hg_m["lower_limit"], hg_m["upper_limit"]) #rename + hg_m["v"] = new_motor_angle if not compliance_mode and not ui_interaction_mode: app_status = "dancing"