|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) FIRST and other WPILib contributors. |
| 4 | +# Open Source Software; you can modify and/or share it under the terms of |
| 5 | +# the WPILib BSD license file in the root directory of this project. |
| 6 | +# |
| 7 | + |
| 8 | +# |
| 9 | +# Example that shows how to connect to a XRP from RobotPy |
| 10 | +# |
| 11 | +# Requirements |
| 12 | +# ------------ |
| 13 | +# |
| 14 | +# You must have the robotpy-xrp package installed. This is best done via: |
| 15 | +# |
| 16 | +# # Windows |
| 17 | +# py -3 -m pip install robotpy[commands2,sim,xrp] |
| 18 | +# |
| 19 | +# # Linux/macOS |
| 20 | +# pip3 install robotpy[commands2,sim,xrp] |
| 21 | +# |
| 22 | +# Run the program |
| 23 | +# --------------- |
| 24 | +# |
| 25 | +# To run the program you will need to explicitly use the XRP option: |
| 26 | +# |
| 27 | +# # Windows |
| 28 | +# py -3 robotpy sim --xrp |
| 29 | +# |
| 30 | +# # Linux/macOS |
| 31 | +# python robotpy sim --xrp |
| 32 | +# |
| 33 | +# By default the WPILib simulation GUI will be displayed. To disable the display |
| 34 | +# you can add the --nogui option |
| 35 | +# |
| 36 | + |
| 37 | +import os |
| 38 | +import typing |
| 39 | + |
| 40 | +import wpilib |
| 41 | +import commands2 |
| 42 | + |
| 43 | +from robotcontainer import RobotContainer |
| 44 | + |
| 45 | +# If your XRP isn't at the default address, set that here |
| 46 | +os.environ["HALSIMXRP_HOST"] = "192.168.42.1" |
| 47 | +os.environ["HALSIMXRP_PORT"] = "3540" |
| 48 | + |
| 49 | + |
| 50 | +class MyRobot(commands2.TimedCommandRobot): |
| 51 | + """ |
| 52 | + Command v2 robots are encouraged to inherit from TimedCommandRobot, which |
| 53 | + has an implementation of robotPeriodic which runs the scheduler for you |
| 54 | + """ |
| 55 | + |
| 56 | + autonomousCommand: typing.Optional[commands2.Command] = None |
| 57 | + |
| 58 | + def robotInit(self) -> None: |
| 59 | + """ |
| 60 | + This function is run when the robot is first started up and should be used for any |
| 61 | + initialization code. |
| 62 | + """ |
| 63 | + |
| 64 | + # Instantiate our RobotContainer. This will perform all our button bindings, and put our |
| 65 | + # autonomous chooser on the dashboard. |
| 66 | + self.container = RobotContainer() |
| 67 | + |
| 68 | + def disabledInit(self) -> None: |
| 69 | + """This function is called once each time the robot enters Disabled mode.""" |
| 70 | + |
| 71 | + def disabledPeriodic(self) -> None: |
| 72 | + """This function is called periodically when disabled""" |
| 73 | + |
| 74 | + def autonomousInit(self) -> None: |
| 75 | + """This autonomous runs the autonomous command selected by your RobotContainer class.""" |
| 76 | + self.autonomousCommand = self.container.getAutonomousCommand() |
| 77 | + |
| 78 | + if self.autonomousCommand: |
| 79 | + self.autonomousCommand.schedule() |
| 80 | + |
| 81 | + def autonomousPeriodic(self) -> None: |
| 82 | + """This function is called periodically during autonomous""" |
| 83 | + |
| 84 | + def teleopInit(self) -> None: |
| 85 | + # This makes sure that the autonomous stops running when |
| 86 | + # teleop starts running. If you want the autonomous to |
| 87 | + # continue until interrupted by another command, remove |
| 88 | + # this line or comment it out. |
| 89 | + if self.autonomousCommand: |
| 90 | + self.autonomousCommand.cancel() |
| 91 | + |
| 92 | + def teleopPeriodic(self) -> None: |
| 93 | + """This function is called periodically during operator control""" |
| 94 | + |
| 95 | + def testInit(self) -> None: |
| 96 | + # Cancels all running commands at the start of test mode |
| 97 | + commands2.CommandScheduler.getInstance().cancelAll() |
0 commit comments