Skip to content

Commit f68a8e4

Browse files
committed
reformating/updating to match java examples
1 parent 48c5df6 commit f68a8e4

File tree

7 files changed

+78
-110
lines changed

7 files changed

+78
-110
lines changed

arcade-drive-xbox-controller/robot.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@ class MyRobot(wpilib.TimedRobot):
1818
def robotInit(self):
1919
"""Robot initialization function"""
2020

21-
# create motor controller objects
22-
left = wpilib.PWMSparkMax(0)
23-
right = wpilib.PWMSparkMax(1)
24-
25-
# object that handles basic drive operations
26-
self.robotDrive = wpilib.drive.DifferentialDrive(left, right)
27-
28-
# xbox controller 0 on the driver station
21+
leftMotor = wpilib.PWMSparkMax(0)
22+
rightMotor = wpilib.PWMSparkMax(1)
23+
self.robotDrive = wpilib.drive.DifferentialDrive(leftMotor, rightMotor)
2924
self.driverController = wpilib.XboxController(0)
3025

3126
# We need to invert one side of the drivetrain so that positive voltages
3227
# result in both sides moving forward. Depending on how your robot's
3328
# gearbox is constructed, you might have to invert the left side instead.
34-
right.setInverted(True)
29+
rightMotor.setInverted(True)
3530

3631
def teleopPeriodic(self):
3732
# Drive with split arcade style

arcade-drive/robot.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,21 @@ class MyRobot(wpilib.TimedRobot):
1818
def robotInit(self):
1919
"""Robot initialization function"""
2020

21-
# create motor controller objects
22-
left = wpilib.PWMSparkMax(0)
23-
right = wpilib.PWMSparkMax(1)
24-
25-
# object that handles basic drive operations
26-
self.robotDrive = wpilib.drive.DifferentialDrive(left, right)
27-
28-
# joystick 0 on the driver station
21+
leftMotor = wpilib.PWMSparkMax(0)
22+
rightMotor = wpilib.PWMSparkMax(1)
23+
self.robotDrive = wpilib.drive.DifferentialDrive(leftMotor, rightMotor)
2924
self.stick = wpilib.Joystick(0)
3025

3126
# We need to invert one side of the drivetrain so that positive voltages
3227
# result in both sides moving forward. Depending on how your robot's
3328
# gearbox is constructed, you might have to invert the left side instead.
34-
right.setInverted(True)
29+
rightMotor.setInverted(True)
3530

3631
def teleopPeriodic(self):
37-
# Drive with split arcade style
38-
# That means that the Y axis of the left stick moves forward
39-
# and backward, and the X of the right stick turns left and right.
40-
self.robotDrive.arcadeDrive(-self.stick.getY(), -self.stick.getX())
32+
# Drive with arcade drive.
33+
# That means that the Y axis drives forward
34+
# and backward, and the X turns left and right.
35+
self.robotDrive.arcadeDrive(self.stick.getY(), self.stick.getX())
4136

4237

4338
if __name__ == "__main__":

canpdp/robot.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#
77

88
import wpilib
9-
import wpilib.drive
109

1110

1211
class MyRobot(wpilib.TimedRobot):

gyro/robot.py

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,49 @@
66
#
77

88
import wpilib
9-
from wpilib.drive import DifferentialDrive
9+
import wpilib.drive
1010

1111

1212
class MyRobot(wpilib.TimedRobot):
13-
"""This is a demo program showing how to use Gyro control with the
14-
DifferentialDrive class."""
13+
"""
14+
This is a sample program to demonstrate how to use a gyro sensor to make a robot drive straight.
15+
This program uses a joystick to drive forwards and backwards while the gyro is used for direction
16+
keeping.
17+
"""
1518

16-
def robotInit(self):
17-
"""Robot initialization function"""
18-
gyroChannel = 0 # analog input
19-
20-
self.joystickChannel = 0 # usb number in DriverStation
19+
ANGLE_SETPOINT = 0.0
20+
P = 0.005 # propotional turning constant
2121

22-
# channels for motors
23-
self.leftMotorChannel = 1
24-
self.rightMotorChannel = 0
25-
self.leftRearMotorChannel = 3
26-
self.rightRearMotorChannel = 2
22+
# gyro calibration constant, may need to be adjusted;
23+
# gyro value of 360 is set to correspond to one full revolution
24+
VOLTS_PER_DEGREE_PER_SECOND = 0.0128
2725

