Kotlin Default Commands Not Running

Hello all!

As the title implies, a project I’ve been working on has had some immediate success in most areas, although there is one major bug that has stumped me; my default drive command.

First, just to reiterate, I am using command-based, and have previously used Python, so the Gradle project is still relatively new to me. I have looked at several examples in Kotlin and Java however, and I thought I was doing it correctly. My code in my subsystem looks like this:

init {
        defaultCommand = DriveCommand()
    }

If this is not this simple, I would certainly not be surprised. My command looks like this:

package frc.team2539.robot.commands.drivetrain

import frc.team2539.robot.subsystems.DriveTrain

import frc.team2539.robot.Controls
import frc.team2539.robot.cougartools.CougarCommand

class DriveCommand : CougarCommand() {

    init {
        addRequirements(DriveTrain)
    }

    override fun initialize() {
        DriveTrain.stop()
    }

    override fun execute() {
        print("Driving")
        DriveTrain.move(
            y = Controls.driverController.getRawAxis(1),
            rotate = Controls.driverController.getRawAxis(3)
        )
    }

    override fun end(interrupted: Boolean) {
        DriveTrain.stop()
    }

}

Please note that it appears that only the default commands are not functioning properly. Other commands I run off of buttons work. Also, due to the current health crisis, I have very limited access to my buildsite, and when I am there, the window of opportunity is rather small.

Again, any insights onto why this would not be executing this default command would be great. My Robot.kt and Main.kt can be found at my github, here.

Thanks!

I had just limited time to look at your code, and I’m only sort of familiar with Kotlin, but where is the instance of the Subsystem being constructed?

In Java and the new CommandBased framework, all that is done in the RobotContainer init method.

I’m looking at DriveTrain.kt and the last change to it is 9 days ago. I’m going to assume you have it updated with the defaultCommand = DriveTrain that you had in your post.

It looks like he’s using a Kotlin object, which basically makes it a singleton. I believe Kotlin does lazy initialization, so the first time DriveTrain is referenced it will be initialized.

I’m going to take a wild guess and say that your DriveTrain object is never initialized so the default command is never set. I haven’t looked though all your code, so I may be wrong here.

I know you had problems getting the simulation running in the other thread. If you have any new updates on that you could try bumping that thread. I think I remember that it worked when you manually ran gradlew.bat simulateJava or ./gradlew simulateJava, but maybe not when doing it in VS Code?

Yes, Kotlin objects act as a singleton and are initialized by reference, as it is lazy. You very well might be right, and I will get back to you as soon as possible!

And my apologies. I thought I had updated it. If you’re interested, I am pushing it in the next few minutes. Thanks again, and I’ll respond with more information.

I should’ve mentioned: drive base doesn’t contain that exact line of code. Instead it inherits from a class, known as Basedrive.kt. That code has the default command. My mistake, and my apologies.