TrafficLightsGUIPlugin.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2015 Open Source Robotics Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <sstream>
  18. #include <gazebo/msgs/msgs.hh>
  19. #include <gazebo/rendering/UserCamera.hh>
  20. #include <gazebo/gui/KeyEventHandler.hh>
  21. #include "TrafficLightsGUIPlugin.hh"
  22. using namespace gazebo;
  23. // Register this plugin with the simulator
  24. GZ_REGISTER_GUI_PLUGIN(TrafficLightsGUIPlugin)
  25. /////////////////////////////////////////////////
  26. TrafficLightsGUIPlugin::TrafficLightsGUIPlugin()
  27. : GUIPlugin()
  28. {
  29. auto mainFrame = new QFrame();
  30. auto mainLayout = new QHBoxLayout;
  31. mainLayout->addWidget(mainFrame);
  32. this->setLayout(mainLayout);
  33. this->resize(1, 1);
  34. }
  35. /////////////////////////////////////////////////
  36. void TrafficLightsGUIPlugin::Load(sdf::ElementPtr _sdf)
  37. {
  38. // Get input params from SDF
  39. if (!_sdf->HasElement("key"))
  40. return;
  41. sdf::ElementPtr keyElem = _sdf->GetElement("key");
  42. while (keyElem)
  43. {
  44. Key key;
  45. key.value = keyElem->Get<int>("value");
  46. key.model = keyElem->Get<std::string>("model");
  47. key.color = keyElem->Get<std::string>("color");
  48. this->keys.push_back(key);
  49. keyElem = keyElem->GetNextElement("key");
  50. }
  51. // Initialize transport
  52. this->node = gazebo::transport::NodePtr(
  53. new gazebo::transport::Node());
  54. this->node->Init();
  55. this->keyboardSub =
  56. this->node->Subscribe("/gazebo/default/keyboard/keypress",
  57. &TrafficLightsGUIPlugin::OnKeyPress, this, true);
  58. // Publish to visuals
  59. this->visPub =
  60. this->node->Advertise<gazebo::msgs::Visual>("/gazebo/default/visual");
  61. }
  62. /////////////////////////////////////////////////
  63. void TrafficLightsGUIPlugin::OnKeyPress(ConstAnyPtr &_msg)
  64. {
  65. // Colors
  66. auto red = std::make_pair(gazebo::common::Color::Red, "red");
  67. auto yellow = std::make_pair(gazebo::common::Color::Yellow, "yellow");
  68. auto green = std::make_pair(gazebo::common::Color::Green, "green");
  69. auto black = gazebo::common::Color::Black;
  70. for (auto key : this->keys)
  71. {
  72. if (_msg->int_value() == key.value)
  73. {
  74. auto color = key.color;
  75. auto model = key.model;
  76. for (auto c : {red, yellow, green})
  77. {
  78. for (auto l : {"right_light", "center_light"})
  79. {
  80. gazebo::msgs::Visual msg;
  81. msg.set_type(gazebo::msgs::Visual::VISUAL);
  82. msg.set_parent_name(model + "::" + l + "::link");
  83. msg.set_name(model + "::" + l + "::link::" + c.second);
  84. auto matMsg = msg.mutable_material();
  85. if (c.second == color)
  86. {
  87. gazebo::msgs::Set(matMsg->mutable_emissive(), c.first);
  88. gazebo::msgs::Set(matMsg->mutable_ambient(), c.first);
  89. }
  90. else
  91. {
  92. gazebo::msgs::Set(matMsg->mutable_emissive(), black);
  93. gazebo::msgs::Set(matMsg->mutable_ambient(), black);
  94. }
  95. this->visPub->Publish(msg);
  96. }
  97. }
  98. }
  99. }
  100. }