Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python examples #24

Merged
merged 18 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion python/ArcadeDrive/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This is a demo program for arcade drive in Python with Phoenix 6
"""
import wpilib
from phoenix6 import hardware, controls
from phoenix6 import hardware, controls, unmanaged


class MyRobot(wpilib.TimedRobot):
Expand Down Expand Up @@ -46,6 +46,12 @@ def teleopPeriodic(self):
self.front_left_motor.set_control(self.left_out.with_output(throttle + wheel))
self.front_right_motor.set_control(self.right_out.with_output(throttle - wheel))

def _simulationPeriodic(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

robotpy projects are encouraged to use a physics.py instead of _simulationPeriodic. See https://github.com/robotpy/examples/tree/main/physics

""""""
# If the driver station is enabled, then feed enable for phoenix devices
if wpilib.DriverStation.isEnabled():
unmanaged.feed_enable(100)


if __name__ == "__main__":
wpilib.run(MyRobot)
9 changes: 8 additions & 1 deletion python/CANcoder/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import wpilib
from wpilib import Timer
from phoenix6 import hardware
from phoenix6 import hardware, unmanaged

class MyRobot(wpilib.TimedRobot):
"""
Expand All @@ -25,6 +25,7 @@ def teleopPeriodic(self):
"""Every 100ms, print the status of the StatusSignal"""

if self.timer.hasElapsed(0.1):
self.timer.reset()
# get_position automatically calls refresh(), no need to manually refresh.
#
# StatusSignals also implement the str dunder to provide a useful print of the signal
Expand All @@ -39,5 +40,11 @@ def teleopPeriodic(self):

print("")

def _simulationPeriodic(self):
""""""
# If the driver station is enabled, then feed enable for phoenix devices
if wpilib.DriverStation.isEnabled():
unmanaged.feed_enable(100)

if __name__ == "__main__":
wpilib.run(MyRobot)
9 changes: 8 additions & 1 deletion python/PositionClosedLoop/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import wpilib
from wpilib import Timer, XboxController
from phoenix6 import hardware, controls, configs
from phoenix6 import hardware, controls, configs, unmanaged

class MyRobot(wpilib.TimedRobot):
"""
Expand Down Expand Up @@ -42,6 +42,7 @@ def teleopPeriodic(self):
self.talonfx.set_control(self.position_request.with_position(self.joystick.getLeftY() * 10))

if self.timer.hasElapsed(0.1):
self.timer.reset()
# Print the position & velocity to see what they are
pos = self.talonfx.get_position()
vel = self.talonfx.get_velocity()
Expand All @@ -51,5 +52,11 @@ def teleopPeriodic(self):

print("")

def _simulationPeriodic(self):
""""""
# If the driver station is enabled, then feed enable for phoenix devices
if wpilib.DriverStation.isEnabled():
unmanaged.feed_enable(100)

if __name__ == "__main__":
wpilib.run(MyRobot)
8 changes: 8 additions & 0 deletions python/StatusSignals/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
controls,
SignalLogger,
BaseStatusSignal,
unmanaged
)


Expand Down Expand Up @@ -47,6 +48,7 @@ def teleopPeriodic(self):
"""Every 100ms, print the status of the StatusSignal"""

if self.timer.hasElapsed(0.1):
self.timer.reset()
BaseStatusSignal.refresh_all(self.pos, self.vel)

pos_timestamp = self.pos.all_timestamps.get_device_timestamp().time
Expand All @@ -57,6 +59,12 @@ def teleopPeriodic(self):
)
print(f"Latency compensated position is {latency_compensated_pos}")

def _simulationPeriodic(self):
""""""
# If the driver station is enabled, then feed enable for phoenix devices
if wpilib.DriverStation.isEnabled():
unmanaged.feed_enable(100)


if __name__ == "__main__":
wpilib.run(MyRobot)
16 changes: 14 additions & 2 deletions python/TalonFX/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This is a demo program for TalonFX usage in Phoenix 6
"""
import wpilib
from wpilib import Timer
from phoenix6 import hardware
from wpilib import Timer, XboxController
from phoenix6 import hardware, unmanaged, controls

class MyRobot(wpilib.TimedRobot):
"""
Expand All @@ -17,14 +17,20 @@ def robotInit(self):

# Keep a reference to all the motor controllers used
self.talonfx = hardware.TalonFX(1, "canivore")
self.control = controls.DutyCycleOut(0)

self.timer = Timer()
self.timer.start()

self.joystick = XboxController(0)

def teleopPeriodic(self):
"""Every 100ms, print the status of the StatusSignal"""

self.talonfx.set_control(self.control.with_output(self.joystick.getLeftY()))

if self.timer.hasElapsed(0.1):
self.timer.reset()
# get_position automatically calls refresh(), no need to manually refresh.
#
# StatusSignals also implement the str dunder to provide a useful print of the signal
Expand All @@ -39,5 +45,11 @@ def teleopPeriodic(self):

print("")

def _simulationPeriodic(self):
""""""
# If the driver station is enabled, then feed enable for phoenix devices
if wpilib.DriverStation.isEnabled():
unmanaged.feed_enable(100)

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