-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
G2CoreController.java
193 lines (155 loc) · 7.27 KB
/
G2CoreController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2019-2023 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS 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 3 of the License, or
(at your option) any later version.
UGS 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 UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender;
import com.google.gson.JsonObject;
import com.willwinder.universalgcodesender.communicator.ICommunicator;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.ControllerStatus;
import com.willwinder.universalgcodesender.listeners.ControllerStatusBuilder;
import com.willwinder.universalgcodesender.listeners.MessageType;
import com.willwinder.universalgcodesender.model.CommunicatorState;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_IDLE;
import com.willwinder.universalgcodesender.model.PartialPosition;
/**
* G2Core Control layer.
*
* @author Joacim Breiler
*/
public class G2CoreController extends TinyGController {
private static final String STATUS_REPORT_CONFIG = "{sr:{posx:t, posy:t, posz:t, mpox:t, mpoy:t, mpoz:t, plan:t, vel:t, unit:t, stat:t, dist:t, admo:t, frmo:t, coor:t}}";
/**
* A temporary flag for emulating a JOG state when parsing the controller status
*/
private boolean isJogging = false;
public G2CoreController() {
super();
}
public G2CoreController(ICommunicator communicator) {
super(communicator);
}
@Override
protected void openCommAfterEvent() {
// We don't need this
}
@Override
public void softReset() throws Exception {
this.comm.sendByteImmediately(TinyGUtils.COMMAND_KILL_JOB);
this.comm.sendByteImmediately(TinyGUtils.COMMAND_QUEUE_FLUSH);
this.comm.sendByteImmediately((byte) '\n');
setCurrentState(CommunicatorState.COMM_DISCONNECTED);
controllerStatus = ControllerStatusBuilder.newInstance(controllerStatus)
.setState(ControllerState.DISCONNECTED)
.build();
dispatchStatusString(controllerStatus);
sendInitCommands();
}
@Override
protected void handleReadyResponse(String response, JsonObject jo) {
if (TinyGUtils.isTinyGVersion(jo)) {
firmwareVersionNumber = TinyGUtils.getVersion(jo);
firmwareVersion = "G2Core " + firmwareVersionNumber;
}
capabilities.addCapability(CapabilitiesConstants.X_AXIS);
capabilities.addCapability(CapabilitiesConstants.Y_AXIS);
capabilities.addCapability(CapabilitiesConstants.Z_AXIS);
capabilities.addCapability(CapabilitiesConstants.A_AXIS);
capabilities.addCapability(CapabilitiesConstants.B_AXIS);
capabilities.addCapability(CapabilitiesConstants.C_AXIS);
capabilities.addCapability(CapabilitiesConstants.RETURN_TO_ZERO);
capabilities.addCapability(CapabilitiesConstants.JOGGING);
capabilities.addCapability(CapabilitiesConstants.CONTINUOUS_JOGGING);
capabilities.addCapability(CapabilitiesConstants.HOMING);
capabilities.addCapability(CapabilitiesConstants.FIRMWARE_SETTINGS);
capabilities.removeCapability(CapabilitiesConstants.OVERRIDES);
capabilities.removeCapability(CapabilitiesConstants.SETUP_WIZARD);
setCurrentState(COMM_IDLE);
dispatchConsoleMessage(MessageType.INFO, "[ready] " + response + "\n");
try {
comm.sendByteImmediately(TinyGUtils.COMMAND_ENQUIRE_STATUS);
} catch (Exception e) {
dispatchConsoleMessage(MessageType.ERROR, "Couldn't enquire for controller status\n");
}
}
@Override
protected void sendInitCommands() {
// Enable JSON mode
// 0=text mode, 1=JSON mode
comm.queueCommand(getCommandCreator().createCommand("{ej:1}"));
// Configure status reports
comm.queueCommand(getCommandCreator().createCommand(STATUS_REPORT_CONFIG));
// JSON verbosity
// 0=silent, 1=footer, 2=messages, 3=configs, 4=linenum, 5=verbose
comm.queueCommand(getCommandCreator().createCommand("{jv:4}"));
// Queue report verbosity
// 0=off, 1=filtered, 2=verbose
comm.queueCommand(getCommandCreator().createCommand("{qv:0}"));
// Status report verbosity
// 0=off, 1=filtered, 2=verbose
comm.queueCommand(getCommandCreator().createCommand("{sv:1}"));
// Request firmware settings
comm.queueCommand(getCommandCreator().createCommand("$$"));
// Enable feed overrides
comm.queueCommand(getCommandCreator().createCommand("{mfoe:1}"));
comm.queueCommand(getCommandCreator().createCommand("{mtoe:1}"));
comm.queueCommand(getCommandCreator().createCommand("{ssoe:1}"));
// Activate motors by default
comm.queueCommand(getCommandCreator().createCommand("{xam:1, yam:1, zam:1}"));
// Request initial status report
comm.queueCommand(getCommandCreator().createCommand("{sr:n}"));
comm.streamCommands();
// Refresh the status update
setStatusUpdateRate(getStatusUpdateRate());
}
@Override
protected void cancelSendAfterEvent() throws Exception {
// Canceling the job on the controller (which will also flush the buffer)
comm.sendByteImmediately(TinyGUtils.COMMAND_KILL_JOB);
// Work around for clearing the sent buffer size
comm.cancelSend();
// We will end up in an alarm state, clear the alarm
killAlarmLock();
}
@Override
public void jogMachine(PartialPosition distance, double feedRate) throws Exception {
isJogging = true;
super.jogMachine(distance, feedRate);
}
@Override
public void jogMachineTo(final PartialPosition position, final double feedRate) throws Exception {
isJogging = true;
super.jogMachineTo(position, feedRate);
}
/**
* Parses the controller status object and returns the current status.
* In this method we emulate states that the controller doesn't currently handle
*
* @param jo a json object with the controller status
* @return the new current controller status
*/
@Override
protected ControllerStatus parseControllerStatus(JsonObject jo) {
ControllerStatus controllerStatus = TinyGUtils.updateControllerStatus(this.controllerStatus, jo);
// If controller is in the state RUN but we are jogging emulate a JOG-state
if (controllerStatus.getState() == ControllerState.RUN && isJogging) {
controllerStatus = ControllerStatusBuilder
.newInstance(controllerStatus)
.setState(ControllerState.JOG)
.build();
} else if (controllerStatus.getState() == ControllerState.IDLE && isJogging) {
isJogging = false;
}
return controllerStatus;
}
}