28-
self.angleSetpoint = 0.0
29-
self.pGain = 1 # propotional turning constant
26+
LEFT_MOTOR_PORT = 0
27+
RIGHT_MOTOR_PORT = 1
28+
GYRO_PORT = 0
29+
JOYSTICK_PORT = 0
3030

31-
# gyro calibration constant, may need to be adjusted
32-
# gyro value of 360 is set to correspond to one full revolution
33-
self.voltsPerDegreePerSecond = 0.0128
31+
def robotInit(self):
32+
"""Robot initialization function"""
3433

35-
self.left = wpilib.MotorControllerGroup(
36-
wpilib.Talon(self.leftMotorChannel), wpilib.Talon(self.leftRearMotorChannel)
37-
)
38-
self.right = wpilib.MotorControllerGroup(
39-
wpilib.Talon(self.rightMotorChannel),
40-
wpilib.Talon(self.rightRearMotorChannel),
41-
)
42-
self.myRobot = DifferentialDrive(self.left, self.right)
34+
self.leftDrive = wpilib.PWMSparkMax(self.LEFT_MOTOR_PORT)
35+
self.rightDrive = wpilib.PWMSparkMax(self.RIGHT_MOTOR_PORT)
36+
self.myRobot = wpilib.drive.DifferentialDrive(self.leftDrive, self.rightDrive)
37+
self.gyro = wpilib.AnalogGyro(self.GYRO_PORT)
38+
self.joystick = wpilib.Joystick(self.JOYSTICK_PORT)
4339

44-
self.gyro = wpilib.AnalogGyro(gyroChannel)
45-
self.joystick = wpilib.Joystick(self.joystickChannel)
40+
self.gyro.setSensitivity(self.VOLTS_PER_DEGREE_PER_SECOND)
4641

47-
def teleopInit(self):
48-
"""
49-
Runs at the beginning of the teleop period
50-
"""
51-
self.gyro.setSensitivity(
52-
self.voltsPerDegreePerSecond
53-
) # calibrates gyro values to equal degrees
42+
# We need to invert one side of the drivetrain so that positive voltages
43+
# result in both sides moving forward. Depending on how your robot's
44+
# gearbox is constructed, you might have to invert the left side instead.
45+
self.rightDrive.setInverted(True)
5446

5547
def teleopPeriodic(self):
56-
"""
57-
Sets the gyro sensitivity and drives the robot when the joystick is pushed. The
58-
motor speed is set from the joystick while the RobotDrive turning value is assigned
59-
from the error between the setpoint and the gyro angle.
60-
"""
61-
62-
turningValue = (self.angleSetpoint - self.gyro.getAngle()) * self.pGain
63-
if self.joystick.getY() <= 0:
64-
# forwards
65-
self.myRobot.arcadeDrive(self.joystick.getY(), turningValue)
66-
elif self.joystick.getY() > 0:
67-
# backwards
68-
self.myRobot.arcadeDrive(self.joystick.getY(), -turningValue)
48+
# The motor speed is set from the joystick while the DifferentialDrive turning value is assigned
49+
# from the error between the setpoint and the gyro angle.
50+
turningValue = (self.ANGLE_SETPOINT - self.gyro.getAngle()) * self.P
51+
self.myRobot.arcadeDrive(-self.joystick.getY(), -turningValue)
6952

7053

7154
if __name__ == "__main__":

motor-control/robot.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,42 @@
77

88
import wpilib
99

10+
import math
11+
1012

1113
class MyRobot(wpilib.TimedRobot):
1214
"""
13-
This sample program shows how to control a motor using a joystick. In the
14-
operator control part of the program, the joystick is read and the value
15-
is written to the motor.
16-
17-
Joystick analog values range from -1 to 1 and speed controller inputs also
18-
range from -1 to 1 making it easy to work together. The program also delays
19-
a short time in the loop to allow other threads to run. This is generally
20-
a good idea, especially since the joystick values are only transmitted
21-
from the Driver Station once every 20ms.
15+
This sample program shows how to control a motor using a joystick. In the operator control part
16+
of the program, the joystick is read and the value is written to the motor.
17+
18+
<p>Joystick analog values range from -1 to 1 and motor controller inputs also range from -1 to 1
19+
making it easy to work together.
20+
21+
<p>In addition, the encoder value of an encoder connected to ports 0 and 1 is consistently sent
22+
to the Dashboard.
2223
"""
2324

