Converting G2 / G3 to start angle / stop angle / radius
Moderator: andrew
Converting G2 / G3 to start angle / stop angle / radius
Hi everyone
Everything is almost in the subject. I need to convert G2 / G3 to something like
F26 "Start Angle" "Stop Angle" "Radius" (Clockwise)
F27 "Start Angle" "Stop Angle" "Radius" (CCW)
Any usefull tips ?
Everything is almost in the subject. I need to convert G2 / G3 to something like
F26 "Start Angle" "Stop Angle" "Radius" (Clockwise)
F27 "Start Angle" "Stop Angle" "Radius" (CCW)
Any usefull tips ?
Re: Converting G2 / G3 to start angle / stop angle / radius
Hi,
What dialect/machine is that?
'F', is that not the reserved code for Feed in Gcode?
F26 would be: Set Feed to 26units/minute.
Exporters are configurable, so arc X to your F26/27 won't be a problem.
All the specifics are ready available. (Begin /End angle, radius, reversed)
Andrew can explain you how.
On the other hand if the source is Gcode you'll need to apply some basic trig math.
R would be sqrt(i²+j²) (and as 'distance' always positive)
Gcode is consecutive and only includes the end position.
The Endangle can be found as the angle of the line from the centerpoint to the endpoint.
The centerpoint can be found by applying i and j on X and Y.
For the Beginangle you'll need the final position of the previous command.
A trig pitfall could be the 'G2 or G3 side' where the center is located and the positive radius.
Hopes this helps a little
Regards,
CVH
What dialect/machine is that?
'F', is that not the reserved code for Feed in Gcode?
F26 would be: Set Feed to 26units/minute.
Exporters are configurable, so arc X to your F26/27 won't be a problem.
All the specifics are ready available. (Begin /End angle, radius, reversed)
Andrew can explain you how.
On the other hand if the source is Gcode you'll need to apply some basic trig math.
R would be sqrt(i²+j²) (and as 'distance' always positive)
Gcode is consecutive and only includes the end position.
The Endangle can be found as the angle of the line from the centerpoint to the endpoint.
The centerpoint can be found by applying i and j on X and Y.
For the Beginangle you'll need the final position of the previous command.
A trig pitfall could be the 'G2 or G3 side' where the center is located and the positive radius.
Hopes this helps a little
Regards,
CVH
Re: Converting G2 / G3 to start angle / stop angle / radius
Hi CVF
Thanks for your reply.
The machine I'm using doesn't understand GCode. It's a very specifi command made for turning. I wrote a post about it : https://www.qcad.org/rsforum/viewtopic. ... 6f31204df6
You can take a look at the various functions here : http://www.kolly.ch/wpcontent/uploads/ ... nglish.pdf
You're almost right with F26 for a controller that understand GCode. On mine it's either F0 to F9 (no space) for preset feed values, or F xxx (with a space) wich means Feed at xxx mm/min. F26 / F27 are functions that acts like G2 / G3 with GCode except that the syntax is F26 "Start angle" "Stop angle" "Radius".
My problem is how to write the postprocessor to translate I/J/IA/JA/RADIUS/DIAMETER/SWEEP to the values that I need.
I wonder if there is existing variables for start / end angle in QCad ? Or if someone could help me with js ?
If you have any idea / tips you're welcome
Jeff
Thanks for your reply.
The machine I'm using doesn't understand GCode. It's a very specifi command made for turning. I wrote a post about it : https://www.qcad.org/rsforum/viewtopic. ... 6f31204df6
You can take a look at the various functions here : http://www.kolly.ch/wpcontent/uploads/ ... nglish.pdf
You're almost right with F26 for a controller that understand GCode. On mine it's either F0 to F9 (no space) for preset feed values, or F xxx (with a space) wich means Feed at xxx mm/min. F26 / F27 are functions that acts like G2 / G3 with GCode except that the syntax is F26 "Start angle" "Stop angle" "Radius".
My problem is how to write the postprocessor to translate I/J/IA/JA/RADIUS/DIAMETER/SWEEP to the values that I need.
I wonder if there is existing variables for start / end angle in QCad ? Or if someone could help me with js ?
If you have any idea / tips you're welcome
Jeff
Re: Converting G2 / G3 to start angle / stop angle / radius
I am not a QCAM guy.
Not quite standard Gcode.
In QCAD & javascript for an RArc entity you have:
RArc.getCenter()
Rarc.getRadius()
RArc.getStartAngle()
Rarc.getEndAngle()
RArc.isReversed ()
That should cover F26/27
From G2/3 X,Y,I,J the way is pure math and explaind.
I presume the relative version works with I IA J JA.
Strange that you mention and RADIUS, and DIAMETER and on top of that the SWEEP as parameters ...
Or is that the radius form.
Regards,
CVH
Is that what you have to work with?
Not quite standard Gcode.
In QCAD & javascript for an RArc entity you have:
RArc.getCenter()
Rarc.getRadius()
RArc.getStartAngle()
Rarc.getEndAngle()
RArc.isReversed ()
That should cover F26/27
From G2/3 X,Y,I,J the way is pure math and explaind.
I presume the relative version works with I IA J JA.
Strange that you mention and RADIUS, and DIAMETER and on top of that the SWEEP as parameters ...
Or is that the radius form.
Regards,
CVH
Last edited by CVH on Wed Jul 15, 2020 9:04 am, edited 1 time in total.
Re: Converting G2 / G3 to start angle / stop angle / radius
Pictures say more then words:
The 'relative' is a big guess.
CVH
PS Remember to catch the limits of the Atan2 function.
The 'relative' is a big guess.
CVH
PS Remember to catch the limits of the Atan2 function.
 Attachments

 Gcode2Polar.dxf
 (118.26 KiB) Downloaded 107 times
