configuration of precision
Stefan Schuermans

Stefan Schuermans commited on 2013-01-26 18:55:56
Showing 7 changed files, with 53 additions and 6 deletions.

... ...
@@ -5,6 +5,7 @@ cmd G17
5 5
 cmd G40
6 6
 cmd G49
7 7
 
8
+set_precision 0.01
8 9
 set_tool_diameter 3
9 10
 set_move_z 3
10 11
 set_base_z 0
... ...
@@ -5,6 +5,7 @@
5 5
 
6 6
 #include <fstream>
7 7
 #include <iostream>
8
+#include <math.h>
8 9
 #include <sstream>
9 10
 #include <string>
10 11
 
... ...
@@ -220,6 +221,30 @@ bool CmdParser::procCmd_set_move_z(std::istream &strm)
220 221
   return true;
221 222
 }
222 223
 
224
+/**
225
+ * @brief process set_precision command
226
+ * @param[in] strm stream to read command arguments from
227
+ * @return if processing command was successful
228
+ */
229
+bool CmdParser::procCmd_set_precision(std::istream &strm)
230
+{
231
+  // get arguments and check them
232
+  double precision;
233
+  strm >> precision;
234
+  if (strm.fail()) {
235
+    std::cerr << "missing precision" << std::endl;
236
+    return false;
237
+  }
238
+  if (precision < 1.0e-8 || precision > 1.0) {
239
+    std::cerr << "invalid precision (" << precision << ")" << std::endl;
240
+    return false;
241
+  }
242
+
243
+  // update settings
244
+  mSettings.precision = precision;
245
+  return true;
246
+}
247
+
223 248
 /**
224 249
  * @brief process set_tool_diameter command
225 250
  * @param[in] strm stream to read command arguments from
... ...
@@ -262,8 +287,11 @@ bool CmdParser::procCmd_write_ngc(std::istream &strm)
262 287
   // re-base NGC file name
263 288
   fileName = filename_rebase(fileName, mBaseDir);
264 289
 
290
+  // calculate number of digits after decimal point
291
+  unsigned int digits = (unsigned int)ceil(-log10(mSettings.precision));
292
+
265 293
   // write NGC file
266
-  if (!mGCode.toFile(fileName)) {
294
+  if (!mGCode.toFile(fileName, digits)) {
267 295
     std::cerr << "could not write NGC file \"" << fileName << "\""
268 296
               << std::endl;
269 297
     return false;
... ...
@@ -306,6 +334,8 @@ bool CmdParser::procLine(const std::string &strLine)
306 334
     return procCmd_set_feed_mill(strm);
307 335
   else if (cmd == "set_move_z")
308 336
     return procCmd_set_move_z(strm);
337
+  else if (cmd == "set_precision")
338
+    return procCmd_set_precision(strm);
309 339
   else if (cmd == "set_tool_diameter")
310 340
     return procCmd_set_tool_diameter(strm);
311 341
   else if (cmd == "write_ngc")
... ...
@@ -79,6 +79,13 @@ public:
79 79
    */
80 80
   bool procCmd_set_move_z(std::istream &strm);
81 81
 
82
+  /**
83
+   * @brief process set_precision command
84
+   * @param[in] strm stream to read command arguments from
85
+   * @return if processing command was successful
86
+   */
87
+  bool procCmd_set_precision(std::istream &strm);
88
+
82 89
   /**
83 90
    * @brief process set_tool_diameter command
84 91
    * @param[in] strm stream to read command arguments from
... ...
@@ -100,9 +100,13 @@ void GCode::appendCustom(const std::string &cmd)
100 100
 /**
101 101
  * @brief output G-code to stream
102 102
  * @param[in] strm stream to write G-code to
103
+ * @param[in] digits number of digits to output after decimal point
103 104
  */
104
-void GCode::toStrm(std::ostream &strm) const
105
+void GCode::toStrm(std::ostream &strm, unsigned int digits) const
105 106
 {
107
+  strm.precision(digits);
108
+  strm << std::fixed;
109
+
106 110
   GCmds::const_iterator gcmd;
107 111
   for (gcmd = mGCmds.begin(); gcmd != mGCmds.end(); ++gcmd)
108 112
     (*gcmd)->toStrm(strm);
... ...
@@ -111,9 +115,10 @@ void GCode::toStrm(std::ostream &strm) const
111 115
 /**
112 116
  * @brief output G-code to file
113 117
  * @param[in] strFileName name of file to write G-code to
118
+   * @param[in] digits number of digits to output after decimal point
114 119
  * @return if writing G-code was successful
115 120
  */
116
-bool GCode::toFile(const std::string &strFileName) const
121
+bool GCode::toFile(const std::string &strFileName, unsigned int digits) const
117 122
 {
118 123
   std::ofstream ofstrm(strFileName.c_str(), std::ios::out);
119 124
   if (!ofstrm.is_open()) {
... ...
@@ -122,7 +127,7 @@ bool GCode::toFile(const std::string &strFileName) const
122 127
     return false;
123 128
   }
124 129
 
125
-  toStrm(ofstrm);
130
+  toStrm(ofstrm, digits);
126 131
   return true;
127 132
 }
128 133
 
... ...
@@ -72,15 +72,17 @@ public:
72 72
   /**
73 73
    * @brief output G-code to stream
74 74
    * @param[in] strm stream to write G-code to
75
+   * @param[in] digits number of digits to output after decimal point
75 76
    */
76
-  void toStrm(std::ostream &strm) const;
77
+  void toStrm(std::ostream &strm, unsigned int digits) const;
77 78
 
78 79
   /**
79 80
    * @brief output G-code to file
80 81
    * @param[in] strFileName name of file to write G-code to
82
+   * @param[in] digits number of digits to output after decimal point
81 83
    * @return if writing G-code was successful
82 84
    */
83
-  bool toFile(const std::string &strFileName) const;
85
+  bool toFile(const std::string &strFileName, unsigned int digits) const;
84 86
 
85 87
   /// G-code commands
86 88
   GCmds mGCmds;
... ...
@@ -13,6 +13,7 @@ Settings::Settings():
13 13
   feed_drill(10.0),
14 14
   feed_mill(10.0),
15 15
   move_z(10.0),
16
+  precision(0.001),
16 17
   tool_diameter(0.0)
17 18
 {
18 19
 }
... ...
@@ -18,6 +18,7 @@ public:
18 18
   double feed_drill;    ///< feed rate for drilling
19 19
   double feed_mill;     ///< feed rate for milling
20 20
   double move_z;        ///< z coordinate for moving
21
+  double precision;     ///< precision, >= 1.0e-8 , <= 1.0
21 22
   double tool_diameter; ///< diameter of cutting tool, >= 0.0
22 23
 };
23 24
 
24 25