24-
#: update every 0.005 seconds/5 milliseconds (200Hz)
25-
kUpdatePeriod = 0.005
25+
MOTOR_PORT = 0
26+
JOYSTICK_PORT = 0
27+
ENCODER_PORT_A = 0
28+
ENCODER_PORT_B = 1
2629

2730
def robotInit(self):
2831
"""Robot initialization function"""
2932

30-
self.motor = wpilib.Talon(0) # initialize the motor as a Talon on channel 0
31-
self.stick = wpilib.Joystick(0) # initialize the joystick on port 0
33+
self.motor = wpilib.PWMSparkMax(self.MOTOR_PORT)
34+
self.joystick = wpilib.Joystick(self.JOYSTICK_PORT)
35+
self.encoder = wpilib.Encoder(self.ENCODER_PORT_A, self.ENCODER_PORT_B)
36+
# Use SetDistancePerPulse to set the multiplier for GetDistance
37+
# This is set up assuming a 6 inch wheel with a 360 CPR encoder.
38+
self.encoder.setDistancePerPulse((math.pi * 6) / 360.0)
3239

33-
def teleopPeriodic(self):
34-
"""Runs the motor from a joystick."""
35-
36-
# Set the motor's output.
37-
# This takes a number from -1 (100% speed in reverse) to
38-
# +1 (100% speed going forward)
40+
def robotPeriodic(self):
41+
"""The RobotPeriodic function is called every control packet no matter the robot mode."""
42+
wpilib.SmartDashboard.putNumber("Encoder", self.encoder.getDistance())
3943

40-
self.motor.set(self.stick.getY())
44+
def teleopPeriodic(self):
45+
self.motor.set(self.joystick.getY())
4146

4247

4348
if __name__ == "__main__":

tank-drive-xbox-controller/robot.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77

88
import wpilib
9-
from wpilib.drive import DifferentialDrive
9+
import wpilib.drive
1010

1111

1212
class MyRobot(wpilib.TimedRobot):
@@ -18,19 +18,15 @@ class MyRobot(wpilib.TimedRobot):
1818
def robotInit(self):
1919
"""Robot initialization function"""
2020

21-
# object that handles basic drive operations
22-
left = wpilib.PWMSparkMax(0)
23-
right = wpilib.PWMSparkMax(1)
24-
25-
self.robotDrive = DifferentialDrive(left, right)
26-
27-
# xbox controller 0 on the driver station
21+
leftMotor = wpilib.PWMSparkMax(0)
22+
rightMotor = wpilib.PWMSparkMax(1)
23+
self.robotDrive = wpilib.drive.DifferentialDrive(leftMotor, rightMotor)
2824
self.driverController = wpilib.XboxController(0)
2925

3026
# We need to invert one side of the drivetrain so that positive voltages
3127
# result in both sides moving forward. Depending on how your robot's
3228
# gearbox is constructed, you might have to invert the left side instead.
33-
right.setInverted(True)
29+
rightMotor.setInverted(True)
3430

3531
def teleopPeriodic(self):
3632
# Drive with tank drive.

tank-drive/robot.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77

88
import wpilib
9-
from wpilib.drive import DifferentialDrive
9+
import wpilib.drive
1010

1111

1212
class MyRobot(wpilib.TimedRobot):
@@ -18,23 +18,18 @@ class MyRobot(wpilib.TimedRobot):
1818
def robotInit(self):
1919
"""Robot initialization function"""
2020

21-
# object that handles basic drive operations
22-
left = wpilib.PWMSparkMax(0)
23-
right = wpilib.PWMSparkMax(1)
24-
25-
self.robotDrive = DifferentialDrive(left, right)
26-
27-
# joysticks 1 & 2 on the driver station
21+
leftMotor = wpilib.PWMSparkMax(0)
22+
rightMotor = wpilib.PWMSparkMax(1)
23+
self.robotDrive = wpilib.drive.DifferentialDrive(leftMotor, rightMotor)
2824
self.leftStick = wpilib.Joystick(0)
2925
self.rightStick = wpilib.Joystick(1)
3026

3127
# We need to invert one side of the drivetrain so that positive voltages
3228
# result in both sides moving forward. Depending on how your robot's
3329
# gearbox is constructed, you might have to invert the left side instead.
34-
right.setInverted(True)
30+
rightMotor.setInverted(True)
3531

3632
def teleopPeriodic(self):
37-
"""Drive with tank style"""
3833
self.robotDrive.tankDrive(-self.leftStick.getY(), -self.rightStick.getY())
3934

4035

0 commit comments

Comments
 (0)