Advertisement
robathome

Mesh_Randomizer

Jan 14th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. % Clear the workspace
  2. clear all;
  3. close all;
  4. clc;
  5.  
  6. % Reload breakpoints
  7. ActivateBreakpoints;
  8.  
  9. % Define program variables
  10. Update_size = 30;
  11. GCode_size = 40;
  12. flags.Randomize = false;
  13.  
  14. % Update command line
  15. UpdateStr = sprintf(' -------- PROGRAM START! --------\n');
  16. UpdateStr = [UpdateStr,sprintf('Started %s\n',datestr(now,'HH:MM:SS dd-mmm-yyyy'))];
  17. fprintf(UpdateStr);
  18. RunTimer = tic;
  19.  
  20. % Define bed parameters
  21. Bed_size = [400,400];
  22. Soft_border = [10,10];
  23.  
  24. Nozzle_offset = [40,18];
  25.  
  26. Mesh_edges = [Soft_border(1),Soft_border(2);
  27. Bed_size(1)-Soft_border(1),Bed_size(2)-Soft_border(2)];
  28. Mesh_pts = [3,3];
  29.  
  30. Z_travel = 10;
  31. Z_probe = 5;
  32. Z_liftspeed = 20;
  33. Z_divespeed = 10;
  34. XY_speed = 50;
  35.  
  36. nSamples = 3;
  37. nReps = 1;
  38.  
  39. %% Create coordinates list
  40.  
  41. % Update command line
  42. UpdateStr = sprintf('Generating coordinate list...');
  43. Padding = repmat(',',1,Update_size-length(UpdateStr));
  44. fprintf(UpdateStr);
  45.  
  46. Coord_timer = tic;
  47.  
  48. try
  49.  
  50. X_list = linspace(Mesh_edges(1,1),Mesh_edges(2,1),Mesh_pts(1));
  51. Y_list = linspace(Mesh_edges(1,2),Mesh_edges(2,2),Mesh_pts(2));
  52.  
  53. Lines_total = Mesh_pts(1)*Mesh_pts(2)*nReps;
  54. Coord_list = [];
  55.  
  56. Line_ind = 1;
  57. for Rep_count = 1:nReps
  58. for X_ind = 1:Mesh_pts(1)
  59.  
  60. if ~flags.Randomize && mod(X_ind,2) == 0
  61. Y_start = Mesh_pts(2);
  62. Y_step = -1;
  63. Y_finish = 1;
  64. elseif flags.Randomize || mod(X_ind,2) ~= 0
  65. Y_start = 1;
  66. Y_step = 1;
  67. Y_finish = Mesh_pts(2);
  68. end
  69.  
  70. for Y_ind = Y_start:Y_step:Y_finish
  71.  
  72. Temp_coords = [X_list(X_ind),Y_list(Y_ind)] + Nozzle_offset;
  73.  
  74. if isempty(Coord_list)
  75. Coord_list = Temp_coords;
  76. else
  77. Coord_list = [Coord_list; Temp_coords];
  78. end
  79.  
  80. end
  81. end
  82. end
  83.  
  84. % Randomize coordinate list
  85. if flags.Randomize
  86. randInds = randperm(length(Coord_list));
  87. Coord_list = Coord_list(randInds,:);
  88. end
  89.  
  90. catch ErrorData
  91.  
  92. ErrorMsg = sprintf('\n----- CRITICAL STOP! -----\n');
  93.  
  94. ErrorMsg = ErrorData.message;
  95. ErrorFiles = split(ErrorData.stack(end).file,'\');
  96. ErrorFile = ErrorFiles(end);
  97. ErrorLine = ErrorData.stack(end).line;
  98.  
  99. ErrorOutput = sprintf('\tCritical error in %s, line %g\n\tError: %s\n\n',ErrorFile{1},ErrorLine,ErrorMsg);
  100. throw(MException('MeshMaker:FailedCoordList',ErrorOutput));
  101.  
  102. x = 1;
  103.  
  104. end
  105.  
  106. fprintf([Padding,sprintf('Done!\n')]);
  107. Coord_time = toc(Coord_timer);
  108.  
  109. %% Prepare gcode file
  110.  
  111. UpdateStr = sprintf('Preparing G-code...');
  112. Padding = repmat('.',1,Update_size-length(UpdateStr)+1);
  113. fprintf(UpdateStr);
  114.  
  115. GCode_timer = tic;
  116.  
  117. try
  118.  
  119. % Define log file name
  120. Logfile_name = 'eventlog_14-Jan-202.txt';
  121.  
  122. % Define startup text
  123. CodeStr = {sprintf(' ; ----- High-density Bed Meshing ----- \n')};
  124. CodeStr{end+1} = sprintf(' ; Authored by Rob Smith\n');
  125. CodeStr{end+1} = sprintf(' ; Prepared on %s\n',datestr(now,'HH:MM:SS dd-mmm-yyyy'));
  126. CodeStr{end+1} = sprintf(' ; \n');
  127. CodeStr{end+1} = sprintf(' ; Mesh parameters:\n');
  128. CodeStr{end+1} = sprintf(' ; Bed dimensions:\t%gmm x %gmm\n',Bed_size(1),Bed_size(2));
  129.  
  130. X_spacing = Bed_size(1)/Mesh_pts(1);
  131. Y_spacing = Bed_size(2)/Mesh_pts(2);
  132.  
  133. CodeStr{end+1} = sprintf(' ; Site spacing:\t\t%3.2fmm\n',(Bed_size(1)-2*Soft_border(1))/(Mesh_pts(1)+1));
  134. CodeStr{end+1} = sprintf(' ; Probe sites:\t\t%g\n',Mesh_pts(1)*Mesh_pts(2));
  135. CodeStr{end+1} = sprintf(' ; Sample size:\t\t%g\n',nSamples);
  136. CodeStr{end+1} = sprintf(' ; Repetitions:\t\t%g\n',nReps);
  137. CodeStr{end+1} = sprintf(' ; Datapoints:\t\t%g\n',Mesh_pts(1)*Mesh_pts(2)*nSamples*nReps);
  138. CodeStr{end+1} = sprintf(' ; \n');
  139.  
  140. CodeStr{end+1} = sprintf(' ; ----- PROGRAM START! -----\n\n');
  141. CodeStr{end+1} = PadGCode('G28','Home the printer',GCode_size);
  142. CodeStr{end+1} = PadGCode(sprintf('M929 P"%s" S1',Logfile_name),'Enable communication logging\n');
  143. CodeStr{end+1} = PadGCode('M503','Copy original EEPROM settings\n');
  144. CodeStr{end+1} = PadGCode(sprintf('M558 F%g H3 T%g A%g S0.001',Z_divespeed*60,XY_speed*60,nSamples),'Modify z-probe configuration\n');
  145. CodeStr{end+1} = PadGCode(sprintf('G1 Z%g F%g',Z_travel,Z_liftspeed*60),'Lift effector to start position\n\n');
  146.  
  147. i = 1;
  148. Spinner = ['|';'/';'-';'\'];
  149. Spinner_ind = 1;
  150. WipeCoords = '';
  151. while i <= length(Coord_list)
  152.  
  153. NewCoordStr = sprintf('\n%g - (%3.3f,%3.3f)',i,Coord_list(i,1),Coord_list(i,2));
  154. fprintf([WipeCoords,NewCoordStr]);
  155. WipeCoords = repmat(sprintf('\b'),1,length(NewCoordStr));
  156.  
  157. % Check for previous repetitions
  158. CheckStr = sprintf('X%3.3f Y%3.3f',Coord_list(i,1),Coord_list(i,2));
  159. RepCheck = cellfun(@(x) contains(x,CheckStr),CodeStr,'UniformOutput',false);
  160. RepCount = numel(find(cell2mat(RepCheck))) + 1;
  161.  
  162. % Generate gcode set
  163. CodeStr{end+1} = sprintf('\n ; -- Probe site %s%g, rep %s%g\n',char(35),i,char(35),RepCount);
  164. CodeStr{end+1} = PadGCode(sprintf('G1 X%3.3f Y%3.3f F%g',Coord_list(i,1),Coord_list(i,2),XY_speed*60),'Move to coordinates\n');
  165. CodeStr{end+1} = PadGCode(sprintf('G1 Z%3.3f F%g',Z_travel,Z_divespeed*60),'Dive to probe height\n');
  166. CodeStr{end+1} = PadGCode(sprintf('G30 S-1'),'Invoke probe series\n');
  167. CodeStr{end+1} = PadGCode(sprintf('G1 Z%3.3f F%g',Z_travel,Z_liftspeed*60),'Lift to travel height\n');
  168.  
  169. % Spin the spinny thing
  170. if mod(i,5) == 0
  171.  
  172. Spinner_ind = Spinner_ind + 1;
  173. if (Spinner_ind + 1) > length(Spinner)
  174. Spinner_ind = 1;
  175. end
  176.  
  177. fprintf('\b%s',Spinner(Spinner_ind));
  178.  
  179. end
  180.  
  181. % Increment to next coordinate pair
  182. i = i + 1;
  183.  
  184. end
  185.  
  186. fprintf([WipeCoords,sprintf('\b')]);
  187.  
  188. CodeStr{end+1} = sprintf('\n ; Data collection complete!\n');
  189. CodeStr{end+1} = PadGCode(sprintf('G1 X%3.3f Y%3.3f F%g',Bed_size(1)/2,Bed_size(2)/2,XY_speed*60),'Move to bed center\n');
  190. CodeStr{end+1} = PadGCode(sprintf('G1 Z30 F%g',Z_liftspeed*60),'Lift effector to end position\n');
  191. CodeStr{end+1} = PadGCode('M929 S0','Disable communication logging\n');
  192. CodeStr{end+1} = PadGCode('M84','Disable stepper motors\n');
  193. CodeStr{end+1} = sprintf('\n\n ----- PROGRAM COMPLETE! -----\n\n');
  194.  
  195.  
  196. catch ErrorData
  197.  
  198. fprintf('\n----- CRITICAL STOP! -----\n');
  199.  
  200. ErrorMsg = ErrorData.message;
  201. ErrorFiles = split(ErrorData.stack(end).file,'\');
  202. ErrorFile = ErrorFiles(end);
  203. ErrorLine = ErrorData.stack(end).line;
  204.  
  205. ErrorOutput = sprintf('\tCritical error in %s, line %g\n\tError: %s\n\n',ErrorFile{1},ErrorLine,ErrorMsg);
  206. throw(MException('MeshMaker:FailedCoordList',ErrorOutput));
  207.  
  208. x = 1;
  209.  
  210. end
  211.  
  212. fprintf([Padding,sprintf('Done!\n')]);
  213. GCode_time = toc(GCode_timer);
  214.  
  215.  
  216. %% Output code to file
  217. UpdateStr = sprintf('Writing gcode to file...');
  218. Padding = repmat('.',1,Update_size-length(UpdateStr));
  219. fprintf(UpdateStr);
  220.  
  221. File_timer = tic;
  222.  
  223. try
  224.  
  225. File_name = 'MajorRandomness.gcode';
  226. File_ID = fopen(File_name,'w+');
  227. for i = 1:length(CodeStr)
  228. newLine = CodeStr{i};
  229. if iscell(newLine)
  230. newLine = newLine{1};
  231. end
  232. fprintf(File_ID,newLine);
  233. end
  234. fclose(File_ID);
  235.  
  236. catch ErrorData
  237.  
  238. ErrorMsg = fprintf('\n----- CRITICAL STOP! -----\n');
  239.  
  240. OpenFiles = fopen('all');
  241. if ~isempty(OpenFiles)
  242. UpdateStr = sprintf('<Closing file(s)...>');
  243. fprintf(UpdateStr);
  244. for i = 1:numel(OpenFiles)
  245. fclose(OpenFiles(i));
  246. end
  247. Padding = repmat(',',1,Update_size-length(UpdateStr));
  248. fprintf([Padding,sprintf('Done!\n')]);
  249. end
  250.  
  251. ErrorMsg = ErrorData.message;
  252. ErrorFiles = split(ErrorData.stack(end).file,'\');
  253. ErrorFile = ErrorFiles(end);
  254. ErrorLine = ErrorData.stack(end).line;
  255.  
  256. ErrorOutput = sprintf('\tCritical error in %s, line %g\n\tError: %s\n\n',ErrorFile{1},ErrorLine,ErrorMsg(1:end-1));
  257. throw(MException('MeshMaker:FailedCoordList',ErrorOutput));
  258.  
  259. x = 1;
  260.  
  261. end
  262.  
  263.  
  264. fprintf([Padding,sprintf('Done!\n')]);
  265.  
  266. File_time = toc(File_timer);
  267.  
  268.  
  269. %% End program
  270.  
  271. fprintf('----- PROGRAM COMPLETE! -----\n\n');
  272. File_length = 5 + (i*4);
  273. UpdateStr = sprintf('Gcode size: %g lines\n',File_length);
  274. Runtime = toc(RunTimer);
  275. Run_mins = floor(Runtime/60);
  276. Run_secs = Runtime - Run_mins*60;
  277. UpdateStr = [UpdateStr,sprintf('Generating coordinate list: %gs\n',Coord_time)];
  278. UpdateStr = [UpdateStr,sprintf('Preparing G-code: %gs\n',GCode_time)];
  279. UpdateStr = [UpdateStr,sprintf('Output to file: %gs\n\n',File_time)];
  280. UpdateStr = [UpdateStr,sprintf('Run time: %gm%gs\n\n',Run_mins,Run_secs)];
  281. fprintf(UpdateStr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement