supoort rotation around z axis
Stefan Schuermans

Stefan Schuermans commited on 2013-05-18 14:37:50
Showing 7 changed files, with 56 additions and 2 deletions.

... ...
@@ -489,6 +489,26 @@ bool CmdParser::procCmd_set_precision(std::istream &strm)
489 489
   return true;
490 490
 }
491 491
 
492
+/**
493
+ * @brief process set_rotation_z command
494
+ * @param[in] strm stream to read command arguments from
495
+ * @return if processing command was successful
496
+ */
497
+bool CmdParser::procCmd_set_rotation_z(std::istream &strm)
498
+{
499
+  // get arguments
500
+  double z_angle;
501
+  strm >> z_angle;
502
+  if (strm.fail()) {
503
+    std::cerr << "missing Z axis rotation angle" << std::endl;
504
+    return false;
505
+  }
506
+
507
+  // update settings
508
+  mSettings.rotation_z = z_angle;
509
+  return true;
510
+}
511
+
492 512
 /**
493 513
  * @brief process set_tool_diameter command
494 514
  * @param[in] strm stream to read command arguments from
... ...
@@ -589,6 +609,8 @@ bool CmdParser::procLine(const std::string &strLine)
589 609
     return procCmd_set_layer_mode(strm);
590 610
   else if (cmd == "set_move_z")
591 611
     return procCmd_set_move_z(strm);
612
+  else if (cmd == "set_rotation_z")
613
+    return procCmd_set_rotation_z(strm);
592 614
   else if (cmd == "set_offset_x")
593 615
     return procCmd_set_offset_x(strm);
594 616
   else if (cmd == "set_offset_y")
... ...
@@ -158,6 +158,13 @@ public:
158 158
    */
159 159
   bool procCmd_set_precision(std::istream &strm);
160 160
 
161
+  /**
162
+   * @brief process set_rotation_z command
163
+   * @param[in] strm stream to read command arguments from
164
+   * @return if processing command was successful
165
+   */
166
+  bool procCmd_set_rotation_z(std::istream &strm);
167
+
161 168
   /**
162 169
    * @brief process set_tool_diameter command
163 170
    * @param[in] strm stream to read command arguments from
... ...
@@ -3,6 +3,7 @@
3 3
  * Copyleft: CC-BY-SA http://creativecommons.org/licenses/by-sa/3.0/
4 4
  */
5 5
 
6
+#include <math.h>
6 7
 #include <vector>
7 8
 
8 9
 #include "gcode.h"
... ...
@@ -93,6 +94,7 @@ void Path::removeEqPoints(double eqDist)
93 94
  */
94 95
 void Path::toGCode(const Settings &settings, double z, GCode &gcode) const
95 96
 {
97
+  double rot =  settings.rotation_z * M_PI / 180.0;
96 98
   Point offset(settings.offset_x, settings.offset_y);
97 99
 
98 100
   // leave if no points
... ...
@@ -103,7 +105,7 @@ void Path::toGCode(const Settings &settings, double z, GCode &gcode) const
103 105
   gcode.appendUp(settings.move_z);
104 106
 
105 107
   // move to start
106
-  gcode.appendFast(mPoints.front() + offset);
108
+  gcode.appendFast(mPoints.front().rotate(rot) + offset);
107 109
 
108 110
   // dwell
109 111
   if (settings.dwell_time > 0.0)
... ...
@@ -117,7 +119,7 @@ void Path::toGCode(const Settings &settings, double z, GCode &gcode) const
117 119
   gcode.appendFeed(settings.feed_mill);
118 120
   Points::const_iterator pt = mPoints.begin();
119 121
   for (++pt; pt != mPoints.end(); ++pt)
120
-    gcode.appendLinear(*pt + offset);
122
+    gcode.appendLinear(pt->rotate(rot) + offset);
121 123
 
122 124
   // move up
123 125
   gcode.appendUp(settings.move_z);
... ...
@@ -92,3 +92,17 @@ bool Point::equals(const Point &that, double eqDist) const
92 92
   return (*this - that).abs_sq() <= eqDist * eqDist;
93 93
 }
94 94
 
95
+/**
96
+ * @brief rotate around origin
97
+ * @param[in] rad angle in radians
98
+ * @return rotated point
99
+ */
100
+Point Point::rotate(double rad) const
101
+{
102
+  double c = cos(rad);
103
+  double s = sin(rad);
104
+  double rx = mX * c - mY * s;
105
+  double ry = mY * c + mX * s;
106
+  return Point(rx, ry);
107
+}
108
+
... ...
@@ -30,6 +30,13 @@ public:
30 30
    */
31 31
   bool equals(const Point &that, double eqDist) const;
32 32
 
33
+  /**
34
+   * @brief rotate around origin
35
+   * @param[in] rad angle in radians
36
+   * @return rotated point
37
+   */
38
+  Point rotate(double rad) const;
39
+
33 40
   double mX; ///< x coordinate
34 41
   double mY; ///< y coordinate
35 42
 };
... ...
@@ -18,6 +18,7 @@ Settings::Settings():
18 18
   offset_x(0.0),
19 19
   offset_y(0.0),
20 20
   precision(0.001),
21
+  rotation_z(00.0),
21 22
   tool_diameter(1.0)
22 23
 {
23 24
 }
... ...
@@ -29,6 +29,7 @@ public:
29 29
   double    offset_x;      ///< offset to add to X coordinate
30 30
   double    offset_y;      ///< offset to add to Y coordinate
31 31
   double    precision;     ///< precision, >= 1.0e-8 , <= 1.0
32
+  double    rotation_z;    ///< rotation around Z axis in degrees
32 33
   double    tool_diameter; ///< diameter of cutting tool, >= 0.0
33 34
 };
34 35
 
35 36