DESERT 3.5.1
Loading...
Searching...
No Matches
udpposition.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
40#include <iostream>
41#include "udpposition.h"
42
43/* ======================================================================
44 TCL Hooks for the simulator
45 ====================================================================== */
46static class UDPPositionClass : public TclClass
47{
48public:
50 : TclClass("Position/UDP")
51 {
52 }
53 TclObject *
54 create(int, const char *const *)
55 {
56 return (new UDPPosition());
57 }
59
61 : Position()
62{
63 bind("debug_", &debug_);
64 bind("udp_receive_port_", &m_UdpReceivePort);
65}
66
67int UDPPosition::command(int argc, const char *const *argv)
68{
69 // Tcl &tcl = Tcl::instance();
70 if (strcasecmp(argv[1], "start") == 0)
71 {
73 LOG_MSG_ERROR(NOW << "::UDPPosition: position listener thread already started!");
74 return TCL_ERROR;
75 }
76
77 struct timeval tv;
78 tv.tv_sec = 0;
79 tv.tv_usec = m_SocketReadTimeout;
82 {
83 if (debug_ >= 1)
84 LOG_MSG_INFO(NOW << "::UDPPosition: starting position listener on port " << m_UdpReceivePort);
86 return TCL_OK;
87 }
88
89 return TCL_ERROR;
90 }
91 else if (strcasecmp(argv[1], "stop") == 0)
92 {
94 {
96 {
97 if (debug_ >= 1)
98 LOG_MSG_INFO(NOW << "::UDPPosition: stopping position listener");
100 }
101 delete p_PositionListener;
102 p_PositionListener = nullptr;
103 }
104 return TCL_OK;
105 }
106 return Position::command(argc, argv);
107}
108
110{
111 if (debug_ >= 1)
112 LOG_MSG_INFO(NOW << "::UDPPosition: setting position to [" << pos.x << "," << pos.y << "," << pos.z << "]");
113 std::unique_lock<std::mutex> lock(mutex_);
114 Position::setX(pos.x);
115 Position::setY(pos.y);
116 Position::setZ(pos.z);
117}
118
119double
121{
122 std::unique_lock<std::mutex> lock(mutex_);
123 return Position::getX();
124}
125double
127{
128 std::unique_lock<std::mutex> lock(mutex_);
129 return Position::getY();
130}
131double
133{
134 std::unique_lock<std::mutex> lock(mutex_);
135 return Position::getZ();
136}
137
138void UDPPosition::setX(double x)
139{
140 std::unique_lock<std::mutex> lock(mutex_);
141 Position::setX(x);
142}
143void UDPPosition::setY(double y)
144{
145 std::unique_lock<std::mutex> lock(mutex_);
146 Position::setY(y);
147}
148void UDPPosition::setZ(double z)
149{
150 std::unique_lock<std::mutex> lock(mutex_);
151 Position::setZ(z);
152}
Position listener thread with UDP socket.
bool Running()
Returns the current state of the thread.
bool Start(bool exc_info=false)
Start the thread.
void Stop(bool wait=false)
Stop the thread, needs call(s) to StopRequested() in the Run() worker function to check for the stop ...
TclObject * create(int, const char *const *)
virtual double getX()
Method that return the current projection of the node on the x-axis.
virtual void setZ(double z)
virtual void setY(double y)
std::mutex mutex_
unsigned int m_SocketReadTimeout
Socket timeout for select() call in [us].
UDPPosition()
Constructor.
virtual void setX(double x)
virtual int command(int argc, const char *const *argv)
TCL command interpreter setdest <integer value>integer value>integer value>: set the movement pattern...
void setPosition(const PositionData &pos)
PositionListener< UDPPosition > * p_PositionListener
virtual double getY()
Method that return the current projection of the node on the y-axis.
unsigned int m_UdpReceivePort
Position receive port number for UDP socket.
virtual double getZ()
Method that return the current projection of the node on the z-axis.
#define LOG_MSG_INFO(msg)
Definition logging.h:75
#define LOG_MSG_ERROR(msg)
Definition logging.h:77
Position data structure for submitting node positions to DESERT in ENU coordinates.
double x
East in [m].
double z
Up in [m].
double y
North in [m].
UDPPositionClass class_uwsmposition
Provides the definition of the class UDPPosition.