Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The following table the currently supported devices within this project:
| Sensor | 1-Wire | (1) | [DHT22 Temp/Humidity sensor](src/main/java/com/pi4j/devices/dht22/README.md)
| Sensor | | | [HC-SR04 Ultrasonic Sensor](src/main/java/com/pi4j/devices/hcsr04/README.md)
| Sensor | I2C | (1) | [MPL3115A2 Temp/Pressure/Altitude device](src/main/java/com/pi4j/devices/mpl3115a2/README.md)
| Servo | PWM | | [SG90 180 degree servo device](src/main/java/com/pi4j/devices/sg90/README.md)


### Notes:
Expand Down
2 changes: 1 addition & 1 deletion assets/runDHT22.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
java -classpath ./*:../classes com.pi4j.devices.dht22.DHT22_App $@
java -classpath ./*:../classes com.pi4j.devices.dht22.DHT22_App $@
4 changes: 4 additions & 0 deletions assets/runSG90.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash


java -classpath ./*:../classes com.pi4j.devices.sg90.SG90_App $@
89 changes: 50 additions & 39 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<java.version>25</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
<maven-bundle-plugin.version>5.1.9</maven-bundle-plugin.version>
<maven-javadoc-plugin.version>3.3.2</maven-javadoc-plugin.version>
Expand All @@ -33,9 +33,9 @@

<!-- DEPENDENCIES VERSIONS 2.0.3 -SNAPSHOT -->
<slf4j.version>2.0.17</slf4j.version>
<pi4j.version>4.0.1</pi4j.version>
<!--pi4j.version>5.0.0-SNAPSHOT</pi4j.version -->

<pi4j.version>5.0.0-SNAPSHOT</pi4j.version>
<pi4j-driver.version>2.0.0-SNAPSHOT</pi4j-driver.version>
<!-- BUILD PLUGIN VERSIONS -->
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
Expand All @@ -60,6 +60,17 @@
<name>Sonatype OSS Maven2 Repository</name>
<url>https://mvnrepository.com/artifact/org.apache.felix/maven-bundle-plugin</url>
</repository>
<repository>
<id>sonatype-snapshots</id>
<name>Maven Central Snapshots</name>
<url>https://central.sonatype.com/repository/maven-snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>sonatype-oss-staging</id>
<name>Sonatype Open Source Maven Repository</name>
Expand All @@ -84,27 +95,27 @@
<artifactId>pi4j-plugin-gpiod</artifactId>
<version>${pi4j.version}</version>
</dependency-->
<!--dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-linuxfs</artifactId>
<version>${pi4j.version}</version>
</dependency-->
<!--dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-linuxfs</artifactId>
<version>${pi4j.version}</version>
</dependency-->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.12.1</version>
</dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.12.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- include Pi4J Core -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- include Pi4J Core -->
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
Expand All @@ -119,15 +130,15 @@
<!--- log4j - version 2 -->

<!-- include Pi4J Plugins (Platforms and I/O Providers) -->
<dependency>
<!--dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>${pi4j.version}</version>
</dependency>
</dependency-->
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-drivers</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>${pi4j-driver.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
Expand Down Expand Up @@ -190,19 +201,19 @@
</plugin-->

<!-- INCLUDE SOURCE JAR -->
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- INCLUDE JAVADOC JAR -->
<!--plugin>
Expand Down Expand Up @@ -282,7 +293,7 @@
<argument>--module-path</argument>
<argument>${project.build.directory}/distribution</argument>
<argument>--module</argument>
<!-- <argument>com.pi4j.devices.tca9548/com.pi4j.devices.tca9548.SampleTca9548App</argument> -->
<!-- <argument>com.pi4j.devices.tca9548/com.pi4j.devices.tca9548.SampleTca9548App</argument> -->
<argument>com.pi4j.devices.mcp23008/com.pi4j.devices.mcp23008.Mcp23008App</argument>
</arguments>
</configuration>
Expand Down Expand Up @@ -373,7 +384,7 @@
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun-plugin.version}</version>
<executions>
<executions>
<execution>
<id>copySH</id>
<phase>install</phase>
Expand Down Expand Up @@ -460,4 +471,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
2 changes: 1 addition & 1 deletion src/main/java/com/pi4j/devices/mcp4725/MCP4725App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class MCP4725App {

static void main(String[] args) {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "OFF");
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "INFO");

Logger logger = LoggerFactory.getLogger(MCP4725App.class);
logger.trace(">>> Enter: init");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pi4j/devices/mcp4728/MCP4728App.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class MCP4728App {

static void main(String[] args) {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "OFF");
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "iNFO");
Logger logger = LoggerFactory.getLogger(MCP4728App.class);
logger.trace(">>> Enter: init");

Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/pi4j/devices/sg90/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# SG90 servo motor

https://www.scribd.com/document/420327552/Servo
https://www.friendlywire.com/projects/ne555-servo-safe/SG90-datasheet.pdf

These devices do not have a customary DataSheet. Most servo devices provide limited specifications
for required pulse width, they just say 1 - 2 millisecond.

The SG90 defines a specific PWM interface and exactly how the PWN signal
controls the servo. There are many different manufacturers of the SG90 servo.
The pulse width required for setting the servo at 0 degrees or 180 degrees varies by
manufacturer. The driver by default uses the pulse width timing as
(low)1000 - (high)2000 microseconds. The user of this example application can change these defaults when first
invoking the program.

If the servo does not rotate to the desired position, Example: to rotate closer to the 0 degree
point reduce the -low argument, to rotate closer to the 180 degree point increase the
-high argument. The -low -high change should be balanced or 90 degree will not properly align.
Meaning if you reduce the -low by 200, you should increase the -high by 200, or visa-versa.




SG90 Pi
Grey/Brown Ground
Red 5V
Yellow GPIO18 config.sys contains dtoverlay=pwm-2chan
Comment thread
taartspi marked this conversation as resolved.



1. ./mvnw clean package
2. cd target/distribution
3. ./runSG90.sh args

-c channel (default 2) set the channel, depending on config.txt 1 2 3 4
-low float pulse width for 0 degree microseconds
-high float pulse width for 180 degree microseconds
-q quit
-h help

./runSG90.sh begins execution of the application, you then enter the desired servo degree of rotation.
Then continue entering the requested degree value, or any key will exist the program.

Set ouput shaft at 120 degree
./runSG90.sh
120

Driver defaults to 0 degree requires a 1000 micro second pulse, 180 degree requires a 2000 microsecond pulse
If your servo requires different pulse widths

Set low high pulse widths, then set output shaft at 120 degree
./runSG90.sh -low 600 -high 2400
120

My SG90 aligns correctly with these values
./runSG90.sh -low 550 -high 2450

If your configuration uses a channel other than 2
./runSG90.sh -c 1
120
135 changes: 135 additions & 0 deletions src/main/java/com/pi4j/devices/sg90/SG90_App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* /*
* *
*
*/

