Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a component for Linuxcnc HAL
- // Copyright 2020 Sam Sokolik <[email protected]>
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 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 General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- component polygon "Create a spindle synced motion that carves a polygon using eoffset inputs. Assumes positive rotation.";
- pin in bit enable;
- pin in bit enablexslave "enables connecting x postion to radius";
- pin in bit enablescan "place at 0,0 in the current coordinate system then enable scan";
- pin in bit startscan "move to the edge of shape then start scan and then start motion";
- pin in bit enablescanrun "enable this befor enable and enableslave so comp knows to use scanned data";
- pin in bit lathemode "switches to lathe mode - material spining";
- pin out bit iscaptured "used to only select the center of scanned feature once";
- pin out float scancenterx "keeps center of shape to be scanned";
- pin out float scancenteryz "keeps center of shape to be scanned";
- pin in float xpos "connect to the x axis potion in hal";
- pin in float ypos "connect to the y axis positin in hal";
- pin in float zpos "connect to the z axis positin in hal";
- pin out float xposaten "when componant is enabled - grab current x postion";
- pin out float zposaten "when componant is enabled - grab the current z postion";
- pin in float zrotratio "what to multiply and add to the z postion for rotation degrees/distance";
- pin in unsigned numsides=3 "Number of sides in the polygon";
- pin in float inscribedradius=1 "inscribed radius of the polygon";
- pin in float dsize "distance from flat to edge of radius";
- pin in float cutterdiam "cutter diameter";
- pin in float eoffsetscale "scale that eoffset is set to.";
- pin in float toolang "angle difference between index and actual tool poition";
- pin in float polyang "angle of the polygon";
- pin in float spindlepos "Spindle position scaled to 1 per rev";
- pin in float cornerrad "Radius of the corner for the polygon";
- pin out float spindlerad "Spindle postion in radians";
- pin out float polyradius "radious of the polygon at given spindle position";
- pin out float tangentang "+/- angle from polygon point for radious";
- pin out float knowndist "testing just to make sure it was calculated right";
- pin out float testingstuff "testing some math..";
- pin out float xoffset "x offset to send to offset componant";
- pin out float yoffset "y offset to send to the offset componant";
- pin out signed xeoffset "x eoffset output";
- pin out signed yeoffset "y eoffset output";
- pin out bit isindex "set index enable only once";
- pin io bit indexenable "hooked to index enable of encoder";
- variable float shape[100000];
- function _;
- license "GPL";
- ;;
- #include <rtapi_math.h>
- #define PI 3.14159265358979323846
- float spinang;
- float scale;
- float scaletri;
- float spinangoffset;
- float polyangoffset;
- float radiusslave=0;
- float dang=0;
- float secdist=0;
- float thirddist=0;
- float forthdist=0;
- float fifthdist=0;
- float sixthdist=0;
- float seventhdist=0;
- float scanyz=0;
- float scanrad=0;
- float scanang=0;
- long saveval;
- // Find center of the scanned shape
- if (enablescan && !iscaptured){
- scancenterx=xpos;
- if (lathemode){
- scancenteryz=zpos;
- }else{
- scancenteryz=ypos;
- }
- iscaptured = true;
- }
- if (!enablescan){
- iscaptured = false;
- }
- // Scan shape into the array.
- if (startscan){
- if(lathemode){
- scanyz=zpos;
- }else{
- scanyz=ypos;
- }
- if ((scanyz-scancenteryz) >= 0 && (xpos-scancenterx) >= 0){
- scanang=atan((scanyz-scancenteryz)/(xpos-scancenterx));
- }
- if (scanyz-scancenteryz > 0 && xpos-scancenterx < 0){
- scanang=(PI/2-fabs(atan((scanyz-scancenteryz)/(xpos-scancenterx))))+PI/2;
- }
- if (scanyz-scancenteryz <= 0 && xpos-scancenterx <= 0){
- scanang=atan((scanyz-scancenteryz)/(xpos-scancenterx)) + PI;
- }
- if (scanyz-scancenteryz < 0 && xpos-scancenterx > 0){
- scanang=(PI/2-fabs(atan((scanyz-scancenteryz)/(xpos-scancenterx))))+PI+PI/2;
- }
- saveval = (50000/(2*PI)*scanang);
- //testing polyradius = saveval;
- // spindlerad = scanang;
- shape[saveval] = sqrt((xpos-scancenterx)*(xpos-scancenterx) + (scanyz-scancenteryz)*(scanyz-scancenteryz));
- }
- if (!enable) {
- xoffset=0;
- yoffset=0;
- xeoffset=0;
- yeoffset=0;
- isindex = false;
- xposaten = 0;
- zposaten = 0;
- return;
- }
- // only set index enable once
- if (enable && !isindex) {
- isindex = true;
- indexenable = true;
- zposaten=zpos;
- if (enablexslave){
- xposaten=xpos;
- }
- return;
- }
- // wait for spindle index before actually enabling
- if (enable && isindex && indexenable){
- return;
- }
- //convert spinangoffset to ratio.. for adding to spindle pos - plus a couple rotations
- //so the spindle position isn't negative
- spinangoffset = (toolang/360-(polyang+(zpos-zposaten)*zrotratio)/360)+2;
- polyangoffset = ((polyang+(zpos-zposaten)*zrotratio) /360)*2*PI;
- //formual I found is for circumscribed - convert to inscribed - makes more sense.
- spinang = ((spindlepos + spinangoffset) - (int)(spindlepos + spinangoffset))*2*PI;
- //calculate radius based on if x moves
- if (enablexslave){
- radiusslave=xpos-xposaten;
- }
- if(enable&&enablescan){
- scale=(inscribedradius-radiusslave);
- }else{
- scale=(inscribedradius-radiusslave)/cos(PI/numsides);
- }
- scaletri=((dsize-inscribedradius)-radiusslave)/cos(PI/3);
- if (dsize > 0){
- dang=acos((dsize-inscribedradius)/inscribedradius);
- }
- if (cornerrad >0){
- secdist=sin(PI/numsides)*cornerrad;
- thirddist=secdist/tan((((numsides-2)*(PI))/numsides)/2);
- tangentang=atan(secdist/(scale-thirddist));
- forthdist=cos(PI/numsides)*cornerrad;
- knowndist=scale-(thirddist+forthdist);
- }
- if (spinang > (PI - dang) && spinang < (PI + dang)){
- polyradius = ((cos(PI/3)/cos((fmod(spinang, (2*PI/3))-PI/3)))*scaletri-cutterdiam/2);
- }else if ((fmod(spinang, (2*PI/numsides)) < tangentang || 2*PI/numsides - fmod(spinang, (2*PI/numsides)) < tangentang) && cornerrad>0 ){
- if(fmod(spinang, (2*PI/numsides)) < tangentang){
- fifthdist=cos(fmod(spinang, (2*PI/numsides)))*knowndist;
- seventhdist=sin(fmod(spinang, (2*PI/numsides)))*knowndist;
- sixthdist=sqrt(cornerrad*cornerrad-seventhdist*seventhdist);
- polyradius=(fifthdist+sixthdist)-cutterdiam/2;
- }else{
- fifthdist=cos(2*PI/numsides -fmod(spinang, (2*PI/numsides)))*knowndist;
- seventhdist=sin(2*PI/numsides -fmod(spinang, (2*PI/numsides)))*knowndist;
- sixthdist=sqrt(cornerrad*cornerrad-seventhdist*seventhdist);
- polyradius=(fifthdist+sixthdist)-cutterdiam/2;
- }
- }else{
- if(enable&&enablescan){
- saveval = (50000/(2*PI)*spinang);
- polyradius = shape[saveval] *scale-cutterdiam/2;
- }else{
- polyradius = ((cos(PI/numsides)/cos((fmod(spinang, (2*PI/numsides))-PI/numsides)))*scale-cutterdiam/2);
- }
- }
- //actual offsets applied - could be used for offset componant.
- if (lathemode) {
- xoffset = polyradius * -1-(radiusslave);
- yoffset = 0;
- xeoffset = xoffset / eoffsetscale;
- yeoffset = 0;
- }else{
- xoffset = cos(spinang+polyangoffset)*polyradius * -1-(radiusslave);
- yoffset = sin(spinang+polyangoffset)*polyradius *1;
- //counts for use with eoffset functionality
- xeoffset = xoffset / eoffsetscale;
- yeoffset = yoffset / eoffsetscale;
- }
- spindlerad = spinang;
Advertisement
Add Comment
Please, Sign In to add comment