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

[wpilib] Pregenerate PWM motor controllers #6742

Merged
merged 10 commits into from
Jun 18, 2024

Conversation

pjreiniger
Copy link
Contributor

Fixes #6739

@pjreiniger pjreiniger requested a review from a team as a code owner June 14, 2024 05:06
@calcmogul calcmogul changed the title [wpilibj/c] Pregenerate PWM motor controllers [wpilib] Pregenerate PWM motor controllers Jun 15, 2024
Comment on lines -12 to +20
* VEX Robotics Victor 888 Motor Controller The Vex Robotics Victor 884 Motor Controller can also be
* used with this class but may need to be calibrated per the Victor 884 user manual.
* Vex Robotics Victor 888 Motor Controller.
*
* <p>Note that the Victor uses the following bounds for PWM values. These values were determined
* empirically and optimized for the Victor 888. These values should work reasonably well for Victor
* 884 controllers also but if users experience issues such as asymmetric behavior around the
* deadband or inability to saturate the controller in either direction, calibration is recommended.
* The calibration procedure can be found in the Victor 884 User Manual available from VEX Robotics:
* <a
* href="http://content.vexrobotics.com/docs/ifi-v884-users-manual-9-25-06.pdf">http://content.vexrobotics.com/docs/ifi-v884-users-manual-9-25-06.pdf</a>
* <p>Note that the Victor 888 uses the following bounds for PWM values. These values should work
* reasonably well for most controllers, but if users experience issues such as asymmetric behavior
* around the deadband or inability to saturate the controller in either direction, calibration is
* recommended. The calibration procedure can be found in the Victor 888 User Manual available from
* Vex Robotics.

This comment was marked as resolved.

@TheTripleV
Copy link
Member

Consider using pathlib instead of wrangling names manually. The trailing backslash vs no trailing backslash can bite someone who tries to update it in the future.

Do explicitly set utf-8 as the encoding on file operations in case someone tries to run this on Windows. Python on Windows will switch to utf-8 by default (ignoring locale) in 2026.

gpt conversion doing both for example
#!/usr/bin/env python3

# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
import argparse
import json
import sys
from pathlib import Path

from jinja2 import Environment, FileSystemLoader


def render_template(template, output_dir, filename, controller):
    output_dir_path = Path(output_dir)
    output_dir_path.mkdir(parents=True, exist_ok=True)

    (output_dir_path / filename).write_text(template.render(controller), encoding="utf-8")


def generate_pwm_motor_controllers(output_root, template_root):
    template_root_path = Path(template_root)
    with (template_root_path / "pwm_motor_controllers.json").open(encoding="utf-8") as f:
        controllers = json.load(f)

    env = Environment(
        loader=FileSystemLoader(str(template_root_path)),
        autoescape=False,
        keep_trailing_newline=True,
    )

    root_path = Path(output_root) / "main/java/edu/wpi/first/wpilibj/motorcontrol"
    template = env.get_template("pwm_motor_controller.java.jinja")

    for controller in controllers:
        controller_name = f"{controller['name']}.java"
        render_template(template, root_path, controller_name, controller)


def main(argv):
    script_path = Path(__file__).resolve()
    dirname = script_path.parent

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--output_directory",
        help="Optional. If set, will output the generated files to this directory, otherwise it will use a path relative to the script",
        default=dirname / "src/generated",
    )
    parser.add_argument(
        "--template_root",
        help="Optional. If set, will use this directory as the root for the jinja templates",
        default=dirname / "src/generate",
    )
    args = parser.parse_args(argv)

    generate_pwm_motor_controllers(args.output_directory, args.template_root)


if __name__ == "__main__":
    main(sys.argv[1:])

@PeterJohnson PeterJohnson merged commit b6bd798 into wpilibsuite:main Jun 18, 2024
29 checks passed
@pjreiniger pjreiniger deleted the generate_pwm branch August 24, 2024 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Jinja to generate PWM motor controller classes
7 participants