Skip to content

Commit 65dbc95

Browse files
authored
Change placeholder python code (OpMode, etc) to match Peter's API (#353)
* Change placeholder python code (OpMode, etc) to match Peter's API * Updated generate_json.py and regenerated json files. * Updated json used when creating a new mechanism or opmode. * Added upgrade code to rename 'loop' to 'Periodic' in opmode modules. Added upgrade code to rename 'update' to 'opmode_periodic' in mechanism modules. * Make mrc_component blocks have a tooltip. Removed staticFunctionName and mrcStaticFunctionName. * Added a comment explaining some code in checkOpMode. * Always generate the define_hardware method for a robot. * Updated constants for class and method names. * Update extended_python_generator.ts: Changed code that generates the type decorator to use the full decorator name (including the module blocks_base_class) so the Teleop decorator doesn't collide with the Teleop class. Generate code that was in robot_base.py, but is not in wpilib.OpModeRobot. Generate code that was in opmode.py, but is not in wpilib.PeriodicOpMode.
1 parent d51af8a commit 65dbc95

32 files changed

+3136
-3500
lines changed

external_samples/color_range_sensor.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
# @fileoverview This is a sample for a color/range sensor
1717
# @author [email protected] (Alan Smith)
1818

19-
from typing import Protocol, Self
20-
from component import Component, InvalidPortException
19+
from typing import Protocol
20+
from component import Component
2121
from port import Port, PortType
2222

2323
class DistanceCallable(Protocol):
@@ -32,29 +32,9 @@ def __call__(self, hue : int, saturation : int, value : int) -> None:
3232

3333
class ColorRangeSensor(Component):
3434
def __init__(self, port : Port):
35+
'''REV Robotics Color Sensor v3. Part number REV-31-1557. https://www.revrobotics.com/rev-31-1557'''
3536
super().__init__( port, expectedType = PortType.I2C_PORT)
3637

37-
def get_manufacturer(self) -> str:
38-
return "REV Robotics"
39-
40-
def get_name(self) -> str:
41-
return "Color Sensor v3"
42-
43-
def get_part_number(self) -> str:
44-
return "REV-31-1557"
45-
46-
def get_url(self) -> str:
47-
return "https://www.revrobotics.com/rev-31-1557"
48-
49-
def get_version(self) -> tuple[int, int, int]:
50-
return (1, 0, 0)
51-
52-
def reset(self) -> None:
53-
pass
54-
55-
def periodic(self) -> None:
56-
pass
57-
5838
# Component specific methods
5939

6040
def get_color_rgb(self) -> tuple[int, int, int]:

external_samples/component.py

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# @fileoverview This is an abstract class for all components
1717
# @author [email protected] (Alan Smith)
1818

19-
from abc import ABC, abstractmethod
19+
from abc import ABC
2020
from typing import Protocol
2121
from port import Port, PortType
2222

@@ -38,57 +38,18 @@ def __init__(self, port : Port, expectedType : PortType):
3838

3939
self.port = port
4040

41-
# Returns the manufacturer of the component
42-
@abstractmethod
43-
def get_manufacturer(self) -> str:
41+
def opmode_start(self) -> None:
4442
pass
4543

46-
# Returns the name of the component
47-
@abstractmethod
48-
def get_name(self) -> str:
44+
def opmode_periodic(self) -> None:
4945
pass
5046

51-
# Returns the part number of the component
52-
@abstractmethod
53-
def get_part_number(self) -> str:
54-
pass
55-
56-
# Returns the URL of the component
57-
@abstractmethod
58-
def get_url(self) -> str:
59-
pass
60-
61-
# Returns the version of the software (returned as a (major, minor, patch) tuple where
62-
# major, minor and patch are all positive integers
63-
# This MUST follow semantic versioning as described here: https://semver.org/
64-
@abstractmethod
65-
def get_version(self) -> tuple[int, int, int]:
66-
pass
67-
68-
def start(self) -> None:
69-
pass
70-
71-
def update(self) -> None:
72-
pass
73-
74-
# This stops all movement (if any) for the component
75-
def stop(self) -> None:
76-
pass
77-
78-
# This performs any reset required (if any) at the beginning of each opmode
79-
# This might remove any registered callbacks
80-
@abstractmethod
81-
def reset(self) -> None:
47+
# Subclasses should override opmode_end to stop all movement (if any) for the component
48+
def opmode_end(self) -> None:
8249
pass
8350

