001/* 002 * Copyright (C) Cross The Road Electronics. All rights reserved. 003 * License information can be found in CTRE_LICENSE.txt 004 * For support and suggestions contact support@ctr-electronics.com or file 005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases 006 */ 007package com.ctre.phoenix6.controls; 008 009import com.ctre.phoenix6.CANBus; 010import com.ctre.phoenix6.StatusCode; 011import com.ctre.phoenix6.controls.jni.ControlJNI; 012import com.ctre.phoenix6.hardware.traits.*; 013 014import edu.wpi.first.units.*; 015import edu.wpi.first.units.measure.*; 016import static edu.wpi.first.units.Units.*; 017 018import java.util.HashMap; 019import java.util.Map; 020 021/** 022 * Requests Motion Magic® to target a final velocity using a motion profile, and 023 * PID to a differential position setpoint. This allows smooth transitions 024 * between velocity set points. 025 * <p> 026 * Motion Magic® Velocity produces a motion profile in real-time while attempting to honor the specified 027 * Acceleration and (optional) Jerk. This control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA 028 * configs. 029 * <p> 030 * Acceleration and jerk are specified in the Motion Magic® persistent configuration values. If Jerk is set 031 * to zero, Motion Magic® will produce a trapezoidal acceleration profile. 032 * <p> 033 * Target velocity can also be changed on-the-fly and Motion Magic® will do its best to adjust the profile. 034 * This control mode is duty cycle based, so relevant closed-loop gains will use fractional duty cycle for the 035 * numerator: +1.0 represents full forward output. 036 */ 037public final class DifferentialMotionMagicVelocityDutyCycle implements ControlRequest, Cloneable { 038 /** 039 * Average velocity to drive toward in rotations per second. 040 * 041 * <ul> 042 * <li> Units: rotations per second 043 * </ul> 044 * 045 */ 046 public double AverageVelocity; 047 /** 048 * Differential position to drive toward in rotations. 049 * 050 * <ul> 051 * <li> Units: rotations 052 * </ul> 053 * 054 */ 055 public double DifferentialPosition; 056 /** 057 * Set to true to use FOC commutation (requires Phoenix Pro), which increases 058 * peak power by ~15% on supported devices (see {@link SupportsFOC}). Set to 059 * false to use trapezoidal commutation. 060 * <p> 061 * FOC improves motor performance by leveraging torque (current) control. 062 * However, this may be inconvenient for applications that require specifying 063 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that 064 * combines the performances gains of FOC while still allowing applications to 065 * provide duty cycle or voltage demand. This not to be confused with simple 066 * sinusoidal control or phase voltage control which lacks the performance 067 * gains. 068 */ 069 public boolean EnableFOC = true; 070 /** 071 * Select which gains are applied to the average controller by selecting the 072 * slot. Use the configuration api to set the gain values for the selected slot 073 * before enabling this feature. Slot must be within [0,2]. 074 */ 075 public int AverageSlot = 0; 076 /** 077 * Select which gains are applied to the differential controller by selecting 078 * the slot. Use the configuration api to set the gain values for the selected 079 * slot before enabling this feature. Slot must be within [0,2]. 080 */ 081 public int DifferentialSlot = 1; 082 /** 083 * Set to true to static-brake the rotor when output is zero (or within 084 * deadband). Set to false to use the NeutralMode configuration setting 085 * (default). This flag exists to provide the fundamental behavior of this 086 * control when output is zero, which is to provide 0V to the motor. 087 */ 088 public boolean OverrideBrakeDurNeutral = false; 089 /** 090 * Set to true to force forward limiting. This allows users to use other limit 091 * switch sensors connected to robot controller. This also allows use of active 092 * sensors that require external power. 093 */ 094 public boolean LimitForwardMotion = false; 095 /** 096 * Set to true to force reverse limiting. This allows users to use other limit 097 * switch sensors connected to robot controller. This also allows use of active 098 * sensors that require external power. 099 */ 100 public boolean LimitReverseMotion = false; 101 /** 102 * Set to true to ignore hardware limit switches and the LimitForwardMotion and 103 * LimitReverseMotion parameters, instead allowing motion. 104 * <p> 105 * This can be useful on mechanisms such as an intake/feeder, where a limit 106 * switch stops motion while intaking but should be ignored when feeding to a 107 * shooter. 108 * <p> 109 * The hardware limit faults and Forward/ReverseLimit signals will still report 110 * the values of the limit switches regardless of this parameter. 111 */ 112 public boolean IgnoreHardwareLimits = false; 113 /** 114 * Set to true to ignore software limits, instead allowing motion. 115 * <p> 116 * This can be useful when calibrating the zero point of a mechanism such as an 117 * elevator. 118 * <p> 119 * The software limit faults will still report the values of the software limits 120 * regardless of this parameter. 121 */ 122 public boolean IgnoreSoftwareLimits = false; 123 /** 124 * Set to true to delay applying this control request until a timesync boundary 125 * (requires Phoenix Pro and CANivore). This eliminates the impact of 126 * nondeterministic network delays in exchange for a larger but deterministic 127 * control latency. 128 * <p> 129 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs. 130 * Additionally, when this is enabled, the UpdateFreqHz of this request should 131 * be set to 0 Hz. 132 */ 133 public boolean UseTimesync = false; 134 135 /** 136 * The frequency at which this control will update. 137 * This is designated in Hertz, with a minimum of 20 Hz 138 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms). 139 * Some update frequencies are not supported and will be 140 * promoted up to the next highest supported frequency. 141 * <p> 142 * If this field is set to 0 Hz, the control request will 143 * be sent immediately as a one-shot frame. This may be useful 144 * for advanced applications that require outputs to be 145 * synchronized with data acquisition. In this case, we 146 * recommend not exceeding 50 ms between control calls. 147 */ 148 public double UpdateFreqHz = 100; 149 150 /** 151 * Requests Motion Magic® to target a final velocity using a motion profile, and 152 * PID to a differential position setpoint. This allows smooth transitions 153 * between velocity set points. 154 * <p> 155 * Motion Magic® Velocity produces a motion profile in real-time while 156 * attempting to honor the specified Acceleration and (optional) Jerk. This 157 * control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA configs. 158 * <p> 159 * Acceleration and jerk are specified in the Motion Magic® persistent 160 * configuration values. If Jerk is set to zero, Motion Magic® will produce a 161 * trapezoidal acceleration profile. 162 * <p> 163 * Target velocity can also be changed on-the-fly and Motion Magic® will do its 164 * best to adjust the profile. This control mode is duty cycle based, so 165 * relevant closed-loop gains will use fractional duty cycle for the numerator: 166 * +1.0 represents full forward output. 167 * 168 * @param AverageVelocity Average velocity to drive toward in rotations per 169 * second. 170 * @param DifferentialPosition Differential position to drive toward in 171 * rotations. 172 */ 173 public DifferentialMotionMagicVelocityDutyCycle(double AverageVelocity, double DifferentialPosition) { 174 this.AverageVelocity = AverageVelocity; 175 this.DifferentialPosition = DifferentialPosition; 176 } 177 178 /** 179 * Requests Motion Magic® to target a final velocity using a motion profile, and 180 * PID to a differential position setpoint. This allows smooth transitions 181 * between velocity set points. 182 * <p> 183 * Motion Magic® Velocity produces a motion profile in real-time while 184 * attempting to honor the specified Acceleration and (optional) Jerk. This 185 * control mode does not use the CruiseVelocity, Expo_kV, or Expo_kA configs. 186 * <p> 187 * Acceleration and jerk are specified in the Motion Magic® persistent 188 * configuration values. If Jerk is set to zero, Motion Magic® will produce a 189 * trapezoidal acceleration profile. 190 * <p> 191 * Target velocity can also be changed on-the-fly and Motion Magic® will do its 192 * best to adjust the profile. This control mode is duty cycle based, so 193 * relevant closed-loop gains will use fractional duty cycle for the numerator: 194 * +1.0 represents full forward output. 195 * 196 * @param AverageVelocity Average velocity to drive toward in rotations per 197 * second. 198 * @param DifferentialPosition Differential position to drive toward in 199 * rotations. 200 */ 201 public DifferentialMotionMagicVelocityDutyCycle(AngularVelocity AverageVelocity, Angle DifferentialPosition) { 202 this(AverageVelocity.in(RotationsPerSecond), DifferentialPosition.in(Rotations)); 203 } 204 205 @Override 206 public String getName() { 207 return "DifferentialMotionMagicVelocityDutyCycle"; 208 } 209 210 @Override 211 public String toString() { 212 String ss = "Control: DifferentialMotionMagicVelocityDutyCycle\n"; 213 ss += " AverageVelocity: " + AverageVelocity + " rotations per second" + "\n"; 214 ss += " DifferentialPosition: " + DifferentialPosition + " rotations" + "\n"; 215 ss += " EnableFOC: " + EnableFOC + "\n"; 216 ss += " AverageSlot: " + AverageSlot + "\n"; 217 ss += " DifferentialSlot: " + DifferentialSlot + "\n"; 218 ss += " OverrideBrakeDurNeutral: " + OverrideBrakeDurNeutral + "\n"; 219 ss += " LimitForwardMotion: " + LimitForwardMotion + "\n"; 220 ss += " LimitReverseMotion: " + LimitReverseMotion + "\n"; 221 ss += " IgnoreHardwareLimits: " + IgnoreHardwareLimits + "\n"; 222 ss += " IgnoreSoftwareLimits: " + IgnoreSoftwareLimits + "\n"; 223 ss += " UseTimesync: " + UseTimesync + "\n"; 224 return ss; 225 } 226 227 @Override 228 public StatusCode sendRequest(CANBus network, int deviceHash) { 229 return StatusCode.valueOf(ControlJNI.JNI_RequestControlDifferentialMotionMagicVelocityDutyCycle( 230 network.getNameUTF8Bytes(), deviceHash, UpdateFreqHz, AverageVelocity, DifferentialPosition, EnableFOC, AverageSlot, DifferentialSlot, OverrideBrakeDurNeutral, LimitForwardMotion, LimitReverseMotion, IgnoreHardwareLimits, IgnoreSoftwareLimits, UseTimesync)); 231 } 232 233 /** 234 * Gets information about this control request. 235 * 236 * @return Map of control parameter names and corresponding applied values 237 */ 238 @Override 239 public Map<String, String> getControlInfo() { 240 var controlInfo = new HashMap<String, String>(); 241 controlInfo.put("Name", getName()); 242 controlInfo.put("AverageVelocity", String.valueOf(this.AverageVelocity)); 243 controlInfo.put("DifferentialPosition", String.valueOf(this.DifferentialPosition)); 244 controlInfo.put("EnableFOC", String.valueOf(this.EnableFOC)); 245 controlInfo.put("AverageSlot", String.valueOf(this.AverageSlot)); 246 controlInfo.put("DifferentialSlot", String.valueOf(this.DifferentialSlot)); 247 controlInfo.put("OverrideBrakeDurNeutral", String.valueOf(this.OverrideBrakeDurNeutral)); 248 controlInfo.put("LimitForwardMotion", String.valueOf(this.LimitForwardMotion)); 249 controlInfo.put("LimitReverseMotion", String.valueOf(this.LimitReverseMotion)); 250 controlInfo.put("IgnoreHardwareLimits", String.valueOf(this.IgnoreHardwareLimits)); 251 controlInfo.put("IgnoreSoftwareLimits", String.valueOf(this.IgnoreSoftwareLimits)); 252 controlInfo.put("UseTimesync", String.valueOf(this.UseTimesync)); 253 return controlInfo; 254 } 255 256 /** 257 * Modifies this Control Request's AverageVelocity parameter and returns itself for 258 * method-chaining and easier to use request API. 259 * <p> 260 * Average velocity to drive toward in rotations per second. 261 * 262 * <ul> 263 * <li> Units: rotations per second 264 * </ul> 265 * 266 * 267 * @param newAverageVelocity Parameter to modify 268 * @return Itself 269 */ 270 public DifferentialMotionMagicVelocityDutyCycle withAverageVelocity(double newAverageVelocity) { 271 AverageVelocity = newAverageVelocity; 272 return this; 273 } 274 275 /** 276 * Modifies this Control Request's AverageVelocity parameter and returns itself for 277 * method-chaining and easier to use request API. 278 * <p> 279 * Average velocity to drive toward in rotations per second. 280 * 281 * <ul> 282 * <li> Units: rotations per second 283 * </ul> 284 * 285 * 286 * @param newAverageVelocity Parameter to modify 287 * @return Itself 288 */ 289 public DifferentialMotionMagicVelocityDutyCycle withAverageVelocity(AngularVelocity newAverageVelocity) { 290 AverageVelocity = newAverageVelocity.in(RotationsPerSecond); 291 return this; 292 } 293 294 /** 295 * Helper method to get this Control Request's AverageVelocity parameter converted 296 * to a unit type. If not using the Java units library, {@link #AverageVelocity} 297 * can be accessed directly instead. 298 * <p> 299 * Average velocity to drive toward in rotations per second. 300 * 301 * <ul> 302 * <li> Units: rotations per second 303 * </ul> 304 * 305 * 306 * @return AverageVelocity 307 */ 308 public AngularVelocity getAverageVelocityMeasure() { 309 return RotationsPerSecond.of(AverageVelocity); 310 } 311 312 /** 313 * Modifies this Control Request's DifferentialPosition parameter and returns itself for 314 * method-chaining and easier to use request API. 315 * <p> 316 * Differential position to drive toward in rotations. 317 * 318 * <ul> 319 * <li> Units: rotations 320 * </ul> 321 * 322 * 323 * @param newDifferentialPosition Parameter to modify 324 * @return Itself 325 */ 326 public DifferentialMotionMagicVelocityDutyCycle withDifferentialPosition(double newDifferentialPosition) { 327 DifferentialPosition = newDifferentialPosition; 328 return this; 329 } 330 331 /** 332 * Modifies this Control Request's DifferentialPosition parameter and returns itself for 333 * method-chaining and easier to use request API. 334 * <p> 335 * Differential position to drive toward in rotations. 336 * 337 * <ul> 338 * <li> Units: rotations 339 * </ul> 340 * 341 * 342 * @param newDifferentialPosition Parameter to modify 343 * @return Itself 344 */ 345 public DifferentialMotionMagicVelocityDutyCycle withDifferentialPosition(Angle newDifferentialPosition) { 346 DifferentialPosition = newDifferentialPosition.in(Rotations); 347 return this; 348 } 349 350 /** 351 * Helper method to get this Control Request's DifferentialPosition parameter converted 352 * to a unit type. If not using the Java units library, {@link #DifferentialPosition} 353 * can be accessed directly instead. 354 * <p> 355 * Differential position to drive toward in rotations. 356 * 357 * <ul> 358 * <li> Units: rotations 359 * </ul> 360 * 361 * 362 * @return DifferentialPosition 363 */ 364 public Angle getDifferentialPositionMeasure() { 365 return Rotations.of(DifferentialPosition); 366 } 367 368 /** 369 * Modifies this Control Request's EnableFOC parameter and returns itself for 370 * method-chaining and easier to use request API. 371 * <p> 372 * Set to true to use FOC commutation (requires Phoenix Pro), which increases 373 * peak power by ~15% on supported devices (see {@link SupportsFOC}). Set to 374 * false to use trapezoidal commutation. 375 * <p> 376 * FOC improves motor performance by leveraging torque (current) control. 377 * However, this may be inconvenient for applications that require specifying 378 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that 379 * combines the performances gains of FOC while still allowing applications to 380 * provide duty cycle or voltage demand. This not to be confused with simple 381 * sinusoidal control or phase voltage control which lacks the performance 382 * gains. 383 * 384 * @param newEnableFOC Parameter to modify 385 * @return Itself 386 */ 387 public DifferentialMotionMagicVelocityDutyCycle withEnableFOC(boolean newEnableFOC) { 388 EnableFOC = newEnableFOC; 389 return this; 390 } 391 392 /** 393 * Modifies this Control Request's AverageSlot parameter and returns itself for 394 * method-chaining and easier to use request API. 395 * <p> 396 * Select which gains are applied to the average controller by selecting the 397 * slot. Use the configuration api to set the gain values for the selected slot 398 * before enabling this feature. Slot must be within [0,2]. 399 * 400 * @param newAverageSlot Parameter to modify 401 * @return Itself 402 */ 403 public DifferentialMotionMagicVelocityDutyCycle withAverageSlot(int newAverageSlot) { 404 AverageSlot = newAverageSlot; 405 return this; 406 } 407 408 /** 409 * Modifies this Control Request's DifferentialSlot parameter and returns itself for 410 * method-chaining and easier to use request API. 411 * <p> 412 * Select which gains are applied to the differential controller by selecting 413 * the slot. Use the configuration api to set the gain values for the selected 414 * slot before enabling this feature. Slot must be within [0,2]. 415 * 416 * @param newDifferentialSlot Parameter to modify 417 * @return Itself 418 */ 419 public DifferentialMotionMagicVelocityDutyCycle withDifferentialSlot(int newDifferentialSlot) { 420 DifferentialSlot = newDifferentialSlot; 421 return this; 422 } 423 424 /** 425 * Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for 426 * method-chaining and easier to use request API. 427 * <p> 428 * Set to true to static-brake the rotor when output is zero (or within 429 * deadband). Set to false to use the NeutralMode configuration setting 430 * (default). This flag exists to provide the fundamental behavior of this 431 * control when output is zero, which is to provide 0V to the motor. 432 * 433 * @param newOverrideBrakeDurNeutral Parameter to modify 434 * @return Itself 435 */ 436 public DifferentialMotionMagicVelocityDutyCycle withOverrideBrakeDurNeutral(boolean newOverrideBrakeDurNeutral) { 437 OverrideBrakeDurNeutral = newOverrideBrakeDurNeutral; 438 return this; 439 } 440 441 /** 442 * Modifies this Control Request's LimitForwardMotion parameter and returns itself for 443 * method-chaining and easier to use request API. 444 * <p> 445 * Set to true to force forward limiting. This allows users to use other limit 446 * switch sensors connected to robot controller. This also allows use of active 447 * sensors that require external power. 448 * 449 * @param newLimitForwardMotion Parameter to modify 450 * @return Itself 451 */ 452 public DifferentialMotionMagicVelocityDutyCycle withLimitForwardMotion(boolean newLimitForwardMotion) { 453 LimitForwardMotion = newLimitForwardMotion; 454 return this; 455 } 456 457 /** 458 * Modifies this Control Request's LimitReverseMotion parameter and returns itself for 459 * method-chaining and easier to use request API. 460 * <p> 461 * Set to true to force reverse limiting. This allows users to use other limit 462 * switch sensors connected to robot controller. This also allows use of active 463 * sensors that require external power. 464 * 465 * @param newLimitReverseMotion Parameter to modify 466 * @return Itself 467 */ 468 public DifferentialMotionMagicVelocityDutyCycle withLimitReverseMotion(boolean newLimitReverseMotion) { 469 LimitReverseMotion = newLimitReverseMotion; 470 return this; 471 } 472 473 /** 474 * Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for 475 * method-chaining and easier to use request API. 476 * <p> 477 * Set to true to ignore hardware limit switches and the LimitForwardMotion and 478 * LimitReverseMotion parameters, instead allowing motion. 479 * <p> 480 * This can be useful on mechanisms such as an intake/feeder, where a limit 481 * switch stops motion while intaking but should be ignored when feeding to a 482 * shooter. 483 * <p> 484 * The hardware limit faults and Forward/ReverseLimit signals will still report 485 * the values of the limit switches regardless of this parameter. 486 * 487 * @param newIgnoreHardwareLimits Parameter to modify 488 * @return Itself 489 */ 490 public DifferentialMotionMagicVelocityDutyCycle withIgnoreHardwareLimits(boolean newIgnoreHardwareLimits) { 491 IgnoreHardwareLimits = newIgnoreHardwareLimits; 492 return this; 493 } 494 495 /** 496 * Modifies this Control Request's IgnoreSoftwareLimits parameter and returns itself for 497 * method-chaining and easier to use request API. 498 * <p> 499 * Set to true to ignore software limits, instead allowing motion. 500 * <p> 501 * This can be useful when calibrating the zero point of a mechanism such as an 502 * elevator. 503 * <p> 504 * The software limit faults will still report the values of the software limits 505 * regardless of this parameter. 506 * 507 * @param newIgnoreSoftwareLimits Parameter to modify 508 * @return Itself 509 */ 510 public DifferentialMotionMagicVelocityDutyCycle withIgnoreSoftwareLimits(boolean newIgnoreSoftwareLimits) { 511 IgnoreSoftwareLimits = newIgnoreSoftwareLimits; 512 return this; 513 } 514 515 /** 516 * Modifies this Control Request's UseTimesync parameter and returns itself for 517 * method-chaining and easier to use request API. 518 * <p> 519 * Set to true to delay applying this control request until a timesync boundary 520 * (requires Phoenix Pro and CANivore). This eliminates the impact of 521 * nondeterministic network delays in exchange for a larger but deterministic 522 * control latency. 523 * <p> 524 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs. 525 * Additionally, when this is enabled, the UpdateFreqHz of this request should 526 * be set to 0 Hz. 527 * 528 * @param newUseTimesync Parameter to modify 529 * @return Itself 530 */ 531 public DifferentialMotionMagicVelocityDutyCycle withUseTimesync(boolean newUseTimesync) { 532 UseTimesync = newUseTimesync; 533 return this; 534 } 535 536 /** 537 * Sets the frequency at which this control will update. 538 * This is designated in Hertz, with a minimum of 20 Hz 539 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms). 540 * Some update frequencies are not supported and will be 541 * promoted up to the next highest supported frequency. 542 * <p> 543 * If this field is set to 0 Hz, the control request will 544 * be sent immediately as a one-shot frame. This may be useful 545 * for advanced applications that require outputs to be 546 * synchronized with data acquisition. In this case, we 547 * recommend not exceeding 50 ms between control calls. 548 * 549 * @param newUpdateFreqHz Parameter to modify 550 * @return Itself 551 */ 552 @Override 553 public DifferentialMotionMagicVelocityDutyCycle withUpdateFreqHz(double newUpdateFreqHz) { 554 UpdateFreqHz = newUpdateFreqHz; 555 return this; 556 } 557 558 /** 559 * Sets the frequency at which this control will update. 560 * This is designated in Hertz, with a minimum of 20 Hz 561 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms). 562 * Some update frequencies are not supported and will be 563 * promoted up to the next highest supported frequency. 564 * <p> 565 * If this field is set to 0 Hz, the control request will 566 * be sent immediately as a one-shot frame. This may be useful 567 * for advanced applications that require outputs to be 568 * synchronized with data acquisition. In this case, we 569 * recommend not exceeding 50 ms between control calls. 570 * 571 * @param newUpdateFreqHz Parameter to modify 572 * @return Itself 573 */ 574 @Override 575 public DifferentialMotionMagicVelocityDutyCycle withUpdateFreqHz(Frequency newUpdateFreqHz) { 576 UpdateFreqHz = newUpdateFreqHz.in(Hertz); 577 return this; 578 } 579 580 @Override 581 public DifferentialMotionMagicVelocityDutyCycle clone() { 582 try { 583 return (DifferentialMotionMagicVelocityDutyCycle)super.clone(); 584 } catch (CloneNotSupportedException ex) { 585 /* this should never happen */ 586 throw new RuntimeException(ex); 587 } 588 } 589} 590