Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 62 additions & 34 deletions source/MRMesh/MRGcodeProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,47 @@ GcodeProcessor::MoveAction GcodeProcessor::processLine( const std::string_view&
return {};
}

void GcodeProcessor::setRotationParams( RotationAxisName paramName, const Vector3f& rotationAxis )
{
const int intParamName = int( paramName );
const bool validParamName = intParamName < int( RotationAxisName::Count );
if ( !validParamName )
return;

if ( rotationAxis.lengthSq() < 0.01f )
return;

rotationAxes_[intParamName] = rotationAxis;
}

Vector3f GcodeProcessor::getRotationParams( RotationAxisName paramName ) const
{
const int intParamName = int( paramName );
const bool validParamName = intParamName < int( RotationAxisName::Count );
if ( !validParamName )
return {};

return rotationAxes_[intParamName];
}

void GcodeProcessor::setRotationOrder( const RotationAxisOrder& rotationAxesOrder )
{
rotationAxesOrder_.clear();
rotationAxesOrderMap_.clear();
for ( int i = 0; i < rotationAxesOrder.size(); ++i )
{
const int intRotationAxis = int( rotationAxesOrder[i] );
if ( intRotationAxis == int( RotationAxisName::Count ) )
continue;

if ( std::find( rotationAxesOrderMap_.begin(), rotationAxesOrderMap_.end(), intRotationAxis ) != rotationAxesOrderMap_.end() )
continue;

rotationAxesOrder_.push_back( rotationAxesOrder[i] );
rotationAxesOrderMap_.push_back( intRotationAxis );
}
}

std::vector<GcodeProcessor::Command> GcodeProcessor::parseFrame_( const std::string_view& frame )
{
std::vector<Command> commands;
Expand Down Expand Up @@ -202,8 +243,7 @@ GcodeProcessor::MoveAction GcodeProcessor::generateMoveAction_()
res = getToolRotationPoints_();
}

// test
//assert( res.action.path.size() == res.toolDirection.size() );
assert( res.action.path.size() == res.toolDirection.size() );

translationPos_ = newMotorsPos;
if ( inputRotation_ )
Expand Down Expand Up @@ -438,52 +478,40 @@ MR::Vector3f GcodeProcessor::calcCoordMotors_()

MR::Vector3f GcodeProcessor::calcRealCoord_( const Vector3f& translationPos, const Vector3f& rotationAngles )
{
const Matrix3f rotationMatrixA = Matrix3f::rotation( Vector3f::plusX(), -rotationAngles[0] / 180.f * PI_F );
return rotationMatrixA * translationPos;
Vector3f res = translationPos;
for ( int i = 0; i < rotationAxesOrderMap_.size(); ++i )
{
const int axisNumber = rotationAxesOrderMap_[i];
const Matrix3f rotationMatrix = Matrix3f::rotation( rotationAxes_[axisNumber], rotationAngles[axisNumber] / 180.f * PI_F );
res = rotationMatrix * res;
}
return res;
}

void GcodeProcessor::updateRotationAngleAndMatrix_( const Vector3f& rotationAngles )
{
if ( rotationAngles[0] != rotationAngles_[0] )
{
rotationAngles_[0] = rotationAngles[0];
cacheRotationMatrix_[0] = Matrix3f::rotation( Vector3f::plusX(), -rotationAngles_[0] / 180.f * PI_F );
}
if ( rotationAngles[1] != rotationAngles_[1] )
{
rotationAngles_[1] = rotationAngles[1];
cacheRotationMatrix_[1] = Matrix3f::rotation( Vector3f::plusY(), -rotationAngles_[1] / 180.f * PI_F );
}
if ( rotationAngles[2] != rotationAngles_[2] )
for ( int i = 0; i < 3; ++i )
{
rotationAngles_[2] = rotationAngles[2];
cacheRotationMatrix_[2] = Matrix3f::rotation( Vector3f::plusZ(), -rotationAngles_[2] / 180.f * PI_F );
rotationAngles_[i] = rotationAngles[i];
cacheRotationMatrix_[i] = Matrix3f::rotation( rotationAxes_[i], rotationAngles_[i] / 180.f * PI_F );
}
}

