Skip to content

Commit

Permalink
Merge pull request #92 from robotpy/defaults
Browse files Browse the repository at this point in the history
Add pidIdx and timeoutMs defaults
  • Loading branch information
virtuald committed Jan 14, 2019
2 parents 540bdd7 + a2f1b86 commit be29d52
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
20 changes: 20 additions & 0 deletions gen/MotController_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ c_MotController_GetTemperature:
c_MotController_ConfigSelectedFeedbackSensor:
code: |
self.hal_data['pid%d_feedback' % pidIdx] = feedbackDevice
defaults:
pidIdx: 0
doc: |
Select the feedback device for the motor controller.
Most CTRE CAN motor controllers will support remote sensors over CAN.
Expand All @@ -455,6 +457,8 @@ c_MotController_ConfigSelectedFeedbackSensor:
c_MotController_ConfigSelectedFeedbackCoefficient:
code: |
self.hal_data['pid%d_coefficient' % pidIdx] = coefficient
defaults:
pidIdx: 0
doc: |
The Feedback Coefficient is a scalar applied to the value of the
feedback sensor. Useful when you need to scale your sensor values
Expand Down Expand Up @@ -520,6 +524,8 @@ c_MotController_GetSelectedSensorPosition:
raise NotImplementedError("getSelectedSensorPosition not implemented for %s" % fd)
else:
retval = self.hal_data['%sposition' % prefix]
defaults:
pidIdx: 0
doc: |
Get the selected sensor position.
Expand All @@ -536,6 +542,8 @@ c_MotController_GetSelectedSensorVelocity:
raise NotImplementedError("getSelectedSensorVelocity not implemented for %s" % fd)
else:
retval = self.hal_data['%svelocity' % prefix]
defaults:
pidIdx: 0
doc: |
Get the selected sensor velocity.
Expand All @@ -551,6 +559,8 @@ c_MotController_SetSelectedSensorPosition:
raise NotImplementedError("setSelectedSensorPosition not implemented for %s" % fd)
else:
self.hal_data['%sposition' % prefix] = sensorPos
defaults:
pidIdx: 0
doc: |
Sets the sensor position to the given value.
Expand Down Expand Up @@ -891,6 +901,8 @@ c_MotController_ConfigClosedLoopPeriod:
c_MotController_SetIntegralAccumulator:
code: |
self.hal_data['pid%d_iaccum' % pidIdx] = iaccum
defaults:
pidIdx: 0
doc: |
Sets the integral accumulator. Typically this is used to clear/zero the
integral accumulator, however some use cases may require seeding the
Expand All @@ -909,6 +921,8 @@ c_MotController_SetIntegralAccumulator:
c_MotController_GetClosedLoopError:
code: |
retval = int(self.hal_data['pid%d_error' % pidIdx])
defaults:
pidIdx: 0
doc: |
Gets the closed-loop error. The units depend on which control mode is in
use. See Phoenix-Documentation information on units.
Expand All @@ -919,6 +933,8 @@ c_MotController_GetClosedLoopError:
c_MotController_GetIntegralAccumulator:
code: |
retval = self.hal_data['pid%d_iaccum' % pidIdx]
defaults:
pidIdx: 0
doc: |
Gets the iaccum value.
Expand All @@ -928,6 +944,8 @@ c_MotController_GetIntegralAccumulator:
c_MotController_GetErrorDerivative:
code: |
retval = self.hal_data['pid%d_errorDerivative' % pidIdx]
defaults:
pidIdx: 0
doc: |
Gets the derivative of the closed-loop error.
Expand Down Expand Up @@ -1708,6 +1726,8 @@ c_MotController_GetLimitSwitchState:
c_MotController_GetClosedLoopTarget:
code: |
retval = self.hal_data['pid%d_target' % pidIdx]
defaults:
pidIdx: 0
doc: |
Gets the current target of a given closed loop.
Expand Down
26 changes: 19 additions & 7 deletions gen/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ def function_hook(fn, data):

param_offset = 0 if x_name.startswith("create") else 1

data = data.get("data", {}).get(fn["name"])
if data is None:
# ensure every function is in our yaml
print("WARNING", fn["name"])
data = {}
# assert False, fn['name']

param_defaults = data.get("defaults", {})

for i, p in enumerate(fn["parameters"][param_offset:]):
if p["name"] == "":
p["name"] = "param%s" % i
Expand All @@ -136,6 +145,14 @@ def function_hook(fn, data):
p["x_pyann"] = "%(name)s: %(x_pyann_type)s" % p
p["x_pyarg"] = 'py::arg("%(name)s")' % p

if p["name"] in param_defaults:
_pname = param_defaults.pop(p["name"])
p["x_pyann"] += " = " + str(_pname)
p["x_pyarg"] += "=" + str(_pname)
elif p["name"].lower() == "timeoutms":
p["x_pyann"] += " = 0"
p["x_pyarg"] += "=0"

if p["pointer"]:
p["x_callname"] = "&%(x_callname)s" % p
x_out_params.append(p)
Expand All @@ -158,6 +175,8 @@ def function_hook(fn, data):

p["x_decl"] = "%s %s" % (p["x_type"], p["name"])

assert not param_defaults

x_callstart = ""
x_callend = ""
x_wrap_return = ""
Expand Down Expand Up @@ -224,13 +243,6 @@ def function_hook(fn, data):

args_comma = ", " if x_in_params else ""

data = data.get("data", {}).get(fn["name"])
if data is None:
# ensure every function is in our yaml
print("WARNING", fn["name"])
data = {}
# assert False, fn['name']

if "return" in data.get("code", ""):
raise ValueError("%s: Do not use return, assign to retval instead" % fn["name"])

Expand Down

0 comments on commit be29d52

Please sign in to comment.