Skip to content

Commit d2f915a

Browse files
QingZhangBambulanewei120
authored andcommitted
ENH: gcodechecker updare
check nozzle temp e/flow_ratio before calculate the width Signed-off-by: qing.zhang <[email protected]> Change-Id: I5c95522f061671d4bf503258ef3cd6be714db631 (cherry picked from commit c15d6efdd36db1773ad762e6a09cf21e1a3d9c8b)
1 parent 12206e5 commit d2f915a

File tree

2 files changed

+137
-4
lines changed

2 files changed

+137
-4
lines changed

bbs_test_tools/bbs_gcode_checker/GCodeChecker.cpp

+96-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#include <fstream>
33
#include <math.h>
44
#include <map>
5-
5+
#include <string.h>
66
namespace BambuStudio {
77

88
//BBS: only check wodth when dE is longer than this value
99
const double CHECK_WIDTH_E_THRESHOLD = 0.0025;
10-
const double WIDTH_THRESHOLD = 0.03;
10+
const double WIDTH_THRESHOLD = 0.02;
1111
const double RADIUS_THRESHOLD = 0.005;
1212

1313
const double filament_diameter = 1.75;
@@ -19,6 +19,11 @@ const std::string Wipe_Start_Tag = " WIPE_START";
1919
const std::string Wipe_End_Tag = " WIPE_END";
2020
const std::string Layer_Change_Tag = " CHANGE_LAYER";
2121
const std::string Height_Tag = " LAYER_HEIGHT: ";
22+
const std::string filament_flow_ratio_tag = " filament_flow_ratio";
23+
const std::string nozzle_temperature_Tag = " nozzle_temperature =";
24+
const std::string nozzle_temperature_initial_layer_Tag = " nozzle_temperature_initial_layer";
25+
const std::string Z_HEIGHT_TAG = " Z_HEIGHT: ";
26+
const std::string Initial_Layer_Ptint_Height_Tag = " initial_layer_print_height =";
2227

2328
GCodeCheckResult GCodeChecker::parse_file(const std::string& path)
2429
{
@@ -105,6 +110,19 @@ GCodeCheckResult GCodeChecker::parse_comment(GCodeLine& line)
105110
// extrusion role tag
106111
if (starts_with(comment, Extrusion_Role_Tag)) {
107112
m_role = string_to_role(comment.substr(Extrusion_Role_Tag.length()));
113+
if (m_role == erExternalPerimeter) {
114+
115+
if (z_height == initial_layer_height && nozzle_temp != nozzle_temperature_initial_layer[filament_id]) {
116+
std::cout << "invalid filament nozzle initial layer temperature comment with invalid value!" << std::endl;
117+
return GCodeCheckResult::ParseFailed;
118+
}
119+
120+
if (z_height != initial_layer_height && nozzle_temp != nozzle_temperature[filament_id]) {
121+
std::cout << "invalid filament nozzle temperature comment with invalid value!" << std::endl;
122+
return GCodeCheckResult::ParseFailed;
123+
}
124+
}
125+
108126
} else if (starts_with(comment, Wipe_Start_Tag)) {
109127
m_wiping = true;
110128
} else if (starts_with(comment, Wipe_End_Tag)) {
@@ -123,7 +141,41 @@ GCodeCheckResult GCodeChecker::parse_comment(GCodeLine& line)
123141
}
124142
} else if (starts_with(comment, Layer_Change_Tag)) {
125143
m_layer_num++;
144+
} else if (starts_with(comment, filament_flow_ratio_tag))
145+
{
146+
std::string str = comment.substr(filament_flow_ratio_tag.size()+3);
147+
if (!parse_double_from_str(str, filament_flow_ratio))
148+
{
149+
std::cout << "invalid filament flow ratio comment with invalid value!" << std::endl;
150+
return GCodeCheckResult::ParseFailed;
151+
}
152+
}
153+
else if (starts_with(comment, nozzle_temperature_Tag)) {
154+
std::string str = comment.substr(nozzle_temperature_Tag.size() + 1);
155+
if (!parse_double_from_str(str, nozzle_temperature)) {
156+
std::cout << "invalid nozzle temperature comment with invalid value!" << std::endl;
157+
return GCodeCheckResult::ParseFailed;
158+
}
126159
}
160+
else if (starts_with(comment, nozzle_temperature_initial_layer_Tag)) {
161+
std::string str = comment.substr(nozzle_temperature_initial_layer_Tag.size() + 3);
162+
if (!parse_double_from_str(str, nozzle_temperature_initial_layer)) {
163+
std::cout << "invalid nozzle temperature initial layer comment with invalid value!" << std::endl;
164+
return GCodeCheckResult::ParseFailed;
165+
}
166+
} else if (starts_with(comment, Z_HEIGHT_TAG)) {
167+
std::string str = comment.substr(Z_HEIGHT_TAG.size());
168+
if (!parse_double_from_str(str, z_height)) {
169+
std::cout << "invalid z height comment with invalid value!" << std::endl;
170+
return GCodeCheckResult::ParseFailed;
171+
}
172+
} else if (starts_with(comment, Initial_Layer_Ptint_Height_Tag)) {
173+
std::string str = comment.substr(Initial_Layer_Ptint_Height_Tag.size());
174+
if (!parse_double_from_str(str, initial_layer_height)) {
175+
std::cout << "invalid initial layer height comment with invalid value!" << std::endl;
176+
return GCodeCheckResult::ParseFailed;
177+
}
178+
}
127179

128180
return GCodeCheckResult::Success;
129181
}
@@ -153,11 +205,32 @@ GCodeCheckResult GCodeChecker::parse_command(GCodeLine& gcode_line)
153205
{
154206
case 82: { ret = parse_M82(gcode_line); break; } // Set to Absolute extrusion
155207
case 83: { ret = parse_M83(gcode_line); break; } // Set to Relative extrusion
208+
case 104: {
209+
ret = parse_M104_M109(gcode_line);
210+
break;
211+
} // Set to nozzle temperature
212+
case 109: {
213+
ret = parse_M104_M109(gcode_line);
214+
break;
215+
} // Set to nozzle temperature
156216
default: { break; }
157217
}
158218
break;
159219
}
160220
case 'T':{
221+
222+
int pt = ::atoi(&cmd[1]);
223+
if (pt == 1000 || pt == 1100 || pt == 255) {
224+
break;
225+
}
226+
227+
if (pt < 0 || pt > 254 || pt >= filament_flow_ratio.size()) {
228+
std::cout << "Invalid T command"<<std::endl;
229+
ret = GCodeCheckResult::ParseFailed;
230+
break;
231+
}
232+
filament_id = pt;
233+
flow_ratio = filament_flow_ratio[pt];
161234
break;
162235
}
163236
default: {
@@ -358,11 +431,30 @@ GCodeCheckResult GCodeChecker::parse_M83(const GCodeLine& gcode_line)
358431
return GCodeCheckResult::Success;
359432
}
360433

