84 lines
2.8 KiB
JavaScript
84 lines
2.8 KiB
JavaScript
import URDFLoader from './URDFLoader.js';
|
|
|
|
export default
|
|
class ExtendedURDFLoader extends URDFLoader {
|
|
constructor(manager) {
|
|
super(manager);
|
|
this.transmissionMap = {};
|
|
}
|
|
|
|
parseTransmissions(xml) {
|
|
const transmissions = xml.querySelectorAll('transmission');
|
|
this.transmissionMap = {};
|
|
|
|
transmissions.forEach(trans => {
|
|
const jointName = trans.querySelector('joint')?.getAttribute('name');
|
|
const actuator = trans.querySelector('actuator');
|
|
const reduction = parseFloat(actuator?.querySelector('mechanicalReduction')?.textContent ?? '1');
|
|
const actuatorName = actuator?.getAttribute('name') ?? null;
|
|
const encoderTicks = parseInt(actuator?.querySelector('encoderTicks')?.textContent ?? '4096');
|
|
const encoderValidMin = parseInt(actuator?.querySelector('encoderValidMin')?.textContent ?? '0');
|
|
const encoderValidMax = parseInt(actuator?.querySelector('encoderValidMax')?.textContent ?? '4095');
|
|
const encoderRange = parseInt(actuator?.querySelector('encoderRange')?.textContent ?? '180');
|
|
const motorID = parseInt(actuator?.querySelector('motorID')?.textContent ?? '47');
|
|
|
|
if (jointName) {
|
|
this.transmissionMap[jointName] = {
|
|
mechanicalReduction: reduction,
|
|
actuatorName,
|
|
encoderTicks,
|
|
encoderValidMin,
|
|
encoderValidMax,
|
|
encoderRange,
|
|
motorID
|
|
};
|
|
}
|
|
});
|
|
}
|
|
|
|
parseMimics(xml) {
|
|
const joints = xml.querySelectorAll('joint');
|
|
joints.forEach(jointEl => {
|
|
const jointName = jointEl.getAttribute('name');
|
|
const mimicEl = jointEl.querySelector('mimic');
|
|
if (mimicEl && jointName) {
|
|
const target = mimicEl.getAttribute('joint');
|
|
const multiplier = parseFloat(mimicEl.getAttribute('multiplier') ?? '1.0');
|
|
const offset = parseFloat(mimicEl.getAttribute('offset') ?? '0.0');
|
|
|
|
// Attach mimic info directly to the joint object
|
|
if (this.robot?.joints[jointName]) {
|
|
this.robot.joints[jointName].mimic = { target, multiplier, offset };
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadFromString(urdfText, options = {}) {
|
|
|
|
const xml = new DOMParser().parseFromString(urdfText, 'application/xml');
|
|
const parserError = xml.querySelector('parsererror');
|
|
if (parserError) {
|
|
console.error('❌ XML parser error:', parserError.textContent);
|
|
throw new Error('Invalid URDF XML: ' + parserError.textContent);
|
|
}
|
|
|
|
this.parseTransmissions(xml);
|
|
//this.parseMimics(xml);
|
|
|
|
const robot = this.parse(xml); // ✅ Use inherited parse() directly
|
|
//console.log(robot.joints);
|
|
for (const jointName in robot.joints) {
|
|
if (this.transmissionMap[jointName]) {
|
|
robot.joints[jointName].transmission = this.transmissionMap[jointName];
|
|
}
|
|
}
|
|
|
|
return robot;
|
|
}
|
|
}
|