Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/pi4j/devices/bmp280/BMP280I2cExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ static void main(String[] args) {
System.out.println("Humidity: " + FORMAT.format(measurement.getHumidity()) + " %");
});



commandHandler.setDefault("r");
commandHandler.process(args);

Expand Down
249 changes: 84 additions & 165 deletions src/main/java/com/pi4j/devices/fs90r/Fs90rApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,190 +6,109 @@

package com.pi4j.devices.fs90r;


import com.pi4j.drivers.motor.fs90r.Fs90rDriver;
import com.pi4j.io.pwm.Pwm;
import com.pi4j.io.pwm.PwmConfigBuilder;
import com.pi4j.boardinfo.util.PwmChipUtil;
import com.pi4j.Pi4J;
import com.pi4j.boardinfo.util.PwmChipUtil;
import com.pi4j.context.Context;
import com.pi4j.io.pwm.PwmConfig;
import com.pi4j.drivers.motor.fs90r.Fs90rDriver;
import com.pi4j.examples.utils.commandhandler.DeviceCommandHandler;
import com.pi4j.examples.utils.commandhandler.DoubleParameter;
import com.pi4j.examples.utils.commandhandler.IntegerParameter;
import com.pi4j.examples.utils.commandhandler.StringParameter;
import com.pi4j.io.pwm.Pwm;
import com.pi4j.io.pwm.PwmType;
import com.pi4j.util.Console;
import com.pi4j.util.Delay;

