From 61f6c1897e45a1950083d37cfad4fcd2fffa68dd Mon Sep 17 00:00:00 2001 From: Aidan Heller Date: Sun, 26 Aug 2018 11:31:00 -0700 Subject: [PATCH] Add ability to use subsystems as robot class constructor arguments (#53) --- core/src/main/kotlin/org/sertain/Robot.kt | 5 ++++- core/src/main/kotlin/org/sertain/command/Subsystem.kt | 6 ------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/core/src/main/kotlin/org/sertain/Robot.kt b/core/src/main/kotlin/org/sertain/Robot.kt index 250ce05..6d78c5c 100644 --- a/core/src/main/kotlin/org/sertain/Robot.kt +++ b/core/src/main/kotlin/org/sertain/Robot.kt @@ -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 @@ -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) { @@ -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:") diff --git a/core/src/main/kotlin/org/sertain/command/Subsystem.kt b/core/src/main/kotlin/org/sertain/command/Subsystem.kt index 0ca5f73..4d678db 100644 --- a/core/src/main/kotlin/org/sertain/command/Subsystem.kt +++ b/core/src/main/kotlin/org/sertain/command/Subsystem.kt @@ -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 @@ -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) }