Skip to content

Commit

Permalink
feat: add kotlin example
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Nov 18, 2024
1 parent ccca65b commit 36eefd5
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 15 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
## About

The Hydrozoa Java SDK is a software library that allows Java programs to access the peripherals on VEX V5 robots. It
The Hydrozoa Java SDK is a software library that allows Java and Kotlin programs to access the peripherals on VEX V5
robots. It
also includes a Gradle plugin to make it easier to deploy Hydrozoa programs to robots.

## Running Examples
Expand All @@ -14,3 +15,9 @@ Run the following command with a VEX V5 robot connected via USB.
```shell
./gradlew examples:clawbot:upload
```

Other examples can be run similarly:

```shell
./gradlew examples:clawbot-kotlin:upload
```
4 changes: 4 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ subprojects {
// Add Hydrozoa SDK from project root
implementation(project(":"))
}

hydrozoa {
runtime = project.parent!!.parent!!.layout.projectDirectory.file("runtime.bin")
}
}
11 changes: 11 additions & 0 deletions examples/clawbot-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
kotlin("jvm") version "1.9.20"
}

hydrozoa {
entrypoint = "dev.vexide.hydrozoa.clawbotkt.Main"
}

kotlin {
jvmToolchain(21)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@file:JvmName("Main")

package dev.vexide.hydrozoa.clawbotkt

import dev.vexide.hydrozoa.CompetitionRuntime

fun main() {
CompetitionRuntime.start(::Robot)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.vexide.hydrozoa.clawbotkt

import dev.vexide.hydrozoa.CompetitionRobot
import dev.vexide.hydrozoa.Controller
import dev.vexide.hydrozoa.Peripherals
import dev.vexide.hydrozoa.devices.DeviceException
import dev.vexide.hydrozoa.devices.smart.Motor

class Robot(peripherals: Peripherals) : CompetitionRobot {
private val leftMotor = Motor(peripherals.takePort(2), Motor.Gearset.Green, Motor.Direction.Forward)
private val rightMotor = Motor(peripherals.takePort(8), Motor.Gearset.Green, Motor.Direction.Reverse)

private val controller = peripherals.takeController(Controller.Id.Primary)

override fun driverPeriodic() {
val input = controller.state.orElseGet(Controller.State::empty)

try {
val forward = input.leftStick.y
val turn = input.rightStick.x

val leftVoltage = (forward + turn) * Motor.V5_MAX_VOLTAGE
val rightVoltage = (forward - turn) * Motor.V5_MAX_VOLTAGE

leftMotor.setVoltage(leftVoltage)
rightMotor.setVoltage(rightVoltage)
} catch (err: DeviceException) {
throw RuntimeException(err)
}
}
}
1 change: 0 additions & 1 deletion examples/clawbot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
hydrozoa {
entrypoint = "dev.vexide.hydrozoa.clawbot.Main"
runtime = project.parent!!.parent!!.layout.projectDirectory.file("runtime.bin")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dev.vexide.hydrozoa.devices.DeviceException;
import dev.vexide.hydrozoa.devices.smart.Motor;

public class Robot extends CompetitionRobot {
public class Robot implements CompetitionRobot {
private final Motor leftMotor;
private final Motor rightMotor;

Expand Down
5 changes: 5 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
rootProject.name = "vex-java-project"

includeBuild("gradle-plugin")
Expand All @@ -6,3 +9,5 @@ include("vex-sdk")

include("examples:clawbot")
findProject(":examples:clawbot")?.name = "clawbot"
include("examples:clawbot-kotlin")
findProject(":examples:clawbot-kotlin")?.name = "clawbot-kotlin"
42 changes: 30 additions & 12 deletions src/main/java/dev/vexide/hydrozoa/CompetitionRobot.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
package dev.vexide.hydrozoa;

public abstract class CompetitionRobot {
public void connected() {}
public void disconnected() {}
public interface CompetitionRobot {
default void connected() {
}

public void disabledInit() {}
public void disabledPeriodic() {}
public void disabledExit() {}
default void disconnected() {
}

public void autonomousInit() {}
public void autonomousPeriodic() {}
public void autonomousExit() {}
default void disabledInit() {
}

public void driverInit() {}
public void driverPeriodic() {}
public void driverExit() {}
default void disabledPeriodic() {
}

default void disabledExit() {
}

default void autonomousInit() {
}

default void autonomousPeriodic() {
}

default void autonomousExit() {
}

default void driverInit() {
}

default void driverPeriodic() {
}

default void driverExit() {
}
}

0 comments on commit 36eefd5

Please sign in to comment.