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 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
//! # Lynxmotion Smart Servo Driver //! //! [![Crates.io](https://img.shields.io/crates/v/lss_driver.svg)](https://crates.io/crates/lss_driver) //! [![Docs](https://docs.rs/lss_driver/badge.svg)](https://docs.rs/lss_driver) //! [![Rust](https://github.com/dmweis/lss_driver/workflows/Rust/badge.svg)](https://github.com/dmweis/lss_driver/actions) //! [![codecov](https://codecov.io/gh/dmweis/lss_driver/branch/master/graph/badge.svg)](https://codecov.io/gh/dmweis/lss_driver) //! [![License](https://img.shields.io/crates/l/lss_driver)](https://github.com/dmweis/lss_driver) //! //! This crate provides an asynchronous serial driver the the Lynxmotion smart servos. //! //! You can read more about the servos on the official [robotshop wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/) //! //! ## Driver //! //! The Smart servos are controlled over a serial UART protocol. //! //! You can read about the protocol [here](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/) //! It's highly recommended to at least skim the protocol page before trying to use this driver. //! //! This driver currently doesn't implement all features from the protocol. //! Some missing features are modifiers and setup commands. //! If there are any missing commands or features you'd like added feel free to raise a PR or an issue. //! //! This driver uses `async/await`. As a result you will need to use an async runtime. //! The driver is based on [tokio-serial](https://github.com/berkowski/tokio-serial) so tokio would be a good choice but any should work. //! //! ## Usage //! //! This crate comes with multiple [examples](https://github.com/dmweis/lss_driver/tree/master/examples). //! These are a good start if you want to learn how to use it. //! //! ```no_run //! //! #[tokio::main] //! async fn main() { //! // Create a driver on port `COM14` or `/dev/ttyUSB0`... //! let mut driver = lss_driver::LSSDriver::new("COM14").unwrap(); //! // In case there is only one servo connected //! // we can query it's ID using the broadcast ID //! let id = driver.query_id(lss_driver::BROADCAST_ID).await.unwrap(); //! // move motor with ID 5 to 90.0 degrees //! driver.move_to_position(5, 90.0).await.unwrap(); //! // Set color of servo with ID 5 to Magenta //! driver.set_color(5, lss_driver::LedColor::Magenta).await.unwrap(); //! } //! //! ``` //! //! ## Building //! //! This package shouldn't depend on any native libraries. //! Rust [serialport](https://gitlab.com/susurrus/serialport-rs) depends on `pkg-config` and `libudev-dev` on GNU Linux but they should be disabled for this crate. //! If you do run into issues with them failing it may be worth looking into their dependencies and raising an issue here. //! //! ## Disclaimer //! //! _This software is not officially endorsed by Lynxmotion or Robotshop!_ //! //! All product names, logos, and brands are property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement. mod message_types; mod serial_driver; pub use message_types::*; use serial_driver::{FramedDriver, FramedSerialDriver, LssCommand}; use std::str; /// ID used to talk to all motors on a bus at once pub const BROADCAST_ID: u8 = 254; type DriverResult<T> = Result<T, LssDriverError>; /// Driver for the LSS servo pub struct LSSDriver { driver: Box<dyn FramedDriver + Send + Sync>, } impl LSSDriver { /// Create new driver on a serial port with default settings /// /// Default baud_rate is 115200 /// /// # Arguments /// /// * `post` - Port to use. e.g. COM1 or /dev/ttyACM0 /// /// # Example /// /// ```no_run /// use lss_driver::LSSDriver; /// let mut driver = LSSDriver::new("COM1").unwrap(); /// ``` pub fn new(port: &str) -> DriverResult<LSSDriver> { let driver = FramedSerialDriver::new(port)?; Ok(LSSDriver { driver: Box::new(driver), }) } /// Create new driver on a serial port with custom baud rate /// /// # Arguments /// /// * `post` - Port to use. e.g. COM1 or /dev/ttyACM0 /// * `baud_rate` - Baudrate. e.g. 115200 /// /// # Example /// /// ```no_run /// use lss_driver::LSSDriver; /// let mut driver = LSSDriver::with_baud_rate("COM1", 115200).unwrap(); /// ``` pub fn with_baud_rate(port: &str, baud_rate: u32) -> DriverResult<LSSDriver> { let driver = FramedSerialDriver::with_baud_rate(port, baud_rate)?; Ok(LSSDriver { driver: Box::new(driver), }) } /// Creates new LSS driver with a custom implementation of the transport /// /// This is used for tests and can be used if you want to reimplement the driver over network pub fn with_driver(driver: Box<dyn FramedDriver + Send + Sync>) -> LSSDriver { LSSDriver { driver } } /// Soft reset /// This command does a "soft reset" and reverts all commands to those stored in EEPROM /// /// [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HReset) /// /// # Arguments /// /// * `id` - ID of servo you want to reset pub async fn reset(&mut self, id: u8) -> DriverResult<()> { self.driver.send(LssCommand::simple(id, "RESET")).await?; Ok(()) } /// Query value of ID /// Especially useful with BROADCAST_ID /// /// [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HIdentificationNumber28ID29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// /// # Example /// /// ```no_run /// use lss_driver::LSSDriver; /// async fn async_main(){ /// let mut driver = LSSDriver::with_baud_rate("COM1", 115200).unwrap(); /// let id = driver.query_id(lss_driver::BROADCAST_ID).await.unwrap(); /// } /// ``` pub async fn query_id(&mut self, id: u8) -> DriverResult<u8> { self.driver.send(LssCommand::simple(id, "QID")).await?; let response = self.driver.receive().await?; let value = response.get_val("QID")?; Ok(value as u8) } /// Set value of ID /// Saved to EEPROM /// Only takes effect after restart /// /// [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HIdentificationNumber28ID29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `new_id` - ID You want that servo to have pub async fn set_id(&mut self, id: u8, new_id: u8) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "CID", new_id as i32)) .await?; Ok(()) } /// set color for driver with id /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `color` - Color to set pub async fn set_color(&mut self, id: u8, color: LedColor) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "LED", color as i32)) .await?; Ok(()) } /// Query color of servo LED /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_color(&mut self, id: u8) -> DriverResult<LedColor> { self.driver.send(LssCommand::simple(id, "QLED")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QLED")?; Ok(LedColor::from_i32(value)?) } /// Move to absolute position in degrees /// /// Supports virtual positions that are more than 360 degrees /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `position` - Absolute position in degrees /// /// # Example /// /// ```no_run /// use lss_driver::LSSDriver; /// async fn async_main(){ /// let mut driver = LSSDriver::with_baud_rate("COM1", 115200).unwrap(); /// driver.move_to_position(5, 180.0).await; /// driver.move_to_position(5, 480.0).await; /// } /// ``` pub async fn move_to_position(&mut self, id: u8, position: f32) -> DriverResult<()> { let angle = (position * 10.0).round() as i32; self.driver .send(LssCommand::with_param(id, "D", angle)) .await?; Ok(()) } /// Move to absolute position in degrees /// /// Same as `move_to_position` /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `position` - Absolute position in degrees /// /// # Example /// /// ```no_run /// use lss_driver::LSSDriver; /// async fn async_main(){ /// let mut driver = LSSDriver::with_baud_rate("COM1", 115200).unwrap(); /// driver.set_target_position(5, 180.0).await; /// driver.set_target_position(5, 480.0).await; /// } /// ``` pub async fn set_target_position(&mut self, id: u8, position: f32) -> DriverResult<()> { self.move_to_position(id, position).await } /// Query absolute current position in degrees /// /// Supports virtual positions that are more than 360 degrees /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_position(&mut self, id: u8) -> DriverResult<f32> { self.driver.send(LssCommand::simple(id, "QD")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QD")?; Ok(value as f32 / 10.0) } /// Query absolute target position in degrees /// /// Supports virtual positions that are more than 360 degrees /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_target_position(&mut self, id: u8) -> DriverResult<f32> { self.driver.send(LssCommand::simple(id, "QDT")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QDT")?; Ok(value as f32 / 10.0) } /// Set continuous rotation speed in °/s /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `speed` - Speed in °/s pub async fn set_rotation_speed(&mut self, id: u8, speed: f32) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "WD", speed as i32)) .await?; Ok(()) } /// Query absolute rotation speed in °/s /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_rotation_speed(&mut self, id: u8) -> DriverResult<f32> { self.driver.send(LssCommand::simple(id, "QWD")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QWD")?; Ok(value as f32) } /// Query status of a motor /// /// View more on [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HQueryStatus28Q29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_status(&mut self, id: u8) -> DriverResult<MotorStatus> { self.driver.send(LssCommand::simple(id, "Q")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("Q")?; Ok(MotorStatus::from_i32(value)?) } /// Query safety status of a motor /// /// View more on [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HQueryStatus28Q29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_safety_status(&mut self, id: u8) -> DriverResult<SafeModeStatus> { self.driver.send(LssCommand::simple(id, "Q1")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("Q")?; Ok(SafeModeStatus::from_i32(value)?) } /// Set motion profile enabled or disabled. /// If the motion profile is enabled, angular acceleration (AA) and angular deceleration(AD) will have an effect on the motion. Also, SD/S and T modifiers can be used. /// /// With motion profile enabled servos will follow a motion curve /// With motion profile disabled servos move towards target location at full speed /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `motion_profile` - set motion profile on/off pub async fn set_motion_profile(&mut self, id: u8, motion_profile: bool) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "EM", motion_profile as i32)) .await?; Ok(()) } /// query motion profile enabled or disabled. /// If the motion profile is enabled, angular acceleration (AA) and angular deceleration(AD) will have an effect on the motion. Also, SD/S and T modifiers can be used. /// /// With motion profile enabled servos will follow a motion curve /// With motion profile disabled servos move towards target location at full speed /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_motion_profile(&mut self, id: u8) -> DriverResult<bool> { self.driver.send(LssCommand::simple(id, "QEM")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QEM")?; Ok(value != 0) } /// Set filter position count /// /// Change the Filter Position Count value for this session. /// Affects motion only when motion profile is disabled (EM0) /// /// more info at the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HFilterPositionCount28FPC29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `filter_position_count` - default if 5 pub async fn set_filter_position_count( &mut self, id: u8, filter_position_count: u8, ) -> DriverResult<()> { self.driver .send(LssCommand::with_param( id, "FPC", filter_position_count as i32, )) .await?; Ok(()) } /// Query filter position count /// /// Query the Filter Position Count value. /// Affects motion only when motion profile is disabled (EM0) /// /// more info at the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HFilterPositionCount28FPC29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_filter_position_count(&mut self, id: u8) -> DriverResult<u8> { self.driver.send(LssCommand::simple(id, "QFPC")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QFPC")?; Ok(value as u8) } /// Set angular stiffness /// /// Read more about [Angular stiffness](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularStiffness28AS29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `angular_stiffness` - value for angular stiffness (-10 to 10) (recommended -4 to 4) pub async fn set_angular_stiffness( &mut self, id: u8, angular_stiffness: i32, ) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "AS", angular_stiffness)) .await?; Ok(()) } /// Query angular stiffness /// /// Read more about [Angular stiffness](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularStiffness28AS29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_angular_stiffness(&mut self, id: u8) -> DriverResult<i32> { self.driver.send(LssCommand::simple(id, "QAS")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QAS")?; Ok(value) } /// Set angular holding stiffness /// /// Read more about [Angular holding stiffness](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularHoldingStiffness28AH29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `angular_holding` - value for angular holding stiffness (-10 to 10) pub async fn set_angular_holding_stiffness( &mut self, id: u8, angular_holding: i32, ) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "AH", angular_holding)) .await?; Ok(()) } /// Query angular holding stiffness /// /// Read more about [Angular holding stiffness](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularHoldingStiffness28AH29) /// /// # Arguments /// /// * `id` - ID of servo you want to control pub async fn query_angular_holding_stiffness(&mut self, id: u8) -> DriverResult<i32> { self.driver.send(LssCommand::simple(id, "QAH")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QAH")?; Ok(value) } /// Set angular acceleration in degrees per second squared (°/s2) /// /// Accepts values between 1 and 100. Increments of 10 /// Only used when motion profile is enabled /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularAcceleration28AA29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `angular_acceleration` - value for angular acceleration (1 to 100, Increments 10) pub async fn set_angular_acceleration( &mut self, id: u8, angular_acceleration: i32, ) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "AA", angular_acceleration)) .await?; Ok(()) } /// Query angular acceleration in degrees per second squared (°/s2) /// /// Accepts values between 1 and 100. Increments of 10 /// Only used when motion profile is enabled /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularAcceleration28AA29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_angular_acceleration(&mut self, id: u8) -> DriverResult<i32> { self.driver.send(LssCommand::simple(id, "QAA")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QAA")?; Ok(value) } /// Set angular deceleration in degrees per second squared (°/s2) /// /// Accepts values between 1 and 100. Increments of 10 /// Only used when motion profile is enabled /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularDeceleration28AD29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `angular_deceleration` - value for angular deceleration (1 to 100, Increments 10) pub async fn set_angular_deceleration( &mut self, id: u8, angular_deceleration: i32, ) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "AD", angular_deceleration)) .await?; Ok(()) } /// Query angular deceleration in degrees per second squared (°/s2) /// /// Accepts values between 1 and 100. Increments of 10 /// Only used when motion profile is enabled /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HAngularDeceleration28AD29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_angular_deceleration(&mut self, id: u8) -> DriverResult<i32> { self.driver.send(LssCommand::simple(id, "QAD")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QAD")?; Ok(value) } /// Set maximum motor duty /// /// Accepts values between 255 and 1023 /// Only used when motion profile is disabled /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HMaximumMotorDuty28MMD29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `maximum_motor_duty` - value for maximum motor duty (255 to 1023) pub async fn set_maximum_motor_duty( &mut self, id: u8, maximum_motor_duty: i32, ) -> DriverResult<()> { self.driver .send(LssCommand::with_param(id, "MMD", maximum_motor_duty)) .await?; Ok(()) } /// Query maximum motor duty /// /// Accepts values between 255 and 1023 /// Only used when motion profile is disabled /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HMaximumMotorDuty28MMD29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_maximum_motor_duty(&mut self, id: u8) -> DriverResult<i32> { self.driver.send(LssCommand::simple(id, "QMMD")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QMMD")?; Ok(value) } /// Set maximum speed in degrees per second /// /// Accepts values up to 180.0 /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HMaximumSpeedinDegrees28SD29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `maximum_speed` - value for maximum speed pub async fn set_maximum_speed(&mut self, id: u8, maximum_speed: f32) -> DriverResult<()> { self.driver .send(LssCommand::with_param( id, "SD", (maximum_speed * 10.) as i32, )) .await?; Ok(()) } /// Query maximum speed in degrees per second /// /// Accepts values up to 180.0 /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HMaximumSpeedinDegrees28SD29) /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_maximum_speed(&mut self, id: u8) -> DriverResult<f32> { self.driver.send(LssCommand::simple(id, "QSD")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QSD")?; Ok(value as f32 / 10.) } /// Disables power to motor allowing it to be back driven /// /// # Arguments /// /// * `id` - ID of servo you want to control pub async fn limp(&mut self, id: u8) -> DriverResult<()> { self.driver.send(LssCommand::simple(id, "L")).await?; Ok(()) } /// Stops any ongoing motor motion and actively holds position /// /// # Arguments /// /// * `id` - ID of servo you want to control pub async fn halt_hold(&mut self, id: u8) -> DriverResult<()> { self.driver.send(LssCommand::simple(id, "H")).await?; Ok(()) } /// Query voltage of motor in volts /// /// # Arguments /// /// * `id` - ID of servo you want to Query pub async fn query_voltage(&mut self, id: u8) -> DriverResult<f32> { // response message looks like *5QV11200<cr> // Response is in mV self.driver.send(LssCommand::simple(id, "QV")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QV")?; Ok(value as f32 / 1000.0) } /// Query temperature of motor in celsius /// /// # Arguments /// /// * `id` - ID of servo you want to Query pub async fn query_temperature(&mut self, id: u8) -> DriverResult<f32> { // response message looks like *5QT441<cr> // Response is in 10s of celsius // 441 would be 44.1 celsius self.driver.send(LssCommand::simple(id, "QT")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QT")?; Ok(value as f32 / 10.0) } /// Query current of motor in Amps /// /// # Arguments /// /// * `id` - ID of servo you want to Query pub async fn query_current(&mut self, id: u8) -> DriverResult<f32> { // response message looks like *5QT441<cr> // Response is in mA self.driver.send(LssCommand::simple(id, "QC")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate("QC")?; Ok(value as f32 / 1000.0) } /// Query model string /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_model(&mut self, id: u8) -> DriverResult<Model> { self.driver.send(LssCommand::simple(id, "QMS")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate_string("QMS")?; Ok(Model::from_str(&value)) } /// Query firmware version /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_firmware_version(&mut self, id: u8) -> DriverResult<String> { self.driver.send(LssCommand::simple(id, "QF")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate_string("QF")?; Ok(value) } /// Query serial number /// /// # Arguments /// /// * `id` - ID of servo you want to query pub async fn query_serial_number(&mut self, id: u8) -> DriverResult<String> { self.driver.send(LssCommand::simple(id, "QN")).await?; let response = self.driver.receive().await?; let (_, value) = response.separate_string("QN")?; Ok(value) } /// Set LED blinking mode /// /// Read more on the [wiki](https://www.robotshop.com/info/wiki/lynxmotion/view/lynxmotion-smart-servo/lss-communication-protocol/#HConfigureLEDBlinking28CLB29) /// /// # Arguments /// /// * `id` - ID of servo you want to control /// * `blinking_mode` - Blinking mode desired. Can be combination to make motor blink during multiple modes pub async fn set_led_blinking( &mut self, id: u8, blinking_mode: Vec<LedBlinking>, ) -> DriverResult<()> { let sum = blinking_mode .iter() .map(|item| *item as i32) .sum::<i32>() .min(LedBlinking::AlwaysBlink as i32); self.driver .send(LssCommand::with_param(id, "CLB", sum)) .await?; Ok(()) } } #[cfg(test)] mod tests { use super::serial_driver::LssResponse; use super::*; use approx::assert_relative_eq; use async_trait::async_trait; struct MockedDriver { expected_send: Vec<String>, receive: Vec<String>, } #[async_trait] impl FramedDriver for MockedDriver { async fn send(&mut self, command: LssCommand) -> DriverResult<()> { let expected = self.expected_send.pop().unwrap(); assert_eq!(expected, command.as_str().to_owned()); Ok(()) } async fn receive(&mut self) -> DriverResult<LssResponse> { Ok(LssResponse::new(self.receive.pop().unwrap())) } } #[tokio::test] async fn async_test_builds() {} #[tokio::test] async fn test_limp_color_move_hold() { let mocked_framed_driver = MockedDriver { expected_send: vec![ "#5QV\r".to_owned(), "#4H\r".to_owned(), "#3D1800\r".to_owned(), "#2LED1\r".to_owned(), "#1L\r".to_owned(), ], receive: vec!["*5QV11200\r".to_owned()], }; let mut driver = LSSDriver::with_driver(Box::new(mocked_framed_driver)); driver.limp(1).await.unwrap(); driver.set_color(2, LedColor::Red).await.unwrap(); driver.move_to_position(3, 180.0).await.unwrap(); driver.halt_hold(4).await.unwrap(); let voltage = driver.query_voltage(5).await.unwrap(); assert_relative_eq!(voltage, 11.2); } macro_rules! test_command { ($name:ident, $expected:expr, $command:expr) => { #[tokio::test] #[allow(clippy::redundant_closure_call)] async fn $name() { let mocked_framed_driver = MockedDriver { expected_send: vec![$expected.to_owned()], receive: vec![], }; let driver = LSSDriver::with_driver(Box::new(mocked_framed_driver)); ($command)(driver).await; } }; } macro_rules! test_query { ($name:ident, $expected:expr, $recv:expr, $command:expr, $val:expr) => { #[tokio::test] #[allow(clippy::redundant_closure_call)] async fn $name() { let mocked_framed_driver = MockedDriver { expected_send: vec![$expected.to_owned()], receive: vec![$recv.to_owned()], }; let driver = LSSDriver::with_driver(Box::new(mocked_framed_driver)); let res = ($command)(driver).await; assert_eq!(res, $val); } }; } macro_rules! test_query_float { ($name:ident, $expected:expr, $recv:expr, $command:expr, $val:expr) => { #[tokio::test] #[allow(clippy::redundant_closure_call)] async fn $name() { let mocked_framed_driver = MockedDriver { expected_send: vec![$expected.to_owned()], receive: vec![$recv.to_owned()], }; let driver = LSSDriver::with_driver(Box::new(mocked_framed_driver)); let res = ($command)(driver).await; assert_relative_eq!(res, $val); } }; } test_command!( test_hold_command, "#4H\r", |mut driver: LSSDriver| async move { driver.halt_hold(4_u8).await.unwrap(); } ); test_query!( test_query_id, "#254QID\r", "*QID5\r", |mut driver: LSSDriver| async move { driver.query_id(BROADCAST_ID).await.unwrap() }, 5 ); test_command!( test_set_id, "#1CID2\r", |mut driver: LSSDriver| async move { driver.set_id(1, 2).await.unwrap() } ); // Motion test_command!( test_move_to, "#1D200\r", |mut driver: LSSDriver| async move { driver.move_to_position(1, 20.0).await.unwrap() } ); test_command!( test_set_target_position, "#1D200\r", |mut driver: LSSDriver| async move { driver.set_target_position(1, 20.0).await.unwrap() } ); test_query_float!( test_query_current_position, "#5QD\r", "*5QD132\r", |mut driver: LSSDriver| async move { driver.query_position(5).await.unwrap() }, 13.2 ); test_query_float!( test_query_target_position, "#5QDT\r", "*5QDT6783\r", |mut driver: LSSDriver| async move { driver.query_target_position(5).await.unwrap() }, 678.3 ); // Wheel mode test_command!( test_set_rotation_speed_degrees, "#5WD90\r", |mut driver: LSSDriver| async move { driver.set_rotation_speed(5, 90.0).await.unwrap() } ); test_query_float!( test_query_rotation_speed_degrees, "#5QWD\r", "*5QWD90\r", |mut driver: LSSDriver| async move { driver.query_rotation_speed(5).await.unwrap() }, 90.0 ); // Status test_query!( test_unknown_status, "#5Q\r", "*5Q0\r", |mut driver: LSSDriver| async move { driver.query_status(5).await.unwrap() }, MotorStatus::Unknown ); test_query!( test_holding_status, "#5Q\r", "*5Q6\r", |mut driver: LSSDriver| async move { driver.query_status(5).await.unwrap() }, MotorStatus::Holding ); test_query!( test_safety_status, "#5Q1\r", "*5Q3\r", |mut driver: LSSDriver| async move { driver.query_safety_status(5).await.unwrap() }, SafeModeStatus::TemperatureLimit ); test_command!(test_limp, "#5L\r", |mut driver: LSSDriver| async move { driver.limp(5).await.unwrap() }); test_command!( test_halt_hold, "#5H\r", |mut driver: LSSDriver| async move { driver.halt_hold(5).await.unwrap() } ); // LED test_command!( test_set_led, "#5LED3\r", |mut driver: LSSDriver| async move { driver.set_color(5, LedColor::Blue).await.unwrap() } ); test_query!( test_query_led, "#5QLED\r", "*5QLED5\r", |mut driver: LSSDriver| async move { driver.query_color(5).await.unwrap() }, LedColor::Cyan ); // motion profile test_command!( test_motion_profile_on, "#5EM1\r", |mut driver: LSSDriver| async move { driver.set_motion_profile(5, true).await.unwrap() } ); test_command!( test_motion_profile_off, "#5EM0\r", |mut driver: LSSDriver| async move { driver.set_motion_profile(5, false).await.unwrap() } ); test_query!( test_query_motion_profile_on, "#5QEM\r", "*5QEM1\r", |mut driver: LSSDriver| async move { driver.query_motion_profile(5).await.unwrap() }, true ); test_query!( test_query_motion_profile_off, "#5QEM\r", "*5QEM0\r", |mut driver: LSSDriver| async move { driver.query_motion_profile(5).await.unwrap() }, false ); test_command!( test_set_filter_position_count, "#5FPC10\r", |mut driver: LSSDriver| async move { driver.set_filter_position_count(5, 10).await.unwrap() } ); test_query!( test_query_filter_position_count, "#5QFPC\r", "*5QFPC10\r", |mut driver: LSSDriver| async move { driver.query_filter_position_count(5).await.unwrap() }, 10 ); test_command!( test_set_angular_stiffness, "#5AS-2\r", |mut driver: LSSDriver| async move { driver.set_angular_stiffness(5, -2).await.unwrap() } ); test_query!( test_query_angular_stiffness, "#5QAS\r", "*5QAS-2\r", |mut driver: LSSDriver| async move { driver.query_angular_stiffness(5).await.unwrap() }, -2 ); test_command!( test_set_angular_holding_stiffness, "#5AH3\r", |mut driver: LSSDriver| async move { driver.set_angular_holding_stiffness(5, 3).await.unwrap() } ); test_query!( test_query_angular_holding_stiffness, "#5QAH\r", "*5QAH3\r", |mut driver: LSSDriver| async move { driver.query_angular_holding_stiffness(5).await.unwrap() }, 3 ); test_command!( test_set_angular_acceleration, "#5AA30\r", |mut driver: LSSDriver| async move { driver.set_angular_acceleration(5, 30).await.unwrap() } ); test_query!( test_query_angular_acceleration, "#5QAA\r", "*5QAA30\r", |mut driver: LSSDriver| async move { driver.query_angular_acceleration(5).await.unwrap() }, 30 ); test_command!( test_set_angular_deceleration, "#5AD30\r", |mut driver: LSSDriver| async move { driver.set_angular_deceleration(5, 30).await.unwrap() } ); test_query!( test_query_angular_deceleration, "#5QAD\r", "*5QAD30\r", |mut driver: LSSDriver| async move { driver.query_angular_deceleration(5).await.unwrap() }, 30 ); test_command!( test_maximum_motor_duty, "#5MMD512\r", |mut driver: LSSDriver| async move { driver.set_maximum_motor_duty(5, 512).await.unwrap() } ); test_query!( test_query_maximum_motor_duty, "#5QMMD\r", "*5QMMD512\r", |mut driver: LSSDriver| async move { driver.query_maximum_motor_duty(5).await.unwrap() }, 512 ); test_command!( test_maximum_speed, "#5SD1800\r", |mut driver: LSSDriver| async move { driver.set_maximum_speed(5, 180.).await.unwrap() } ); test_query_float!( test_query_maximum_speed, "#5QSD\r", "*5QSD1800\r", |mut driver: LSSDriver| async move { driver.query_maximum_speed(5).await.unwrap() }, 180. ); // test telemetry queries test_query_float!( test_query_voltage, "#5QV\r", "*5QV11200\r", |mut driver: LSSDriver| async move { driver.query_voltage(5).await.unwrap() }, 11.2 ); test_query_float!( test_query_temperature, "#5QT\r", "*5QT564\r", |mut driver: LSSDriver| async move { driver.query_temperature(5).await.unwrap() }, 56.4 ); test_query_float!( test_query_current, "#5QC\r", "*5QC140\r", |mut driver: LSSDriver| async move { driver.query_current(5).await.unwrap() }, 0.14 ); test_query!( test_query_model_string, "#5QMS\r", "*5QMSLSS-HS1\r", |mut driver: LSSDriver| async move { driver.query_model(5).await.unwrap() }, Model::HS1 ); test_query!( test_query_firmware_version, "#5QF\r", "*5QF368\r", |mut driver: LSSDriver| async move { driver.query_firmware_version(5).await.unwrap() }, "368".to_owned() ); test_query!( test_query_serial_number, "#5QN\r", "*5QN12345678\r", |mut driver: LSSDriver| async move { driver.query_serial_number(5).await.unwrap() }, "12345678".to_owned() ); // Test blinking modes test_command!( test_blinking_mode_1, "#5CLB0\r", |mut driver: LSSDriver| async move { driver .set_led_blinking(5, vec![LedBlinking::NoBlinking]) .await .unwrap() } ); test_command!( test_blinking_mode_2, "#5CLB1\r", |mut driver: LSSDriver| async move { driver .set_led_blinking(5, vec![LedBlinking::Limp]) .await .unwrap() } ); test_command!( test_blinking_mode_3, "#5CLB2\r", |mut driver: LSSDriver| async move { driver .set_led_blinking(5, vec![LedBlinking::Holding]) .await .unwrap() } ); test_command!( test_blinking_mode_4, "#5CLB12\r", |mut driver: LSSDriver| async move { driver .set_led_blinking( 5, vec![LedBlinking::Accelerating, LedBlinking::Decelerating], ) .await .unwrap() } ); test_command!( test_blinking_mode_5, "#5CLB48\r", |mut driver: LSSDriver| async move { driver .set_led_blinking(5, vec![LedBlinking::Free, LedBlinking::Travelling]) .await .unwrap() } ); test_command!( test_blinking_mode_6, "#5CLB63\r", |mut driver: LSSDriver| async move { driver .set_led_blinking(5, vec![LedBlinking::AlwaysBlink]) .await .unwrap() } ); test_command!( test_reset, "#254RESET\r", |mut driver: LSSDriver| async move { driver.reset(BROADCAST_ID).await.unwrap() } ); }