blob: 33663bdfd1b9e08a888fd0b6910a6ee6cec56f42 [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/SimGyro.h"
9
10#include "simulation/MainNode.h"
11
12using namespace frc;
13
14SimGyro::SimGyro(std::string topic) {
15 commandPub = MainNode::Advertise<gazebo::msgs::GzString>("~/simulator/" +
16 topic + "/control");
17
18 posSub = MainNode::Subscribe("~/simulator/" + topic + "/position",
19 &SimGyro::positionCallback, this);
20 velSub = MainNode::Subscribe("~/simulator/" + topic + "/velocity",
21 &SimGyro::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 gyro exist?"
29 << std::endl;
30 }
31}
32
33void SimGyro::Reset() { sendCommand("reset"); }
34
35double SimGyro::GetAngle() { return position; }
36
37double SimGyro::GetVelocity() { return velocity; }
38
39void SimGyro::sendCommand(std::string cmd) {
40 gazebo::msgs::GzString msg;
41 msg.set_data(cmd);
42 commandPub->Publish(msg);
43}
44
45void SimGyro::positionCallback(const gazebo::msgs::ConstFloat64Ptr& msg) {
46 position = msg->data();
47}
48
49void SimGyro::velocityCallback(const gazebo::msgs::ConstFloat64Ptr& msg) {
50 velocity = msg->data();
51}