gpsinertialpose.js 2.2 KB
arcs_module(
    function(ARCS) {
        var GPSInertialPose = new ARCS.Component(
            function() {
                var id = 0;
                var pose = {
                    position : [ 0, 0, 0],
                    rotation : [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]
                };
                var cos = Math.cos;
                var sin = Math.sin;
                var meterToDegree = 0.00001;
                
                this.setId = function(identifier) {
                    id = identifier;
                };
                
                this.setPosition = function(longitude, latitude) {
                    pose.position = [ longitude, latitude, 1.2*meterToDegree ];
                    
                    this.emit('sendPose', [{ id: id,  pose : pose}]);
                };
                
                // this function computes the rotation matrix according to
                // https://w3c.github.io/deviceorientation/spec-source-orientation.html
                this.setOrientation = function(orientation) {
                    var alpha = orientation.alpha;
                    var beta = orientation.beta;
                    var gamma = orientation.gamma;
                    
                    pose.rotation = [[
                            cos(alpha)*cos(gamma) - sin(alpha)*sin(beta)*sin(gamma),
                            -cos(beta)*sin(alpha),
                            cos(gamma)*sin(alpha)*sin(beta) + cos(alpha)*sin(gamma)
                        ], [
                            cos(gamma)*sin(alpha) + cos(alpha)*sin(beta)*sin(gamma),
                            cos(alpha)*cos(beta),
                            sin(alpha)*sin(gamma) - cos(alpha)*cos(gamma)*sin(beta)
                        ], [
                            -cos(beta)*sin(gamma),
                            sin(beta),
                            cos(beta)*cos(gamma)
                        ]
                    ];
                    
                    this.emit('sendPose', [{ id: id,  pose : pose}]);
                };
            },
            ['setPosition','setOrientation'],
            ['sendPose']
        );
            
        
        return { GPSInertialPose: GPSInertialPose };
    }      
);