Skip to content

Commit

Permalink
improved stepper controls
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Jun 12, 2023
1 parent 7bf6c3c commit ba331b5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
package com.marginallyclever.robotoverlord.components.motors;

import com.marginallyclever.robotoverlord.SerializationContext;
import com.marginallyclever.robotoverlord.parameters.IntParameter;
import org.json.JSONException;
import org.json.JSONObject;

public class StepperMotorComponent extends MotorComponent {
public static final String [] directionNames = {"Backward","Forward"};
public static final int DIRECTION_BACKWARD=0;
public static final int DIRECTION_FORWARD=1;

private int direction=DIRECTION_FORWARD;
private int stepCount=0;
private int stepAbsolute=0;
private int stepPerRevolution=200;
private int microStepping=1;
public final IntParameter direction = new IntParameter("Direction",DIRECTION_FORWARD);
public final IntParameter stepPerRevolution = new IntParameter("Step/turn",200);
public final IntParameter microStepping = new IntParameter("Microsteps",1);

/**
* @param direction either DIRECTION_BACKWARD or DIRECTION_FORWARD
*/
public void setDirection(int direction) {
this.direction = direction;
}

public void step() {
stepCount+=(direction*2)-1;
stepAbsolute++;
}

public int getStepCount() {
return stepCount;
}

public void resetStepAbsolute() {
stepAbsolute=0;
}
public int getStepAbsolute() {
return stepAbsolute;
if(direction!=DIRECTION_BACKWARD && direction!=DIRECTION_FORWARD) {
throw new IllegalArgumentException("direction must be either DIRECTION_BACKWARD or DIRECTION_FORWARD.");
}
this.direction.set(direction);
}

public void setStepPerRevolution(int stepPerRevolution) {
this.stepPerRevolution = stepPerRevolution;
this.stepPerRevolution.set(stepPerRevolution);
}

public int getStepPerRevolution() {
return stepPerRevolution;
return stepPerRevolution.get();
}

/**
Expand All @@ -48,14 +38,32 @@ public int getStepPerRevolution() {
public void setMicroStepping(int microStepping) {
if(microStepping<1) throw new IllegalArgumentException("microStepping must be greater than zero.");
if(isNotPowerOf2(microStepping)) throw new IllegalArgumentException("microStepping must be power of two.");
this.microStepping = microStepping;
this.microStepping.set(microStepping);
}

private boolean isNotPowerOf2(int v) {
return (v & (v - 1)) != 0;
}

public int getMicroStepping() {
return microStepping;
return microStepping.get();
}

@Override
public JSONObject toJSON(SerializationContext context) {
JSONObject json = super.toJSON(context);
json.put("direction",direction.toJSON(context));
json.put("stepPerRevolution",stepPerRevolution.toJSON(context));
json.put("microStepping",microStepping.toJSON(context));
return json;

}

@Override
public void parseJSON(JSONObject jo, SerializationContext context) throws JSONException {
super.parseJSON(jo, context);
if(jo.has("direction")) direction.parseJSON(jo.getJSONObject("direction"),context);
if(jo.has("stepPerRevolution")) stepPerRevolution.parseJSON(jo.getJSONObject("stepPerRevolution"),context);
if(jo.has("microStepping")) microStepping.parseJSON(jo.getJSONObject("microStepping"),context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.marginallyclever.robotoverlord.components.Component;
import com.marginallyclever.robotoverlord.components.PoseComponent;
import com.marginallyclever.robotoverlord.components.motors.DCMotorComponent;
import com.marginallyclever.robotoverlord.components.motors.MotorComponent;
import com.marginallyclever.robotoverlord.components.motors.ServoComponent;
import com.marginallyclever.robotoverlord.components.motors.StepperMotorComponent;
Expand Down Expand Up @@ -40,13 +41,28 @@ public MotorSystem(EntityManager entityManager) {
*/
@Override
public void decorate(ViewPanelFactory view, Component component) {
if (component instanceof ServoComponent) decorateServo(view, component);
if (component instanceof MotorComponent) decorateMotor(view, component);
if (component instanceof ServoComponent) decorateServo(view, (ServoComponent) component);
if (component instanceof StepperMotorComponent) decorateStepper(view, (StepperMotorComponent)component);
if (component instanceof MotorComponent) decorateMotor(view, (MotorComponent) component);
}

private void decorateServo(ViewPanelFactory view, Component component) {
ServoComponent servo = (ServoComponent) component;
private void decorateStepper(ViewPanelFactory view, StepperMotorComponent stepper) {
view.add(stepper.stepPerRevolution);
view.add(stepper.microStepping);
view.addComboBox(stepper.direction,StepperMotorComponent.directionNames);
view.addButton("Step").addActionEventListener(e -> stepNow(stepper));
}

private void stepNow(StepperMotorComponent stepper) {
double degreePerStep = 360.0/(stepper.microStepping.get() * stepper.stepPerRevolution.get());
double angle = stepper.currentAngle.get();
if(stepper.direction.get()==StepperMotorComponent.DIRECTION_BACKWARD) angle -= degreePerStep;
else angle += degreePerStep;

rotateMotor(stepper,angle);
}

private void decorateServo(ViewPanelFactory view, ServoComponent servo) {
ViewElementButton bCurve = view.addButton("Tune PID");
bCurve.addActionEventListener(e -> editPID(bCurve, servo));
view.add(servo.kP);
Expand All @@ -60,8 +76,7 @@ private void editPID(JComponent parent, ServoComponent servo) {
EntitySystemUtils.makePanel(panel, parent, "Tune PID");
}

private void decorateMotor(ViewPanelFactory view, Component component) {
MotorComponent motor = (MotorComponent) component;
private void decorateMotor(ViewPanelFactory view, MotorComponent motor) {
view.add(motor.currentAngle);
view.add(motor.currentRPM);
view.add(motor.desiredRPM);
Expand Down Expand Up @@ -95,9 +110,8 @@ public void update(double dt) {

public void updateMotor(MotorComponent motor, double dt) {
if(motor instanceof ServoComponent) updateServo((ServoComponent)motor,dt);
else if(motor instanceof StepperMotorComponent) {}
//else if(motor instanceof DCMotorComponent) {}
else updateMotorBasic(motor, dt);
else if(motor instanceof StepperMotorComponent) updateStepper((StepperMotorComponent)motor,dt);
else if(motor instanceof DCMotorComponent) updateMotorBasic(motor, dt);
}

private void updateServo(ServoComponent servo, double dt) {
Expand Down Expand Up @@ -133,6 +147,16 @@ private void updateServo(ServoComponent servo, double dt) {
updateMotorBasic(servo, dt);
}

private void updateStepper(StepperMotorComponent motor, double dt) {
if(!motor.enabled.get()) return;

// adjust angle
double degreesPerSecond = motor.getCurrentRPM()*360.0/60.0;
double newAngle = motor.currentAngle.get() + degreesPerSecond * dt;

rotateMotor(motor, newAngle);
}

private void updateMotorBasic(MotorComponent motor, double dt) {
if(!motor.enabled.get()) return;

Expand All @@ -158,6 +182,11 @@ private void updateMotorBasic(MotorComponent motor, double dt) {
rotateMotor(motor, newAngle);
}

/**
* Rotate the motor to the specified angle.
* @param motor the motor to rotate
* @param newAngle the new angle in degrees
*/
public void rotateMotor(MotorComponent motor, double newAngle) {
double oldAngle = motor.currentAngle.get();

Expand Down

0 comments on commit ba331b5

Please sign in to comment.