2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2015-04-26 19:19:57 -04:00
|
|
|
#ifdef _WIN32
|
2016-05-20 17:30:37 -07:00
|
|
|
// Ensure that Winsock2.h is included before Windows.h, which can get
|
|
|
|
|
// pulled in by anybody (e.g., Boost).
|
|
|
|
|
#include <Winsock2.h>
|
2015-04-26 19:19:57 -04:00
|
|
|
#endif
|
|
|
|
|
|
2014-06-30 17:32:00 -07:00
|
|
|
#include "pneumatic_piston.h"
|
|
|
|
|
|
2016-05-25 22:41:58 -07:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-06-30 17:32:00 -07:00
|
|
|
#include <gazebo/physics/physics.hh>
|
|
|
|
|
#include <gazebo/transport/transport.hh>
|
|
|
|
|
|
|
|
|
|
GZ_REGISTER_MODEL_PLUGIN(PneumaticPiston)
|
|
|
|
|
|
|
|
|
|
void PneumaticPiston::Load(physics::ModelPtr model, sdf::ElementPtr sdf) {
|
|
|
|
|
this->model = model;
|
|
|
|
|
signal = 0;
|
|
|
|
|
|
|
|
|
|
// Parse SDF properties
|
|
|
|
|
joint = model->GetJoint(sdf->Get<std::string>("joint"));
|
|
|
|
|
if (sdf->HasElement("topic")) {
|
|
|
|
|
topic = sdf->Get<std::string>("topic");
|
|
|
|
|
} else {
|
2016-05-20 17:30:37 -07:00
|
|
|
topic = "~/" + sdf->GetAttribute("name")->GetAsString();
|
2014-06-30 17:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
forward_force = sdf->Get<double>("forward-force");
|
2014-07-21 11:22:52 -04:00
|
|
|
reverse_force = sdf->Get<double>("reverse-force");
|
2014-06-30 17:32:00 -07:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
if (sdf->HasElement("direction") &&
|
|
|
|
|
sdf->Get<std::string>("direction") == "reversed") {
|
2014-06-30 17:32:00 -07:00
|
|
|
forward_force = -forward_force;
|
|
|
|
|
reverse_force = -reverse_force;
|
|
|
|
|
}
|
2015-04-26 19:19:57 -04:00
|
|
|
|
2014-06-30 17:32:00 -07:00
|
|
|
gzmsg << "Initializing piston: " << topic << " joint=" << joint->GetName()
|
|
|
|
|
<< " forward_force=" << forward_force
|
2016-05-20 17:30:37 -07:00
|
|
|
<< " reverse_force=" << reverse_force << std::endl;
|
2014-06-30 17:32:00 -07:00
|
|
|
|
|
|
|
|
// Connect to Gazebo transport for messaging
|
2016-05-20 17:30:37 -07:00
|
|
|
std::string scoped_name =
|
|
|
|
|
model->GetWorld()->GetName() + "::" + model->GetScopedName();
|
2014-06-30 17:32:00 -07:00
|
|
|
boost::replace_all(scoped_name, "::", "/");
|
|
|
|
|
node = transport::NodePtr(new transport::Node());
|
|
|
|
|
node->Init(scoped_name);
|
|
|
|
|
sub = node->Subscribe(topic, &PneumaticPiston::Callback, this);
|
|
|
|
|
|
|
|
|
|
// Connect to the world update event.
|
|
|
|
|
// This will trigger the Update function every Gazebo iteration
|
2016-05-20 17:30:37 -07:00
|
|
|
updateConn = event::Events::ConnectWorldUpdateBegin(
|
|
|
|
|
boost::bind(&PneumaticPiston::Update, this, _1));
|
2014-06-30 17:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void PneumaticPiston::Update(const common::UpdateInfo& info) {
|
2014-06-30 17:32:00 -07:00
|
|
|
joint->SetForce(0, signal);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void PneumaticPiston::Callback(const msgs::ConstFloat64Ptr& msg) {
|
|
|
|
|
if (msg->data() < -0.001) {
|
|
|
|
|
signal = -reverse_force;
|
|
|
|
|
} else if (msg->data() > 0.001) {
|
|
|
|
|
signal = forward_force;
|
|
|
|
|
}
|
2014-06-30 17:32:00 -07:00
|
|
|
}
|