Unexpected ans output no idea why

2 Ansichten (letzte 30 Tage)
Andrew Smelser
Andrew Smelser am 25 Jan. 2019
Beantwortet: Star Strider am 25 Jan. 2019
I'm working on developing a code that will convert a few inputs into a .gcode file for some research I'm doing. Its working pretty well except that it spits out "ans = 52" during the run. I've check both the main file (aafcl_gcode.m) and the paged file (stepIncrement.m) and can't find any unsuppressed lines that would output this number. I've tried clearing the workspace, restarting matlab (R2018a) so I have no idea whats giong on. The math it's doing is simple... it works perfect its just annoying that it spits out that value. Anybody know what's happening?
Here's the main code and the other function (sorry for the length I'm an engineer not a programmer)
function [out] = aafcl_gcode()
clc
%===== READ THE HELP FILE =====%
fprintf("\n\nRead the help file! It will help you!\n")
fprintf("Type 'help aafcl_gcode' in the Command Window to access the help file!\n")
fprintf('Or click <a href="matlab: help aafcl_gcode">Here</a>\n')
%===== END READ THE HELP FILE =====%
%===== GUI PROMPT =====%
prompt = {'Units (mm or inch):','Max Travel:','Travel Increment:','Your Name:','Starting Position:','Movement Type:','End Position:'};
dlg_title = 'Gcode Inputs';
num_lines = 1;
defAns = {'mm','457.2','0.1','Name','0','incremental','450'};
options.Resize = 'on';
options.Interpreter = 'tex';
answer = inputdlg(prompt,dlg_title,num_lines,defAns,options);
%===== END GUI PROMPT =====%
%===== CONVERT PROMPT ANSWERS =====%
units = answer{1}; % what type of units do you want to use?
maxTravel = str2num(answer{2}); % max travel distance, mm or inch
dTravel = str2num(answer{3}); % travel increment, mm or inch
name = answer{4}; % get user's name
name = strtrim(name); % remove any trailing spaces
name = strrep(name,' ','_'); % remove spaces from the name
startPos = str2num(answer{5}); % starting position in x-direction, mm or inch from absolute 0
movType = answer{6}; % movement type; incremental = G91 command, absolute = G90 command
endPos = str2num(answer{7}); % define the stop position for the test, mm or inch
inputs = [units,maxTravel,dTravel,name,startPos,movType,endPos];
%===== END CONVERT PROMPT ANSWERS =====%
%===== DEFAULT VALUES =====%
if defAns{1} == 'mm'
unitDefault = 21;
unitPrint = 'G21';
elseif defAns{1} == 'inch'
unitDefault = 20;
unitPrint = 'G20';
else
error('Something wrong in the unit default select');
end
if defAns{6} == 'incremental'
movTypeDefault = 91; % 91 for G91 = incremental movement
movPrintDefault = 'G91'; % this is printed to the gcode
elseif defAns{6} == 'absolute'
movTypeDefault = 90; % 90 for G90 = absolute movement
movPrintDefault = 'G90'; % this is printed to the gcode
else
error('Something is broken in the movement type default select')
end
maxTravelDefault = str2num(defAns{1}); % pull the default value from default answers struct
startPosDefault = str2num(defAns{5}); % default start position answer in mm
endPosDefault = str2num(defAns{5}); % default end position in mm
inchStrings = {'in' 'inch' 'Inch' 'inches' 'Inches' 'INCHES'};
absoluteStrings = {'absolute' 'Absolute' 'ABSOLUTE'};
incrementalStrings = {'incremental' 'Incremental' 'INCREMENTAL'};
%===== END DEFAULT VALUES =====%
% ===== ERROR CHECKING =====%
if dTravel > maxTravel
error("Travel Increment cannot be larger than maximum travel");
end
if maxTravel <= 0
error("Max Travel cannot be less than or equal to 0");
end
if strcmpi(units,'mm') == 1
unitSelect = 21; % 21 for G21 = gcode for mm units
unitPrint = 'G21 ; units are in mm'; % this is printed to the gcode file
elseif strcmpi(units, inchStrings) == 1
unitSelect = 20; % 20 for G20 = gcode for inch units
unitPrint = 'G20 ; units are in inches';% this is printed to the gcode file
else
warning('Didn''t get your units, defaulting to millimeters');
unitSelect = 21; % default to mm
unitPrint = 'G21 ; units are in mm';
maxTravel = maxTravelDefault; % reset max travel to a defualt value to avoid traverse stand damage
end
if startPos < 0
warning('Starting position cannot be less than 0. Changing to default.');
startPos = startPosDefault;
end
if endPos <= startPos
warning('End Position cannot be less than or equal to the starting position. Changing to default.');
endPos = endPosDefault;
elseif endPos >= maxTravel
warning('End Position cannot be greater than or equal to max travel. Changing to default.');
endPos = endPosDefault;
end
if strcmpi(movType,incrementalStrings) == 1
movTypeSelect = 91; % 91 for G91, select incremental movement
movPrint = 'G91 ; relative movement'; % this gets printed to the gcode
elseif strcmpi(movType,absoluteStrings) == 1
movTypeSelect = 90; % 90 for G90, select absolute movement
movPrint = 'G90 ; absolute movement'; % this gets printed to the gcode
else
warning('I didn''t get your movement type. Changing to the default.');
movTypeSelect = movTypeDefault;
movPrint = movPrintDefault;
end
%===== END ERROR CHECKING =====%
%===== FILE NAME =====%
fileExtension = '.gcode'; % file type extension
path = pwd; % current directory
folderName = 'TravStand_Gcode_Files';
if ~exist(strcat(path,'/',folderName), 'dir')
mkdir(folderName,'dir') % create a new folder to put all the gcode files in
fprintf('\n!! Created a folder called %s for the gcode files\n',folderName);
end
savePath = strcat(path,'\',folderName); % save the gcode files in the new folder
date = datestr(datetime('now'),'dd_mmmm_yyyy');
time = datestr(datetime('now'),'HH_MM_SS_AM');
time = strrep(time,' ',''); % remove the space in the time string
dateTime = strcat(date,'_',time,'_',name);
fileName = strcat(dateTime,fileExtension);
%===== END FILE NAME =====%
%===== MATH =====%
yy = 'Y0 F0'; % just so I don't have to type this a million times
stepCount = 0; % initialize step counter
startPosPrint = strcat('G1 X',num2str(startPos),yy,'; move to the start position'); % define the movement to the starting position
numSteps = (endPos - startPos) / dTravel; % calculate the number of steps
if ~isinteger(numSteps)
numStepsRounded = floor(numSteps); % round down to an integer value
fprintf('Had to round down the number of steps from %.2f to %.1f\n',numSteps,numStepsRounded);
numSteps = numStepsRounded;
else
fprintf('Number of Steps: %.1f\n',numSteps);
end
%===== END MATH =====%
%===== MAKE THE GCODE =====%
saveFileName = strcat(savePath,'\',fileName); % where to save the file and what to call it when it's created
fid = fopen(saveFileName,'w'); % create the file
fprintf(fid,'; File Name: %s\n',fileName);
fprintf(fid,'; Save Location: %s\n',savePath);
fprintf(fid,'; Created by: %s\n',name);
fprintf(fid,'; Created on: %s\n',dateTime);
fprintf(fid,';==================================================\n');
fprintf(fid,'%s\n',unitPrint); % print units
fprintf(fid,'G0 X0 ; rapid motion to home position\n'); % go home
fprintf(fid,'%s\n',startPosPrint); % move to the starting position
fprintf(fid,'%s\n',movPrint); % print movement type
fprintf(fid,'G17 ; set xy working plane\n');% only working in the xy plane
fprintf(fid,';==================================================\n');
[~,~] = stepIncrement(movTypeSelect,stepCount,dTravel,endPos,numSteps,startPos,fid);
% fprintf('Step: %.1f/%.1f Remaining: %.1f\n',stepCount,numSteps,stepsRemaining)
fclose(fid);
%===== END MAKE THE GCODE =====%
fprintf('Code Generation complete.\n')
fprintf('Save file: %s\n',fileName)
fprintf('Save location: %s\n',savePath)
fprintf('\nNavigate to the file location and open it in <a href="https://notepad-plus-plus.org/">Notepad++</a> if you wish to view it (don''t use notepad)\n')
out = 'done';
end
and the paged file:
function [stepCount,stepsRemaining] = stepIncrement(movTypeSelect,stepCount,dTravel,endPos,numSteps,startPos,fid)
if movTypeSelect == 91
% incremental movement calculations
while stepCount <= numSteps
newPos = startPos + dTravel; % new position, mm or inches
newStepCount = stepCount + 1; % increment step counter
stepsRemaining = numSteps - stepCount; % probably a useless feature
if newPos >= endPos
% check if the new position will be greater then or equal to
% the ending position. If it is, stop the program. Otherwise
% continue the program.
break
end
command = strcat('G0 X',num2str(newPos),' Y0 F0 ; move to new position');
fprintf(fid,'%s\n',command); % print the new command
startPos = newPos; % for incremental travel each loop means the current startPos is the last newPos
stepCount = newStepCount; % for incremental travel each loop means the current step count is the last newStepCount
end
elseif movTypeSelect == 90
% absolute movement calculations
while stepCount <= numSteps
newPos = startPos + (stepCount*dTravel);
newStepCount = stepCount + 1; % increment step counter
stepsRemaining = numSteps - stepCount; % probably a useless feature
if newPos >= endPos
% check if the new position will be greater then or equal to
% the ending position. If it is, stop the program. Otherwise
% continue the program.
break
end
command = strcat('G0 X',num2str(newPos),' Y0 F0 ; move to new position');
fprintf(fid,'%s\n',command); % print the new command
% for absolute movement, you don't need to reset the starting position.
% The starting position will never change.
stepCount = newStepCount;
end
else
error('Couldn''t determine how you want to move')
end
end

Antworten (1)

Star Strider
Star Strider am 25 Jan. 2019
I had this same sort of problem is code that I run frequently. The way I solved it was to put a return call in the middle of it, then repositioned the return in halves of each remaining section of my code as long as the ‘ans’ appeared until I found the line that was causing the output. It turned out to be an
fclose('all');
call that was properly terminated by a semicolon. The solution was to assign the output to a variable:
STATUS = fclose('all');

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by