Skip to content

Commit

Permalink
Merge pull request #22 from ThePinkAlliance/logging-update
Browse files Browse the repository at this point in the history
Logging update
  • Loading branch information
devsamuelv authored Aug 25, 2023
2 parents 824da1b + 128b5e0 commit 2f8772f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 7 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ https://raw.githubusercontent.com/ThePinkAlliance/core/main/ThePinkAlliance.json

## Linear Interpolation

The linear interpolation class takes a list of integer pairs and will interpolate between the input (the first number in the pair) to the calculated output (the mathematically calculated second number in the pairs). Some use cases for linear interpolation could be using a table of distances give by a limelight to the real distance.
The linear interpolation class takes a list of integer pairs and will interpolate between the input (the first number in the pair) to the calculated output (the mathematically calculated second number in the pairs). Some use cases for linear interpolation may involve using a table of distances give by a limelight to the real distance.

### Example

Expand All @@ -32,15 +32,32 @@ The linear interpolation class takes a list of integer pairs and will interpolat
double real_distance = table.interp(123); // Outputs 128 inches
```

## Telemetry

The purpose of the Telemetery class is to serve as a replacement for SmartDashboard. Over time, the use of smart dashboard can lead to a cluttered dashboard and can make tuning difficult during competitions. The Telemetry class allows tuning data to be stored in its own tab and organized by parent class.

### Example

```java
Telemetry.logData("Motor Output", motor.getPower(), JoystickDrive.class);
Telemetry.logData("x", x, JoystickDrive.class);
```

The code above generates this list which stores all the values being logged for this specific class. Just to clear up some things, how the logger creates list names it starts with the name of the parent class first. After the two colons, `::` it appends what class its extending to the end of the name. The purpose of this makes distinguishing `subsystems` from `commands` easier.

_NOTE: Sometimes the list isn't added to Shuffleboard and may need to be added manually._

![telemetry example](docs/telemetry-example1.png)

## Spherical Coordinates

The spherical coordinates class makes using the spherical coordinate system easier with some builtin utilites. Just as a quick refresher instead of using x, y, and z for units spherical coordinates use projection (r), azimuth (θ), and elevation (Φ). If your not familar with spherical coordinates theres a great article [here](https://mathinsight.org/spherical_coordinates) to help you out.
The spherical coordinates class makes using the spherical coordinate system easier with some built-in utilites. As a quick refresher, instead of using x, y, and z for units, spherical coordinates use projection (r), azimuth (θ), and elevation (Φ). If you're not familar with spherical coordinates theres a great article [here](https://mathinsight.org/spherical_coordinates) to help you out.

### Example

```java
// Creating coordinates with known constants.
double x= 0;
double x = 0;
double y = 0;
double z = 0;

Expand Down
4 changes: 2 additions & 2 deletions ThePinkAlliance.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"fileName": "ThePinkAlliance.json",
"name": "ThePinkAlliance",
"version": "2.3.17",
"version": "2.3.3",
"uuid": "9619F7EA-7F96-4236-9D94-02338DFED572",
"mavenUrls": [
"https://jitpack.io",
Expand All @@ -13,7 +13,7 @@
{
"groupId": "com.github.ThePinkAlliance",
"artifactId": "core",
"version": "2.3.17"
"version": "2.3.3"
}
],
"jniDependencies": [],
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java-library'
apply plugin: 'maven-publish'

group = 'com.ThePinkAlliance.core'
version = '2.3.17'
version = '2.3.3'

sourceCompatibility = JavaVersion.VERSION_11 // java 11
targetCompatibility = JavaVersion.VERSION_11
Expand Down
Binary file added docs/telemetry-example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions src/main/java/com/ThePinkAlliance/core/logging/Telemetry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.ThePinkAlliance.core.logging;

import java.util.HashMap;

import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardContainer;
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardLayout;
import edu.wpi.first.wpilibj.shuffleboard.SimpleWidget;

public class Telemetry {
private static ShuffleboardContainer tab = Shuffleboard.getTab("debug");
private static HashMap<Integer, ShuffleboardLayout> layouts = new HashMap<>();
private static HashMap<String, SimpleWidget> widgets = new HashMap<>();

public Telemetry() {
}

/**
* Logs data to the debug tab on Shuffleboard stored in a list with the name of
* the parent class.
*
* @param title Name of the data being logged.
* @param data The data object being logged. Permitted types: Strings,
* Doubles,
* Integers, and Longs.
* @param parent The parent class object.
*/
public static void logData(String title, Object data, Class<?> parent) {
Class<?> superClass = parent.getSuperclass();
String className = parent.getSimpleName();
String superClassName = superClass != null ? superClass.getSimpleName() : "Unknown";
int id = (className + superClassName).hashCode();
ShuffleboardLayout layout = layouts.get(id);
SimpleWidget currentWidget = widgets.get(title);

if (layout == null) {
layout = tab.getLayout(className + " :: " + superClassName, "List");
}

if (currentWidget == null) {
currentWidget = layout.add(title, serializeDataType(data));
}

if (data instanceof Integer) {
currentWidget.getEntry().setInteger((long) data);
} else if (data instanceof String) {
currentWidget.getEntry().setString((String) data);
} else if (data instanceof Double) {
currentWidget.getEntry().setDouble((double) data);
} else {
currentWidget.getEntry().setString(data.toString());
}

if (layout == null) {
layouts.put(id, layout);
}

widgets.put(title, currentWidget);
}

/**
* Serialize the data type. And convert it into string if it's not a permitted
* data
* type (String, Long, Integer, Double).
*/
private static Object serializeDataType(Object data) {
boolean validType = data instanceof Integer || data instanceof String || data instanceof Double
|| data instanceof Long;

if (!validType) {
return data.toString();
} else {
return data;
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/LinearInterpolationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.List;

import com.ThePinkAlliance.core.logging.Telemetry;
import com.ThePinkAlliance.core.math.LinearInterpolationTable;
import com.ThePinkAlliance.core.math.Vector2d;
import edu.wpi.first.math.Pair;
import org.junit.Test;

Expand Down

0 comments on commit 2f8772f

Please sign in to comment.