<commit_before>var quad = new require('rolling-spider')({
    logger: console.log
});
var hid = require('node-hid');
// console.log(hid.devices());
// Logitech Extreme 3D Pro's vendorID and productID: 1133:49685 (i.e. 046d:c215)
var joystick = new hid.HID(1133, 49685);
// console.log(joystick);

var flying = false;
var prevButtons;

quad.connect(function(err) {
    if (err) console.log(err);
    quad.setup(function(err) {
        if (err) console.log(err);
        quad.flatTrim();
        quad.startPing();
        quad.flatTrim();

        joystick.on('data', function(buf) {
            var controls = parseControls(buf);
            if (!prevButtons)
                prevButtons = controls.buttons;
            if (prevButtons[0] === 0 && controls.buttons[0] === 1) {
                if (flying) {
                    quad.land();
                    flying = false;
                } else {
                    quad.flatTrim();
                    quad.takeoff();
                    flying = true;
                }
            }
            prevButtons = controls.buttons;
            if (controls.buttons[1]) {
                if (controls.pitch <= 512) quad.forward(controlObject(controls.pitch, 512));
                else quad.backward(-controlObject(controls.pitch, 512));

                if (controls.roll <= 512) quad.left(controlObject(controls.roll, 512));
                else quad.right(-controlObject(controls.roll, 512));

                if (controls.yaw <= 128)    quad.counterClockwise(controlObject(controls.yaw, 128));
                else quad.clockwise(-controlObject(controls.yaw, 128));
                
                if(controls.throttle <= 128)  quad.down(controlObject(controls.throttle, 128));
                else quad.up(-controlObject(controls.throttle, 128));
            }
            // console.log(JSON.stringify(controls));
        });
        joystick.on('error', function() {
            quad.land();
        });
    });
});


function controlObject(val, range) {
    return {
        speed: 100 / range * (range - val),
        time: 1
    }
}

function parseControls(buf) {
    var ch = buf.toString('hex').match(/.{1,2}/g).map(function(c) {
        return parseInt(c, 16);
    });
    return {
        roll: ((ch[1] & 0x03) << 8) + ch[0],
        pitch: ((ch[2] & 0x0f) << 6) + ((ch[1] & 0xfc) >> 2),
        yaw: ch[3],<commit_msg>Added control code and tested working<commit_after>var RollingSpider = new require('rolling-spider');
var quad = new RollingSpider({
    logger: console.log
});
var hid = require('node-hid');
// console.log(hid.devices());
// Logitech Extreme 3D Pro's vendorID and productID: 1133:49685 (i.e. 046d:c215)
var joystick = new hid.HID(1133, 49685);
// console.log(joystick);

var flying = false;
var prevButtons;

quad.connect(function(err) {
    if (err) console.log(err);
    quad.setup(function(err) {
        if (err) console.log(err);
        quad.flatTrim();
        quad.startPing();
        quad.flatTrim();

        joystick.on('data', function(buf) {
            var controls = parseControls(buf);
            if (!prevButtons)
                prevButtons = controls.buttons;
            if (prevButtons[0] === 0 && controls.buttons[0] === 1) {
                if (flying) {
                    quad.land();
                    flying = false;
                } else {
                    quad.flatTrim();
                    quad.takeoff();
                    flying = true;
                }
            }
            prevButtons = controls.buttons;
            if (flying) {
                if (controls.buttons[1]) {
                    quad.driveStepsRemaining = 1;
                    quad.speeds = {
                        yaw: 100 * (controls.yaw - 128) / 128,
                        pitch: -100 * (controls.pitch - 512) / 512,
                        roll: 100 * (controls.roll - 512) / 512,
                        altitude: 100 * (controls.throttle - 128) / 128
                    }
                } else quad.hover();
            }
            // console.log(JSON.stringify(controls));
        });
        joystick.on('error', function() {
            quad.land();
        });
    });
});


function parseControls(buf) {
    var ch = buf.toString('hex').match(/.{1,2}/g).map(function(c) {
        return parseInt(c, 16);
    });
    return {
        roll: ((ch[1] & 0x03) << 8) + ch[0],
        pitch: ((ch[2] & 0x0f) << 6) + ((ch[1] & 0xfc) >> 2),
        yaw: ch[3],
{'fraction_non_alphanumeric': 0.15058087578194818, 'fraction_numerical': 0.03865058087578195, 'mean_word_length': 1.656973293768546, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5113138', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1321, 'n_words': 271}