File size: 86,355 Bytes
7bdac1b | 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 | import re,os
from kivy.lang import Builder
import numpy as np
import torch
import torch.nn as nn
import pybullet as p
from panda3d_kivy.mdapp import MDApp
from direct.gui.DirectGui import *
from panda3d.core import AmbientLight, DirectionalLight, Spotlight, PerspectiveLens
import math
import imageio
import time
from surrol.gui.scene import Scene, GymEnvScene
from surrol.gui.application import Application, ApplicationConfig
from surrol.tasks.ecm_misorient import MisOrient
from surrol.tasks.ecm_reach import ECMReach
from surrol.tasks.ecm_active_track import ActiveTrack
from surrol.tasks.ecm_static_track import StaticTrack
from surrol.tasks.gauze_retrieve import GauzeRetrieve
from surrol.tasks.needle_reach import NeedleReach
from surrol.tasks.needle_pick import NeedlePick
from surrol.tasks.peg_board import PegBoard
from surrol.tasks.peg_board_bimanual import BiPegBoard
from surrol.tasks.peg_transfer import PegTransfer
from surrol.tasks.peg_transfer_RL import PegTransferRL
from surrol.tasks.needle_regrasp_bimanual import NeedleRegrasp
from surrol.tasks.peg_transfer_bimanual import BiPegTransfer
from surrol.tasks.pick_and_place import PickAndPlace
from surrol.tasks.match_board import MatchBoard
from surrol.tasks.match_board_ii import MatchBoardII
from surrol.tasks.needle_the_rings import NeedleRings
# from surrol.tasks.match_board_ii import BiMatchBoard
from surrol.tasks.ecm_env import EcmEnv, goal_distance,reset_camera
from surrol.robots.ecm import RENDER_HEIGHT, RENDER_WIDTH, FoV
from surrol.robots.ecm import Ecm
from haptic_src.touch_haptic import initTouch_right, closeTouch_right, getDeviceAction_right, startScheduler, stopScheduler
from haptic_src.touch_haptic import initTouch_left, closeTouch_left, getDeviceAction_left
from direct.task import Task
from surrol.utils.pybullet_utils import step
from rl.agents.goal_conditioned_decision_transformer import GoalConditionedDecisionTransformer
from rl.utils.surrol_data import SurRoLDataset
app = None
hint_printed = False
resetFlag = False
def open_scene(id):
global app, hint_printed,resetFlag
scene = None
menu_dict = {0:StartPage(),1:ECMPage(),2:NeedlePage(),3:PegPage(),4:PickPlacePage()}
task_list =[NeedlePick,PegTransfer,NeedleRegrasp,BiPegTransfer,PickAndPlace,BiPegBoard,NeedleRings,MatchBoard,ECMReach,MisOrient,StaticTrack,ActiveTrack,NeedleReach,GauzeRetrieve]
bimanual_list=[9,10,11,12,16,17,18]
if id < 5:
scene = menu_dict[id]
elif id in bimanual_list:
jaws=[1.0, 1.0] if id==9 or id==10 else [1.0,1.0]
scene = SurgicalSimulatorBimanual(task_list[(id-5)//2], {'render_mode': 'human'}, jaw_states=jaws,id=id) if id %2==1 and id!=9 else \
SurgicalSimulatorBimanual(task_list[(id-5)//2], {'render_mode': 'human'}, jaw_states=jaws,id=id,demo=1)
else:
if id ==15:
scene = SurgicalSimulator(PegBoard,{'render_mode': 'human'},id)
elif id ==8:
scene = SurgicalSimulator(PegTransferRL,{'render_mode': 'human'},id,demo=1)
elif id ==6:
scene = SurgicalSimulator(NeedlePick,{'render_mode': 'human'},id,demo=1)
else:
scene = SurgicalSimulator(task_list[(id-5)//2],{'render_mode': 'human'},id) if id%2==1 else\
SurgicalSimulator(task_list[(id-5)//2],{'render_mode': 'human'},id,demo=1)
# if id == 0:
# scene = StartPage()
# elif id == 1:
# scene = ECMPage()
# elif id == 2:
# scene = NeedlePage()
# elif id == 3:
# scene = PegPage()
# elif id == 4:
# scene = PickPlacePage()
# elif id == 5:
# scene = SurgicalSimulator(NeedlePick, {'render_mode': 'human'},id)
# elif id == 6:
# scene = SurgicalSimulator(NeedlePick, {'render_mode': 'human'},id,demo=1)
# elif id == 7:
# scene = SurgicalSimulator(PegTransfer, {'render_mode': 'human'},id)
# elif id == 8:
# scene = SurgicalSimulator(PegTransfer, {'render_mode': 'human'},id,demo=1)
# elif id == 9:
# scene = SurgicalSimulatorBimanual(NeedleRegrasp, {'render_mode': 'human'}, jaw_states=[1.0, -0.5],id=id)
# elif id == 10:
# scene = SurgicalSimulatorBimanual(NeedleRegrasp, {'render_mode': 'human'}, jaw_states=[1.0, -0.5],id=id,demo=1)
# elif id == 11:
# scene = SurgicalSimulatorBimanual(BiPegTransfer, {'render_mode': 'human'}, jaw_states=[1.0, 1.0],id=id)
# elif id == 12:
# scene = SurgicalSimulatorBimanual(BiPegTransfer, {'render_mode': 'human'}, jaw_states=[1.0, 1.0],id=id,demo=1)
# elif id == 13:
# scene = SurgicalSimulator(PickAndPlace, {'render_mode': 'human'},id)
# elif id == 14:
# scene = SurgicalSimulator(PickAndPlace, {'render_mode': 'human'},id,demo=1)
# elif id == 15:
# scene = SurgicalSimulator(PegBoard, {'render_mode': 'human'},id)
# elif id == 16:
# scene = SurgicalSimulator(PegBoard, {'render_mode': 'human'},id)
# elif id == 17:
# scene = SurgicalSimulatorBimanual(NeedleRings, {'render_mode': 'human'}, jaw_states=[1.0, 1.0],id=id)
# elif id == 18:
# scene = SurgicalSimulatorBimanual(NeedleRings, {'render_mode': 'human'}, jaw_states=[1.0, 1.0],id=id,demo=1)
# elif id == 19:
# scene = SurgicalSimulator(MatchBoard, {'render_mode': 'human'},id)
# elif id == 20:
# scene = SurgicalSimulator(MatchBoard, {'render_mode': 'human'},id,demo=1)
# elif id == 21:
# scene = SurgicalSimulator(ECMReach, {'render_mode': 'human'},id)
# elif id == 22:
# scene = SurgicalSimulator(ECMReach, {'render_mode': 'human'},id,demo=1)
# elif id == 23:
# scene = SurgicalSimulator(MisOrient, {'render_mode': 'human'},id)
# elif id == 24:
# scene = SurgicalSimulator(MisOrient, {'render_mode': 'human'},id,demo=1)
# elif id == 25:
# scene = SurgicalSimulator(StaticTrack, {'render_mode': 'human'},id)
# elif id == 26:
# scene = SurgicalSimulator(StaticTrack, {'render_mode': 'human'},id,demo=1)
# elif id == 27:
# scene = SurgicalSimulator(ActiveTrack, {'render_mode': 'human'},id)
# elif id == 28:
# scene = SurgicalSimulator(ActiveTrack, {'render_mode': 'human'},id,demo=1)
# elif id == 29:
# scene = SurgicalSimulator(NeedleReach, {'render_mode': 'human'},id)
# elif id == 30:
# scene = SurgicalSimulator(NeedleReach, {'render_mode': 'human'},id,demo=1)
# elif id == 31:
# scene = SurgicalSimulator(GauzeRetrieve, {'render_mode': 'human'},id)
# elif id == 32:
# scene = SurgicalSimulator(GauzeRetrieve, {'render_mode': 'human'},id,demo=1)
# if id in (1, 2) and not hint_printed:
# print('Press <W><A><S><D><E><Q><Space> to control the PSM.')
# hint_printed = True
if scene:
app.play(scene)
selection_panel_kv = '''MDBoxLayout:
orientation: 'vertical'
spacing: dp(10)
padding: dp(20)
MDLabel:
text: "SurRol Simulator v2"
theme_text_color: "Primary"
font_style: "H6"
bold: True
size_hint: 1.0, 0.1
MDSeparator:
MDGridLayout:
cols: 2
spacing: "30dp"
padding: "20dp", "10dp", "20dp", "20dp"
size_hint: 1.0, 0.9
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/ecm_track.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "1. Endoscope Fov Control"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Practise ECM control skills"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn1
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/needlepick_poster.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "2. Fundamental Actions"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Practise fundamental actions in surgery"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn2
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/pegtransfer_poster.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "3. Basic Robot Skill Training Tasks"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Pratise positioning and orienting objects"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn3
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
'''
# MDCard:
# orientation: "vertical"
# size_hint: .45, None
# height: box_top.height + box_bottom.height
# MDBoxLayout:
# id: box_top
# spacing: "20dp"
# adaptive_height: True
# FitImage:
# source: "images/pick&place.png"
# size_hint: 0.5, None
# height: text_box.height
# MDBoxLayout:
# id: text_box
# orientation: "vertical"
# adaptive_height: True
# spacing: "10dp"
# padding: 0, "10dp", "10dp", "10dp"
# MDLabel:
# text: "4. Pick & Place Tasks"
# theme_text_color: "Primary"
# font_style: "H6"
# bold: True
# adaptive_height: True
# MDLabel:
# text: "Practice positioning and orienting objects"
# adaptive_height: True
# theme_text_color: "Primary"
# MDSeparator:
# MDBoxLayout:
# id: box_bottom
# adaptive_height: True
# padding: "0dp", 0, 0, 0
# MDRaisedButton:
# id: btn4
# text: "Play"
# size_hint: 0.8, 1.0
# MDIconButton:
# icon: "application-settings"
Peg_panel_kv = '''MDBoxLayout:
orientation: 'vertical'
MDLabel:
text: " Basic Robot Skill Training Tasks"
theme_text_color: "Primary"
font_style: "H4"
bold: True
spacing: "10dp"
size_hint: 1.0, 0.3
MDSeparator:
MDGridLayout:
cols: 2
spacing: "40dp"
padding: "20dp", "10dp", "20dp", "20dp"
size_hint: 1.0, 1.0
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/pegtransfer_poster.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Peg Transfer"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Move the gripper to a randomly sampled position"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn1
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/bipegtransfer_poster.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Bi-Peg Transfer"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Bimanual peg transfer"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn2
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/pegboard.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Peg Board"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Transfer red ring from peg board to peg on floor"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn3
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/pick&place.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Pick and Place"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Place colored jacks into matching containers"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn4
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/matchboard.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Match Board"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Transfer various objects into matching spaces"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn5
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/needlerings.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Needle the Rings"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Pass a needle through the target ring"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn6
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDBoxLayout:
MDRectangleFlatIconButton:
icon: "exit-to-app"
id: btn7
text: "Exit"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "head-lightbulb-outline"
id: btn8
text: "AI Assistant"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "chart-histogram"
id: btn8
text: "Evaluation"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1,0
MDRectangleFlatIconButton:
icon: "help-box"
id: btn8
text: "Help"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1,0
'''
PickPlace_panel_kv = '''MDBoxLayout:
orientation: 'vertical'
MDLabel:
text: " Pick & Place Tasks"
theme_text_color: "Primary"
font_style: "H4"
bold: True
spacing: "10dp"
size_hint: 1.0, 0.3
MDSeparator:
MDGridLayout:
cols: 2
spacing: "40dp"
padding: "20dp", "10dp", "20dp", "20dp"
size_hint: 1.0, 1.0
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/gauze_retrieve.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Gauze Retrieve"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: " Pick the gauze and place it at the target position"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn1
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/pick&place.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Pick and Place"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Place colored jacks into matching colored containers"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn2
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/matchboard.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Match Board"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Pick up various objects and place them into corresponding spaces on the board"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn3
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDBoxLayout:
MDRectangleFlatIconButton:
icon: "exit-to-app"
id: btn4
text: "Exit"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "head-lightbulb-outline"
id: btn5
text: "AI Assistant"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "chart-histogram"
id: btn6
text: "Evaluation"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1,0
MDRectangleFlatIconButton:
icon: "help-box"
id: btn7
text: "Help"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1,0
'''
needle_panel_kv = '''MDBoxLayout:
orientation: 'vertical'
MDLabel:
text: " Fundamental Actions"
theme_text_color: "Primary"
font_style: "H4"
bold: True
spacing: "10dp"
size_hint: 1.0, 0.3
MDSeparator:
MDGridLayout:
cols: 2
spacing: "40dp"
padding: "20dp", "10dp", "20dp", "20dp"
size_hint: 1.0, 1.0
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/needle_reach.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Needle Reach"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Move the gripper to a randomly sampled position"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn1
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/gauze_retrieve.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Gauze Retrieve"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Pick the gauze and place it at the target position"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn2
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/needlepick_poster.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Needle Pick"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Pick up the needle and move to target position"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn3
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/needleregrasp_poster.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Needle Regrasp"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Bimanual version of needle pick"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn4
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDBoxLayout:
MDRectangleFlatIconButton:
icon: "exit-to-app"
id: btn5
text: "Exit"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "head-lightbulb-outline"
id: btn6
text: "AI Assistant"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "chart-histogram"
id: btn6
text: "Evaluation"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1,0
MDRectangleFlatIconButton:
icon: "help-box"
id: btn6
text: "Help"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1,0
'''
ECM_panel_kv = '''MDBoxLayout:
orientation: 'vertical'
MDLabel:
text: " Endoscope Fov Control"
theme_text_color: "Primary"
font_style: "H4"
bold: True
spacing: "10dp"
size_hint: 1.0, 0.3
MDSeparator:
MDGridLayout:
cols: 2
spacing: "40dp"
padding: "20dp", "10dp", "20dp", "20dp"
size_hint: 1.0, 1.0
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/ecm_reach.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "ECM Reach"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Move the ECM to a randomly sampled position"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn1
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/misorient.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "MisOrient"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Adjust ECM to minimize misorientation"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn2
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/static_track.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Static Track"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Make ECM track a static target cube"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn3
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDCard:
orientation: "vertical"
size_hint: .45, None
height: box_top.height + box_bottom.height
MDBoxLayout:
id: box_top
spacing: "20dp"
adaptive_height: True
FitImage:
source: "images/active_track.png"
size_hint: 0.5, None
height: text_box.height
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "10dp"
padding: 0, "10dp", "10dp", "10dp"
MDLabel:
text: "Active Track"
theme_text_color: "Primary"
font_style: "H6"
bold: True
adaptive_height: True
MDLabel:
text: "Make ECM track a active target cube"
adaptive_height: True
theme_text_color: "Primary"
MDSeparator:
MDBoxLayout:
id: box_bottom
adaptive_height: True
padding: "0dp", 0, 0, 0
MDRaisedButton:
id: btn4
text: "Play"
size_hint: 0.8, 1.0
MDIconButton:
icon: "application-settings"
MDBoxLayout:
MDRectangleFlatIconButton:
icon: "exit-to-app"
id: btn5
text: "Exit"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "head-lightbulb-outline"
id: btn6
text: "AI Assistant"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1, 0
MDRectangleFlatIconButton:
icon: "chart-histogram"
id: btn6
text: "Evaluation"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.1,0
MDRectangleFlatIconButton:
icon: "help-box"
id: btn6
text: "Help"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.1,0
'''
class SelectionUI(MDApp):
def __init__(self, panda_app, display_region):
super().__init__(panda_app=panda_app, display_region=display_region)
self.screen = None
def build(self):
self.theme_cls.theme_style = "Dark"
self.screen = Builder.load_string(selection_panel_kv)
return self.screen
def on_start(self):
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(1))
self.screen.ids.btn2.bind(on_press = lambda _: open_scene(2))
self.screen.ids.btn3.bind(on_press = lambda _: open_scene(3))
# self.screen.ids.btn4.bind(on_press = lambda _: open_scene(4))
# self.screen.ids.btn11.bind(on_press = lambda _: open_scene(11))
class NeedleUI(MDApp):
def __init__(self, panda_app, display_region):
super().__init__(panda_app=panda_app, display_region=display_region)
self.screen = None
def build(self):
self.theme_cls.theme_style = "Dark"
self.screen = Builder.load_string(needle_panel_kv)
return self.screen
def on_start(self):
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(29))
self.screen.ids.btn2.bind(on_press = lambda _: open_scene(31))
self.screen.ids.btn3.bind(on_press = lambda _: open_scene(6))
self.screen.ids.btn4.bind(on_press = lambda _: open_scene(9))
self.screen.ids.btn5.bind(on_press = lambda _: open_scene(0))
self.screen.ids.btn6.bind(on_press = lambda _: open_scene(2))
class PegUI(MDApp):
def __init__(self, panda_app, display_region):
super().__init__(panda_app=panda_app, display_region=display_region)
self.screen = None
def build(self):
self.theme_cls.theme_style = "Dark"
self.screen = Builder.load_string(Peg_panel_kv)
return self.screen
def on_start(self):
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(7))
self.screen.ids.btn2.bind(on_press = lambda _: open_scene(11))
self.screen.ids.btn3.bind(on_press = lambda _: open_scene(15))
self.screen.ids.btn4.bind(on_press = lambda _: open_scene(13))
self.screen.ids.btn5.bind(on_press = lambda _: open_scene(19))
self.screen.ids.btn6.bind(on_press = lambda _: open_scene(17))
self.screen.ids.btn7.bind(on_press = lambda _: open_scene(0))
self.screen.ids.btn8.bind(on_press = lambda _: open_scene(3))
class PickPlaceUI(MDApp):
def __init__(self, panda_app, display_region):
super().__init__(panda_app=panda_app, display_region=display_region)
self.screen = None
def build(self):
self.theme_cls.theme_style = "Dark"
self.screen = Builder.load_string(PickPlace_panel_kv)
return self.screen
def on_start(self):
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(31))
self.screen.ids.btn2.bind(on_press = lambda _: open_scene(13))
self.screen.ids.btn3.bind(on_press = lambda _: open_scene(19))
self.screen.ids.btn4.bind(on_press = lambda _: open_scene(0))
self.screen.ids.btn5.bind(on_press = lambda _: open_scene(4))
self.screen.ids.btn6.bind(on_press = lambda _: open_scene(4))
self.screen.ids.btn7.bind(on_press = lambda _: open_scene(4))
class ECMUI(MDApp):
def __init__(self, panda_app, display_region):
super().__init__(panda_app=panda_app, display_region=display_region)
self.screen = None
def build(self):
self.theme_cls.theme_style = "Dark"
self.screen = Builder.load_string(ECM_panel_kv)
return self.screen
def on_start(self):
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(21))
self.screen.ids.btn2.bind(on_press = lambda _: open_scene(23))
self.screen.ids.btn3.bind(on_press = lambda _: open_scene(25))
self.screen.ids.btn4.bind(on_press = lambda _: open_scene(27))
self.screen.ids.btn5.bind(on_press = lambda _: open_scene(0))
self.screen.ids.btn6.bind(on_press = lambda _: open_scene(1))
class StartPage(Scene):
def __init__(self):
super(StartPage, self).__init__()
def on_start(self):
self.ui_display_region = self.build_kivy_display_region(0, 1.0, 0, 1.0)
self.kivy_ui = SelectionUI(
self.app,
self.ui_display_region
)
self.kivy_ui.run()
def on_destroy(self):
# !!! important
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
class NeedlePage(Scene):
def __init__(self):
super(NeedlePage, self).__init__()
def on_start(self):
self.ui_display_region = self.build_kivy_display_region(0, 1.0, 0, 1.0)
self.kivy_ui = NeedleUI(
self.app,
self.ui_display_region
)
self.kivy_ui.run()
def on_destroy(self):
# !!! important
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
class PegPage(Scene):
def __init__(self):
super(PegPage, self).__init__()
def on_start(self):
self.ui_display_region = self.build_kivy_display_region(0, 1.0, 0, 1.0)
self.kivy_ui = PegUI(
self.app,
self.ui_display_region
)
self.kivy_ui.run()
def on_destroy(self):
# !!! important
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
class PickPlacePage(Scene):
def __init__(self):
super(PickPlacePage, self).__init__()
def on_start(self):
self.ui_display_region = self.build_kivy_display_region(0, 1.0, 0, 1.0)
self.kivy_ui = PickPlaceUI(
self.app,
self.ui_display_region
)
self.kivy_ui.run()
def on_destroy(self):
# !!! important
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
class ECMPage(Scene):
def __init__(self):
super(ECMPage, self).__init__()
def on_start(self):
self.ui_display_region = self.build_kivy_display_region(0, 1.0, 0, 1.0)
self.kivy_ui = ECMUI(
self.app,
self.ui_display_region
)
self.kivy_ui.run()
def on_destroy(self):
# !!! important
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
menu_bar_kv_haptic = '''MDBoxLayout:
md_bg_color: (1, 0, 0, 0)
# adaptive_height: True
padding: "0dp", 0, 0, 0
MDRectangleFlatIconButton:
icon: "exit-to-app"
id: btn1
text: "Exit"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.25, 1.0
MDRectangleFlatIconButton:
icon: "head-lightbulb-outline"
id: btn2
text: "AI Assistant"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.25, 1.0
MDRectangleFlatIconButton:
icon: "chart-histogram"
id: btn3
text: "Evaluation"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.25, 1.0
MDRectangleFlatIconButton:
icon: "help-box"
id: btn4
text: "Switch to Demo"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.25, 1.0
'''
menu_bar_kv_RL = '''MDBoxLayout:
md_bg_color: (1, 0, 0, 0)
# adaptive_height: True
padding: "0dp", 0, 0, 0
MDRectangleFlatIconButton:
icon: "exit-to-app"
id: btn1
text: "Exit"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.25, 1.0
MDRectangleFlatIconButton:
icon: "head-lightbulb-outline"
id: btn2
text: "AI Assistant"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.25, 1.0
MDRectangleFlatIconButton:
icon: "chart-histogram"
id: btn3
text: "Evaluation"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.primary_color
size_hint: 0.25, 1.0
MDRectangleFlatIconButton:
icon: "help-box"
id: btn4
text: "Switch to Haptic Device Training"
text_color: (1, 1, 1, 1)
icon_color: (1, 1, 1, 1)
md_bg_color: app.theme_cls.bg_light
size_hint: 0.25, 1.0
'''
class MenuBarUI(MDApp):
def __init__(self, panda_app, display_region,id = None):
super().__init__(panda_app=panda_app, display_region=display_region)
self.screen = None
self.id = id
self.ecm_list=[i for i in range(21,29)]
self.fund_list = [31,32,5,6,9,10,29,30]
self.basic_list=[7,8,11,12,15,16,17,18,13,14,19,20]
def build(self):
self.theme_cls.theme_style = "Dark"
if self.id % 2 == 0:
self.screen = Builder.load_string(menu_bar_kv_RL)
else:
self.screen = Builder.load_string(menu_bar_kv_haptic)
return self.screen
def on_start(self):
# scene_menu = 1
if self.id in self.ecm_list:
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(1))
elif self.id in self.fund_list:
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(2))
elif self.id in self.basic_list:
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(3))
else:
self.screen.ids.btn1.bind(on_press = lambda _: open_scene(0))
if self.id % 2 ==0:
self.screen.ids.btn4.bind(on_press = lambda _: (open_scene(0), open_scene(self.id-1)))
else:
self.screen.ids.btn4.bind(on_press = lambda _:(open_scene(0), open_scene(self.id+1)))
class SurgicalSimulatorBase(GymEnvScene):
def __init__(self, env_type, env_params):
super(SurgicalSimulatorBase, self).__init__(env_type, env_params)
def before_simulation_step(self):
pass
def on_env_created(self):
"""Setup extrnal lights"""
self.ecm_view_out = self.env._view_matrix
table_pos = np.array(self.env.POSE_TABLE[0]) * self.env.SCALING
# ambient light
alight = AmbientLight('alight')
alight.setColor((0.2, 0.2, 0.2, 1))
alnp = self.world3d.attachNewNode(alight)
self.world3d.setLight(alnp)
# directional light
dlight = DirectionalLight('dlight')
dlight.setColor((0.4, 0.4, 0.25, 1))
# dlight.setShadowCaster(True, app.configs.shadow_resolution, app.configs.shadow_resolution)
dlnp = self.world3d.attachNewNode(dlight)
dlnp.setPos(*(table_pos + np.array([1.0, 0.0, 15.0])))
dlnp.lookAt(*table_pos)
self.world3d.setLight(dlnp)
# spotlight
slight = Spotlight('slight')
slight.setColor((0.5, 0.5, 0.5, 1.0))
lens = PerspectiveLens()
lens.setNearFar(0.5, 5)
slight.setLens(lens)
slight.setShadowCaster(True, app.configs.shadow_resolution, app.configs.shadow_resolution)
slnp = self.world3d.attachNewNode(slight)
slnp.setPos(*(table_pos + np.array([0, 0.0, 5.0])))
slnp.lookAt(*(table_pos + np.array([0.6, 0, 1.0])))
self.world3d.setLight(slnp)
def on_start(self):
self.ui_display_region = self.build_kivy_display_region(0, 1.0, 0, 0.061)
self.kivy_ui = MenuBarUI(
self.app,
self.ui_display_region,
self.id
)
self.kivy_ui.run()
def on_destroy(self):
# !!! important
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
class MLP(nn.Module):
def __init__(self, in_dim, out_dim, hidden_dim=256):
super().__init__()
self.mlp = nn.Sequential(
nn.Linear(in_dim, hidden_dim), nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim), nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim), nn.ReLU(),
nn.Linear(hidden_dim, out_dim),
)
def forward(self, input):
return self.mlp(input)
class DeterministicActor(nn.Module):
def __init__(self, dimo, dimg, dima, hidden_dim=256):
super().__init__()
self.trunk = MLP(
in_dim=dimo+dimg,
out_dim=dima,
hidden_dim=hidden_dim
)
def forward(self, obs):
a = self.trunk(obs)
return torch.tanh(a)
class Normalizer:
def __init__(self, size, eps=1e-2, default_clip_range=np.inf):
self.size = size
self.eps = eps
self.default_clip_range = default_clip_range
# some local information
self.total_sum = np.zeros(self.size, np.float32)
self.total_sumsq = np.zeros(self.size, np.float32)
self.total_count = np.zeros(1, np.float32)
# get the mean and std
self.mean = np.zeros(self.size, np.float32)
self.std = np.ones(self.size, np.float32)
# update the parameters of the normalizer
def update(self, v):
v = v.reshape(-1, self.size)
# do the computing
self.total_sum += v.sum(axis=0)
self.total_sumsq += (np.square(v)).sum(axis=0)
self.total_count[0] += v.shape[0]
def recompute_stats(self):
# calculate the new mean and std
self.mean = self.total_sum / self.total_count
self.std = np.sqrt(np.maximum(np.square(self.eps), (self.total_sumsq / self.total_count) - np.square(self.total_sum / self.total_count)))
# normalize the observation
def normalize(self, v, clip_range=None):
if clip_range is None:
clip_range = self.default_clip_range
return np.clip((v - self.mean) / (self.std), -clip_range, clip_range)
class SurgicalSimulator(SurgicalSimulatorBase):
def __init__(self, env_type, env_params,id=None,demo=None):
super(SurgicalSimulator, self).__init__(env_type, env_params)
self.id = id
initTouch_right()
startScheduler()
if env_type.ACTION_SIZE != 3 and env_type.ACTION_SIZE != 1:
self.psm1_action = np.zeros(env_type.ACTION_SIZE)
self.psm1_action[4] = 0.5
self.app.accept('w-up', self.setPsmAction, [2, 0])
self.app.accept('w-repeat', self.addPsmAction, [2, 0.01])
self.app.accept('s-up', self.setPsmAction, [2, 0])
self.app.accept('s-repeat', self.addPsmAction, [2, -0.01])
self.app.accept('d-up', self.setPsmAction, [1, 0])
self.app.accept('d-repeat', self.addPsmAction, [1, 0.01])
self.app.accept('a-up', self.setPsmAction, [1, 0])
self.app.accept('a-repeat', self.addPsmAction, [1, -0.01])
self.app.accept('q-up', self.setPsmAction, [0, 0])
self.app.accept('q-repeat', self.addPsmAction, [0, 0.01])
self.app.accept('e-up', self.setPsmAction, [0, 0])
self.app.accept('e-repeat', self.addPsmAction, [0, -0.01])
self.app.accept('space-up', self.setPsmAction, [4, 1.0])
self.app.accept('space-repeat', self.setPsmAction, [4, -0.5])
self.ecm_view = 0
self.ecm_view_out = None
self.demo = demo
self.start_time = time.time()
exempt_l = [i for i in range(21,23)]
if self.id not in exempt_l:
self.toggleEcmView()
self.has_load_policy = False
self.ecm_action = np.zeros(env_type.ACTION_ECM_SIZE)
if env_type.ACTION_ECM_SIZE == 1:
self.app.accept('1-up', self.setEcmAction, [0, 0])
self.app.accept('1-repeat', self.addEcmAction, [0, 0.2])
self.app.accept('3-up', self.setEcmAction, [0, 0])
self.app.accept('3-repeat', self.addEcmAction, [0, -0.2])
else:
self.app.accept('5-up', self.setEcmAction, [2, 0])
self.app.accept('5-repeat', self.addEcmAction, [2, 0.2])
self.app.accept('2-up', self.setEcmAction, [2, 0])
self.app.accept('2-repeat', self.addEcmAction, [2, -0.2])
self.app.accept('6-up', self.setEcmAction, [1, 0])
self.app.accept('6-repeat', self.addEcmAction, [1, 0.2])
self.app.accept('4-up', self.setEcmAction, [1, 0])
self.app.accept('4-repeat', self.addEcmAction, [1, -0.2])
self.app.accept('1-up', self.setEcmAction, [0, 0])
self.app.accept('1-repeat', self.addEcmAction, [0, 0.2])
self.app.accept('3-up', self.setEcmAction, [0, 0])
self.app.accept('3-repeat', self.addEcmAction, [0, -0.2])
self.app.accept('m-up', self.toggleEcmView)
self.app.accept('r-up', self.resetEcmFlag)
self.device = 'cuda'
self._setup_transformer_trajectory()
self.reward_scale = 1.0
self.t = 0
def _setup_transformer_trajectory(self):
obs = self.env._get_obs()
dim_o = obs['observation'].shape[0]
dim_g = obs['desired_goal'].shape[0]
dim_a = self.env.action_space.shape[0]
self.observations = torch.zeros((0, dim_o), device=self.device, dtype=torch.float32)
self.goals = torch.zeros((0, dim_g), device=self.device, dtype=torch.float32)
self.actions = torch.zeros((0, dim_a), device=self.device, dtype=torch.float32)
self.rewards = torch.zeros(0, device=self.device, dtype=torch.float32)
self.target_return = torch.tensor(self.target_return, device=self.device, dtype=torch.float32).reshape(1, 1)
self.target_time_to_goal = torch.tensor(self.target_time_to_goal_init, device=self.device, dtype=torch.float32).reshape(1, 1)
self.timesteps = torch.tensor(0, device=self.device, dtype=torch.long).reshape(1, 1)
def _update_transformer_trajectory(self, obs):
self.observations = torch.cat([self.observations, torch.from_numpy(obs['observation']).to(self.device)], dim=0)
self.goals = torch.cat([self.goals, torch.from_numpy(obs['desired_goal']).to(self.device)], dim=0)
def _step_simulation_task(self, task):
"""Step simulation
"""
if self.demo == None:
# print(f"scene id:{self.id}")
if task.time - self.time > 1 / 240.0:
self.before_simulation_step()
# Step simulation
p.stepSimulation()
self.after_simulation_step()
# Call trigger update scene (if necessary) and draw methods
p.getCameraImage(
width=1, height=1,
viewMatrix=self.env._view_matrix,
projectionMatrix=self.env._proj_matrix)
p.setGravity(0,0,-10.0)
# print(f"ecm view out matrix:{self.ecm_view_out}")
self.time = task.time
else:
if time.time() - self.time > 1/240:
self.before_simulation_step()
# Step simulation
#pb.stepSimulation()
# self._duration = 0.1 # needle
self._duration = 0.1
step(self._duration)
self.after_simulation_step()
# Call trigger update scene (if necessary) and draw methods
p.getCameraImage(
width=1, height=1,
viewMatrix=self.env._view_matrix,
projectionMatrix=self.env._proj_matrix)
p.setGravity(0,0,-10.0)
self.time = time.time()
# print(f"current time: {self.time}")
# print(f"current task time: {task.time}")
# if time.time()-self.start_time > (self.itr + 1) * time_size:
obs = self.env._get_obs()
obs = self.env._get_obs()['achieved_goal'] if isinstance(obs, dict) else None
# print(f"waypoints: {self.env._waypoints}")
success = self.env._is_success(obs,self.env._sample_goal()) if obs is not None else False
print(f"success: {success}")
wait_list=[12,30]
if (self.id not in wait_list and success) or time.time()-self.start_time > 10:
# if self.cnt>=6:
# self.kivy_ui.stop()
# self.app.win.removeDisplayRegion(self.ui_display_region)
# self.before_simulation_step()
# self._duration = 0.2
# step(self._duration)
# self.after_simulation_step()
open_scene(0)
print(f"xxxx current time:{time.time()}")
open_scene(self.id)
exempt_l = [i for i in range(21,23)]
if self.id not in exempt_l:
self.toggleEcmView()
# self.cnt+=1
return
# self.start_time=time.time()
# self.toggleEcmView()
# self.itr += 1
return Task.cont
def load_policy(self, obs, env):
steps = str(300000)
if self.id == 8:
model_file = './peg_transfer_model'
if self.id == 6:
model_file = './needle_pick_model'
actor_params = os.path.join(model_file, 'actor_' + steps + '.pt')
actor_params = torch.load(actor_params)
dim_o = obs['observation'].shape[0]
dim_g = obs['desired_goal'].shape[0]
dim_a = env.action_space.shape[0]
actor = DeterministicActor(
dim_o, dim_g, dim_a, 256
)
actor.load_state_dict(actor_params)
g_norm = Normalizer(dim_g)
g_norm_stat = np.load(os.path.join(model_file, 'g_norm_' + steps + '_stat.npy'), allow_pickle=True).item()
g_norm.mean = g_norm_stat['mean']
g_norm.std = g_norm_stat['std']
o_norm = Normalizer(dim_o)
o_norm_stat = np.load(os.path.join(model_file, 'o_norm_' + steps + '_stat.npy'), allow_pickle=True).item()
o_norm.mean = o_norm_stat['mean']
o_norm.std = o_norm_stat['std']
return actor, o_norm, g_norm
def _preproc_inputs(self, o, g, o_norm, g_norm):
o_norm = o_norm.normalize(o)
g_norm = g_norm.normalize(g)
inputs = np.concatenate([o_norm, g_norm])
inputs = torch.tensor(inputs, dtype=torch.float32).unsqueeze(0)
return inputs
def load_transformer_policy(self, obs, env):
if self.id == 8:
model_file = './peg_transfer_model'
actor_params = os.path.join(model_file, 'PegTransfer-v0.pth')
if self.id == 6:
model_file = './needle_pick_model'
actor_params = os.path.join(model_file, 'NeedlePick-v0.pth')
actor_params = torch.load(actor_params)
dim_o = obs['observation'].shape[0]
dim_g = obs['desired_goal'].shape[0]
dim_a = env.action_space.shape[0]
self.observation_dim = dim_o
self.goal_dim = dim_g
self.action_dim = dim_a
actor = GoalConditionedDecisionTransformer(
state_dim=dim_o,
goal_dim=dim_g,
act_dim=dim_a,
max_length=100,
max_ep_len=1000,
hidden_size=128,
n_layer=8,
n_head=4,
n_inner=4*128,
activation_function='relu',
n_positions=1024,
resid_pdrop=0.1,
attn_pdrop=0.1,
).to(device=self.device)
actor.load_state_dict(actor_params)
surrol_dataset = SurRoLDataset(model_file, seq_max_length=100, task='PegTranfer-v0' if self.id == 8 else 'NeedlePick-v0', with_relabel=True)
self.offline_data_obs_mean, self.offline_data_obs_std, self.offline_data_goal_mean, self.offline_data_goal_std = surrol_dataset.pop_normalization_parameters()
return actor
def _get_transformer_output(self):
# Add padding
self.actions = torch.cat([self.actions, torch.zeros((1, self.action_dim), device=self.device)], dim=0)
self.rewards = torch.cat([self.rewards, torch.zeros(1, device=self.device)])
action = self.actor.get_action(
(self.observations.to(dtype=torch.float32) - self.offline_data_obs_mean) / self.offline_data_obs_std,
(self.goals.to(dtype=torch.float32) - self.offline_data_goal_mean) / self.offline_data_goal_std,
self.actions.to(dtype=torch.float32),
self.rewards.to(dtype=torch.float32),
self.target_return.to(dtype=torch.float32),
self.target_time_to_goal.to(dtype=torch.float32),
self.timesteps.to(dtype=torch.long),
)
self.actions[-1] = action
action = action.detach().cpu().numpy()
return action
def after_simulation_step(self):
state = self.env._get_obs()
reward = -1.0
cur_observation = torch.from_numpy(state['observation']).to(device=self.device).reshape(1, self.observation_dim)
cur_goal = torch.from_numpy(state['desired_goal']).to(device=self.device).reshape(1, self.goal_dim)
observations = torch.cat([observations, cur_observation], dim=0)
goals = torch.cat([goals, cur_goal], dim=0)
self.rewards[-1] = reward
# Design for delayed reward
pred_return = target_return[0, -1] - (reward / self.reward_scale)
target_return = torch.cat([target_return, pred_return.reshape(1, 1)], dim=1)
pred_time_to_goal = target_time_to_goal[0, -1] - 1.
target_time_to_goal = torch.cat([target_time_to_goal, pred_time_to_goal.reshape(1, 1)], dim=1)
timesteps = torch.cat([timesteps, torch.ones((1, 1), device=self.device, dtype=torch.long) * (self.t+1)], dim=1)
self.t += 1
def before_simulation_step(self):
if (self.id == 8 or self.id == 6) and not self.has_load_policy:
obs = self.env._get_obs()
self.actor = self.load_transformer_policy(obs, self.env)
self.has_load_policy = True
self.ecm_action = np.zeros(self.ecm_action.shape)
retrived_action = np.array([0, 0, 0, 0, 0], dtype = np.float32)
getDeviceAction_right(retrived_action)
if self.demo:
obs = self.env._get_obs()
action = self.env.get_oracle_action(obs)
if self.env.ACTION_SIZE != 3 and self.env.ACTION_SIZE != 1:
# haptic right
# retrived_action-> x,y,z, angle, buttonState(0,1,2)
if retrived_action[4] == 2:
self.psm1_action[0] = 0
self.psm1_action[1] = 0
self.psm1_action[2] = 0
self.psm1_action[3] = 0
else:
self.psm1_action[0] = retrived_action[2]*0.9
self.psm1_action[1] = retrived_action[0]*0.9
self.psm1_action[2] = retrived_action[1]*0.9
self.psm1_action[3] = -retrived_action[3]/math.pi*180*0.5
if retrived_action[4] == 0:
self.psm1_action[4] = 1
if retrived_action[4] == 1:
self.psm1_action[4] = -0.5
if retrived_action[4] == 3:
self.psm1_action[0] = 0
self.psm1_action[1] = 0
self.psm1_action[2] = 0
self.psm1_action[3] = 0
# print(f"len of retrieved action:{len(retrived_action)}")
if self.demo:
if self.id ==8:
obs = self.env._get_obs()
# o, g = obs['observation'], obs['desired_goal']
self._update_transformer_trajectory(obs)
action = self._get_transformer_output()
# action = self.actor(input_tensor).data.numpy().flatten()
print(f"retrieved action is: {self.psm1_action}")
self.psm1_action = action
self.env._set_action(self.psm1_action)
else:
obs = self.env._get_obs()
action = self.env.get_oracle_action(obs)
self.psm1_action = action
self.env._set_action(self.psm1_action)
self.env._step_callback()
else:
self.env._set_action(self.psm1_action)
self.env._step_callback()
if retrived_action[4] == 3 and self.env.ACTION_ECM_SIZE == 3:
self.ecm_action[0] = -retrived_action[0]*0.2
self.ecm_action[1] = -retrived_action[1]*0.2
self.ecm_action[2] = retrived_action[2]*0.2
if retrived_action[4] == 3 and self.env.ACTION_ECM_SIZE == 1:
self.ecm_action[0] = -retrived_action[3]/math.pi*180*0.5
#active track
if self.id == 27 or self.id == 28:
self.env._step_callback()
# self.env._set_action(self.ecm_action)
if self.demo and (self.env.ACTION_ECM_SIZE == 3 or self.env.ACTION_ECM_SIZE == 1):
self.ecm_action = action
self.env._set_action(self.ecm_action)
if self.demo is None:
if self.env.ACTION_ECM_SIZE == 1:
self.env._set_action(self.ecm_action)
else:
self.env._set_action_ecm(self.ecm_action)
self.env.ecm.render_image()
# _,_,rgb,_,_ = p.getCameraImage(
# width=100, height=100,
# viewMatrix=self.env._view_matrix,
# projectionMatrix=self.env._proj_matrix
# )
# rgb_array = np.array(rgb, dtype=np.uint8)
# if len(rgb_array)>0:
# rgb_array = np.reshape(rgb_array, (100, 100, 4))
# rgb_array = rgb_array[:, :, :3]
# imageio.imwrite('test.png',rgb_array)
if self.ecm_view:
self.env._view_matrix = self.env.ecm.view_matrix
else:
self.env._view_matrix = self.ecm_view_out
def setPsmAction(self, dim, val):
self.psm1_action[dim] = val
def addPsmAction(self, dim, incre):
self.psm1_action[dim] += incre
def addEcmAction(self, dim, incre):
self.ecm_action[dim] += incre
def setEcmAction(self, dim, val):
self.ecm_action[dim] = val
def resetEcmFlag(self):
# print("reset enter")
self.env._reset_ecm_pos()
def toggleEcmView(self):
self.ecm_view = not self.ecm_view
def on_destroy(self):
# !!! important
stopScheduler()
closeTouch_right()
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
class SurgicalSimulatorBimanual(SurgicalSimulatorBase):
def __init__(self, env_type, env_params, jaw_states=[1.0, 1.0],id=None,demo=None):
super(SurgicalSimulatorBimanual, self).__init__(env_type, env_params)
initTouch_right()
initTouch_left()
startScheduler()
self.id=id
self.demo = demo
self.closed=True
self.start_time = time.time()
self.psm1_action = np.zeros(env_type.ACTION_SIZE // 2)
self.psm1_action[4] = jaw_states[0]
self.psm2_action = np.zeros(env_type.ACTION_SIZE // 2)
self.psm2_action[4] = jaw_states[1]
self.app.accept('w-up', self.setPsmAction1, [2, 0])
self.app.accept('w-repeat', self.addPsmAction1, [2, 0.01])
self.app.accept('s-up', self.setPsmAction1, [2, 0])
self.app.accept('s-repeat', self.addPsmAction1, [2, -0.01])
self.app.accept('d-up', self.setPsmAction1, [1, 0])
self.app.accept('d-repeat', self.addPsmAction1, [1, 0.01])
self.app.accept('a-up', self.setPsmAction1, [1, 0])
self.app.accept('a-repeat', self.addPsmAction1, [1, -0.01])
self.app.accept('q-up', self.setPsmAction1, [0, 0])
self.app.accept('q-repeat', self.addPsmAction1, [0, 0.01])
self.app.accept('e-up', self.setPsmAction1, [0, 0])
self.app.accept('e-repeat', self.addPsmAction1, [0, -0.01])
self.app.accept('c-up', self.setPsmAction1, [4, 1.0])
self.app.accept('c-repeat', self.setPsmAction1, [4, -0.5])
self.app.accept('i-up', self.setPsmAction2, [2, 0])
self.app.accept('i-repeat', self.addPsmAction2, [2, 0.01])
self.app.accept('k-up', self.setPsmAction2, [2, 0])
self.app.accept('k-repeat', self.addPsmAction2, [2, -0.01])
self.app.accept('l-up', self.setPsmAction2, [1, 0])
self.app.accept('l-repeat', self.addPsmAction2, [1, 0.01])
self.app.accept('j-up', self.setPsmAction2, [1, 0])
self.app.accept('j-repeat', self.addPsmAction2, [1, -0.01])
self.app.accept('u-up', self.setPsmAction2, [0, 0])
self.app.accept('u-repeat', self.addPsmAction2, [0, 0.01])
self.app.accept('o-up', self.setPsmAction2, [0, 0])
self.app.accept('o-repeat', self.addPsmAction2, [0, -0.01])
self.app.accept('n-up', self.setPsmAction2, [4, 1.0])
self.app.accept('n-repeat', self.setPsmAction2, [4, -0.5])
self.ecm_view = 0
self.ecm_view_out = None
exempt_l = [i for i in range(21,23)]
if self.id not in exempt_l:
self.toggleEcmView()
self.ecm_action = np.zeros(env_type.ACTION_ECM_SIZE)
self.app.accept('5-up', self.setEcmAction, [2, 0])
self.app.accept('5-repeat', self.addEcmAction, [2, 0.2])
self.app.accept('2-up', self.setEcmAction, [2, 0])
self.app.accept('2-repeat', self.addEcmAction, [2, -0.2])
self.app.accept('6-up', self.setEcmAction, [1, 0])
self.app.accept('6-repeat', self.addEcmAction, [1, 0.2])
self.app.accept('4-up', self.setEcmAction, [1, 0])
self.app.accept('4-repeat', self.addEcmAction, [1, -0.2])
self.app.accept('1-up', self.setEcmAction, [0, 0])
self.app.accept('1-repeat', self.addEcmAction, [0, 0.2])
self.app.accept('3-up', self.setEcmAction, [0, 0])
self.app.accept('3-repeat', self.addEcmAction, [0, -0.2])
self.app.accept('m-up', self.toggleEcmView)
self.app.accept('r-up', self.resetEcmFlag)
def _step_simulation_task(self, task):
"""Step simulation
"""
if self.demo == None:
# print(f"scene id:{self.id}")
if task.time - self.time > 1 / 240.0:
self.before_simulation_step()
# Step simulation
p.stepSimulation()
self.after_simulation_step()
# Call trigger update scene (if necessary) and draw methods
p.getCameraImage(
width=1, height=1,
viewMatrix=self.env._view_matrix,
projectionMatrix=self.env._proj_matrix)
p.setGravity(0,0,-10.0)
self.time = task.time
else:
if time.time() - self.time > 1/240:
self.before_simulation_step()
# Step simulation
#pb.stepSimulation()
# self._duration = 0.1 # needle
self._duration = 0.1
step(self._duration)
self.after_simulation_step()
# Call trigger update scene (if necessary) and draw methods
p.getCameraImage(
width=1, height=1,
viewMatrix=self.env._view_matrix,
projectionMatrix=self.env._proj_matrix)
p.setGravity(0,0,-10.0)
self.time = time.time()
# print(f"current time: {self.time}")
# print(f"current task time: {task.time}")
# if time.time()-self.start_time > (self.itr + 1) * time_size:
obs = self.env._get_obs()
obs = self.env._get_obs()['achieved_goal'] if isinstance(obs, dict) else None
# success = self.env._is_success(obs,self.env._sample_goal()) if obs is not None else False
if time.time()-self.start_time > 18:
# if self.cnt>=6:
# self.kivy_ui.stop()
# self.app.win.removeDisplayRegion(self.ui_display_region)
open_scene(0)
print(f"xxxx current time:{time.time()}")
open_scene(self.id)
exempt_l = [i for i in range(21,23)]
if self.id not in exempt_l:
self.toggleEcmView()
# self.cnt+=1
return
# self.start_time=time.time()
# self.toggleEcmView()
# self.itr += 1
return Task.cont
def before_simulation_step(self):
# haptic left
retrived_action = np.array([0, 0, 0, 0, 0], dtype = np.float32)
getDeviceAction_left(retrived_action)
# retrived_action-> x,y,z, angle, buttonState(0,1,2)
if self.demo:
obs = self.env._get_obs()
action = self.env.get_oracle_action(obs)
if retrived_action[4] == 2:
self.psm1_action[0] = 0
self.psm1_action[1] = 0
self.psm1_action[2] = 0
self.psm1_action[3] = 0
else:
self.psm1_action[0] = retrived_action[2]*0.7
self.psm1_action[1] = retrived_action[0]*0.7
self.psm1_action[2] = retrived_action[1]*0.7
self.psm1_action[3] = -retrived_action[3]/math.pi*180*0.6
if retrived_action[4] == 0:
self.psm1_action[4] = 1
if retrived_action[4] == 1:
self.psm1_action[4] = -0.5
# # haptic right
retrived_action = np.array([0, 0, 0, 0, 0], dtype = np.float32)
getDeviceAction_right(retrived_action)
# retrived_action-> x,y,z, angle, buttonState(0,1,2)
if retrived_action[4] == 2:
self.psm2_action[0] = 0
self.psm2_action[1] = 0
self.psm2_action[2] = 0
self.psm2_action[3] = 0
else:
self.psm2_action[0] = retrived_action[2]
self.psm2_action[1] = retrived_action[0]
self.psm2_action[2] = retrived_action[1]
self.psm2_action[3] = -retrived_action[3]/math.pi*180
if retrived_action[4] == 0:
self.psm2_action[4] = 1
if retrived_action[4] == 1:
self.psm2_action[4] = -0.5
'''Control ECM'''
if retrived_action[4] == 3:
self.psm1_action[0] = 0
self.psm1_action[1] = 0
self.psm1_action[2] = 0
self.psm1_action[3] = 0
self.ecm_action[0] = -retrived_action[0]*0.2
self.ecm_action[1] = -retrived_action[1]*0.2
self.ecm_action[2] = retrived_action[2]*0.2
if self.demo:
self.env._set_action(action)
self.env._step_callback(demo=self.demo)
else:
self.env._set_action(np.concatenate([self.psm2_action, self.psm1_action], axis=-1))
self.env._step_callback()
# self.env._set_action(self.ecm_action)
self.env._set_action_ecm(self.ecm_action)
self.env.ecm.render_image()
if self.ecm_view:
self.env._view_matrix = self.env.ecm.view_matrix
else:
self.env._view_matrix = self.ecm_view_out
def setPsmAction1(self, dim, val):
self.psm1_action[dim] = val
def addPsmAction1(self, dim, incre):
self.psm1_action[dim] += incre
def setPsmAction2(self, dim, val):
self.psm2_action[dim] = val
def addPsmAction2(self, dim, incre):
self.psm2_action[dim] += incre
def addEcmAction(self, dim, incre):
self.ecm_action[dim] += incre
def setEcmAction(self, dim, val):
self.ecm_action[dim] = val
def resetEcmFlag(self):
# print("reset enter")
self.env._reset_ecm_pos()
def toggleEcmView(self):
self.ecm_view = not self.ecm_view
def on_destroy(self):
# !!! important
stopScheduler()
closeTouch_right()
closeTouch_left()
self.kivy_ui.stop()
self.app.win.removeDisplayRegion(self.ui_display_region)
# ecm steoro size 1024x768
app_cfg = ApplicationConfig(window_width=1200, window_height=900)
app = Application(app_cfg)
open_scene(0)
app.run() |