Skip to content

Commit

Permalink
Add ability to use subsystems as robot class constructor arguments (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawinganimal authored and andrewda committed Aug 26, 2018
1 parent 14f2470 commit 61f6c18
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
5 changes: 4 additions & 1 deletion core/src/main/kotlin/org/sertain/Robot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package org.sertain
import android.support.annotation.VisibleForTesting
import edu.wpi.first.wpilibj.IterativeRobot
import edu.wpi.first.wpilibj.command.Scheduler
import org.sertain.command.Subsystem

private typealias LifecycleDistributor = RobotLifecycle.Companion.Distributor

Expand Down Expand Up @@ -143,7 +144,7 @@ public interface RobotLifecycle {
}

/** Base robot class which must be used for [RobotLifecycle] callbacks to work. */
public abstract class Robot : IterativeRobot(), RobotLifecycle {
public abstract class Robot(vararg subsystems: Subsystem) : IterativeRobot(), RobotLifecycle {
private var mode = State.DISABLED
set(value) {
if (value != field) {
Expand All @@ -160,6 +161,8 @@ public abstract class Robot : IterativeRobot(), RobotLifecycle {
@Suppress("LeakingThis") // Invoked through reflection and initialized later
RobotLifecycle.rawAddListener(this)

subsystems.forEach { RobotLifecycle.addListener(it) }

val existingHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler { t, e ->
println("Exception occurred on thread $t:")
Expand Down
6 changes: 0 additions & 6 deletions core/src/main/kotlin/org/sertain/command/Subsystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
@file:JvmName("SubsystemUtils")
package org.sertain.command

import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder
import org.sertain.RobotLifecycle
import edu.wpi.first.wpilibj.command.Subsystem as WpiLibSubsystem

Expand All @@ -11,10 +10,5 @@ public abstract class Subsystem : WpiLibSubsystem(), RobotLifecycle {
/** @see edu.wpi.first.wpilibj.command.Subsystem.setDefaultCommand */
open val defaultCommand: CommandBase? = null

override fun initSendable(builder: SendableBuilder?) {
super.initSendable(builder)
RobotLifecycle.addListener(this)
}

override fun initDefaultCommand() = setDefaultCommand(defaultCommand?.mirror)
}

0 comments on commit 61f6c18

Please sign in to comment.