Skip to content

Commit

Permalink
Servo driver branch (#1)
Browse files Browse the repository at this point in the history
* - added ServoDriver

* - added a functional implementation of diozero and pi4j for servo hats

* - got the servo controller to work

* - finished RPiBotLib v1.1.0
  • Loading branch information
Ghozti authored Aug 20, 2022
1 parent 21db9d8 commit 53b144d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ allprojects {

group = 'com.github.Ghozti'

version = 'v1.0.0'
version = 'v1.1.0'

ext {
appName = "RPiBotLib"
Expand All @@ -37,6 +37,7 @@ allprojects {
mavenLocal()
mavenCentral()
google()
maven { url 'https://jitpack.io' }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
Expand All @@ -53,6 +54,7 @@ project(":desktop") {
implementation 'com.pi4j:pi4j-core:2.1.1'
implementation 'com.pi4j:pi4j-plugin-raspberrypi:2.1.1'
implementation 'com.pi4j:pi4j-plugin-pigpio:2.1.1'
implementation group: 'com.diozero', name: 'diozero-core', version: '1.3.4'
api "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
Expand All @@ -76,6 +78,7 @@ project(":core") {
implementation 'com.pi4j:pi4j-core:2.1.1'
implementation 'com.pi4j:pi4j-plugin-raspberrypi:2.1.1'
implementation 'com.pi4j:pi4j-plugin-pigpio:2.1.1'
implementation group: 'com.diozero', name: 'diozero-core', version: '1.3.4'
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
api "com.badlogicgames.gdx-controllers:gdx-controllers-core:$gdxControllersVersion"
Expand Down
4 changes: 3 additions & 1 deletion core/src/Robot/Robot.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package Robot;

import com.pi4j.context.Context;
import pibotlib.lib.addons.TimedRobotBase;
import pibotlib.lib.gamecontrollers.LocalXboxController;

public class Robot extends TimedRobotBase {

Context context;

public Robot(){

}
Expand Down
82 changes: 82 additions & 0 deletions core/src/pibotlib/lib/addons/hats/ServoDriver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package pibotlib.lib.addons.hats;

import java.util.ArrayList;
import com.diozero.api.I2CConstants;
import com.diozero.api.ServoDevice;
import com.diozero.api.ServoTrim;
import com.diozero.devices.PCA9685;

public class ServoDriver {

private int maxServos = 16;
private PCA9685 pca9685;
private ArrayList<ServoDevice> servos = new ArrayList<>();

public ServoDriver(int constant, int address, int pwmFrequency){
try {
pca9685 = new PCA9685(constant,address,pwmFrequency);
}catch (Exception e){
System.out.println("Servo device instantiation failed");
e.printStackTrace();
}
}

public ServoDriver(){
try {
pca9685 = new PCA9685(I2CConstants.CONTROLLER_1,0x40,50);
}catch (Exception e){
System.out.println("Servo device instantiation failed");
e.printStackTrace();
}
}

public void addServo(int gpio, ServoTrim trim){
if (servos.size() >= maxServos){
System.out.println("servo limit is 12");
return;
}else {
try {
servos.add(ServoDevice.newBuilder(gpio).setDeviceFactory(pca9685).setTrim(trim).build());
}catch (Exception e){
System.out.println("Servo init failed");
e.printStackTrace();
}
}
}

public void addAllServos(ServoTrim trim){
if (servos.size() >= 12){
System.out.println("servo limit is 12");
return;
}else {
try {
for (int i = 0; i < maxServos; i++){
servos.add(ServoDevice.newBuilder(i).setDeviceFactory(pca9685).setTrim(trim).build());
}
}catch (Exception e){
System.out.println("Servo init failed");
e.printStackTrace();
}
}
}

public ServoDevice getServo(int ID){
return servos.get(ID);
}

public void setServoMin(int ID){
servos.get(ID).min();
}

public void setServoMid(int ID){
servos.get(ID).mid();
}

public void setServoMax(int ID){
servos.get(ID).max();
}

public void setServoAngle(int ID, float angle){
servos.get(ID).setAngle(angle);
}
}
2 changes: 1 addition & 1 deletion core/src/pibotlib/lib/constants/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class Constants {

public static class LibConstants {
public final static String LIB_VERSION = "1.0.0";
public final static String LIB_VERSION = "1.1.0";
}

public static class Graphical {
Expand Down
16 changes: 16 additions & 0 deletions core/src/pibotlib/lib/gamecontrollers/LocalXboxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,20 @@ public synchronized boolean getX(){
public synchronized boolean getY(){
return controller.getButton(controller.getMapping().buttonY);
}

/**
*returns a boolean if the D pad up button is pressed*/
public synchronized boolean getDPadUp(){return controller.getButton(controller.getMapping().buttonDpadUp);}

/**
*returns a boolean if the D pad down button is pressed*/
public synchronized boolean getDPadDown(){return controller.getButton(controller.getMapping().buttonDpadDown);}

/**
*returns a boolean if the D pad left button is pressed*/
public synchronized boolean getDPadLeft(){return controller.getButton(controller.getMapping().buttonDpadLeft);}

/**
*returns a boolean if the D pad right button is pressed*/
public synchronized boolean getDPadRight(){return controller.getButton(controller.getMapping().buttonDpadRight);}
}
Binary file added jars/desktop-v1.0.0.jar
Binary file not shown.

0 comments on commit 53b144d

Please sign in to comment.