File size: 4,359 Bytes
72f0edb
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
6bc7874
 
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { XMLParser } from "npm:[email protected]";
import { serve } from "https://deno.land/[email protected]/http/server.ts";

interface UrdfLink {
  name: string;
  mass: number;
  inertia: {
    ixx: number;
    ixy: number;
    ixz: number;
    iyy: number;
    iyz: number;
    izz: number;
  };
  has_visual: boolean;
  has_collision: boolean;
}

interface UrdfJoint {
  name: string;
  type: string;
  parent: string;
  child: string;
  limits?: {
    lower: number;
    upper: number;
    effort: number;
    velocity: number;
  };
}

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Headers":
    "authorization, x-client-info, apikey, content-type",
  "Access-Control-Allow-Methods": "POST, OPTIONS",
};

serve(async (req) => {
  // Handle CORS preflight requests
  if (req.method === "OPTIONS") {
    return new Response("ok", { headers: corsHeaders });
  }

  try {
    if (req.method !== "POST") {
      throw new Error("Method not allowed");
    }

    const { urdfContent } = await req.json();

    if (!urdfContent) {
      throw new Error("No Urdf content provided");
    }

    // Parse Urdf XML using fast-xml-parser
    const parser = new XMLParser({
      ignoreAttributes: false,
      attributeNamePrefix: "",
      parseAttributeValue: true,
    });

    const doc = parser.parse(urdfContent);

    if (!doc || !doc.robot) {
      throw new Error("No robot element found in Urdf");
    }

    // Extract robot data
    const robotElement = doc.robot;
    const name = robotElement.name || "unnamed_robot";
    const version = robotElement.version;
    const links: UrdfLink[] = [];
    const joints: UrdfJoint[] = [];
    let total_mass = 0;

    // Parse links
    if (robotElement.link) {
      const linkElements = Array.isArray(robotElement.link)
        ? robotElement.link
        : [robotElement.link];
      linkElements.forEach((link: any) => {
        const linkName = link.name || "";
        const inertial = link.inertial;
        const mass = inertial?.mass?.value || 0;
        total_mass += mass;

        const linkData: UrdfLink = {
          name: linkName,
          mass,
          inertia: {
            ixx: 0,
            ixy: 0,
            ixz: 0,
            iyy: 0,
            iyz: 0,
            izz: 0,
          },
          has_visual: !!link.visual,
          has_collision: !!link.collision,
        };

        // Parse inertia
        if (inertial?.inertia) {
          const inertiaEl = inertial.inertia;
          linkData.inertia = {
            ixx: inertiaEl.ixx || 0,
            ixy: inertiaEl.ixy || 0,
            ixz: inertiaEl.ixz || 0,
            iyy: inertiaEl.iyy || 0,
            iyz: inertiaEl.iyz || 0,
            izz: inertiaEl.izz || 0,
          };
        }

        links.push(linkData);
      });
    }

    // Parse joints
    if (robotElement.joint) {
      const jointElements = Array.isArray(robotElement.joint)
        ? robotElement.joint
        : [robotElement.joint];
      jointElements.forEach((joint: any) => {
        const jointData: UrdfJoint = {
          name: joint.name || "",
          type: joint.type || "",
          parent: joint.parent?.link || "",
          child: joint.child?.link || "",
        };

        // Parse limits
        if (joint.limit) {
          jointData.limits = {
            lower: joint.limit.lower || 0,
            upper: joint.limit.upper || 0,
            effort: joint.limit.effort || 0,
            velocity: joint.limit.velocity || 0,
          };
        }

        joints.push(jointData);
      });
    }

    const response = {
      success: true,
      data: {
        name,
        version,
        links,
        joints,
        total_mass,
        num_links: links.length,
        num_joints: joints.length,
      },
    };

    return new Response(JSON.stringify(response), {
      headers: {
        ...corsHeaders,
        "Content-Type": "application/json",
      },
    });
  } catch (error) {
    console.error("Error parsing Urdf:", error);
    return new Response(
      JSON.stringify({
        success: false,
        error:
          error instanceof Error ? error.message : "Unknown error occurred",
      }),
      {
        status: 500,
        headers: {
          ...corsHeaders,
          "Content-Type": "application/json",
        },
      }
    );
  }
});