Skip to content

Commit

Permalink
Merge pull request #2 from nobody-particular/main
Browse files Browse the repository at this point in the history
Added driver
  • Loading branch information
madacker authored Oct 30, 2024
2 parents 0ced89d + 1baeb77 commit d031250
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# Header

A description of the repository goes here.
# Description:
This repository contains a driver for integrating the [SparkFun Ultrasonic Distance Sensor - TCT40 (Qwiic)](https://www.sparkfun.com/products/24805), an I2C device, into the FTC SDK. You may want to use this sensor because, as of 2024, the walls of the competition area are clear, and so a conventional laser distance sensor would not be able to detect the wall.

# Installation:
## Step 1:
Put instructions here:
First, go to your `build.gradle` file and go to `repositories`. Add `maven { url "https://jitpack.io" }` as a repository. (If it's already there, don't do anything.)
```Java
repositories {

// Your other stuff here...
maven { url "https://jitpack.io" }
}
```

## Step 2:
Then more instructions here:
Then, go to `dependencies` in the same `build.gradle` file. Add `com.github.SwerveRobotics:UltrasonicDistanceSensor:0.0.1` as a dependency.
```Java
dependencies {

// Your other stuff here...

This comment has been minimized.

Copy link
@madacker

madacker Oct 30, 2024

Author Contributor

It's actually com.github.SwerveRobotics:SparkFunUltrasonicDriver, not UltrasonicDistanceSensor.

implementation 'com.github.SwerveRobotics:UltrasonicDistanceSensor:0.0.1'
}
```

## Step 3:
Last instructions here.
Now, you may type `import org.swerverobotics.ftc.UltrasonicDistanceSensor` in one of your Java class files to import the driver!
44 changes: 42 additions & 2 deletions src/main/java/org/swerverobotics/ftc/UltrasonicDistanceSensor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package org.swerverobotics.ftc;

public class UltrasonicDistanceSensor {
import com.qualcomm.robotcore.hardware.I2cAddr;
import com.qualcomm.robotcore.hardware.I2cDeviceSynchDevice;
import com.qualcomm.robotcore.hardware.I2cDeviceSynchSimple;
import com.qualcomm.robotcore.hardware.configuration.annotations.DeviceProperties;
import com.qualcomm.robotcore.hardware.configuration.annotations.I2cDeviceType;

}
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;

@I2cDeviceType()
@DeviceProperties(name = "SparkFun Ultrasonic Distance Sensor", description = "QWIIC Ultrasonic Distance Sensor TCT40", xmlTag = "QWIIC_ULTRASONIC_DISTANCE_SENSOR")
public class UltrasonicDistanceSensor extends I2cDeviceSynchDevice<I2cDeviceSynchSimple> {
final int TRIGGER_AND_READ_REGISTER = 0x01;

public double getDistance(DistanceUnit distanceUnit) {
byte[] rawData = deviceClient.read(TRIGGER_AND_READ_REGISTER, 2);
int distance = ((rawData[0] & 0xff) << 8) | (rawData[1] & 0xff);
return distanceUnit.fromUnit(DistanceUnit.MM, distance);
}

@Override
protected boolean doInitialize() {
return true;
}

@Override
public Manufacturer getManufacturer() {
return Manufacturer.SparkFun;
}

@Override
public String getDeviceName() {
return "SparkFun Ultrasonic Distance Sensor";
}

private final static I2cAddr ADDRESS_I2C_DEFAULT = I2cAddr.create7bit(0x2F);

public UltrasonicDistanceSensor(I2cDeviceSynchSimple deviceClient, boolean deviceClientIsOwned) {
super(deviceClient, deviceClientIsOwned);

this.deviceClient.setI2cAddress(ADDRESS_I2C_DEFAULT);
super.registerArmingStateCallback(false);
}
}

0 comments on commit d031250

Please sign in to comment.