1757WestwoodRobotics / 2022-RapidReact

Competition Bot for FRC 2022 Season
MIT License
5 stars 1 forks source link

Use separate classes for sim/real instead of using branches #86

Closed virtuald closed 2 years ago

virtuald commented 2 years ago

For example in Falcon:

    def neutralOutput(self) -> None:
        if RobotBase.isReal():
            self.motor.neutralOutput()
        else:
            self.setSpeed(0)

Function calls in python are relatively expensive compared to other code execution, so prefer to lift them out so that they're executed less frequently (or only once if their output isn't going to change, such as the case here):

    if RobotBase.isReal():
        def neutralOutput(self) -> None:
            self.motor.neutralOutput()
    else:
        def neutralOutput(self) -> None:
            self.setSpeed(0)

Alternatively, use completely different classes:

if RobotBase.isReal():
    class Falcon:

        ...

        def neutralOutput(self) -> None:
            self.motor.neutralOutput()
else:

    class Falcon:

        ...

        def neutralOutput(self) -> None:
            self.setSpeed(0)
lmaxwell24 commented 2 years ago

Resolved with #88