Welcome to mirror list, hosted at ThFree Co, Russian Federation.

arc_interpolation.cpp « ArcWelderInverseProcessor - github.com/FormerLurker/ArcWelderLib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: feb045bb49d2fc29fc03f36b3aafbf7ec543bfcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Arc Welder: Inverse Processor (firmware simulator).  
// Please see the copyright notices in the function definitions
//
// Converts G2/G3(arc) commands back to G0/G1 commands.  Intended to test firmware changes to improve arc support.
// This reduces file size and the number of gcodes per second.
// 
// Based on arc interpolation implementations from:
//    Marlin 1.x (see https://github.com/MarlinFirmware/Marlin/blob/1.0.x/LICENSE for the current license)
//    Marlin 2.x (see https://github.com/MarlinFirmware/Marlin/blob/2.0.x/LICENSE for the current license)
//    Prusa-Firmware (see https://github.com/prusa3d/Prusa-Firmware/blob/MK3/LICENSE for the current license)
//    Smoothieware (see https://github.com/Smoothieware/Smoothieware for the current license)
//    Repetier (see https://github.com/repetier/Repetier-Firmware for the current license)
// 
// Built using the 'Arc Welder: Anti Stutter' library
//
// Copyright(C) 2021 - Brad Hochgesang
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This program is free software : you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU Affero General Public License for more details.
//
//
// You can contact the author at the following email address: 
// FormerLurker@pm.me
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "arc_interpolation.h"
#include <string>
#include "gcode_position.h"
#include "marlin_1.h"
#include "marlin_2.h"
#include "repetier.h"
#include "prusa.h"
#include "smoothieware.h"
#include "utilities.h"


gcode_position_args arc_interpolation::get_args_(bool g90_g91_influences_extruder, int buffer_size)
{
  gcode_position_args args;
  // Configure gcode_position_args
  args.g90_influences_extruder = g90_g91_influences_extruder;
  args.position_buffer_size = buffer_size;
  args.autodetect_position = true;
  args.home_x = 0;
  args.home_x_none = true;
  args.home_y = 0;
  args.home_y_none = true;
  args.home_z = 0;
  args.home_z_none = true;
  args.shared_extruder = true;
  args.zero_based_extruder = true;


  args.default_extruder = 0;
  args.xyz_axis_default_mode = "absolute";
  args.e_axis_default_mode = "absolute";
  args.units_default = "millimeters";
  args.location_detection_commands = std::vector<std::string>();
  args.is_bound_ = false;
  args.is_circular_bed = false;
  args.x_min = -9999;
  args.x_max = 9999;
  args.y_min = -9999;
  args.y_max = 9999;
  args.z_min = -9999;
  args.z_max = 9999;
  return args;
}

arc_interpolation::arc_interpolation()
{
  p_current_firmware_ = NULL;
}

arc_interpolation::arc_interpolation(arc_interpolation_args args) 
{
  args_ = args;
  switch (args.firmware_args.firmware_type)
  {
    case firmware_types::MARLIN_1:
      p_current_firmware_ = new marlin_1(args.firmware_args);
      break;
    case firmware_types::MARLIN_2:
      p_current_firmware_ = new marlin_2(args.firmware_args);
      break;
    case firmware_types::REPETIER:
      p_current_firmware_ = new repetier(args.firmware_args);
      break;
    case firmware_types::PRUSA:
      p_current_firmware_ = new prusa(args.firmware_args);
      break;
    case firmware_types::SMOOTHIEWARE:
      p_current_firmware_ = new smoothieware(args.firmware_args);
  }
  // Initialize the source position
  p_source_position_ = new gcode_position(get_args_(p_current_firmware_->get_g90_g91_influences_extruder(), DEFAULT_GCODE_BUFFER_SIZE));
}

arc_interpolation::~arc_interpolation()
{
  delete p_source_position_;
  if (p_current_firmware_ != NULL)
  {
    delete p_current_firmware_;
  }
 
}

