use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.
the class AutoLevelerTopComponent method scanSurfaceButtonActionPerformed.
// </editor-fold>//GEN-END:initComponents
private void scanSurfaceButtonActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_scanSurfaceButtonActionPerformed
if (scanner == null || scanner.getProbeStartPositions() == null || scanner.getProbeStartPositions().isEmpty()) {
return;
}
scanningSurface = true;
try {
Units u = this.unitMM.isSelected() ? Units.MM : Units.INCH;
AutoLevelSettings als = settings.getAutoLevelSettings();
for (Position p : scanner.getProbeStartPositions()) {
backend.sendGcodeCommand(true, String.format("G90 G21 G0 X%f Y%f Z%f", p.x, p.y, p.z));
backend.probe("Z", als.probeSpeed, this.scanner.getProbeDistance(), u);
backend.sendGcodeCommand(true, String.format("G90 G21 G0 Z%f", p.z));
}
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
} finally {
scanningSurface = false;
}
}
use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.
the class SurfaceScanner method update.
/**
* Provides two points of the scanners bounding box and the number of points to sample in the X/Y directions.
*/
public void update(final Position corner1, final Position corner2, double resolution) {
if (corner1.getUnits() != corner2.getUnits()) {
throw new IllegalArgumentException("Provide same unit for both measures.");
}
if (resolution == 0)
return;
Units units = corner1.getUnits();
double minx = Math.min(corner1.x, corner2.x);
double maxx = Math.max(corner1.x, corner2.x);
double miny = Math.min(corner1.y, corner2.y);
double maxy = Math.max(corner1.y, corner2.y);
double minz = Math.min(corner1.z, corner2.z);
double maxz = Math.max(corner1.z, corner2.z);
Position newMin = new Position(minx, miny, minz, units);
Position newMax = new Position(maxx, maxy, maxz, units);
// Make sure the position changed before resetting things.
if (newMin.equals(this.minXYZ) && newMax.equals(this.maxXYZ) && this.resolution == resolution) {
return;
}
this.minXYZ = newMin;
this.maxXYZ = newMax;
this.probeDistance = minz - maxz;
this.resolution = resolution;
this.xAxisPoints = (int) (Math.ceil((maxx - minx) / resolution)) + 1;
this.yAxisPoints = (int) (Math.ceil((maxy - miny) / resolution)) + 1;
this.probePositionGrid = new Position[this.xAxisPoints][this.yAxisPoints];
// Calculate probe locations.
ImmutableList.Builder<Position> probePositionBuilder = ImmutableList.builder();
for (int x = 0; x < this.xAxisPoints; x++) {
for (int y = 0; y < this.yAxisPoints; y++) {
Position p = new Position(minx + Math.min(maxx - minx, x * resolution), miny + Math.min(maxy - miny, y * resolution), maxz, units);
probePositionBuilder.add(p);
}
}
this.probePositions = probePositionBuilder.build();
}
use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.
the class SizeDisplay method getTextForMM.
private static String getTextForMM(double mm, Units goal) {
Position p = new Position(mm, 0, 0, Units.MM);
double converted = p.getPositionIn(goal).x;
return FORMATTER.format(converted) + " " + goal.abbreviation;
}
use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.
the class GrblController method rawResponseHandler.
@Override
protected void rawResponseHandler(String response) {
String processed = response;
try {
boolean verbose = false;
if (GrblUtils.isOkResponse(response)) {
this.commandComplete(processed);
} else // Error case.
if (GrblUtils.isOkErrorAlarmResponse(response)) {
if (GrblUtils.isAlarmResponse(response)) {
// this is not updating the state to Alarm in the GUI, and the alarm is no longer being processed
// TODO: Find a builder library.
this.controllerStatus = new ControllerStatus(lookupCode(response, true), this.controllerStatus.getMachineCoord(), this.controllerStatus.getWorkCoord(), this.controllerStatus.getFeedSpeed(), this.controllerStatus.getSpindleSpeed(), this.controllerStatus.getOverrides(), this.controllerStatus.getWorkCoordinateOffset(), this.controllerStatus.getEnabledPins(), this.controllerStatus.getAccessoryStates());
dispatchStatusString(this.controllerStatus);
dispatchStateChange(COMM_IDLE);
}
// If there is an active command, mark it as completed with error
Optional<GcodeCommand> activeCommand = this.getActiveCommand();
if (activeCommand.isPresent()) {
processed = String.format(Localization.getString("controller.exception.sendError"), activeCommand.get().getCommandString(), lookupCode(response, false)).replaceAll("\\.\\.", "\\.");
this.errorMessageForConsole(processed + "\n");
this.commandComplete(processed);
} else {
processed = String.format(Localization.getString("controller.exception.unexpectedError"), lookupCode(response, false)).replaceAll("\\.\\.", "\\.");
this.errorMessageForConsole(processed + "\n");
}
checkStreamFinished();
processed = "";
} else if (GrblUtils.isGrblVersionString(response)) {
this.isReady = true;
resetBuffers();
// single step mode
if (getControlState() != COMM_CHECK) {
this.controllerStatus = null;
}
this.stopPollingPosition();
positionPollTimer = createPositionPollTimer();
this.beginPollingPosition();
// In case a reset occurred while streaming.
if (this.isStreaming()) {
checkStreamFinished();
}
this.grblVersion = GrblUtils.getVersionDouble(response);
this.grblVersionLetter = GrblUtils.getVersionLetter(response);
this.capabilities = GrblUtils.getGrblStatusCapabilities(this.grblVersion, this.grblVersionLetter);
try {
this.sendCommandImmediately(createCommand(GrblUtils.GRBL_VIEW_SETTINGS_COMMAND));
this.sendCommandImmediately(createCommand(GrblUtils.GRBL_VIEW_PARSER_STATE_COMMAND));
} catch (Exception e) {
throw new RuntimeException(e);
}
Logger.getLogger(GrblController.class.getName()).log(Level.CONFIG, "{0} = {1}{2}", new Object[] { Localization.getString("controller.log.version"), this.grblVersion, this.grblVersionLetter });
Logger.getLogger(GrblController.class.getName()).log(Level.CONFIG, "{0} = {1}", new Object[] { Localization.getString("controller.log.realtime"), this.capabilities.hasCapability(GrblCapabilitiesConstants.REAL_TIME) });
} else if (GrblUtils.isGrblProbeMessage(response)) {
Position p = GrblUtils.parseProbePosition(response, getFirmwareSettings().getReportingUnits());
if (p != null) {
dispatchProbeCoordinates(p);
}
} else if (GrblUtils.isGrblStatusString(response)) {
// Only 1 poll is sent at a time so don't decrement, reset to zero.
this.outstandingPolls = 0;
// Status string goes to verbose console
verbose = true;
this.handleStatusString(response);
} else if (GrblUtils.isGrblFeedbackMessage(response, capabilities)) {
GrblFeedbackMessage grblFeedbackMessage = new GrblFeedbackMessage(response);
// Convert feedback message to raw commands to update modal state.
this.updateParserModalState(new GcodeCommand(GrblUtils.parseFeedbackMessage(response, capabilities)));
this.verboseMessageForConsole(grblFeedbackMessage.toString() + "\n");
setDistanceModeCode(grblFeedbackMessage.getDistanceMode());
setUnitsCode(grblFeedbackMessage.getUnits());
dispatchStateChange(COMM_IDLE);
} else if (GrblUtils.isGrblSettingMessage(response)) {
GrblSettingMessage message = new GrblSettingMessage(response);
processed = message.toString();
}
if (StringUtils.isNotBlank(processed)) {
if (verbose) {
this.verboseMessageForConsole(processed + "\n");
} else {
this.messageForConsole(processed + "\n");
}
}
} catch (Exception e) {
String message = "";
if (e.getMessage() != null) {
message = ": " + e.getMessage();
}
message = Localization.getString("controller.error.response") + " <" + processed + ">" + message;
logger.log(Level.SEVERE, message, e);
this.errorMessageForConsole(message + "\n");
}
}
use of com.willwinder.universalgcodesender.model.Position in project Universal-G-Code-Sender by winder.
the class GrblUtils method getStatusFromStatusString.
/**
* Parses a GRBL status string in the legacy format or v1.x format:
* legacy: <status,WPos:1,2,3,MPos:1,2,3>
* 1.x: <status|WPos:1,2,3|Bf:0,0|WCO:0,0,0>
* @param lastStatus required for the 1.x version which requires WCO coords
* and override status from previous status updates.
* @param status the raw status string
* @param version capabilities flags
* @param reportingUnits units
* @return
*/
protected static ControllerStatus getStatusFromStatusString(ControllerStatus lastStatus, final String status, final Capabilities version, Units reportingUnits) {
// Legacy status.
if (!version.hasCapability(GrblCapabilitiesConstants.V1_FORMAT)) {
return new ControllerStatus(getStateFromStatusString(status, version), getMachinePositionFromStatusString(status, version, reportingUnits), getWorkPositionFromStatusString(status, version, reportingUnits));
} else {
String state = "";
Position MPos = null;
Position WPos = null;
Position WCO = null;
OverridePercents overrides = null;
EnabledPins pins = null;
AccessoryStates accessoryStates = null;
Double feedSpeed = null;
Double spindleSpeed = null;
boolean isOverrideReport = false;
// Parse out the status messages.
for (String part : status.substring(0, status.length() - 1).split("\\|")) {
if (part.startsWith("<")) {
int idx = part.indexOf(':');
if (idx == -1)
state = part.substring(1);
else
state = part.substring(1, idx);
} else if (part.startsWith("MPos:")) {
MPos = GrblUtils.getPositionFromStatusString(status, machinePattern, reportingUnits);
} else if (part.startsWith("WPos:")) {
WPos = GrblUtils.getPositionFromStatusString(status, workPattern, reportingUnits);
} else if (part.startsWith("WCO:")) {
WCO = GrblUtils.getPositionFromStatusString(status, wcoPattern, reportingUnits);
} else if (part.startsWith("Ov:")) {
isOverrideReport = true;
String[] overrideParts = part.substring(3).trim().split(",");
if (overrideParts.length == 3) {
overrides = new OverridePercents(Integer.parseInt(overrideParts[0]), Integer.parseInt(overrideParts[1]), Integer.parseInt(overrideParts[2]));
}
} else if (part.startsWith("F:")) {
feedSpeed = Double.parseDouble(part.substring(2));
} else if (part.startsWith("FS:")) {
String[] parts = part.substring(3).split(",");
feedSpeed = Double.parseDouble(parts[0]);
spindleSpeed = Double.parseDouble(parts[1]);
} else if (part.startsWith("Pn:")) {
String value = part.substring(part.indexOf(':') + 1);
pins = new EnabledPins(value);
} else if (part.startsWith("A:")) {
String value = part.substring(part.indexOf(':') + 1);
accessoryStates = new AccessoryStates(value);
}
}
// Grab WCO from state information if necessary.
if (WCO == null) {
// Grab the work coordinate offset.
if (lastStatus != null && lastStatus.getWorkCoordinateOffset() != null) {
WCO = lastStatus.getWorkCoordinateOffset();
} else {
WCO = new Position(0, 0, 0, reportingUnits);
}
}
// Calculate missing coordinate with WCO
if (WPos == null) {
WPos = new Position(MPos.x - WCO.x, MPos.y - WCO.y, MPos.z - WCO.z, reportingUnits);
}
if (MPos == null) {
MPos = new Position(WPos.x + WCO.x, WPos.y + WCO.y, WPos.z + WCO.z, reportingUnits);
}
if (!isOverrideReport && lastStatus != null) {
overrides = lastStatus.getOverrides();
pins = lastStatus.getEnabledPins();
accessoryStates = lastStatus.getAccessoryStates();
} else if (isOverrideReport) {
// set all pins to a disabled state.
if (pins == null) {
pins = new EnabledPins("");
}
// Likewise for accessory states.
if (accessoryStates == null) {
accessoryStates = new AccessoryStates("");
}
}
return new ControllerStatus(state, MPos, WPos, feedSpeed, spindleSpeed, overrides, WCO, pins, accessoryStates);
}
}
Aggregations