robotpy / robotpy-wpilib-utilities

Useful utility functions/objects for RobotPy
BSD 3-Clause "New" or "Revised" License
11 stars 20 forks source link

MagicRobot does not execute setup or execute after injection #166

Closed boranseckin closed 4 years ago

boranseckin commented 4 years ago

While using MagicRobot, createObjects() at robot.py initializes all the objects and the injection works correctly. I can access modules from swervedrive.py. However, after the injection, setup() and execute() do not get called. When running the simulation, I can only get init printed.

I cannot get the excepted behaviour that is explained in the docs.

robot.py:

#!/usr/bin/env python3

import wpilib

from magicbot import MagicRobot
from components import swervedrive, swervemodule

class MyRobot(MagicRobot):
    drive = swervedrive.SwerveDrive

    frontLeftModule = swervemodule.SwerveModule
    frontRightModule = swervemodule.SwerveModule
    rearLeftModule = swervemodule.SwerveModule
    rearRightModule = swervemodule.SwerveModule

    def createObjects(self):
        self.gamempad = wpilib.Joystick(2)

        self.frontLeftMotor = wpilib.PWMVictorSPX(0)
        self.frontRightMotor = wpilib.PWMVictorSPX(1)
        self.rearLeftMotor = wpilib.PWMVictorSPX(2)
        self.rearRightMotor = wpilib.PWMVictorSPX(3)

        self.frontLeftRotate = wpilib.PWMVictorSPX(4)
        self.frontRightRotate = wpilib.PWMVictorSPX(5)
        self.rearLeftRotate = wpilib.PWMVictorSPX(6)
        self.rearRightRotate = wpilib.PWMVictorSPX(7)

        self.frontLeftModule = swervemodule.SwerveModule(self.frontLeftMotor, self.frontLeftRotate)
        self.frontRightModule = swervemodule.SwerveModule(self.frontRightMotor, self.frontRightRotate)
        self.rearLeftModule = swervemodule.SwerveModule(self.rearLeftMotor, self.rearLeftRotate)
        self.rearRightModule = swervemodule.SwerveModule(self.rearRightMotor, self.rearRightRotate)

        self.drive = swervedrive.SwerveDrive()

    def teleopPeriodic(self):
        pass

if __name__ == "__main__":
    wpilib.run(MyRobot)

components/swervedrive.py:

import wpilib

from magicbot import magiccomponent
from components import swervemodule

class SwerveDrive:
    frontLeftModule = swervemodule.SwerveModule
    frontRightModule = swervemodule.SwerveModule
    rearLeftModule = swervemodule.SwerveModule
    rearRightModule = swervemodule.SwerveModule

    def __init__(self):
        print('init')

    def setup(self):
        print('setup')

    def execute(self):
        print('execute')

components/swervemodule.py

import wpilib

class SwerveModule:
    def __init__(self, driveMotor, rotateMotor):
        self.driveMotor = driveMotor
        self.rotateMotor = rotateMotor

    def execute(self):
        print('module exe')
virtuald commented 4 years ago

The docs are out of date, you need to use variable annotations instead of equal signs.

https://github.com/robotpy/examples/blob/39b3ad42c2bebad1a1904293a67555c853c4595a/magicbot-simple/robot.py#L16

virtuald commented 4 years ago

Also, for creating multiple objects of the same type, see https://robotpy.readthedocs.io/en/stable/frameworks/magicbot.html#variable-injection starting at "Sometimes, it’s useful to use multiple instances of the same class"