8451
# Returns the port this connects to of the PortType enumeration
8552
def get_connection_port_type(self) -> PortType | None:
8653
if self.port:
8754
return self.port.get_type()
8855
return None
89-
90-
# This is called periodically when an opmode is running. The component might use this
91-
# to talk to hardware and then call callbacks
92-
@abstractmethod
93-
def periodic(self) -> None:
94-
pass

external_samples/expansion_hub_motor.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,20 @@
2020

2121
__author__ = "[email protected] (Liz Looney)"
2222

23-
from typing import Self
24-
from component import Component, InvalidPortException
23+
from component import Component
2524
from port import Port, PortType
26-
import expansion_hub
25+
from wpilib_placeholders import expansion_hub
2726
import wpimath
2827

2928
class ExpansionHubMotor(Component):
3029
def __init__(self, port : Port):
30+
'''REV Robotics Expansion Hub Motor'''
3131
super().__init__(port, PortType.EXPANSION_HUB_MOTOR)
3232
self.expansion_hub_motor = expansion_hub.ExpansionHubMotor(self.port.port1.location, self.port.port2.location)
3333

34-
def get_manufacturer(self) -> str:
35-
return "REV Robotics"
36-
37-
def get_name(self) -> str:
38-
return "Expansion Hub Motor"
39-
40-
def get_part_number(self) -> str:
41-
return ""
42-
43-
def get_url(self) -> str:
44-
return ""
45-
46-
def get_version(self) -> tuple[int, int, int]:
47-
return (1, 0, 0)
48-
49-
def start(self) -> None:
34+
def opmode_start(self) -> None:
5035
self.expansion_hub_motor.setEnabled(True)
5136

52-
def stop(self) -> None:
53-
# TODO: Send stop command to motor.
54-
pass
55-
56-
def reset(self) -> None:
57-
pass
58-
59-
def periodic(self) -> None:
60-
pass
61-
6237
# Component specific methods
6338

6439
# Methods from expansion_hub.ExpansionHubMotor

external_samples/expansion_hub_servo.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,19 @@
2020

2121
__author__ = "[email protected] (Liz Looney)"
2222

23-
from typing import Self
24-
from component import Component, PortType, InvalidPortException
23+
from component import Component
2524
from port import Port, PortType
26-
import expansion_hub
25+
from wpilib_placeholders import expansion_hub
26+
import wpimath
2727

2828
class ExpansionHubServo(Component):
2929
def __init__(self, port : Port):
30+
'''REV Robotics Expansion Hub Servo'''
3031
super().__init__(port, PortType.EXPANSION_HUB_SERVO)
3132
self.expansion_hub_servo = expansion_hub.ExpansionHubServo(self.port.port1.location, self.port.port2.location)
3233

33-
def get_manufacturer(self) -> str:
34-
return "REV Robotics"
35-
36-
def get_name(self) -> str:
37-
return "Expansion Hub Servo"
38-
39-
def get_part_number(self) -> str:
40-
return ""
41-
42-
def get_url(self) -> str:
43-
return ""
44-
45-
def get_version(self) -> tuple[int, int, int]:
46-
return (1, 0, 0)
47-
48-
def start(self) -> None:
34+
def opmode_start(self) -> None:
4935
self.expansion_hub_servo.setEnabled(True)
50-
pass
51-
52-
def stop(self) -> None:
53-
# TODO: Send stop command to servo.
54-
pass
55-
56-
def reset(self) -> None:
57-
pass
58-
59-
def get_connection_port_type(self) -> list[PortType]:
60-
return [PortType.USB_PORT, PortType.USB_PORT]
61-
62-
def periodic(self) -> None:
63-
pass
6436

6537
# Component specific methods
6638

@@ -78,7 +50,7 @@ def setEnabled(self, enabled: bool):
7850
def isHubConnected(self) -> bool:
7951
return self.expansion_hub_servo.isHubConnected()
8052

81-
def setFramePeriod(self, framePeriod: int):
53+
def setFramePeriod(self, framePeriod: wpimath.units.microseconds):
8254
self.expansion_hub_servo.setFramePeriod(framePeriod)
8355

8456
def setPulseWidth(self, pulseWidth: int):

external_samples/port.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# @author [email protected] (Alan Smith)
1818
from abc import ABC, abstractmethod
1919
from enum import Enum
20-
from typing import Final, Self
20+
from typing import Final
2121

2222
_BASE_COMPOUND: Final[int] = 256
2323

external_samples/rev_touch_sensor.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,20 @@
1616
# @fileoverview This is a sample for a touch sensor
1717
# @author [email protected] (Alan Smith)
1818

19-
from typing import Self
20-
from component import Component, PortType, InvalidPortException, EmptyCallable
19+
from component import Component, EmptyCallable
2120
from port import Port, PortType
2221