void arc_interpolation::process()
{
  // Create a stringstream we can use for messaging.
  std::stringstream stream;

  // Create a current and target position variable for arc processing
  firmware_state state;
  firmware_position current;
  firmware_position target;
  // I, J, and R parameter values
  double i=0, j=0, r=0;
  // bool values for is_clockwise and is_relative
  bool is_clockwise = false, is_relative = false;
  // e absolute offset, in case we are outputting absolute e
  double offset_absolute_e = 0;

  int read_lines_before_clock_check = 5000;
  //std::cout << "stabilization::process_file - Processing file.\r\n";
  stream << "Decompressing gcode file.";
  stream << "Source File: " << args_.source_path << "\n";
  stream << "Target File: " << args_.target_path << "\n";
  std::cout << stream.str();
  const clock_t start_clock = clock();

  // Create the source file read stream and target write stream
  std::ifstream gcode_file;
  gcode_file.open(args_.source_path.c_str());
  output_file_.open(args_.target_path.c_str());
  std::string line;
  int lines_with_no_commands = 0;
  gcode_file.sync_with_stdio(false);
  output_file_.sync_with_stdio(false);
  gcode_parser parser;
  int gcodes_processed = 0;
  if (gcode_file.is_open())
  {
    if (output_file_.is_open())
    {
      // Add the gcode file header
        output_file_ << p_current_firmware_->get_gcode_header_comment()<<"\n";
      parsed_command cmd;
      // Communicate every second
      while (std::getline(gcode_file, line))
      {
        lines_processed_++;

        cmd.clear();
        parser.try_parse_gcode(line.c_str(), cmd);
        bool has_gcode = false;
        if (cmd.gcode.length() > 0)
        {
          has_gcode = true;
          gcodes_processed++;
        }
        else
        {
          lines_with_no_commands++;
        }

        p_source_position_->update(cmd, lines_processed_, gcodes_processed, -1);

        if (cmd.command == "G2" || cmd.command == "G3")
        {
          // increment the number of arc commands encountered
          num_arc_commands_++;
          // Get the current and previous positions
          position* p_cur_pos = p_source_position_->get_current_position_ptr();
          position* p_pre_pos = p_source_position_->get_previous_position_ptr();
          // create the current and target positions
          current.x = p_pre_pos->get_gcode_x();
          current.y = p_pre_pos->get_gcode_y();
          current.z = p_pre_pos->get_gcode_z();
          current.e = p_pre_pos->get_current_extruder().get_offset_e();
          current.f = p_pre_pos->f;
          // set the current firmware position
          p_current_firmware_->set_current_position(current);

          target.x = p_cur_pos->get_gcode_x();
          target.y = p_cur_pos->get_gcode_y();
          target.z = p_cur_pos->get_gcode_z();
          target.e = p_cur_pos->get_current_extruder().get_offset_e();
          target.f = p_cur_pos->f;

          state.is_extruder_relative = p_pre_pos->is_extruder_relative;
          state.is_relative = p_pre_pos->is_relative;
          // set the current firmware state
          p_current_firmware_->set_current_state(state);
          
          // get I, J, and R
          i = 0;
          j = 0;
          r = 0;
          for (unsigned int index = 0; index < cmd.parameters.size(); index++)
          {
            parsed_command_parameter p = cmd.parameters[index];
            if (p.name == "I")
            {
              i = p.double_value;
            }
            else if (p.name == "J")
            {
              j = p.double_value;
            }
            else if (p.name == "R")
            {
              r = p.double_value;
            }
          }

          // If r is 0, calculate the radius
          if(r==0)
          {
            r = utilities::hypot(i, j);
          }
          
          is_clockwise = cmd.command == "G2" ? 1 : 0;
          is_relative = p_cur_pos->is_extruder_relative;
          offset_absolute_e = p_pre_pos->get_current_extruder().get_offset_e();

          // run the callback and capture any created gcode commands
          std::string gcodes = p_current_firmware_->interpolate_arc(target, i, j, r, is_clockwise);
          if (gcodes.length() > 0)
          {
            // there are gcodes to write, write them!
            output_file_ << gcodes << "\n";
          }
        }
        else
        {
          // Nothing to do with the current line, just write it to disk.
          output_file_ << line << "\n";
        }

      }
      output_file_.close();
    }
    else
    {
      std::cout << "Unable to open the output file for writing.\n";
    }
    std::cout << "Closing the input file.\n";
    gcode_file.close();
  }
  else
  {
    std::cout << "Unable to open the gcode file for processing.\n";
  }

  const clock_t end_clock = clock();
  const double total_seconds = (static_cast<double>(end_clock) - static_cast<double>(start_clock)) / CLOCKS_PER_SEC;

  stream.clear();
  stream.str("");
  stream << "Completed file processing\r\n";
  stream << "\tLines Processed       : " << lines_processed_ << "\r\n";
  stream << "\tArc Commands Processed: " << num_arc_commands_ << "\r\n";
  stream << "\tArc Segments Generated: " << p_current_firmware_->get_num_arc_segments_generated() << "\r\n";
  stream << "\tTotal Seconds         : " << total_seconds << "\r\n";
  std::cout << stream.str();
}

std::string arc_interpolation::get_firmware_arguments_description(std::string separator, std::string argument_prefix, std::string replacement_string, std::string replacement_value) const
{
  return p_current_firmware_->get_arguments_description(separator, argument_prefix, replacement_string, replacement_value);
}