434+
GCodeCheckResult GCodeChecker::parse_M104_M109(const GCodeLine &gcode_line)
435+
{
436+
const char *c = gcode_line.m_raw.c_str();
437+
const char *rs = strchr(c,'S');
438+
439+
std::string str=rs;
440+
str = str.substr(1);
441+
for (int i = 0; i < str.size(); i++) {
442+
if (str[i] == ' ')
443+
str=str.substr(0,i);
444+
}
445+
if (!parse_double_from_str(str, nozzle_temp)) {
446+
std::cout << "invalid nozzle temperature comment with invalid value!" << std::endl;
447+
return GCodeCheckResult::ParseFailed;
448+
}
449+
450+
return GCodeCheckResult::Success;
451+
}
452+
361453
double GCodeChecker::calculate_G1_width(const std::array<double, 3>& source,
362454
const std::array<double, 3>& target,
363455
double e, double height, bool is_bridge) const
364456
{
365-
double volume = e * Pi * (filament_diameter/2.0f) * (filament_diameter/2.0f);
457+
double volume = (e / flow_ratio) * Pi * (filament_diameter / 2.0f) * (filament_diameter / 2.0f);
366458
std::array<double, 3> delta = { target[0] - source[0],
367459
target[1] - source[1],
368460
target[2] - source[2] };
@@ -389,7 +481,7 @@ double GCodeChecker::calculate_G2_G3_width(const std::array<double, 2>& source,
389481
(radian < 0 ? -radian : 2 * Pi - radian);
390482
double radius = sqrt(v1[0] * v1[0] + v1[1] * v1[1]);
391483
double length = radius * radian;
392-
double volume = e * Pi * (filament_diameter/2) * (filament_diameter/2);
484+
double volume = (e / flow_ratio) * Pi * (filament_diameter / 2) * (filament_diameter / 2);
393485
double mm3_per_mm = volume / length;
394486

395487
return is_bridge? 2 * sqrt(mm3_per_mm/Pi) :

bbs_test_tools/bbs_gcode_checker/GCodeChecker.h

+41
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class GCodeChecker {
108108
GCodeCheckResult parse_G92(GCodeLine& gcode_line);
109109
GCodeCheckResult parse_M82(const GCodeLine& gcode_line);
110110
GCodeCheckResult parse_M83(const GCodeLine& gcode_line);
111+
GCodeCheckResult parse_M104_M109(const GCodeLine &gcode_line);
111112

112113
GCodeCheckResult parse_comment(GCodeLine& gcode_line);
113114

@@ -160,6 +161,38 @@ class GCodeChecker {
160161
}
161162
}
162163

164+
static bool parse_double_from_str(const std::string &input, std::vector<double> &out)
165+
{
166+
167+
std::string cmd=input;
168+
size_t read = 0;
169+
170+
while (cmd.size() >= 5)
171+
{
172+
int pt = 0;
173+
for (pt = 0; pt < cmd.size(); pt++) {
174+
char temp = cmd[pt];
175+
if (temp == ',')
176+
{
177+
try {
178+
double num = std::stod(cmd.substr(0, pt), &read);
179+
180+
out.push_back(num);
181+
cmd = cmd.substr(pt+1);
182+
break;
183+
} catch (...) {
184+
return false;
185+
}
186+
}
187+
}
188+
}
189+
190+
double num = std::stod(cmd, &read);
191+
out.push_back(num);
192+
193+
return true;
194+
}
195+
163196
private:
164197
EPositioningType m_global_positioning_type = EPositioningType::Absolute;
165198
EPositioningType m_e_local_positioning_type = EPositioningType::Absolute;
@@ -174,6 +207,14 @@ class GCodeChecker {
174207
int m_layer_num = 0;
175208
double m_height = 0.0;
176209
double m_width = 0.0;
210+
double z_height=0.0f;
211+
double initial_layer_height=0.0f;
212+
int filament_id;
213+
double flow_ratio = 0;
214+
double nozzle_temp = 0.0f;
215+
std::vector<double> filament_flow_ratio;
216+
std::vector<double> nozzle_temperature;
217+
std::vector<double> nozzle_temperature_initial_layer;
177218
};
178219

179220
}

0 commit comments

Comments
 (0)