Gazebo Sim

API Reference

7.7.0
ElevatorStateMachineImpl.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 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 
18 /*
19  * \author Nick Lamprianidis <nlamprian@gmail.com>
20  * \date January 2021
21  */
22 
23 #include <deque>
24 #include <memory>
25 #include <sstream>
26 #include <utility>
27 
28 #include <gz/msgs/double.pb.h>
29 
30 #include <gz/common/Console.hh>
31 
32 #include "../ElevatorStateMachine.hh"
33 
34 #include "../ElevatorCommonPrivate.hh"
35 
36 namespace gz
37 {
38 namespace sim
39 {
40 // Inline bracket to help doxygen filtering
41 inline namespace GZ_SIM_VERSION_NAMESPACE {
42 namespace systems
43 {
45 {
50 
55  public: void EnqueueNewTarget(double _target);
56 
60  public: void SendCmd(transport::Node::Publisher &_pub, double _cmd);
61 
64 
67 };
68 
70 ElevatorStateMachinePrivate::ElevatorStateMachinePrivate(
72  : system(_system)
73 {
74 }
75 
78 {
79  // Ignore duplicate targets
80  auto it = std::find(this->targets.cbegin(), this->targets.cend(), _target);
81  if (it != this->targets.cend())
82  return;
83 
84  // Prioritize target in the queue
85  bool enqueued = false;
86  int32_t prevTarget = this->system->state;
87  for (it = this->targets.cbegin(); it != this->targets.cend(); ++it)
88  {
89  int32_t lower = prevTarget;
90  int32_t upper = *it;
91  if (upper < lower) std::swap(lower, upper);
92  if (_target >= lower && _target < upper)
93  {
94  this->targets.insert(it, _target);
95  enqueued = true;
96  break;
97  }
98  prevTarget = *it;
99  }
100  if (!enqueued)
101  this->targets.push_back(_target);
102 
104  ss << "The elevator enqueued target " << _target << " [ ";
105  for (const auto &target : this->targets) ss << target << " ";
106  gzmsg << ss.str() << "]" << std::endl;
107 }
108 
111  double _cmd)
112 {
113  msgs::Double msg;
114  msg.set_data(_cmd);
115  _pub.Publish(msg);
116 }
117 
121  : dataPtr(std::make_unique<ElevatorStateMachinePrivate>(_system))
122 {
123 }
124 
127 
128 } // namespace systems
129 } // namespace GZ_SIM_VERSION_NAMESPACE
130 } // namespace sim
131 } // namespace gz
132 
133 #include "EventsImpl.hh"
134 
135 #include "ActionsImpl.hh"
136 #include "GuardsImpl.hh"
137 #include "StatesImpl.hh"