blob: 141a371a31a85f330d560150265f9a50e6687ac9 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
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
12using namespace frc;
13
14SimEncoder::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
33void SimEncoder::Reset() { sendCommand("reset"); }
34
35void SimEncoder::Start() { sendCommand("start"); }
36
37void SimEncoder::Stop() { sendCommand("stop"); }
38
39double SimEncoder::GetPosition() { return position; }
40
41double SimEncoder::GetVelocity() { return velocity; }
42
43void SimEncoder::sendCommand(std::string cmd) {
44 gazebo::msgs::GzString msg;
45 msg.set_data(cmd);
46 commandPub->Publish(msg);
47}
48
49void SimEncoder::positionCallback(const gazebo::msgs::ConstFloat64Ptr& msg) {
50 position = msg->data();
51}
52
53void SimEncoder::velocityCallback(const gazebo::msgs::ConstFloat64Ptr& msg) {
54 velocity = msg->data();
55}