Re: Converting G2 / G3 to start angle / stop angle / radius
Thank you CVH for the drawing, it's very clear and it helps !
My main problem is not related to trigonometry and/or understanding GCode and how G2/G3 works. Despite the fact I did some programming in my young ages , my main issue is I'm not at ease with javascript...
Example, I changed the angle to degrees using this :
this.registerVariable("arcSweep", "SWEEP", true, "", "DEFAULT", { factor: 360/Math.PI/2 });
It works. but is it the best way to do it ? Should I have used RMath.rad2deg() instead ? And how ?
Other question, you mentionned Rarc.getStartAngle(). How should I / could I use it in my postprocessor ? Where coud I find resources / explanations about this specific function ?
Generally, is there some documentation on the various CLdata ressources used by QCad ? (Or BTW a good "javascript for dummies" book... )
Finaly, since my command doesn't understand GCode, I wonder if I'd rather write my postprocessor from scratch rather than starting from a copy of GCodebase. My command is not complicated. The only difficulty I'm having is with my circular interpolation functions. If someone could help me on how to write this in the postprocessor would be very helpfull.
(Either using this RArc.getStartAngle() or using the atan() function) or any other way of getting these start / end angles)
Thanks in advance !
Jeff
My main problem is not related to trigonometry and/or understanding GCode and how G2/G3 works. Despite the fact I did some programming in my young ages , my main issue is I'm not at ease with javascript...
Example, I changed the angle to degrees using this :
this.registerVariable("arcSweep", "SWEEP", true, "", "DEFAULT", { factor: 360/Math.PI/2 });
It works. but is it the best way to do it ? Should I have used RMath.rad2deg() instead ? And how ?
Other question, you mentionned Rarc.getStartAngle(). How should I / could I use it in my postprocessor ? Where coud I find resources / explanations about this specific function ?
Generally, is there some documentation on the various CLdata ressources used by QCad ? (Or BTW a good "javascript for dummies" book... )
Finaly, since my command doesn't understand GCode, I wonder if I'd rather write my postprocessor from scratch rather than starting from a copy of GCodebase. My command is not complicated. The only difficulty I'm having is with my circular interpolation functions. If someone could help me on how to write this in the postprocessor would be very helpfull.
(Either using this RArc.getStartAngle() or using the atan() function) or any other way of getting these start / end angles)
Thanks in advance !
Jeff
Re: Converting G2 / G3 to start angle / stop angle / radius
For the start angle / end angle of an arc:
In your constructor:
Override function "writeEntity":
Lots of assumptions here, but that's basically how you can add entity dependent variables.
In your constructor:
Code: Select all
// define new variables:
this.arcStartAngle = undefined;
this.registerVariable("arcStartAngle", "STARTANGLE", true, "", "DEFAULT", "DEFAULT");
this.arcEndAngle = undefined;
this.registerVariable("arcEndAngle", "ENDANGLE", true, "", "DEFAULT", "DEFAULT");
...
// use new variables for arcs:
this.arcCWMove = ["... [STARTANGLE] [ENDANGLE] ..."];
this.firstArcCWMove = this.arcCWMove;
this.arcCCWMove = ["... [STARTANGLE] [ENDANGLE] ..."];
this.firstArcCCWMove = this.arcCCWMove;
Code: Select all
MyPostProcessor.prototype.writeEntity = function() {
// initialize additional variables:
if (isArcEntity(this.currentEntity)) {
this.arcStartAngle = this.currentEntity.getStartAngle();
this.arcEndAngle = this.currentEntity.getEndAngle();
}
else {
this.arcStartAngle = undefined;
this.arcEndAngle = undefined;
}
GCodeBase.prototype.writeEntity.call(this);
};
Re: Converting G2 / G3 to start angle / stop angle / radius
Wasn't my first question:
Postprocessor or Gcode translator?
Again I am not a QCAM guy, that is for Andrew.
And he replied just now ...
You could try Qcad Classes:
https://www.qcad.org/doc/qcad/latest/de ... index.html
I can't find a source of GCodebase.js so I am not aware how this script acts ...
Searched 2 drives, the Qcad masters at github and the net.
RMath.rad2deg() is about the same as rad2deg().
The plain version is from input.js the other is the standard Math lib parsed as RMath ...
I got the advise from Andrew to not use those from input.js but you can't avoid them everywhere.
Input.js is partially in degrees as I remember.
JavaScript isn't that hard to master a bit.
What and how to do things under QCAD is more vage.
Regards,
CVH
Postprocessor or Gcode translator?
Again I am not a QCAM guy, that is for Andrew.
And he replied just now ...
No, info is scarce.
You could try Qcad Classes:
https://www.qcad.org/doc/qcad/latest/de ... index.html
I can't find a source of GCodebase.js so I am not aware how this script acts ...
Searched 2 drives, the Qcad masters at github and the net.
{ factor: 180/Math.PI } is one division less for start.
RMath.rad2deg() is about the same as rad2deg().
The plain version is from input.js the other is the standard Math lib parsed as RMath ...
I got the advise from Andrew to not use those from input.js but you can't avoid them everywhere.
Input.js is partially in degrees as I remember.
JavaScript isn't that hard to master a bit.
What and how to do things under QCAD is more vage.
Regards,
CVH
Re: Converting G2 / G3 to start angle / stop angle / radius
I did some progress but still have questions.
This is working (thank you Andrew) : this.registerVariable("arcStartAngle", "STARTANGLE", true, "", "DEFAULT", "DEFAULT");
And I can use it like this : this.firstArcCWMove = ["F26 [STARTANGLE] [ENDANGLE] [RADIUS]"];
Now I need to convert STARTANGLE and ENDANGLE in degrees. I tried this in the variable definition but it doesn't work :
this.registerVariable("arcStartAngle", "STARTANGLE", true, "", "DEFAULT", {factor: RMath.rad2deg()});
On the other hand, this is working : this.registerVariable("arcSweep", "SWEEP", true, "", "DEFAULT", { factor: 180/Math.PI });
What am I doing wrong with RMath.rad2deg() ?
Second question, how could I correct STARTANGLE and ENDANGLE for plus or minus a quadrant ? (90 degrees) ?
And last question, I'd like to define a seconf feed rate (feedRate2) that would be half of the normal feed rate for my circular interpolation.
if I do this it works :
this.feedRate2 = 40;
this.registerVariable("feedRate2", "F2", false, "F ", "DEFAULT", "DEFAULT");
this.ArcCWMove = ["F25 [F2!] F26 [STARTANGLE] [ENDANGLE] [RADIUS]"];
but then the feed rate is always 40 (fixed).
If I do this, it doesn't work :
this.feedRate2 = feedRate / 2;
This doesn^t work either :
this.feedRate2 = undefined ;
this.registerVariable("feedRate2", "F2", false, "F ", "DEFAULT", {factor: feedRate / 2});
Can someone help me ?
Thank you in advance.
JF
This is working (thank you Andrew) : this.registerVariable("arcStartAngle", "STARTANGLE", true, "", "DEFAULT", "DEFAULT");
And I can use it like this : this.firstArcCWMove = ["F26 [STARTANGLE] [ENDANGLE] [RADIUS]"];
Now I need to convert STARTANGLE and ENDANGLE in degrees. I tried this in the variable definition but it doesn't work :
this.registerVariable("arcStartAngle", "STARTANGLE", true, "", "DEFAULT", {factor: RMath.rad2deg()});
On the other hand, this is working : this.registerVariable("arcSweep", "SWEEP", true, "", "DEFAULT", { factor: 180/Math.PI });
What am I doing wrong with RMath.rad2deg() ?
Second question, how could I correct STARTANGLE and ENDANGLE for plus or minus a quadrant ? (90 degrees) ?
And last question, I'd like to define a seconf feed rate (feedRate2) that would be half of the normal feed rate for my circular interpolation.
if I do this it works :
this.feedRate2 = 40;
this.registerVariable("feedRate2", "F2", false, "F ", "DEFAULT", "DEFAULT");
this.ArcCWMove = ["F25 [F2!] F26 [STARTANGLE] [ENDANGLE] [RADIUS]"];
but then the feed rate is always 40 (fixed).
If I do this, it doesn't work :
this.feedRate2 = feedRate / 2;
This doesn^t work either :
this.feedRate2 = undefined ;
this.registerVariable("feedRate2", "F2", false, "F ", "DEFAULT", {factor: feedRate / 2});
Can someone help me ?
Thank you in advance.
JF
Last edited by Jeff66 on Wed Oct 14, 2020 10:35 am, edited 1 time in total.
Re: Converting G2 / G3 to start angle / stop angle / radius
Hi,
What I think happens is:
The parameter arcStartAngle is registered as 'STARTANGLE' so that you can use it as [STARTANGLE]
For that it has to exist first:
Presumed that's why it is declared as 'undefined' upfront.
I don't know what the last "DEFAULT" stands for but you reported that { factor: 360/Math.PI/2 } worked.
Simply RMath.rad2deg(voided) won't return much of a value as factor.
Or one provides the angle in degrees:
this.arcStartAngle = RMath.rad2deg(<the value in radians>);
Or one aplies a factor:
{ factor: 360/Math.PI/2 } or shorter { factor: 180/Math.PI }
Regards,
CVH
Code: Select all
this.registerVariable("arcStartAngle", "STARTANGLE", true, "", "DEFAULT", "DEFAULT");
The parameter arcStartAngle is registered as 'STARTANGLE' so that you can use it as [STARTANGLE]
For that it has to exist first:
Presumed that's why it is declared as 'undefined' upfront.
I don't know what the last "DEFAULT" stands for but you reported that { factor: 360/Math.PI/2 } worked.
Simply RMath.rad2deg(voided) won't return much of a value as factor.
Or one provides the angle in degrees:
this.arcStartAngle = RMath.rad2deg(<the value in radians>);
Or one aplies a factor:
{ factor: 360/Math.PI/2 } or shorter { factor: 180/Math.PI }
Regards,
CVH
Re: Converting G2 / G3 to start angle / stop angle / radius
In GCode we use G2/G3 from current position to X/Y as end position and I/J as the arc center position.
On mine it's a bit different. It starts from the current position of the tool. It assumes it is on a circle of radius R. I must specify the starting angle on this circle, and the ending angle, as well as the rotation (CW or CCW).
On QCAD 0 is at 3 o'clock, 180 at 9 o'clock, etc... Let's assume it is shifted from 90 degrees on mine and I need to correct this. Taking into account that if I do a +90 to the angle, I must also do a MOD(360) somewhere.
On mine it's a bit different. It starts from the current position of the tool. It assumes it is on a circle of radius R. I must specify the starting angle on this circle, and the ending angle, as well as the rotation (CW or CCW).
On QCAD 0 is at 3 o'clock, 180 at 9 o'clock, etc... Let's assume it is shifted from 90 degrees on mine and I need to correct this. Taking into account that if I do a +90 to the angle, I must also do a MOD(360) somewhere.
Re: Converting G2 / G3 to start angle / stop angle / radius
That is by standard in a Cartesian plane.
+90 degrees shift in a Cartesian plane is CCW, so your zero is at 6 o'clock ?!
Or is it at 12 o'clock >> 90 degrees
Depends if the target requires a standarized angle.
An orientation of 372 degrees is merely 12 degrees.
But an arc from 0 to 372 degrees is more as a full circle arc.
I don't know the fancy way like the 'factor:' method.
If you provides the angle in degrees:
Code: Select all
this.arcStartAngle = RMath.rad2deg(RMath.getNormalizedAngle(<the value in radians> +/ Math.PI/2));
CVH
Re: Converting G2 / G3 to start angle / stop angle / radius
Thank you CVH. I solved it by changing it on my prototype like this :
if (isArcEntity(this.currentEntity)) {
this.arcStartAngle = (rad2deg(this.currentEntity.getStartAngle()) + 90) % 360;
this.arcEndAngle = (rad2deg(this.currentEntity.getEndAngle()) + 90) % 360;
}
and it works fine.
My last problem is with the feed rate. I need to write it with 3 digits even when it's under 100. Like "075" for 75 mm/min (without ")
How can I add a leading 0 to the feedrate ? Or is there a way to force QCAD/CAM to write numbers on 3 digits ?
Thanks again !
Jeff
if (isArcEntity(this.currentEntity)) {
this.arcStartAngle = (rad2deg(this.currentEntity.getStartAngle()) + 90) % 360;
this.arcEndAngle = (rad2deg(this.currentEntity.getEndAngle()) + 90) % 360;
}
and it works fine.
My last problem is with the feed rate. I need to write it with 3 digits even when it's under 100. Like "075" for 75 mm/min (without ")
How can I add a leading 0 to the feedrate ? Or is there a way to force QCAD/CAM to write numbers on 3 digits ?
Thanks again !
Jeff
Re: Converting G2 / G3 to start angle / stop angle / radius
Math in degrees, ok. I would stick to radians as long as one can.
There is a thing with the ECMAScript % mod function ... and negative values.
Not certain that arc...Angle always returns a normalized angle.
Not a QCAM guy, would not know.
ECMAScript has the tendency for strings:
Code: Select all
// With CAM one should always be 110% sure that values are valid !!
// With a valid Feedrate as integer:
var textFeedrate = "000" + <Feedrate>;
textFeedrate = textFeedrate.right(3);
CVH