Skip to content

Commit

Permalink
G17, G18, G19 commands support added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denvi committed Oct 30, 2015
1 parent f07e4ee commit 49a0a85
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 11 deletions.
Binary file modified bin/grblControl.exe
Binary file not shown.
7 changes: 6 additions & 1 deletion gcodeparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ QList<PointSegment*> GcodeParser::expandArc()
QVector3D *center = lastSegment->center();
double radius = lastSegment->getRadius();
bool clockwise = lastSegment->isClockwise();
PointSegment::planes plane = startSegment->plane();

//
// Start expansion.
//

QList<QVector3D> expandedPoints = GcodePreprocessorUtils::generatePointsAlongArcBDring(*start, *end, *center, clockwise, radius, m_smallArcThreshold, m_smallArcSegmentLength);
QList<QVector3D> expandedPoints = GcodePreprocessorUtils::generatePointsAlongArcBDring(plane, *start, *end, *center, clockwise, radius, m_smallArcThreshold, m_smallArcSegmentLength);

// Validate output of expansion.
if (expandedPoints.length() == 0) {
Expand Down Expand Up @@ -293,6 +294,7 @@ PointSegment *GcodeParser::addArcPointSegment(QVector3D nextPoint, bool clockwis
ps->setIsClockwise(clockwise);
ps->setIsAbsolute(this->m_inAbsoluteMode);
ps->setSpeed(this->m_lastSpeed);
ps->setPlane(m_currentPlane);
this->m_points.append(ps);

// Save off the endpoint.
Expand Down Expand Up @@ -320,6 +322,9 @@ PointSegment * GcodeParser::handleGCode(QString code, QList<QString> &args) {
else if (code == "38.2") ps = addLinearPointSegment(nextPoint, false);
else if (code == "2") ps = addArcPointSegment(nextPoint, true, args);
else if (code == "3") ps = addArcPointSegment(nextPoint, false, args);
else if (code == "17") this->m_currentPlane = PointSegment::XY;
else if (code == "18") this->m_currentPlane = PointSegment::ZX;
else if (code == "19") this->m_currentPlane = PointSegment::YZ;
else if (code == "20") this->m_isMetric = false;
else if (code == "21") this->m_isMetric = true;
else if (code == "90") this->m_inAbsoluteMode = true;
Expand Down
1 change: 1 addition & 0 deletions gcodeparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public slots:
QString m_lastGcodeCommand;
QVector3D m_currentPoint;
int m_commandNumber;
PointSegment::planes m_currentPlane;

// Settings
double m_speedOverride;
Expand Down
41 changes: 36 additions & 5 deletions gcodepreprocessorutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,27 @@ double GcodePreprocessorUtils::calculateSweep(double startAngle, double endAngle
/**
* Generates the points along an arc including the start and end points.
*/
QList<QVector3D> GcodePreprocessorUtils::generatePointsAlongArcBDring(QVector3D start, QVector3D end, QVector3D center, bool clockwise, double R, double minArcLength, double arcSegmentLength)
QList<QVector3D> GcodePreprocessorUtils::generatePointsAlongArcBDring(PointSegment::planes plane, QVector3D start, QVector3D end, QVector3D center, bool clockwise, double R, double minArcLength, double arcSegmentLength)
{
double radius = R;

// Rotate vectors according to plane
QMatrix4x4 m;
m.setToIdentity();
switch (plane) {
case PointSegment::XY:
break;
case PointSegment::ZX:
m.rotate(90, 1.0, 0.0, 0.0);
break;
case PointSegment::YZ:
m.rotate(-90, 0.0, 1.0, 0.0);
break;
}
start = m * start;
end = m * end;
center = m * center;

// Calculate radius if necessary.
if (radius == 0) {
radius = sqrt(pow((double)(start.x() - center.x()), 2.0) + pow((double)(end.y() - center.y()), 2.0));
Expand Down Expand Up @@ -441,17 +458,31 @@ QList<QVector3D> GcodePreprocessorUtils::generatePointsAlongArcBDring(QVector3D
numPoints = (int)ceil(arcLength/arcSegmentLength);
}

return generatePointsAlongArcBDring(start, end, center, clockwise, radius, startAngle, sweep, numPoints);
return generatePointsAlongArcBDring(plane, start, end, center, clockwise, radius, startAngle, sweep, numPoints);
}

/**
* Generates the points along an arc including the start and end points.
*/
QList<QVector3D> GcodePreprocessorUtils::generatePointsAlongArcBDring(QVector3D p1, QVector3D p2,
QList<QVector3D> GcodePreprocessorUtils::generatePointsAlongArcBDring(PointSegment::planes plane, QVector3D p1, QVector3D p2,
QVector3D center, bool isCw,
double radius, double startAngle,
double sweep, int numPoints)
{
// Prepare rotation matrix to restore plane
QMatrix4x4 m;
m.setToIdentity();
switch (plane) {
case PointSegment::XY:
break;
case PointSegment::ZX:
m.rotate(-90, 1.0, 0.0, 0.0);
break;
case PointSegment::YZ:
m.rotate(90, 0.0, 1.0, 0.0);
break;
}

QVector3D lineEnd(p2.x(), p2.y(), p1.z());
QList<QVector3D> segments;
double angle;
Expand All @@ -478,10 +509,10 @@ QList<QVector3D> GcodePreprocessorUtils::generatePointsAlongArcBDring(QVector3D
lineEnd.setY(sin(angle) * radius + center.y());
lineEnd.setZ(lineEnd.z() + zIncrement);

segments.append(lineEnd);
segments.append(m * lineEnd);
}

segments.append(p2);
segments.append(m * p2);

return segments;
}
Expand Down
8 changes: 5 additions & 3 deletions gcodepreprocessorutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#define GCODEPREPROCESSORUTILS_H

#include <QObject>
#include <QMatrix4x4>
#include "pointsegment.h"

class GcodePreprocessorUtils : public QObject
{
Q_OBJECT
public:
public:
static QString overrideSpeed(QString command, double speed);
static QString removeComment(QString command);
static QString parseComment(QString command);
Expand All @@ -32,8 +34,8 @@ class GcodePreprocessorUtils : public QObject
static QString generateG1FromPoints(QVector3D start, QVector3D end, bool absoluteMode, int precision);
static double getAngle(QVector3D start, QVector3D end);
static double calculateSweep(double startAngle, double endAngle, bool isCw);
static QList<QVector3D> generatePointsAlongArcBDring(QVector3D start, QVector3D end, QVector3D center, bool clockwise, double R, double minArcLength, double arcSegmentLength);
static QList<QVector3D> generatePointsAlongArcBDring(QVector3D p1, QVector3D p2, QVector3D center, bool isCw, double radius, double startAngle, double sweep, int numPoints);
static QList<QVector3D> generatePointsAlongArcBDring(PointSegment::planes plane, QVector3D start, QVector3D end, QVector3D center, bool clockwise, double R, double minArcLength, double arcSegmentLength);
static QList<QVector3D> generatePointsAlongArcBDring(PointSegment::planes plane, QVector3D p1, QVector3D p2, QVector3D center, bool isCw, double radius, double startAngle, double sweep, int numPoints);
static inline bool isDigit(char c);
static inline bool isLetter(char c);
static inline char toUpper(char c);
Expand Down
2 changes: 1 addition & 1 deletion gcodeviewparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ QList<LineSegment *> GcodeViewParse::getLinesFromParser(GcodeParser *gp, double
// Expand arc for graphics.
if (ps->isArc()) {
QList<QVector3D> points =
GcodePreprocessorUtils::generatePointsAlongArcBDring(
GcodePreprocessorUtils::generatePointsAlongArcBDring(ps->plane(),
*start, *end, *ps->center(), ps->isClockwise(), ps->getRadius(), minArcLength, arcSegmentLength);
// Create line segments from points.
if (points.length() > 0) {
Expand Down
1 change: 0 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <QApplication>
#include <QDebug>
#include <QVector3D>
#include <QGLWidget>
#include <QLocale>
#include <QTranslator>
Expand Down
11 changes: 11 additions & 0 deletions pointsegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,14 @@ void PointSegment::setIsAbsolute(bool isAbsolute)
m_isAbsolute = isAbsolute;
}

PointSegment::planes PointSegment::plane() const
{
return m_plane;
}

void PointSegment::setPlane(const planes &plane)
{
m_plane = plane;
}


10 changes: 10 additions & 0 deletions pointsegment.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class PointSegment : public QObject
{
Q_OBJECT
public:
enum planes {
XY,
ZX,
YZ
};

PointSegment(QObject *parent = 0);
PointSegment(PointSegment *ps);
PointSegment(const QVector3D *b, int num);
Expand Down Expand Up @@ -52,6 +58,9 @@ class PointSegment : public QObject
bool isAbsolute() const;
void setIsAbsolute(bool isAbsolute);

planes plane() const;
void setPlane(const planes &plane);

signals:

public slots:
Expand All @@ -67,6 +76,7 @@ public slots:
bool m_isFastTraverse;
bool m_isAbsolute;
int m_lineNumber;
planes m_plane;
};

#endif // POINTSEGMENT_H

0 comments on commit 49a0a85

Please sign in to comment.