SOLA
Loading...
Searching...
No Matches
amr_physical_asset.h
1// Copyright 2023 The SOLA authors
2//
3// This file is part of DAISI.
4//
5// DAISI is free software: you can redistribute it and/or modify it under the terms of the GNU
6// General Public License as published by the Free Software Foundation; version 2.
7//
8// DAISI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
9// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
10// Public License for more details.
11//
12// You should have received a copy of the GNU General Public License along with DAISI. If not, see
13// <https://www.gnu.org/licenses/>.
14//
15// SPDX-License-Identifier: GPL-2.0-only
16
17#ifndef DAISI_CPPS_AMR_PHYSICAL_AMR_PHYSICAL_ASSET_H_
18#define DAISI_CPPS_AMR_PHYSICAL_AMR_PHYSICAL_ASSET_H_
19
20#include <deque>
21#include <functional>
22#include <random>
23#include <vector>
24
25#include "cpps/amr/message/amr_order_info.h"
26#include "cpps/amr/message/amr_state.h"
27#include "cpps/amr/physical/amr_asset_connector.h"
28#include "cpps/amr/physical/amr_order.h"
29#include "cpps/amr/physical/functionality.h"
30#include "cpps/model/order_states.h"
31#include "fsmlite/fsm.h"
32#include "ns3/application.h"
33#include "ns3/event-id.h"
34#include "ns3/object.h"
35#include "ns3/simulator.h"
36#include "ns3/socket.h"
37#include "ns3/vector.h"
38
39namespace daisi::cpps {
40
41// fsmlite events
42struct ReceivedOrder {};
43struct ReachedTarget {};
44struct LoadedPayload {};
47
50class AmrPhysicalAsset : public fsmlite::fsm<AmrPhysicalAsset, OrderStates> {
51 friend class fsm; // base class needs access to transition_table
52
53public:
54 AmrPhysicalAsset(AmrAssetConnector connector, const Topology &topology);
55 explicit AmrPhysicalAsset(AmrAssetConnector connector);
56
57 void init();
58
59 void connect(const ns3::InetSocketAddress &endpoint);
60
63 void updateFunctionality(const FunctionalityVariant &functionality); // TODO can this be private?
64
65 using event = int;
66 using state_type = OrderStates;
67
68private:
69 // communication with Logical
70 ns3::Ptr<ns3::Socket> socket_;
72 void sendVehicleStatusUpdateNs3(bool force);
74 void sendOrderUpdateNs3();
76 void readSocket(ns3::Ptr<ns3::Socket> socket);
78 void sendDescriptionNs3();
79 void scheduleVehicleStatusUpdateNs3();
80 void startVehicleStatusUpdates();
81 void stopVehicleStatusUpdatesNs3() const;
82 ns3::EventId next_update_event_;
83
84 void processMessageOrderInfo(const AmrOrderInfo &order_info);
85
86 util::Position getPosition() const;
87
88 AmrAssetConnector connector_;
89 std::deque<FunctionalityVariant> functionality_queue_;
90 util::Position last_sent_position_;
91 AmrState amr_state_ = AmrState::kIdle;
92
93 // fsmlite helpers
94 void executeFrontFunctionality();
95 bool holdsMoveType(const FunctionalityVariant &f) const;
96
97 // fsmlite actions
98 template <typename T> void charge(const T &t);
99 template <typename T> void execute(const T &t);
100 template <typename T> void finish(const T &t);
101
102#define MAKE_SKIP_HANDLE_FUNC_PAIR(FCLASS) \
103 void handleSkip##FCLASS(const FCLASS &) { \
104 process_event(FCLASS()); \
105 sendOrderUpdateNs3(); \
106 } \
107 \
108 void skip(const FCLASS &t) { \
109 ns3::Simulator::Schedule(ns3::Seconds(0), &AmrPhysicalAsset::handleSkip##FCLASS, this, t); \
110 }
111 MAKE_SKIP_HANDLE_FUNC_PAIR(ReceivedOrder)
112 MAKE_SKIP_HANDLE_FUNC_PAIR(ReachedTarget)
113 MAKE_SKIP_HANDLE_FUNC_PAIR(LoadedPayload)
114 MAKE_SKIP_HANDLE_FUNC_PAIR(UnloadedPayload)
115
116 // fsmlite guards
117 template <typename T> bool isMoveToLoad(const T &t) const;
118 template <typename T> bool isMoveToUnload(const T &t) const;
119 template <typename T> bool isMoveToCharge(const T &t) const;
120 template <typename T> bool isMove(const T &t) const;
121 template <typename T> bool isLoad(const T &t) const;
122 template <typename T> bool isUnload(const T &t) const;
123 template <typename T> bool isCharge(const T &t) const;
124 template <typename T> bool isFinish(const T &t) const;
125
126 using m = AmrPhysicalAsset;
127
128 using s = OrderStates;
129
130 using transition_table = table<
131 // kFinished
132 mem_fn_row<s::kFinished, ReceivedOrder, s::kStarted, &m::skip>,
133 // kStarted
134 mem_fn_row<s::kStarted, ReceivedOrder, s::kGoToPickUpLocation, &m::execute, &m::isMoveToLoad>,
135 // kMoveToPickUp
136 mem_fn_row<s::kGoToPickUpLocation, ReachedTarget, s::kReachedPickUpLocation, &m::skip>,
137 // kReachedPickUpLocation
138 mem_fn_row<s::kReachedPickUpLocation, ReachedTarget, s::kLoad, &m::execute, &m::isLoad>,
139 // kMoveToDelivery
140 mem_fn_row<s::kGoToDeliveryLocation, ReachedTarget, s::kReachedDeliveryLocation, &m::skip>,
141 // kReachedDeliveryLocation
142 mem_fn_row<s::kReachedDeliveryLocation, ReachedTarget, s::kUnload, &m::execute, &m::isUnload>,
143 // kLoad
144 mem_fn_row<s::kLoad, LoadedPayload, s::kLoaded, &m::skip>,
145 // kLoaded
146 mem_fn_row<s::kLoaded, LoadedPayload, s::kGoToPickUpLocation, &m::execute, &m::isMoveToLoad>,
147 mem_fn_row<s::kLoaded, LoadedPayload, s::kGoToDeliveryLocation, &m::execute,
148 &m::isMoveToUnload>,
149 mem_fn_row<s::kLoaded, LoadedPayload, s::kFinished, &m::finish, &m::isFinish>,
150 // kUnload
151 mem_fn_row<s::kUnload, UnloadedPayload, s::kUnloaded, &m::skip>,
152 // kUnloaded
153 mem_fn_row<s::kUnloaded, UnloadedPayload, s::kGoToPickUpLocation, &m::execute,
154 &m::isMoveToLoad>,
155 mem_fn_row<s::kUnloaded, UnloadedPayload, s::kFinished, &m::finish, &m::isFinish>>;
156};
157} // namespace daisi::cpps
158#endif
Definition amr_asset_connector.h:33
Definition amr_order_info.h:27
Manages communication with the corresponding logical agent and execution of transport orders.
Definition amr_physical_asset.h:50
void updateFunctionality(const FunctionalityVariant &functionality)
When a functionality is finished, it will be deleted from the vector and the next one will be started...
Definition amr_physical_asset.cpp:54
Definition amr_topology.h:26
Definition amr_physical_asset.h:46
Definition amr_physical_asset.h:44
Definition amr_physical_asset.h:43
Definition amr_physical_asset.h:42
Definition amr_physical_asset.h:45