Swerve Command Schedule Function returns "bad function call"

While working on swerve for the next season, I followed this guide and converted the code from java to python as that is what our team is using. When swapping to autonomous mode in sim, the terminal returns this very unspecific error

Robotics\Swerve2024\robot.py", line 65, in autonomousInit
    self.autonomousCommand.schedule()
RuntimeError: bad function call

which comes from robot.py

def autonomousInit(self) -> None:
        """This autonomous runs the autonomous command selected by your RobotContainer class."""
        #~ will be needed for future use
        self.autonomousCommand = self.Container.getAutonomousCommand()
        print("====================Starting autonomous...====================")
        print(self.autonomousCommand, type(self.autonomousCommand))
        # self.output("ato com", self.autonomousCommand)
       
        if self.autonomousCommand:
           self.autonomousCommand.schedule() <<<--

and robotcontainer.py (getAutonomousCommand)

def getAutonomousCommand(self) -> commands2.Command:
        """Returns the autonomous command to run"""

        # 1. Create Trajectory settings
        self.trajectoryConfig = TrajectoryConfig(
            AutoConstants.kMaxSpeedMetersPerSecond,
            AutoConstants.kMaxAccelerationMetersPerSecondSquared)
        self.trajectoryConfig.setKinematics(RobotConstants.kDriveKinematics)
        print(self.trajectoryConfig)
        # 2. Generate Trajectory
        self.trajectory = TrajectoryGenerator.generateTrajectory(
            # ? initial location and rotation
            Pose2d(0, 0, Rotation2d(0)),
            [
                # ? points we want to hit
                Translation2d(1, 0),
                Translation2d(1, 1),
                Translation2d(0, 1),
            ],
            # ? final location and rotation
            Pose2d(0, 0, Rotation2d(180)),
            self.trajectoryConfig
        )

        # 3. Create PIdControllers to correct and track trajectory
        self.xController = PIDController(AutoConstants.kPXController, 0, 0)
        self.yController = PIDController(AutoConstants.kPYController, 0, 0)
        self.thetaController = ProfiledPIDControllerRadians(
            AutoConstants.kPThetaController, 0, 0, AutoConstants.kThetaControllerConstraints)
        self.thetaController.enableContinuousInput(-math.pi, math.pi)

        # 4. Construct command to follow trajectory
        self.swerveControllerCommand = Swerve4ControllerCommand(
            self.trajectory,
            self.swerve.getPose,
            RobotConstants.kDriveKinematics,
            self.xController,
            self.yController,
            self.thetaController,
            self.swerve.setModuleStates,
            [self.swerve]
        )
        # 5. Add some init and wrap up, and return command 
        self.square = commands2.SequentialCommandGroup(
            commands2.InstantCommand(self.swerve.resetOdometry(self.trajectory.initialPose())),
            self.swerveControllerCommand,
            commands2.InstantCommand(self.swerve.stopModules())
        )
        return self.square

Am I missing something about the command scheduler? The github is here

Any help is greatly appreciated.

You need lambda: s on line 131 and 133. I recommend using a type checker like Pylance to type check your code.

1 Like

thank you so much, it works now!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.