cfry / dde

Dexter Development Environment
Other
44 stars 25 forks source link

Dexter.Vector.DCM_to_quaternion() returns [undefined, undefined, undefined, undefined] for a particular input. #97

Open JamesNewton opened 9 months ago

JamesNewton commented 9 months ago

Dexter.Vector.DCM_to_quaternion() returns [undefined, undefined, undefined, undefined] for a particular input. This bug breaks DH inverse kinematics.It looks like there are a series of if else statements in Dexter.Vector.DCM_to_quaternion() and it is missing a final else statement as a catch all. I have no idea what the correct math would be for this catch all though. However there are known working methods which are used in current firmware.

The following code reproduces the issue:

//Vector.DCM_to_quaternion() bug:

var DCM

//This case works:
DCM = [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
]
out(Vector.DCM_to_quaternion(DCM))

//This case works:
DCM = Vector.make_DCM([30, 45, 60])
out(Vector.DCM_to_quaternion(DCM))

//This case fails 
DCM = [
    [-0.7195571193772048, 0.6909849857540601, -0.06911802526143601],
    [-0.13159082483727083, -0.2334028122649974, -0.9634349910842126],
    [-0.6818514550635101, -0.6841512089364833, 0.2588739394734867]
]
out(Vector.DCM_to_quaternion(DCM))

//Roughly 28% of randomized DCMs fail:
var N = 100000
var fail_count = 0
for(let i = 0; i < N; i++){
    DCM = Vector.make_DCM([Math.random()*180-180, Math.random()*180-180, Math.random()*180-180])
    let quat = Vector.DCM_to_quaternion(DCM)
    if(quat[0] === undefined){
        fail_count++
    }
}
out(Math.round(fail_count / N * 100) + "% out of " + N + " cases fail.")

https://github.com/cfry/dde/blob/5f84cbb2183dd9d52cfa5a737429248e69bea0b5/math/Vector.js#L1388