/*----------------------------------------------------------------------------*/ /* Copyright (c) FIRST 2016-2017. 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. */ /*----------------------------------------------------------------------------*/ #include "pneumatic_piston.h" #include #include #include #ifdef _WIN32 // Ensure that Winsock2.h is included before Windows.h, which can get // pulled in by anybody (e.g., Boost). #include #endif GZ_REGISTER_MODEL_PLUGIN(PneumaticPiston) void PneumaticPiston::Load(gazebo::physics::ModelPtr model, sdf::ElementPtr sdf) { this->model = model; signal = 0; // Parse SDF properties joint = model->GetJoint(sdf->Get("joint")); if (sdf->HasElement("topic")) { topic = sdf->Get("topic"); } else { topic = "~/" + sdf->GetAttribute("name")->GetAsString(); } forward_force = sdf->Get("forward-force"); reverse_force = sdf->Get("reverse-force"); if (sdf->HasElement("direction") && sdf->Get("direction") == "reversed") { forward_force = -forward_force; reverse_force = -reverse_force; } gzmsg << "Initializing piston: " << topic << " joint=" << joint->GetName() << " forward_force=" << forward_force << " reverse_force=" << reverse_force << std::endl; // Connect to Gazebo transport for messaging std::string scoped_name = model->GetWorld()->GetName() + "::" + model->GetScopedName(); boost::replace_all(scoped_name, "::", "/"); node = gazebo::transport::NodePtr(new gazebo::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 updateConn = gazebo::event::Events::ConnectWorldUpdateBegin( boost::bind(&PneumaticPiston::Update, this, _1)); } void PneumaticPiston::Update(const gazebo::common::UpdateInfo& info) { joint->SetForce(0, signal); } void PneumaticPiston::Callback(const gazebo::msgs::ConstFloat64Ptr& msg) { if (msg->data() < -0.001) { signal = -reverse_force; } else if (msg->data() > 0.001) { signal = forward_force; } }