示例#1
0
/**
 * Loops through the gcode file, continuously analysing a buffer to see
 * if it is a valid circle within a tolerance.
 * @param  SPLFixedArray $gcode The gcode as an SPLFixedArray of lines
 * @return string               The resulting GCode string
 */
function processGcode($gcode)
{
    global $debug;
    global $lookahead;
    global $start;
    // Keeps track of the amount of valid buffers (valid circles)
    $totalValidBuffers = 0;
    // The output gcode
    $output = "";
    // The looping buffer
    $buffer = new SplQueue();
    // go back to the start of our gcode
    $gcode->rewind();
    // prefill the buffer
    do {
        $buffer->enqueue($gcode->current());
        $gcode->next();
    } while ($buffer->count() < $lookahead);
    // keeps track of the last valid buffer.
    $lastValid = false;
    // Loop through our gcode
    for (; $gcode->valid(); $gcode->next()) {
        // Don't process for too long, and during debugging, only do a couple of buffers (finding circles
        // takes a long time)
        if (microtime(true) - $start > 120 || $debug === true && $totalValidBuffers > 100) {
            $output .= $gcode->current() . "\n";
            continue;
        }
        // check to see if we have a 'valid' buffer
        $valid = bufferValid($buffer);
        // if the buffer is no longer valid and we have more items in our buffer than
        // the lookahead value, then we must have had a valid buffer at the last step
        if ($valid === false) {
            if ($buffer->count() > $lookahead) {
                // The last element made the buffer invalid, so we pop it so that we
                // can stick it back on the end later
                $temp = $buffer->pop();
                // Our last buffer was a valid circle, so we take that
                $processed = $lastValid;
                // Double check that we had a valid buffer...
                if ($processed !== false) {
                    // creates a line array
                    $lines = getLines($buffer);
                    // If we're debugging, we draw the 'result' on a canvas.
                    if ($debug) {
                        drawResult($processed, $lines, 'gcodeview' . $gcode->key());
                    }
                    // Generate a gcode arc from the lines
                    $output .= generateGcodeArc($processed, $lines) . "\n";
                } else {
                    // otherwise we just stick the buffer on the output.
                    foreach ($buffer as $num => $buffer) {
                        $output .= $buffer . "\n";
                    }
                }
                // Now, we re-initialise the buffer and fill it up
                $buffer = new SplQueue();
                do {
                    $buffer->enqueue($gcode->current());
                    $gcode->next();
                } while ($buffer->count() < $lookahead && $gcode->valid());
                // Increase the amount of valid buffers
                $totalValidBuffers++;
                // Stick our previously popped temp value on the end
                $output .= $temp . "\n";
            } else {
                // otherwise, we dequeue a value off the buffer
                $output .= $buffer->dequeue() . "\n";
            }
        }
        // record the last valid buffer
        $lastValid = $valid;
        // If we still have code to process, stick it on the buffer!
        if ($gcode->valid()) {
            $buffer->enqueue($gcode->current());
        }
    }
    // We're done!
    printf("Total Valid Buffers: %d | ", $totalValidBuffers);
    return $output;
}
示例#2
0
/**
 * Parses gcode string
 * @param  SPLFixedArray $gcode The gcode as an SPLFixedArray of lines
 * @return array                An array of lines where each entry is split into command and parameters
 */
function parseGcode($gcode)
{
    global $debug;
    global $lookahead;
    global $start;
    $output = array();
    $gcode->rewind();
    $lastValid = false;
    for (; $gcode->valid(); $gcode->next()) {
        $output[] = parseLine($gcode->current());
    }
    return array_filter($output);
}