Skip to content

Commit

Permalink
MarlinRobotArm can now receive GCode commands
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Dec 21, 2023
1 parent 53ba58f commit 9cbf0ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public JSONObject toJSON() {
@Override
public void fromJSON(JSONObject from) {
super.fromJSON(from);
if(from.has("axle")) axle = Registry.findNodeByID(from.getString("axle"),Pose.class);
if(from.has("axle")) {
axle = Registry.findNodeByID(from.getString("axle"),Pose.class);
}
if(from.has("angle")) angle = from.getDouble("angle");
if(from.has("minAngle")) minAngle = from.getDouble("minAngle");
if(from.has("maxAngle")) maxAngle = from.getDouble("maxAngle");
Expand All @@ -136,6 +138,10 @@ public double getAngle() {
return angle;
}

public void setAngle(double degrees) {
angle = degrees;
}

public double getMinAngle() {
return minAngle;
}
Expand All @@ -159,4 +165,4 @@ public double getAcceleration() {
public void setAcceleration(double acceleration) {
this.acceleration = acceleration;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,29 @@ public void getComponents(List<JComponent> list) {
// Add a text field to send a position to the robot arm.
JTextField output = new JTextField();
output.setEditable(false);
addLabelAndComponent(pane, "Output", output);
pane.add(output);

// Add a button that displays gcode to the output.
JButton gcodeButton = new JButton("Get");
addLabelAndComponent(pane, "FK as GCode", gcodeButton);
gcodeButton.addActionListener(e-> output.setText(getFKAsGCode()) );
JButton getFKButton = new JButton("Get");
getFKButton.addActionListener(e-> output.setText(getFKAsGCode()) );
pane.add(getFKButton);

// TODO add a text field that will be sent to the robot arm.
// Add a text field that will be sent to the robot arm.
JTextField input = new JTextField();
pane.add(input);
// Add a button to send the text field to the robot arm.
JButton sendButton = new JButton("Send");
sendButton.addActionListener(e-> output.setText(sendGCode(input.getText())) );
pane.add(sendButton);

super.getComponents(list);
}

// take the current angle of each motor hinge and writes as a GCode command.
/**
* Build a string from the current angle of each motor hinge, aka the
* <a href="https://en.wikipedia.org/wiki/Forward_kinematics">Forward Kinematics</a> of the robot arm.
* @return GCode command
*/
public String getFKAsGCode() {
StringBuilder sb = new StringBuilder("G0");
for(Motor motor : motors) {
Expand All @@ -100,4 +110,27 @@ public String getFKAsGCode() {
}
return sb.toString();
}

/**
* Send gcode to robot arm.
* @param gcode GCode command
* @return response from robot arm
*/
public String sendGCode(String gcode) {
if(gcode.startsWith("G0")) {
// parse gcode and set motor angles
String [] parts = gcode.split("\\s+");
for(Motor motor : motors) {
if(motor!=null) {
for(String p : parts) {
if(p.startsWith(motor.getName())) {
motor.getAxle().setAngle(Double.parseDouble(p.substring(motor.getName().length())));
}
}
}
}
return "Ok";
}
return "Error: unknown command";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ private void stopListeningTo(Node node) {
node.removeAttachListener(this);
node.removeDetachListener(this);
node.removeRenameListener(this);

// stop listening to all the children of this node, a reverse of scanTree()
List<Node> toRemove = new ArrayList<>(node.getChildren());
for(Node progeny : toRemove) {
stopListeningTo(progeny);
}
}

/**
Expand Down Expand Up @@ -167,10 +173,7 @@ public void nodeAttached(Node child) {
int index = parent.getChildren().indexOf(child);
branchParent.insert(branchChild,index);

child.addAttachListener(this);
child.addDetachListener(this);
child.addRenameListener(this);

listenTo(child);
scanTree(child);

((DefaultTreeModel) tree.getModel()).nodeStructureChanged(branchParent);
Expand All @@ -179,9 +182,8 @@ public void nodeAttached(Node child) {
@Override
public void nodeDetached(Node child) {
logger.debug("Detached "+child.getAbsolutePath());
child.removeAttachListener(this);
child.removeDetachListener(this);
child.removeRenameListener(this);

stopListeningTo(child);

Node parent = child.getParent();
if(parent==null) throw new RuntimeException("Source node has no parent");
Expand Down

0 comments on commit 9cbf0ba

Please sign in to comment.