Gudu0

Untitled

Nov 12th, 2025 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  4. import com.qualcomm.robotcore.hardware.DcMotor;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.List;
  9. import java.util.Set;
  10.  
  11. public class TestHardware {
  12. private LinearOpMode myOpMode; // access to the calling OpMode
  13.  
  14. // Discovered motors
  15. public DcMotor[] motors = new DcMotor[0];
  16. public String[] motorNames = new String[0];
  17.  
  18. public int motorIndex = -1; // current selected motor index
  19.  
  20. public TestHardware (LinearOpMode opmode) {
  21. myOpMode = opmode;
  22. }
  23.  
  24. public void init() {
  25. // Discover all DcMotor names
  26. Set<String> nameSet = myOpMode.hardwareMap.keySet(DcMotor.class);
  27. List<String> names = new ArrayList<>(nameSet);
  28. // Sort for stable order across runs
  29. Collections.sort(names);
  30.  
  31. List<DcMotor> motorList = new ArrayList<>(names.size());
  32. for (String name : names) {
  33. DcMotor m = myOpMode.hardwareMap.get(DcMotor.class, name);
  34. motorList.add(m);
  35. }
  36. motors = motorList.toArray(new DcMotor[0]);
  37. motorNames = names.toArray(new String[0]);
  38. motorIndex = motors.length > 0 ? 0 : -1;
  39.  
  40. // Telemetry summary
  41. myOpMode.telemetry.addData(">", "Dynamic Hardware Initialized");
  42. myOpMode.telemetry.addData("total motors", motors.length);
  43. if (motors.length > 0) {
  44. myOpMode.telemetry.addData("motor names", String.join(", ", motorNames));
  45. }
  46. myOpMode.telemetry.update();
  47. }
  48.  
  49. // Cycle forward through discovered motors
  50. public void cycleMotorPos() {
  51. if (motorIndex < 0 || motors.length == 0) return;
  52. motorIndex = (motorIndex + 1) % motors.length;
  53. }
  54.  
  55. // Cycle backward through discovered motors
  56. public void cycleMotorNeg() {
  57. if (motorIndex < 0 || motors.length == 0) return;
  58. motorIndex = (motorIndex - 1 + motors.length) % motors.length;
  59. }
  60.  
  61. public String getCurrentMotorName() {
  62. return (motorIndex >= 0 && motorIndex < motorNames.length) ? motorNames[motorIndex] : "";
  63. }
  64.  
  65. public DcMotor getCurrentMotor() {
  66. return (motorIndex >= 0 && motorIndex < motors.length) ? motors[motorIndex] : null;
  67. }
  68.  
  69. public void motorPowerPos() {
  70. DcMotor m = getCurrentMotor();
  71. if (m != null) m.setPower(1.0);
  72. }
  73.  
  74. public void motorPowerNeg() {
  75. DcMotor m = getCurrentMotor();
  76. if (m != null) m.setPower(-1.0);
  77. }
  78.  
  79. public void motorPowerStop() {
  80. DcMotor m = getCurrentMotor();
  81. if (m != null) m.setPower(0.0);
  82. }
  83.  
  84.  
  85. // Update telemetry about current selection
  86. public void reportSelection() {
  87. myOpMode.telemetry.addData("selected index", motorIndex);
  88. myOpMode.telemetry.addData("selected name", getCurrentMotorName());
  89. DcMotor m = getCurrentMotor();
  90. if (m != null) {
  91. myOpMode.telemetry.addData("selected power", m.getPower());
  92. }
  93. myOpMode.telemetry.update();
  94. }
  95.  
  96. public void sleep(int millis) {
  97. try {
  98. Thread.sleep(millis);
  99. } catch (InterruptedException e) {
  100. Thread.currentThread().interrupt();
  101. }
  102. }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment