From e249322af080bdbba19d853b5e5caadd7cd14336 Mon Sep 17 00:00:00 2001 From: Android Date: Sat, 11 Jan 2020 18:07:59 -0600 Subject: [PATCH] code --- Physics.py | 19 +++++++++++ SimRobot.py | 75 +++++++++++++++++++++++++++++++++++++++++ autonomous/Sim_Field.py | 52 ++++++++++++++++++++++++++++ sim/config.json | 26 ++++++++++++++ tests/pyfrc_test.py | 6 ++++ 5 files changed, 178 insertions(+) create mode 100644 Physics.py create mode 100644 SimRobot.py create mode 100644 autonomous/Sim_Field.py create mode 100644 sim/config.json create mode 100644 tests/pyfrc_test.py diff --git a/Physics.py b/Physics.py new file mode 100644 index 0000000..c4762d4 --- /dev/null +++ b/Physics.py @@ -0,0 +1,19 @@ +from pyfrc.physics import drivetrains + + +class PhysicsEngine: + + def __init__(self, physics_controller): + self.physics_controller = physics_controller + self.drive = drivetrains.FourMotorDrivetrain() + + def update_sim(self, hal_data, now, tm_diff): + lr_motor = hal_data["pwm"][1]["value"] + rr_motor = hal_data["pwm"][2]["value"] + lf_motor = hal_data["pwm"][3]["value"] + rf_motor = hal_data["pwm"][4]["value"] + + speed, rotation = self.drive.get_vector(lr_motor, rr_motor, lf_motor, rf_motor) + self.physics_controller.drive(speed, rotation, tm_diff) + + diff --git a/SimRobot.py b/SimRobot.py new file mode 100644 index 0000000..a50c6bd --- /dev/null +++ b/SimRobot.py @@ -0,0 +1,75 @@ +# Imports the packages so they can be used to run the robot. You can import a package from a package to avoid having +# to type in a lengthy string such as wpilib.drive.DifferentialDrive. +import wpilib +from wpilib.drive import DifferentialDrive +import robotpy_ext.autonomous +# ctre lets you use the can bus on the RoboRio. +import ctre + + +class MyRobot(wpilib.SampleRobot): + + # Defines the channels on the RoboRio that the motors are plugged into. There can be up to eight. + FLChannel = 3 + FRChannel = 4 + RLChannel = 1 + RRChannel = 2 + + # Defines the order that the sticks that are plugged in are assigned. + DriveStickChannel = 0 + # ExtraStickChannel = 1 + + def robotInit(self): + + # Launches the camera server so that we can have video through any cameras on the robot. + wpilib.CameraServer.launch() + + # Defines the motors that will actually be on the robot for use in the drive function. + self.FLMotor = wpilib.Spark(self.FLChannel) + self.FRMotor = wpilib.Spark(self.FRChannel) + self.RLMotor = wpilib.Spark(self.RLChannel) + self.RRMotor = wpilib.Spark(self.RRChannel) + + # Puts the motors into groups so that they fit the parameters of the function. + self.LMG = wpilib.SpeedControllerGroup(self.FLMotor, self.RLMotor) + self.RMG = wpilib.SpeedControllerGroup(self.FRMotor, self.RRMotor) + + # The drive function that tells the computer what kind of drive to use and where the motors are. + self.drive = DifferentialDrive(self.LMG, self.RMG) + + # Tells the computer how long to wait without input to turn off the motors + self.drive.setExpiration(0.1) + + # Defines the Joystick that we will be using for driving. + self.DriveStick = wpilib.Joystick(self.DriveStickChannel) + + self.components = {"drive": self.drive} + + self.automodes = robotpy_ext.autonomous.AutonomousModeSelector("autonomous", self.components) + + def autonomous(self): + self.automodes.run() + + def operatorControl(self): + + # Enables the safety on the drive. Very important. DO NOT FORGET! + self.drive.setSafetyEnabled(True) + + # Checks to see if the robot is activated and that operator control is active, so your robot does not move + # when it is not supposed to. + while self.isOperatorControl() and self.isEnabled(): + + # drives the robot with the arcade drive, which uses one joystick and is a bit easier to use. + # It is a part of DifferentialDrive + self.drive.arcadeDrive( + self.DriveStick.getY(), + -self.DriveStick.getX(), + squareInputs=False + ) + wpilib.Timer.delay(0.005) + # Keeps the robot from constantly bombarding your computer for inputs, saving some CPU time for changing + # control modes or turning off the robot + + +if __name__ == '__main__': + wpilib.run(MyRobot) \ No newline at end of file diff --git a/autonomous/Sim_Field.py b/autonomous/Sim_Field.py new file mode 100644 index 0000000..57e258e --- /dev/null +++ b/autonomous/Sim_Field.py @@ -0,0 +1,52 @@ +from robotpy_ext.autonomous import StatefulAutonomous, timed_state +import wpilib +from wpilib.drive import DifferentialDrive + +class SimField(StatefulAutonomous): + + MODE_NAME = "Sim_Field" + + def initialize(self): + + + def drive_backwards(self): + self.drive.setSafetyEnabled(True) + clock = wpilib.Timer() + clock.start() + + while self.isAutonomous() and self.isEnabled(): + if clock.get() < 0.5: + forward = 0 + side = 0.7 + elif clock.get() < 4.5: + forward = -1 + side = -0.2 + elif clock.get() < 5.5: + forward = -0.5 + side = -0.45 + elif clock.get() < 6.5: + forward = -0.95 + side = -0.05 + elif clock.get() < 7.5: + forward = -1 + side = 0 + elif clock.get() < 11.5: + forward = -0.7 + side = 0.25 + elif clock.get() < 12.5: + forward = -0.9 + side = 0.1 + elif clock.get() < 15: + forward = -1 + side = 0 + else: + forward = 0 + side = 0 + + self.drive.arcadeDrive( + forward, + side, + squareInputs=False + ) + + @timed_state(duration=15, next_state="none", first=False) \ No newline at end of file diff --git a/sim/config.json b/sim/config.json new file mode 100644 index 0000000..2a126d5 --- /dev/null +++ b/sim/config.json @@ -0,0 +1,26 @@ +{ + "pyfrc": { + + "robot": { + "w": 2, + "h": 3, + "starting_x": 2, + "starting_y": 20, + "starting_angle": 0 + }, + + "field": { + "w": 25, + "h": 27, + "px_per_ft": 10, + + "objects": [ + { "color": "grey", + "points": [ [7.25,11.21], [10.333,11.21], [10.333,27], [7.25,27] ] }, + + { "color": "grey", + "points": [ [16.83, 0], [19.90, 0], [19.90, 15.75], [16.83, 15.75] ] } + ] + } + } +} \ No newline at end of file diff --git a/tests/pyfrc_test.py b/tests/pyfrc_test.py new file mode 100644 index 0000000..90ae673 --- /dev/null +++ b/tests/pyfrc_test.py @@ -0,0 +1,6 @@ +''' + This test module imports tests that come with pyfrc, and can be used + to test basic functionality of just about any robot. +''' + +from pyfrc.tests import *