package com.pi4j.devices.sg90;

import com.pi4j.boardinfo.util.PwmChipUtil;
import com.pi4j.drivers.motor.sg90.SG90Driver;
import com.pi4j.io.pwm.Pwm;
import com.pi4j.Pi4J;
import com.pi4j.context.Context;
import com.pi4j.io.pwm.PwmConfig;
import com.pi4j.io.pwm.PwmConfigBuilder;
import com.pi4j.io.pwm.PwmType;
import com.pi4j.util.Console;
import com.pi4j.util.Delay;

/**
* SG90 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.
*
* See README.md for more information and detail.
*/
class SG90_App {

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 Integer channel = DEFAULT_CHANNEL_NUMBER ;
private static Console console ;
private static SG90Driver sg90 ;
private static final Delay delay = new Delay();
private static java.util.Scanner scanner;
private static float lowPulse = 1000;
private static float highPulse = 2000;


public SG90_App() {
super();

}


/**
* <p>main.</p>
*
* @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");

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

console = new Console();
Context pi4j = Pi4J.newAutoContext();
console.title("<-- The Pi4J V5 Project Extension -->", "SG90_App");
String helpString = " Parms: -c channel -q quit -low microseconds -high microseconds -h HELP";


for (int i = 0; i < args.length; i++) {
String o = args[i];
if (o.contentEquals("-low")) {
Comment thread
taartspi marked this conversation as resolved.
Outdated
String a = args[i + 1];
lowPulse = Integer.parseInt(a.substring(0));
Comment thread
taartspi marked this conversation as resolved.
Outdated
i++;
} else if (o.contentEquals("-high")) {
String a = args[i + 1];
highPulse = Integer.parseInt(a.substring(0));
i++;
} else if (o.contentEquals("-c")) {
String a = args[i + 1];
channel = Integer.parseInt(a.substring(0));
i++;
}else if (o.contentEquals("-q")) {
Comment thread
taartspi marked this conversation as resolved.
Outdated
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(42);
}
}

Pwm pwm = createPwm(SERVO_NUMBER, channel, pi4j);

sg90 = new SG90Driver(pwm, lowPulse, highPulse);

waitChange( 10L);

pwm.close();

}

static void waitChange(long c){
while(true){
delay.setMillis(c).materialize();
console.println("Enter degree value or enter any key to quit");
if (scanner.hasNextInt() ) {
float nextDegree = scanner.nextFloat();
if((nextDegree < 0) || (nextDegree > 180)){
console.println(" degree must be in the range 0..180");
}else {
sg90.setServoAngle(nextDegree);
}
} else{
break;
}
}
}


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

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

return pi4j.create (config);
}

}
Loading