Skip to content

Commit

Permalink
everythings has been updated and works. Lastt stop: PIDDDDDD
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshBhambhani committed Nov 8, 2019
1 parent b319751 commit 251e15c
Show file tree
Hide file tree
Showing 88 changed files with 82,499 additions and 161 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file added 192.168.4.1
Binary file not shown.
Binary file modified MiniLib/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions MiniLib/.classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/pi4j-core.jar"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="lib/pi4j-core.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added MiniLib/src/.DS_Store
Binary file not shown.
Binary file added MiniLib/src/frc/.DS_Store
Binary file not shown.
Binary file added MiniLib/src/frc/team670/.DS_Store
Binary file not shown.
Binary file added MiniLib/src/frc/team670/pi/.DS_Store
Binary file not shown.
134 changes: 134 additions & 0 deletions MiniLib/src/frc/team670/pi/UltrasonicSensor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package frc.team670.pi;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;

/**
* Represents an ultrasonic sensor connected to the pi motor shield
*
* @author ctychen, lakshbhambhani
*
*/
public class UltrasonicSensor extends Thread {

private GpioPinDigitalInput echo;
private GpioPinDigitalOutput trigger;
private final GpioController gpio = GpioFactory.getInstance();
private int boundary;
private long lastRead, startTime, endTime, elapsed, measure;

/**
*
* @param trig Trigger pin for sending signal to put sensor in detecting
* mode
* @param echo Pin that sends signals back
* @param boundary an integer specifying the minimum distance at which the
* sensor will return a Triggered response of True.
*/
public UltrasonicSensor(Pin trig, Pin echo, int boundary) {
this.trigger = gpio.provisionDigitalOutputPin(trig);
this.trigger.setShutdownOptions(true);
this.echo = gpio.provisionDigitalInputPin(echo);
this.echo.setShutdownOptions(true);
this.boundary = boundary;
lastRead = 0;
start();
}

public void run() {
System.out.println("Ultrasonic started");
if (echo.getState() == PinState.LOW) {
startTime = System.currentTimeMillis();
}
if (echo.getState() == PinState.HIGH) {
endTime = System.currentTimeMillis();
}
elapsed = endTime - startTime;
measure = (elapsed * 34300) / 2;
this.lastRead = measure;
if (this.boundary > measure) {
System.out.println("Boundary breached: " + this.boundary + "Measure: " + this.measure);
this.trigger.setState(PinState.HIGH);
} else {
this.trigger.setState(PinState.LOW);
}

}

public boolean isTriggered() {
return (trigger.getState() == PinState.HIGH);
}

}
=======
package frc.team670.pi;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;

/**
* Represents an ultrasonic sensor connected to the pi motor shield
*
* @author ctychen, lakshbhambhani
*
*/
public class UltrasonicSensor extends Thread {

private GpioPinDigitalInput echo;
private GpioPinDigitalOutput trigger;
private final GpioController gpio = GpioFactory.getInstance();
private int boundary;
private long lastRead, startTime, endTime, elapsed, measure;

/**
*
* @param trig Trigger pin for sending signal to put sensor in detecting
* mode
* @param echo Pin that sends signals back
* @param boundary an integer specifying the minimum distance at which the
* sensor will return a Triggered response of True.
*/
public UltrasonicSensor(Pin trig, Pin echo, int boundary) {
this.trigger = gpio.provisionDigitalOutputPin(trig);
this.trigger.setShutdownOptions(true);
this.echo = gpio.provisionDigitalInputPin(echo);
this.echo.setShutdownOptions(true);
this.boundary = boundary;
lastRead = 0;
start();
}

public void run() {
System.out.println("Ultrasonic started");
if (echo.getState() == PinState.LOW) {
startTime = System.currentTimeMillis();
}
if (echo.getState() == PinState.HIGH) {
endTime = System.currentTimeMillis();
}
elapsed = endTime - startTime;
measure = (elapsed * 34300) / 2;
this.lastRead = measure;
if (this.boundary > measure) {
System.out.println("Boundary breached: " + this.boundary + "Measure: " + this.measure);
this.trigger.setState(PinState.HIGH);
} else {
this.trigger.setState(PinState.LOW);
}

}

public boolean isTriggered() {
return (trigger.getState() == PinState.HIGH);
}

}
>>>>>>> 6802b4ec7965e7c8e883777222c318f6d05e200b
Binary file added MiniLib/src/frc/team670/pi/sensors/.DS_Store
Binary file not shown.
129 changes: 129 additions & 0 deletions MiniLib/src/frc/team670/pi/sensors/Encoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package frc.team670.pi.sensors;

import java.util.ArrayList;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import com.pi4j.io.gpio.PinState;

import edu.wpi.first.wpilibj.RobotState;
import frc.team670.robot.RobotConstants;
import jpigpio.JPigpio;
import jpigpio.PigpioException;
import jpigpio.PigpioSocket;

/**
* Represents an encoder connected to the pi
*
* @author ctychen, lakshbhambhani
*
*/
public class Encoder {

private GpioPinDigitalInput leftPin, rightPin;

private int leftPinState;
private int rightPinState;

public int count;

// private final GpioController gpio = GpioFactory.getInstance();

JPigpio pigpio;

private final int[] outcome = { 0, -1, 1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0 };

int last_AB = 0;

/**
* Runs the encoder counting in another thread
*/
public void update() {
// while(RobotState.isEnabled()) {
int A = leftPinState;
int B = rightPinState;
int current_AB = (A << 1) | B;
int position = (last_AB << 2) | current_AB;
count += outcome[position];
last_AB = current_AB;
// }
}

/**
* Creates an encoder on 2 pins which can be used to get data from the motor
*
* @param leftP Pin for the left sensor on the encoder
* @param rightP Pin for the right sensor on the encoder
* @throws PigpioException
*/
public Encoder(int leftP, int rightP) throws PigpioException {
count = 0;

// leftPin = gpio.provisionDigitalInputPin(leftP);
// leftPin.setShutdownOptions(true);
// rightPin = gpio.provisionDigitalInputPin(rightP);
// rightPin.setShutdownOptions(true);
pigpio = new PigpioSocket("127.0.0.1", 8888);
pigpio.gpioSetAlertFunc(leftP, (gpio, level, tick) -> {
// System.out.println(
// String.format("Callback in Java: We received an alert on: %d with %d at %d",
// gpio, level, tick));
if (level < 2) {
leftPinState = level;
update();
}
});
pigpio.gpioSetAlertFunc(rightP, (gpio, level, tick) -> {
// System.out.println(
// String.format("Callback in Java: We received an alert on: %d with %d at %d",
// gpio, level, tick));
if (level < 2) {
rightPinState = level;
update();
}

});

}

/**
* Returns number of ticks the encoder has gone through
*
* @return int Ticks - the number of ticks
*/
public int getTicks() {
return this.count;
}

/**
* Calculates and returns the number of rotations the wheel has gone through
*
* @return Double rotations - the number of rotations the wheel has gone through
*/
public double getRotations() {
return Math.abs(getTicks()) / 800.0;
}

/**
* Calculates and returns the distance for which the motors have rotated
*
* @return distance traveled in centimeters
*/
public double getDistance() {
return (2 * Math.PI * (RobotConstants.DRIVE_BASE_WHEEL_DIAMETER / 2) * getRotations());
}

// public double getVelocityCm() {
// return (RobotConstants.ENCODER_TICKS_PER_ROTATION);
// }
//
// public double getVelocityTicks() {
//
// }

}
Loading

0 comments on commit 251e15c

Please sign in to comment.