Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "simulation/SimEncoder.h" |
| 9 | |
| 10 | #include "simulation/MainNode.h" |
| 11 | |
| 12 | using namespace frc; |
| 13 | |
| 14 | SimEncoder::SimEncoder(std::string topic) { |
| 15 | commandPub = MainNode::Advertise<gazebo::msgs::GzString>("~/simulator/" + |
| 16 | topic + "/control"); |
| 17 | |
| 18 | posSub = MainNode::Subscribe("~/simulator/" + topic + "/position", |
| 19 | &SimEncoder::positionCallback, this); |
| 20 | velSub = MainNode::Subscribe("~/simulator/" + topic + "/velocity", |
| 21 | &SimEncoder::velocityCallback, this); |
| 22 | |
| 23 | if (commandPub->WaitForConnection( |
| 24 | gazebo::common::Time(5.0))) { // Wait up to five seconds. |
| 25 | std::cout << "Initialized ~/simulator/" + topic << std::endl; |
| 26 | } else { |
| 27 | std::cerr << "Failed to initialize ~/simulator/" + topic + |
| 28 | ": does the encoder exist?" |
| 29 | << std::endl; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void SimEncoder::Reset() { sendCommand("reset"); } |
| 34 | |
| 35 | void SimEncoder::Start() { sendCommand("start"); } |
| 36 | |
| 37 | void SimEncoder::Stop() { sendCommand("stop"); } |
| 38 | |
| 39 | double SimEncoder::GetPosition() { return position; } |
| 40 | |
| 41 | double SimEncoder::GetVelocity() { return velocity; } |
| 42 | |
| 43 | void SimEncoder::sendCommand(std::string cmd) { |
| 44 | gazebo::msgs::GzString msg; |
| 45 | msg.set_data(cmd); |
| 46 | commandPub->Publish(msg); |
| 47 | } |
| 48 | |
| 49 | void SimEncoder::positionCallback(const gazebo::msgs::ConstFloat64Ptr& msg) { |
| 50 | position = msg->data(); |
| 51 | } |
| 52 | |
| 53 | void SimEncoder::velocityCallback(const gazebo::msgs::ConstFloat64Ptr& msg) { |
| 54 | velocity = msg->data(); |
| 55 | } |