|
| 1 | +#include "moteus_poll.h" |
| 2 | + |
| 3 | +MoteusPoll::MoteusPoll() : Node("moteus_poll") { |
| 4 | + transport_ = moteus::Controller::MakeSingletonTransport({}); |
| 5 | + |
| 6 | + for (int id = 1; id <= NUM_JOINTS; id++) { |
| 7 | + moteus::Controller::Options opts; |
| 8 | + opts.id = id; |
| 9 | + auto controller = std::make_shared<moteus::Controller>(opts); |
| 10 | + controllers_.push_back(controller); |
| 11 | + } |
| 12 | + |
| 13 | + timer_ = this->create_wall_timer( |
| 14 | + std::chrono::milliseconds(100), |
| 15 | + std::bind(&MoteusPoll::poll, this) |
| 16 | + ); |
| 17 | + |
| 18 | + RCLCPP_INFO(this->get_logger(), "Polling %d motors at 10 Hz", NUM_JOINTS); |
| 19 | +} |
| 20 | + |
| 21 | +void MoteusPoll::poll() { |
| 22 | + std::vector<moteus::CanFdFrame> command_frames; |
| 23 | + |
| 24 | + for (auto& controller : controllers_) { |
| 25 | + command_frames.push_back(controller->MakeQuery()); |
| 26 | + } |
| 27 | + |
| 28 | + std::vector<moteus::CanFdFrame> replies; |
| 29 | + transport_->BlockingCycle(command_frames.data(), command_frames.size(), &replies); |
| 30 | + |
| 31 | + for (const auto& frame : replies) { |
| 32 | + int motor_id = frame.source; |
| 33 | + auto result = moteus::Query::Parse(frame.data, frame.size); |
| 34 | + |
| 35 | + RCLCPP_INFO(this->get_logger(), |
| 36 | + "ID: %d | Mode: %2d | Fault: %2d | " |
| 37 | + "Pos: %.3f rev | Vel: %.3f rev/s | Torque: %.3f Nm | " |
| 38 | + "Voltage: %.1f V | Temp: %.1f C", |
| 39 | + motor_id, |
| 40 | + static_cast<int>(result.mode), |
| 41 | + static_cast<int>(result.fault), |
| 42 | + result.position, |
| 43 | + result.velocity, |
| 44 | + result.torque, |
| 45 | + result.voltage, |
| 46 | + result.temperature |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + RCLCPP_INFO(this->get_logger(), "---"); |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char* argv[]) { |
| 54 | + rclcpp::init(argc, argv); |
| 55 | + rclcpp::spin(std::make_shared<MoteusPoll>()); |
| 56 | + rclcpp::shutdown(); |
| 57 | + return 0; |
| 58 | +} |
0 commit comments