2322
class RevTouchSensor(Component):
2423
def __init__(self, port : Port):
24+
'''REV Robotics Touch Sensor. Part number REV-31-1425. https://www.revrobotics.com/rev-31-1425'''
2525
super().__init__(port, PortType.SMART_IO_PORT)
2626
self._is_pressed = None
2727

28-
def get_manufacturer(self) -> str:
29-
return "REV Robotics"
30-
31-
def get_name(self) -> str:
32-
return "Touch Sensor"
33-
34-
def get_part_number(self) -> str:
35-
return "REV-31-1425"
36-
37-
def get_url(self) -> str:
38-
return "https://www.revrobotics.com/rev-31-1425/"
39-
40-
def get_version(self) -> tuple[int, int, int]:
41-
return (1, 0, 0)
42-
43-
def reset(self) -> None:
28+
def opmode_stop(self) -> None:
4429
self.pressed_callback = None
4530
self.released_callback = None
46-
pass
4731

48-
def periodic(self) -> None:
32+
def opmode_periodic(self) -> None:
4933
old = self._is_pressed
5034
self._read_hardware()
5135
if old != self._is_pressed:
@@ -57,7 +41,7 @@ def periodic(self) -> None:
5741
# Component specific methods
5842

5943
def _read_hardware(self):
60-
# here read hardware to get the current value of the sensor and set self._is_pressed
44+
# TODO: Read hardware to get the current value of the sensor and set self._is_pressed
6145
pass
6246

6347
def is_pressed(self) -> bool:

external_samples/smart_motor.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
# @fileoverview This is a sample for a smart motor
1717
# @author [email protected] (Alan Smith)
1818

19-
from typing import Self
20-
from component import Component, PortType, InvalidPortException
19+
from component import Component
2120
from port import Port, PortType
2221

2322
class SmartMotor(Component):
@@ -39,16 +38,10 @@ def get_url(self) -> str:
3938
def get_version(self) -> tuple[int, int, int]:
4039
return (1, 0, 0)
4140

42-
def stop(self) -> None:
41+
def opmode_end(self) -> None:
4342
# TODO: send stop command to motor
4443
pass
4544

46-
def reset(self) -> None:
47-
pass
48-
49-
def periodic(self) -> None:
50-
pass
51-
5245
# Component specific methods
5346

5447
def set_speed(self, speed: float) -> None:

external_samples/spark_mini.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
__author__ = "[email protected] (Liz Looney)"
2222

23-
from typing import Self
24-
from component import Component, PortType, InvalidPortException
23+
from component import Component
2524
from port import Port, PortType
2625

2726
import wpilib
@@ -51,16 +50,10 @@ def get_url(self) -> str:
5150
def get_version(self) -> tuple[int, int, int]:
5251
return (1, 0, 0)
5352

54-
def stop(self) -> None:
53+
def opmode_end(self) -> None:
5554
# TODO: send stop command to motor
5655
pass
5756

58-
def reset(self) -> None:
59-
pass
60-
61-
def periodic(self) -> None:
62-
pass
63-
6457
# Component specific methods
6558

6659
# Methods from wpilib.PWMMotorController

external_samples/sparkfun_led_stick.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
__author__ = "[email protected] (Liz Looney)"
1616

17-
from typing import Self
18-
from component import Component, PortType, InvalidPortException
17+
from component import Component
1918
from port import Port, PortType
2019
import wpilib
2120

@@ -38,15 +37,9 @@ def get_url(self) -> str:
3837
def get_version(self) -> tuple[int, int, int]:
3938
return (1, 0, 0)
4039

41-
def stop(self) -> None:
40+
def opmode_end(self) -> None:
4241
self.turn_all_off()
4342

44-
def reset(self) -> None:
45-
pass
46-
47-
def periodic(self) -> None:
48-
pass
49-
5043
# SparkFunLEDStick methods
5144

5245
def set_color(self, position: int, color: wpilib.Color) -> None:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Placeholder classes for future wpilib."""
2+
3+
from .expansion_hub import ExpansionHubPidConstants, ExpansionHubMotor, ExpansionHubServo, ExpansionHub
4+
from .op_mode_robot import OpModeRobot
5+
from .periodic_op_mode import PeriodicOpMode
6+
7+
__all__ = [
8+
'ExpansionHubPidConstants',
9+
'ExpansionHubMotor',
10+
'ExpansionHubServo',
11+
'ExpansionHub',
12+
'OpModeRobot',
13+
'PeriodicOpMode',
14+
]

0 commit comments

Comments
 (0)