/**
* Example implementation for a FS90R servo motor.
* The Fs90R defines a specific PWM interface and exactly how the PWN signal
* controls the servo
*
* https://www.pololu.com/product/2820
*
* See the README.md file for example usage and explanation.
/**
* Example implementation for a FS90R servo motor.
* The Fs90R defines a specific PWM interface and exactly how the PWN signal
* controls the servo
* <p>
* <a href="https://www.pololu.com/product/2820">Servo details</a>
* <p>
* See the README.md file for example usage and explanation.
* <p>
* FS90R servo motor control. The driver will use the PWM device created by
* this app. It is assumed the device is a HardWare PWM. Hardware PWM supports
* a more consistent PWM signal.
* The user controls the direction of rotation via setDirection(), where cw denotes clockwise and
* ccw counterclockwise. The direction must be set prior to use of setSpeed().
* SetSpeed() is a percentage of the maximum RPM.
*
*
*/
public class Fs90rApp {
private static final int SERVO_NUMBER = 1;
private static final int PWM_CHANNEL = 2;
private static Console console;
private static final Delay delay = new Delay();

public Fs90rApp() {
super();

}

/**
* <p>main.</p>
* See the README.md file for example usage.
*
* FS90R servo motor control. The driver will use the PWM device created by
* this app. It is assumed the device is a HardWare PWM. Hardware PWM creates
* a more consistent PWM signal.
* @param args an array of {@link java.lang.String} objects.
* @throws java.lang.Exception if any.
*/
public class Fs90rApp {

private static final int DEFAULT_PWM_FREQUENCY = 50;
private static final int DEFAULT_CHANNEL_NUMBER = 2;
private static final int SERVO_NUMBER = 1;


private static double degree = 90.0;
private static Integer channel = DEFAULT_CHANNEL_NUMBER ;
private static Console console ;
private static Fs90rDriver fs90R ;
private static final Delay delay = new Delay();
private static java .util.Scanner scanner;
private static double lowPulse = 1000.0;
private static double highPulse = 2000.0;


public Fs90rApp() {
super();

}


/**
* <p>main.</p>
* See the README.md file for example usage.
* @param args an array of {@link java.lang.String} objects.
* @throws java.lang.Exception if any.
*/
static void main(String[] args) throws Exception {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "INFO");

int onlyOne = 0; // numerous parms a mutually exclusive

scanner = new java.util.Scanner(System.in);

console = new Console();
Context pi4j = Pi4J.newAutoContext();
console.title("<-- The Pi4J V5 Project Extension -->", "Fs90rApp");
String helpString = " Parms: -c channel -cw (clockwise) S M F -ccw (counter-clockwise) S M F -h HELP \n" +
" -low microseconds -high microseconds -cw -ccw mutually exclusive -d degree \n" +
" degree must be in the range 0..180. 90 stop, 0..89 CW, 91..180 CCW ";


for (int i = 0; i < args.length; i++) {
String o = args[i];
if (o.contentEquals("-low")) {
String a = args[i + 1];
lowPulse = Double.parseDouble(a.substring(0));
i++;
} else if (o.contentEquals("-high")) {
String a = args[i + 1];
highPulse = Double.parseDouble(a.substring(0));
i++;
} else if (o.contentEquals("-d")) {
String a = args[i + 1];
degree = Double.parseDouble(a.substring(0));
if((!Double.isFinite(degree) || degree < 0) || (degree > 180)){
console.println("-d degree must be in range 0..180");
System.exit(40);
}
onlyOne++;
i++;
} else if (o.contentEquals("-c")) {
String a = args[i + 1];
channel = Integer.parseInt(a.substring(0));
i++;
} else if (o.contentEquals("-cw")) { /* 0-89 */
String a = args[i + 1];
onlyOne++;
i++;
if (a.equalsIgnoreCase("S")) {
degree = 75.0;
} else if (a.equalsIgnoreCase("M")) {
degree = 40.0;
} else if (a.equalsIgnoreCase("F")) {
degree = 0.0;
} else {
console.println(" -cw invalid ");
System.exit(41);
}
} else if (o.contentEquals("-ccw")) { /* 191-180 */
String a = args[i + 1];
i++;
onlyOne++;
if (a.equalsIgnoreCase("S")) {
degree = 105.0;
} else if (a.equalsIgnoreCase("M")) {
degree = 140.0;
} else if (a.equalsIgnoreCase("F")) {
degree = 180.0;
} else {
console.println(" -ccw invalid ");
System.exit(42);
}
} else if (o.contentEquals("-q")) {
console.println("Exit");
System.exit(0);
} else if (o.contentEquals("-h")) {
console.println(helpString);
System.exit(0);
}else {
console.println(" !!! Invalid Parm " + o);
console.println(helpString);
System.exit(43);
}
}
static void main(String[] args) throws Exception {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "INFO");
console = new Console();
Context pi4j = Pi4J.newAutoContext();
console.title("<-- The Pi4J V5 Project Extension -->", "Fs90rApp");

DeviceCommandHandler<Fs90rDriver> commandHandler = new DeviceCommandHandler<>(settings ->
new Fs90rDriver(pi4j.create(Pwm.newConfigBuilder()
.channel((Integer) settings.get("c"))
.id("fs90RNumber" + SERVO_NUMBER)
.name("fs90R number " + SERVO_NUMBER)
.pwmType(PwmType.HARDWARE)
.chip(PwmChipUtil.getPWMChip())
.frequency(Fs90rDriver.PWM_FREQUENCY))));

if (onlyOne > 1 ) {
console.println(" mutually exclusive parms used.");
console.println(helpString);
System.exit(44);
}
Pwm pwm = createPwm(SERVO_NUMBER, channel, pi4j);
commandHandler.configuration(
"c",
"Set the PWM channel number",
new IntegerParameter(1, 4),
PWM_CHANNEL);

fs90R = new Fs90rDriver(pwm, lowPulse, highPulse);
// Following actions can be used as command arguments after instantiation of this program via './runSg90.sh - '.

// if user did not provide a -cw -ccw or -d default sets stop
fs90R.setServoRotation(degree);
commandHandler.parameterizedAction("low", "Minimal pulse width", new DoubleParameter(500.0, 1500.0), (device, param) -> {
device.setPulseWidthMin(param);
});

waitChange( 10l);

pi4j.shutdown(pwm);
commandHandler.parameterizedAction("high", "Maximum pulse width", new DoubleParameter(1500.0, 2500.0), (device, param) -> {
device.setPulseWidthMax(param);
});

}
commandHandler.parameterizedAction("percent", "set rotation by percentage of max possible", new DoubleParameter(0.0, 100.0), (device, param) -> {
device.setSpeed(param);
});

commandHandler.parameterizedAction("direction", "set rotation direction", new StringParameter("cw", "ccw"), (device, param) -> {
if (param.equals("cw")) {
device.setDirection(Fs90rDriver.ServoDirection.CLOCKWISE);
} else {
device.setDirection(Fs90rDriver.ServoDirection.COUNTERCLOCKWISE);
}

static Pwm createPwm(int servoNumber, Integer channel, Context pi4j) {
});

commandHandler.process(args);

// Shutdown Pi4J
pi4j.shutdown();

final PwmConfig config = PwmConfigBuilder.newInstance (pi4j)
.id ("fs90RNumber" + servoNumber)
.name ("fs90R number " + servoNumber)
.pwmType(PwmType.HARDWARE)
.channel(channel)
.chip(PwmChipUtil.getPWMChip())
.frequency(DEFAULT_PWM_FREQUENCY)
.build ();

return pi4j.create (config);
}

static void waitChange(long c){
while(true){
delay.setMillis(c).materialize();
console.println("Enter q - quit, or degree value ");
if (scanner.hasNextInt() ) {
double nextDegree = scanner.nextDouble();
if((!Double.isFinite(nextDegree) || nextDegree < 0) || (nextDegree > 180)){
console.println(" degree must be in the range 0..180. 90 stop, 0..89 CW, 91..180 CCW");
}else {
fs90R.setServoRotation(nextDegree);
}
} else{
break;
}
}
}
}

}




Expand Down
Loading
Loading