1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
//! The TachoMotor provides a uniform interface for using motors with positional
//! and directional feedback such as the EV3 and NXT motors.
//! This feedback allows for precise control of the motors.
use std::time::Duration;
use crate::{Ev3Error, Ev3Result};
use super::{LargeMotor, MediumMotor, MotorPort};
#[derive(Debug, Clone)]
enum TachoMotorInner {
LargeMotor { motor: LargeMotor },
MediumMotor { motor: MediumMotor },
}
/// The TachoMotor provides a uniform interface for using motors with positional
/// and directional feedback such as the EV3 and NXT motors.
/// This feedback allows for precise control of the motors.
/// EV3/NXT large servo motor
#[derive(Debug, Clone)]
pub struct TachoMotor {
inner: TachoMotorInner,
}
impl TachoMotor {
/// Try to get a `Self` on the given port. Returns `None` if port is not used or another device is connected.
pub fn get(port: MotorPort) -> Ev3Result<Self> {
let large_motor = LargeMotor::get(port);
if let Ok(motor) = large_motor {
return Ok(TachoMotor {
inner: TachoMotorInner::LargeMotor { motor },
});
}
let medium_motor = MediumMotor::get(port);
if let Ok(motor) = medium_motor {
return Ok(TachoMotor {
inner: TachoMotorInner::MediumMotor { motor },
});
}
Err(Ev3Error::InternalError {
msg: "Could not find a tacho motor at requested port!".to_owned(),
})
}
/// Try to find a `Self`. Only returns a motor if their is exactly one connected, `Error::NotFound` otherwise.
pub fn find() -> Ev3Result<Self> {
let large_motor = LargeMotor::find();
if let Ok(motor) = large_motor {
return Ok(TachoMotor {
inner: TachoMotorInner::LargeMotor { motor },
});
}
let medium_motor = MediumMotor::find();
if let Ok(motor) = medium_motor {
return Ok(TachoMotor {
inner: TachoMotorInner::MediumMotor { motor },
});
}
Err(Ev3Error::InternalError {
msg: "Could not find a tacho motor at requested port!".to_owned(),
})
}
/// Extract list of connected 'Self'
pub fn list() -> Ev3Result<Vec<Self>> {
let large_motor = LargeMotor::list()?;
let medium_motor = MediumMotor::list()?;
let mut vec = Vec::with_capacity(large_motor.len() + medium_motor.len());
vec.extend(large_motor.into_iter().map(|motor| TachoMotor {
inner: TachoMotorInner::LargeMotor { motor },
}));
vec.extend(medium_motor.into_iter().map(|motor| TachoMotor {
inner: TachoMotorInner::MediumMotor { motor },
}));
Ok(vec)
}
/// Try to convert this tacho motor to an `LargeMotor`, return `Self` if this fails.
pub fn into_large_motor(self) -> Result<LargeMotor, TachoMotor> {
match self.inner {
TachoMotorInner::LargeMotor { motor } => Ok(motor),
inner @ TachoMotorInner::MediumMotor { motor: _ } => Err(TachoMotor { inner }),
}
}
/// Try to convert this tacho motor to an `LargeMotor`, return `Self` if this fails.
pub fn into_medium_motor(self) -> Result<MediumMotor, TachoMotor> {
match self.inner {
inner @ TachoMotorInner::LargeMotor { motor: _ } => Err(TachoMotor { inner }),
TachoMotorInner::MediumMotor { motor } => Ok(motor),
}
}
/// Causes the motor to run until another command is sent.
pub const COMMAND_RUN_FOREVER: &'static str = "run-forever";
/// Runs the motor to an absolute position specified by `position_sp`
/// and then stops the motor using the command specified in `stop_action`.
pub const COMMAND_RUN_TO_ABS_POS: &'static str = "run-to-abs-pos";
/// Runs the motor to a position relative to the current position value.
/// The new position will be current `position` + `position_sp`.
/// When the new position is reached, the motor will stop using the command specified by `stop_action`.
pub const COMMAND_RUN_TO_REL_POS: &'static str = "run-to-rel-pos";
/// Run the motor for the amount of time specified in `time_sp`
/// and then stops the motor using the command specified by `stop_action`.
pub const COMMAND_RUN_TIMED: &'static str = "run-timed";
/// Runs the motor using the duty cycle specified by `duty_cycle_sp`.
/// Unlike other run commands, changing `duty_cycle_sp` while running will take effect immediately.
pub const COMMAND_RUN_DIRECT: &'static str = "run-direct";
/// Stop any of the run commands before they are complete using the command specified by `stop_action`.
pub const COMMAND_STOP: &'static str = "stop";
/// Resets all of the motor parameter attributes to their default values.
/// This will also have the effect of stopping the motor.
pub const COMMAND_RESET: &'static str = "reset";
/// A positive duty cycle will cause the motor to rotate clockwise.
pub const POLARITY_NORMAL: &'static str = "normal";
/// A positive duty cycle will cause the motor to rotate counter-clockwise.
pub const POLARITY_INVERSED: &'static str = "inversed";
/// Power is being sent to the motor.
pub const STATE_RUNNING: &'static str = "running";
/// The motor is ramping up or down and has not yet reached a pub constant output level.
pub const STATE_RAMPING: &'static str = "ramping";
/// The motor is not turning, but rather attempting to hold a fixed position.
pub const STATE_HOLDING: &'static str = "holding";
/// The motor is turning as fast as possible, but cannot reach its `speed_sp`.
pub const STATE_OVERLOADED: &'static str = "overloaded";
/// The motor is trying to run but is not turning at all.
pub const STATE_STALLED: &'static str = "stalled";
/// Removes power from the motor. The motor will freely coast to a stop.
pub const STOP_ACTION_COAST: &'static str = "coast";
/// Removes power from the motor and creates a passive electrical load.
/// This is usually done by shorting the motor terminals together.
/// This load will absorb the energy from the rotation of the motors
/// and cause the motor to stop more quickly than coasting.
pub const STOP_ACTION_BRAKE: &'static str = "brake";
/// Causes the motor to actively try to hold the current position.
/// If an external force tries to turn the motor, the motor will “push back” to maintain its position.
pub const STOP_ACTION_HOLD: &'static str = "hold";
/// Returns the number of tacho counts in one rotation of the motor.
///
/// Tacho counts are used by the position and speed attributes,
/// so you can use this value to convert from rotations or degrees to tacho counts.
/// (rotation motors only)
///
/// # Examples
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// // Get position and count_per_rot as f32.
/// let position = motor.get_position()? as f32;
/// let count_per_rot = motor.get_count_per_rot()? as f32;
///
/// // Calculate the rotation count.
/// let rotations = position / count_per_rot;
///
/// println!("The motor did {:.2} rotations", rotations);
/// # Ok(())
/// # }
/// ```
pub fn get_count_per_rot(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_count_per_rot(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_count_per_rot(),
}
}
/// Returns the number of tacho counts in one meter of travel of the motor.
///
/// Tacho counts are used by the position and speed attributes,
/// so you can use this value to convert from distance to tacho counts.
/// (linear motors only)
pub fn get_count_per_m(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_count_per_m(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_count_per_m(),
}
}
/// Returns the number of tacho counts in the full travel of the motor.
///
/// When combined with the count_per_m atribute,
/// you can use this value to calculate the maximum travel distance of the motor.
/// (linear motors only)
pub fn get_full_travel_count(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_full_travel_count(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_full_travel_count(),
}
}
/// Returns the current duty cycle of the motor. Units are percent.
///
/// Values are -100 to 100.
///
/// # Examples
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::thread;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// // Set the motor command `run-direct` to start rotation.
/// motor.run_direct()?;
///
/// // Rotate motor forward and wait 5 seconds.
/// motor.set_duty_cycle_sp(50)?;
/// thread::sleep(Duration::from_secs(5));
///
/// assert_eq!(motor.get_duty_cycle()?, 50);
/// # Ok(())
/// # }
pub fn get_duty_cycle(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_duty_cycle(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_duty_cycle(),
}
}
/// Returns the current duty cycle setpoint of the motor.
///
/// Units are in percent.
/// Valid values are -100 to 100. A negative value causes the motor to rotate in reverse.
///
/// # Examples
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::thread;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// // Rotate motor forward and wait 5 seconds.
/// motor.set_duty_cycle_sp(50)?;
/// thread::sleep(Duration::from_secs(5));
///
/// assert_eq!(motor.get_duty_cycle()?, 50);
/// # Ok(())
/// # }
pub fn get_duty_cycle_sp(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_duty_cycle_sp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_duty_cycle_sp(),
}
}
/// Sets the duty cycle setpoint of the motor.
///
/// Units are in percent.
/// Valid values are -100 to 100. A negative value causes the motor to rotate in reverse.
///
/// # Examples
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::thread;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// // Set the motor command `run-direct` to start rotation.
/// motor.run_direct()?;
///
/// // Rotate motor forward and wait 5 seconds.
/// motor.set_duty_cycle_sp(50)?;
/// thread::sleep(Duration::from_secs(5));
///
/// // Rotate motor backward and wait 5 seconds.
/// motor.set_duty_cycle_sp(-50)?;
/// thread::sleep(Duration::from_secs(5));
/// # Ok(())
/// # }
pub fn set_duty_cycle_sp(&self, duty_cycle: i32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_duty_cycle_sp(duty_cycle),
TachoMotorInner::MediumMotor { ref motor } => motor.set_duty_cycle_sp(duty_cycle),
}
}
/// Returns the current polarity of the motor.
pub fn get_polarity(&self) -> Ev3Result<String> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_polarity(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_polarity(),
}
}
/// Sets the polarity of the motor.
pub fn set_polarity(&self, polarity: &str) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_polarity(polarity),
TachoMotorInner::MediumMotor { ref motor } => motor.set_polarity(polarity),
}
}
/// Returns the current position of the motor in pulses of the rotary encoder.
///
/// When the motor rotates clockwise, the position will increase.
/// Likewise, rotating counter-clockwise causes the position to decrease.
/// The range is -2,147,483,648 and +2,147,483,647 tachometer counts (32-bit signed integer)
///
/// # Examples
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// // Get position and count_per_rot as f32.
/// let position = motor.get_position()? as f32;
/// let count_per_rot = motor.get_count_per_rot()? as f32;
///
/// // Calculate the rotation count.
/// let rotations: f32 = position / count_per_rot;
///
/// println!("The motor did {:.2} rotations", rotations);
/// # Ok(())
/// # }
/// ```
pub fn get_position(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_position(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_position(),
}
}
/// Sets the current position of the motor in pulses of the rotary encoder.
///
/// When the motor rotates clockwise, the position will increase.
/// Likewise, rotating counter-clockwise causes the position to decrease.
/// The range is -2,147,483,648 and +2,147,483,647 tachometer counts (32-bit signed integer)
///
/// # Examples
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// motor.set_position(0)?;
/// let position = motor.get_position()?;
///
/// // If the motor is not moving, the position value
/// // should not change between set and get operation.
/// assert_eq!(position, 0);
/// # Ok(())
/// # }
/// ```
pub fn set_position(&self, position: i32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_position(position),
TachoMotorInner::MediumMotor { ref motor } => motor.set_position(position),
}
}
/// Returns the proportional pub constant for the position PID.
pub fn get_hold_pid_kp(&self) -> Ev3Result<f32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_hold_pid_kp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_hold_pid_kp(),
}
}
/// Sets the proportional pub constant for the position PID.
pub fn set_hold_pid_kp(&self, kp: f32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_hold_pid_kp(kp),
TachoMotorInner::MediumMotor { ref motor } => motor.set_hold_pid_kp(kp),
}
}
/// Returns the integral pub constant for the position PID.
pub fn get_hold_pid_ki(&self) -> Ev3Result<f32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_hold_pid_ki(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_hold_pid_ki(),
}
}
/// Sets the integral pub constant for the position PID.
pub fn set_hold_pid_ki(&self, ki: f32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_hold_pid_ki(ki),
TachoMotorInner::MediumMotor { ref motor } => motor.set_hold_pid_ki(ki),
}
}
/// Returns the derivative pub constant for the position PID.
pub fn get_hold_pid_kd(&self) -> Ev3Result<f32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_hold_pid_kd(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_hold_pid_kd(),
}
}
/// Sets the derivative pub constant for the position PID.
pub fn set_hold_pid_kd(&self, kd: f32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_hold_pid_kd(kd),
TachoMotorInner::MediumMotor { ref motor } => motor.set_hold_pid_kd(kd),
}
}
/// Returns the maximum value that is accepted by the `speed_sp` attribute.
///
/// This value is the speed of the motor at 9V with no load.
/// Note: The actual maximum obtainable speed will be less than this
/// and will depend on battery voltage and mechanical load on the motor.
pub fn get_max_speed(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_max_speed(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_max_speed(),
}
}
/// Returns the current target position for the `run-to-abs-pos` and `run-to-rel-pos` commands.
///
/// Units are in tacho counts.
/// You can use the value returned by `counts_per_rot` to convert tacho counts to/from rotations or degrees.
///
/// The range is -2,147,483,648 and +2,147,483,647 tachometer counts (32-bit signed integer).
pub fn get_position_sp(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_position_sp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_position_sp(),
}
}
/// Sets the target position for the `run-to-abs-pos` and `run-to-rel-pos` commands.
///
/// Units are in tacho counts.
/// You can use the value returned by `counts_per_rot` to convert tacho counts to/from rotations or degrees.
///
/// The range is -2,147,483,648 and +2,147,483,647 tachometer counts (32-bit signed integer).
///
/// # Examples
///
/// ```ignore
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::thread;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// // Save the current position.
/// let old_position = motor.get_position()?;
///
/// // Rotate by 100 ticks
/// let position = motor.set_position_sp(100)?;
/// motor.run_to_rel_pos(None)?;
///
/// // Wait till rotation is finished.
/// motor.wait_until_not_moving(None);
///
/// // The new position should be 100 ticks larger.
/// let new_position = motor.get_position()?;
/// assert_eq!(old_position + 100, new_position);
/// # Ok(())
/// # }
/// ```
pub fn set_position_sp(&self, position_sp: i32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_position_sp(position_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.set_position_sp(position_sp),
}
}
/// Returns the current motor speed in tacho counts per second.
///
/// Note, this is not necessarily degrees (although it is for LEGO motors).
/// Use the `count_per_rot` attribute to convert this value to RPM or deg/sec.
pub fn get_speed(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_speed(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_speed(),
}
}
/// Returns the target speed in tacho counts per second used for all run-* commands except run-direct.
///
/// A negative value causes the motor to rotate in reverse
/// with the exception of run-to-*-pos commands where the sign is ignored.
/// Use the `count_per_rot` attribute to convert RPM or deg/sec to tacho counts per second.
/// Use the `count_per_m` attribute to convert m/s to tacho counts per second.
pub fn get_speed_sp(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_speed_sp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_speed_sp(),
}
}
/// Sets the target speed in tacho counts per second used for all run-* commands except run-direct.
///
/// A negative value causes the motor to rotate in reverse
/// with the exception of run-to-*-pos commands where the sign is ignored.
/// Use the `count_per_rot` attribute to convert RPM or deg/sec to tacho counts per second.
/// Use the `count_per_m` attribute to convert m/s to tacho counts per second.
pub fn set_speed_sp(&self, speed_sp: i32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_speed_sp(speed_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.set_speed_sp(speed_sp),
}
}
/// Returns the current ramp up setpoint.
///
/// Units are in milliseconds and must be positive. When set to a non-zero value,
/// the motor speed will increase from 0 to 100% of `max_speed` over the span of this setpoint.
/// The actual ramp time is the ratio of the difference between the speed_sp
/// and the current speed and max_speed multiplied by ramp_up_sp. Values must not be negative.
pub fn get_ramp_up_sp(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_ramp_up_sp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_ramp_up_sp(),
}
}
/// Sets the ramp up setpoint.
///
/// Units are in milliseconds and must be positive. When set to a non-zero value,
/// the motor speed will increase from 0 to 100% of `max_speed` over the span of this setpoint.
/// The actual ramp time is the ratio of the difference between the speed_sp
/// and the current speed and max_speed multiplied by ramp_up_sp. Values must not be negative.
pub fn set_ramp_up_sp(&self, ramp_up_sp: i32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_ramp_up_sp(ramp_up_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.set_ramp_up_sp(ramp_up_sp),
}
}
/// Returns the current ramp down setpoint.
///
/// Units are in milliseconds and must be positive. When set to a non-zero value,
/// the motor speed will decrease from 100% down to 0 of `max_speed` over the span of this setpoint.
/// The actual ramp time is the ratio of the difference between the speed_sp
/// and the current speed and 0 multiplied by ramp_down_sp. Values must not be negative.
pub fn get_ramp_down_sp(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_ramp_down_sp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_ramp_down_sp(),
}
}
/// Sets the ramp down setpoint.
///
/// Units are in milliseconds and must be positive. When set to a non-zero value,
/// the motor speed will decrease from 100% down to 0 of `max_speed` over the span of this setpoint.
/// The actual ramp time is the ratio of the difference between the speed_sp
/// and the current speed and 0 multiplied by ramp_down_sp. Values must not be negative.
pub fn set_ramp_down_sp(&self, ramp_down_sp: i32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_ramp_down_sp(ramp_down_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.set_ramp_down_sp(ramp_down_sp),
}
}
/// Returns the proportional pub constant for the speed regulation PID.
pub fn get_speed_pid_kp(&self) -> Ev3Result<f32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_speed_pid_kp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_speed_pid_kp(),
}
}
/// Sets the proportional pub constant for the speed regulation PID.
pub fn set_speed_pid_kp(&self, kp: f32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_speed_pid_kp(kp),
TachoMotorInner::MediumMotor { ref motor } => motor.set_speed_pid_kp(kp),
}
}
/// Returns the integral pub constant for the speed regulation PID.
pub fn get_speed_pid_ki(&self) -> Ev3Result<f32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_speed_pid_ki(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_speed_pid_ki(),
}
}
/// Sets the integral pub constant for the speed regulation PID.
pub fn set_speed_pid_ki(&self, ki: f32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_speed_pid_ki(ki),
TachoMotorInner::MediumMotor { ref motor } => motor.set_speed_pid_ki(ki),
}
}
/// Returns the derivative pub constant for the speed regulation PID.
pub fn get_speed_pid_kd(&self) -> Ev3Result<f32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_speed_pid_kd(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_speed_pid_kd(),
}
}
/// Sets the derivative pub constant for the speed regulation PID.
pub fn set_speed_pid_kd(&self, kd: f32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_speed_pid_kd(kd),
TachoMotorInner::MediumMotor { ref motor } => motor.set_speed_pid_kd(kd),
}
}
/// Returns a list of state flags.
pub fn get_state(&self) -> Ev3Result<Vec<String>> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_state(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_state(),
}
}
/// Returns the current stop action.
///
/// The value determines the motors behavior when command is set to stop.
pub fn get_stop_action(&self) -> Ev3Result<String> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_stop_action(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_stop_action(),
}
}
/// Sets the stop action.
///
/// The value determines the motors behavior when command is set to stop.
pub fn set_stop_action(&self, stop_action: &str) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_stop_action(stop_action),
TachoMotorInner::MediumMotor { ref motor } => motor.set_stop_action(stop_action),
}
}
/// Returns a list of stop actions supported by the motor controller.
pub fn get_stop_actions(&self) -> Ev3Result<Vec<String>> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_stop_actions(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_stop_actions(),
}
}
/// Returns the current amount of time the motor will run when using the run-timed command.
///
/// Units are in milliseconds. Values must not be negative.
pub fn get_time_sp(&self) -> Ev3Result<i32> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.get_time_sp(),
TachoMotorInner::MediumMotor { ref motor } => motor.get_time_sp(),
}
}
/// Sets the amount of time the motor will run when using the run-timed command.
///
/// Units are in milliseconds. Values must not be negative.
pub fn set_time_sp(&self, time_sp: i32) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.set_time_sp(time_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.set_time_sp(time_sp),
}
}
/// Runs the motor using the duty cycle specified by `duty_cycle_sp`.
///
/// Unlike other run commands, changing `duty_cycle_sp` while running will take effect immediately.
pub fn run_direct(&self) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.run_direct(),
TachoMotorInner::MediumMotor { ref motor } => motor.run_direct(),
}
}
/// Causes the motor to run until another command is sent.
pub fn run_forever(&self) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.run_forever(),
TachoMotorInner::MediumMotor { ref motor } => motor.run_forever(),
}
}
/// Runs the motor to an absolute position specified by `position_sp`
///
/// and then stops the motor using the command specified in `stop_action`.
pub fn run_to_abs_pos(&self, position_sp: Option<i32>) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.run_to_abs_pos(position_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.run_to_abs_pos(position_sp),
}
}
/// Runs the motor to a position relative to the current position value.
///
/// The new position will be current `position` + `position_sp`.
/// When the new position is reached, the motor will stop using the command specified by `stop_action`.
pub fn run_to_rel_pos(&self, position_sp: Option<i32>) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.run_to_rel_pos(position_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.run_to_rel_pos(position_sp),
}
}
/// Run the motor for the amount of time specified in `time_sp`
///
/// and then stops the motor using the command specified by `stop_action`.
pub fn run_timed(&self, time_sp: Option<Duration>) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.run_timed(time_sp),
TachoMotorInner::MediumMotor { ref motor } => motor.run_timed(time_sp),
}
}
/// Stop any of the run commands before they are complete using the command specified by `stop_action`.
pub fn stop(&self) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.stop(),
TachoMotorInner::MediumMotor { ref motor } => motor.stop(),
}
}
/// Resets all of the motor parameter attributes to their default values.
/// This will also have the effect of stopping the motor.
pub fn reset(&self) -> Ev3Result<()> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.reset(),
TachoMotorInner::MediumMotor { ref motor } => motor.reset(),
}
}
/// Power is being sent to the motor.
pub fn is_running(&self) -> Ev3Result<bool> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.is_running(),
TachoMotorInner::MediumMotor { ref motor } => motor.is_running(),
}
}
/// The motor is ramping up or down and has not yet reached a pub constant output level.
pub fn is_ramping(&self) -> Ev3Result<bool> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.is_ramping(),
TachoMotorInner::MediumMotor { ref motor } => motor.is_ramping(),
}
}
/// The motor is not turning, but rather attempting to hold a fixed position.
pub fn is_holding(&self) -> Ev3Result<bool> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.is_holding(),
TachoMotorInner::MediumMotor { ref motor } => motor.is_holding(),
}
}
/// The motor is turning as fast as possible, but cannot reach its `speed_sp`.
pub fn is_overloaded(&self) -> Ev3Result<bool> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.is_overloaded(),
TachoMotorInner::MediumMotor { ref motor } => motor.is_overloaded(),
}
}
/// The motor is trying to run but is not turning at all.
pub fn is_stalled(&self) -> Ev3Result<bool> {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.is_stalled(),
TachoMotorInner::MediumMotor { ref motor } => motor.is_stalled(),
}
}
/// Wait until condition `cond` returns true or the `timeout` is reached.
///
/// The condition is checked when to the `state` attribute has changed.
/// If the `timeout` is `None` it will wait an infinite time.
///
/// # Examples
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// motor.run_timed(Some(Duration::from_secs(5)))?;
///
/// let cond = || {
/// motor.get_state()
/// .unwrap_or_else(|_| vec![])
/// .iter()
/// .all(|s| s != LargeMotor::STATE_RUNNING)
/// };
/// motor.wait(cond, None);
///
/// println!("Motor has stopped!");
/// # Ok(())
/// # }
/// ```
#[cfg(target_os = "linux")]
pub fn wait<F>(&self, cond: F, timeout: Option<Duration>) -> bool
where
F: Fn() -> bool,
{
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.wait(cond, timeout),
TachoMotorInner::MediumMotor { ref motor } => motor.wait(cond, timeout),
}
}
/// Wait while the `state` is in the vector `self.get_state()` or the `timeout` is reached.
///
/// If the `timeout` is `None` it will wait an infinite time.
///
/// # Example
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// motor.run_timed(Some(Duration::from_secs(5)))?;
///
/// motor.wait_while(LargeMotor::STATE_RUNNING, None);
///
/// println!("Motor has stopped!");
/// # Ok(())
/// # }
/// ```
#[cfg(target_os = "linux")]
pub fn wait_while(&self, state: &str, timeout: Option<Duration>) -> bool {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.wait_while(state, timeout),
TachoMotorInner::MediumMotor { ref motor } => motor.wait_while(state, timeout),
}
}
/// Wait until the `state` is in the vector `self.get_state()` or the `timeout` is reached.
///
/// If the `timeout` is `None` it will wait an infinite time.
///
/// # Example
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// motor.run_timed(Some(Duration::from_secs(5)))?;
///
/// motor.wait_until(LargeMotor::STATE_RUNNING, None);
///
/// println!("Motor has started!");
/// # Ok(())
/// # }
/// ```
#[cfg(target_os = "linux")]
pub fn wait_until(&self, state: &str, timeout: Option<Duration>) -> bool {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.wait_until(state, timeout),
TachoMotorInner::MediumMotor { ref motor } => motor.wait_until(state, timeout),
}
}
/// Wait until the motor is not moving or the timeout is reached.
///
/// This is euqal to `wait_while(STATE_RUNNING, timeout)`.
/// If the `timeout` is `None` it will wait an infinite time.
///
/// # Example
///
/// ```no_run
/// use ev3dev_lang_rust::motors::LargeMotor;
/// use std::time::Duration;
///
/// # fn main() -> ev3dev_lang_rust::Ev3Result<()> {
/// // Init a tacho motor.
/// let motor = LargeMotor::find()?;
///
/// motor.run_timed(Some(Duration::from_secs(5)))?;
///
/// motor.wait_until_not_moving(None);
///
/// println!("Motor has stopped!");
/// # Ok(())
/// # }
/// ```
#[cfg(target_os = "linux")]
pub fn wait_until_not_moving(&self, timeout: Option<Duration>) -> bool {
match self.inner {
TachoMotorInner::LargeMotor { ref motor } => motor.wait_until_not_moving(timeout),
TachoMotorInner::MediumMotor { ref motor } => motor.wait_until_not_moving(timeout),
}
}
}
impl From<LargeMotor> for TachoMotor {
fn from(motor: LargeMotor) -> Self {
Self {
inner: TachoMotorInner::LargeMotor { motor },
}
}
}
impl From<MediumMotor> for TachoMotor {
fn from(motor: MediumMotor) -> Self {
Self {
inner: TachoMotorInner::MediumMotor { motor },
}
}
}