MR::Vector3f GcodeProcessor::calcRealCoordCached_( const Vector3f& translationPos, const Vector3f& rotationAngles )
{
if ( rotationAngles[0] != rotationAngles_[0] )
{
rotationAngles_[0] = rotationAngles[0];
cacheRotationMatrix_[0] = Matrix3f::rotation( Vector3f::plusX(), -rotationAngles_[0] / 180.f * PI_F );
}
if ( rotationAngles[1] != rotationAngles_[1] )
{
rotationAngles_[1] = rotationAngles[1];
cacheRotationMatrix_[1] = Matrix3f::rotation( Vector3f::plusY(), -rotationAngles_[1] / 180.f * PI_F );
}
if ( rotationAngles[2] != rotationAngles_[2] )
{
rotationAngles_[2] = rotationAngles[2];
cacheRotationMatrix_[2] = Matrix3f::rotation( Vector3f::plusZ(), -rotationAngles_[2] / 180.f * PI_F );
}
return cacheRotationMatrix_[0] * cacheRotationMatrix_[2] * translationPos;
updateRotationAngleAndMatrix_( rotationAngles );
return calcRealCoordCached_( translationPos );
}

MR::Vector3f GcodeProcessor::calcRealCoordCached_( const Vector3f& translationPos )
{
return cacheRotationMatrix_[0] * cacheRotationMatrix_[2] * translationPos;
Vector3f res = translationPos;
for ( int i = 0; i < 3; ++i )
{
const int axisNumber = rotationAxesOrderMap_[i];
res = cacheRotationMatrix_[axisNumber] * res;
}
return res;
}

}
23 changes: 21 additions & 2 deletions source/MRMesh/MRGcodeProcessor.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need translation order too? mb some devices has moving axis (that means that translation is applied before rotation)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in current version it has no effect (will added in next PR)

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <string>
#include <optional>
#include <functional>


namespace MR
Expand Down Expand Up @@ -47,6 +48,21 @@ class MRMESH_CLASS GcodeProcessor
// process all commands from one line g-code source and generate corresponding move action
MRMESH_API MoveAction processLine( const std::string_view& line );

// settings
enum class RotationAxisName
{
A,
B,
C,
Count
};
//using RotationAxisMap = std::array<RotationAxisName, size_t( RotationAxisName::Count )>;
using RotationAxisOrder = std::vector<RotationAxisName>;
MRMESH_API void setRotationParams( RotationAxisName paramName, const Vector3f& rotationAxis );
MRMESH_API Vector3f getRotationParams( RotationAxisName paramName ) const;
MRMESH_API void setRotationOrder( const RotationAxisOrder& rotationAxisOrder );
const RotationAxisOrder& getRotationOrder() const { return rotationAxesOrder_; }

private:

struct Command
Expand Down Expand Up @@ -135,15 +151,18 @@ class MRMESH_CLASS GcodeProcessor

// input data (data entered in last line)
Vector3f inputCoords_; // x, y, z
Vector3<bool> inputCoordsReaded_; // any of (x, y, z) was readed
Vector3<bool> inputCoordsReaded_; // any of (x, y, z) was read
std::optional<float> radius_; // r
std::optional<Vector3f> arcCenter_; // i, j, k
std::optional<Vector3f> inputRotation_; // a, b, c

std::vector<std::string_view> gcodeSource_; // string list with sets of command (frames)

// internal settings
// internal / machine settings
float accuracy_ = 1.e-3f;
std::array<Vector3f, 3> rotationAxes_ = { Vector3f::minusX(), Vector3f::minusY(), Vector3f::plusZ() };
RotationAxisOrder rotationAxesOrder_ = { RotationAxisName::A, RotationAxisName::B, RotationAxisName::C };
std::vector<int> rotationAxesOrderMap_ = {0, 1, 2}; // mapping axis sequence number to axis number in storage

};

Expand Down