first commit 1437_31102025

This commit is contained in:
2025-10-31 14:38:00 +07:00
commit 0cd8217bc5
71 changed files with 6160 additions and 0 deletions

185
include/robot/duration.h Normal file
View File

@@ -0,0 +1,185 @@
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#ifndef ROBOT_DURATION_H
#define ROBOT_DURATION_H
/*********************************************************************
** Pragmas
*********************************************************************/
#ifdef _MSC_VER
// Robot_time has some magic interface that doesn't directly include
// its implementation, this just disbales those warnings.
#pragma warning(disable: 4244)
#pragma warning(disable: 4661)
#endif
#include <iostream>
#include <math.h>
#include <stdexcept>
#include <climits>
#include <stdint.h>
#include "robot_time_decl.h"
namespace robot
{
ROBOT_TIME_DECL void normalizeSecNSecSigned(int64_t& sec, int64_t& nsec);
ROBOT_TIME_DECL void normalizeSecNSecSigned(int32_t& sec, int32_t& nsec);
/**
* \brief Base class for Duration implementations. Provides storage, common functions and operator overloads.
* This should not need to be used directly.
*/
template <class T>
class DurationBase
{
public:
int32_t sec, nsec;
DurationBase() : sec(0), nsec(0) { }
DurationBase(int32_t _sec, int32_t _nsec);
explicit DurationBase(double t){fromSec(t);};
T operator+(const T &rhs) const;
T operator-(const T &rhs) const;
T operator-() const;
T operator*(double scale) const;
T& operator+=(const T &rhs);
T& operator-=(const T &rhs);
T& operator*=(double scale);
bool operator==(const T &rhs) const;
inline bool operator!=(const T &rhs) const { return !(*static_cast<const T*>(this) == rhs); }
bool operator>(const T &rhs) const;
bool operator<(const T &rhs) const;
bool operator>=(const T &rhs) const;
bool operator<=(const T &rhs) const;
double toSec() const { return static_cast<double>(sec) + 1e-9*static_cast<double>(nsec); };
int64_t toNSec() const {return static_cast<int64_t>(sec)*1000000000ll + static_cast<int64_t>(nsec); };
T& fromSec(double t);
T& fromNSec(int64_t t);
bool isZero() const;
// boost::posix_time conversion removed in std-only build
static const T MIN; //!< Minimum representable duration (negative)
static const T MAX; //!< Maximum representable duration
static const T ZERO; //!< Zero duration
static const T DAY; //!< One day duration
static const T HOUR; //!< One hour duration
static const T MINUTE; //!< One minute duration
static const T SECOND; //!< One second duration
static const T MILLISECOND; //!< One millisecond duration
static const T MICROSECOND; //!< One microsecond duration
static const T NANOSECOND; //!< One nanosecond duration
};
class Rate;
/**
* \brief Duration representation for use with the Time class.
*
* robot::DurationBase provides most of its functionality.
*/
class ROBOT_TIME_DECL Duration : public DurationBase<Duration>
{
public:
Duration()
: DurationBase<Duration>()
{ }
Duration(int32_t _sec, int32_t _nsec)
: DurationBase<Duration>(_sec, _nsec)
{}
explicit Duration(double t) { fromSec(t); }
explicit Duration(const Rate&);
/**
* \brief sleep for the amount of time specified by this Duration. If a signal interrupts the sleep, resleeps for the time remaining.
* @return True if the desired sleep duration was met, false otherwise.
*/
bool sleep() const;
};
extern ROBOT_TIME_DECL const Duration DURATION_MAX;
extern ROBOT_TIME_DECL const Duration DURATION_MIN;
template<> const Duration DurationBase<Duration>::MAX;
template<> const Duration DurationBase<Duration>::MIN;
template<> const Duration DurationBase<Duration>::ZERO;
template<> const Duration DurationBase<Duration>::DAY;
template<> const Duration DurationBase<Duration>::HOUR;
template<> const Duration DurationBase<Duration>::MINUTE;
template<> const Duration DurationBase<Duration>::SECOND;
template<> const Duration DurationBase<Duration>::MILLISECOND;
template<> const Duration DurationBase<Duration>::MICROSECOND;
template<> const Duration DurationBase<Duration>::NANOSECOND;
/**
* \brief Duration representation for use with the WallTime class.
*
* robot::DurationBase provides most of its functionality.
*/
class ROBOT_TIME_DECL WallDuration : public DurationBase<WallDuration>
{
public:
WallDuration()
: DurationBase<WallDuration>()
{ }
WallDuration(int32_t _sec, int32_t _nsec)
: DurationBase<WallDuration>(_sec, _nsec)
{}
explicit WallDuration(double t) { fromSec(t); }
explicit WallDuration(const Rate&);
/**
* \brief sleep for the amount of time specified by this Duration. If a signal interrupts the sleep, resleeps for the time remaining.
* @return True if the desired sleep duration was met, false otherwise.
*/
bool sleep() const;
};
template<> const WallDuration DurationBase<WallDuration>::MAX;
template<> const WallDuration DurationBase<WallDuration>::MIN;
template<> const WallDuration DurationBase<WallDuration>::ZERO;
template<> const WallDuration DurationBase<WallDuration>::DAY;
template<> const WallDuration DurationBase<WallDuration>::HOUR;
template<> const WallDuration DurationBase<WallDuration>::MINUTE;
template<> const WallDuration DurationBase<WallDuration>::SECOND;
template<> const WallDuration DurationBase<WallDuration>::MILLISECOND;
template<> const WallDuration DurationBase<WallDuration>::MICROSECOND;
template<> const WallDuration DurationBase<WallDuration>::NANOSECOND;
ROBOT_TIME_DECL std::ostream &operator <<(std::ostream &os, const Duration &rhs);
ROBOT_TIME_DECL std::ostream &operator <<(std::ostream &os, const WallDuration &rhs);
}
#endif // ROBOT_DURATION_H

22
include/robot/exception.h Normal file
View File

@@ -0,0 +1,22 @@
/*********************************************************************
* Minimal exception type compatible with existing code.
*********************************************************************/
#ifndef ROBOT_MINIMAL_EXCEPTION_H
#define ROBOT_MINIMAL_EXCEPTION_H
#include <stdexcept>
#include <string>
namespace robot {
class Exception : public std::runtime_error {
public:
explicit Exception(const std::string& what_arg)
: std::runtime_error(what_arg) {}
};
} // namespace robot
#endif // ROBOT_MINIMAL_EXCEPTION_H

View File

@@ -0,0 +1,190 @@
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2010, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#ifndef ROBOT_TIME_IMPL_DURATION_H_INCLUDED
#define ROBOT_TIME_IMPL_DURATION_H_INCLUDED
#include <cmath>
#include <limits>
#include <robot/duration.h>
#include <robot/rate.h>
namespace robot {
//
// DurationBase template member function implementation
//
template<class T>
DurationBase<T>::DurationBase(int32_t _sec, int32_t _nsec)
: sec(_sec), nsec(_nsec)
{
normalizeSecNSecSigned(sec, nsec);
}
template<class T>
T& DurationBase<T>::fromSec(double d)
{
if (!std::isfinite(d))
throw std::runtime_error("Duration has to be finite.");
constexpr double minInt64AsDouble = static_cast<double>(std::numeric_limits<int64_t>::min());
constexpr double maxInt64AsDouble = static_cast<double>(std::numeric_limits<int64_t>::max());
if (d <= minInt64AsDouble || d >= maxInt64AsDouble)
throw std::runtime_error("Duration is out of 64-bit integer range");
int64_t sec64 = static_cast<int64_t>(floor(d));
if (sec64 < std::numeric_limits<int32_t>::min() || sec64 > std::numeric_limits<int32_t>::max())
throw std::runtime_error("Duration is out of dual 32-bit range");
sec = static_cast<int32_t>(sec64);
nsec = static_cast<int32_t>(std::llround((d - sec) * 1e9));
int32_t rollover = nsec / 1000000000ul;
sec += rollover;
nsec %= 1000000000ul;
return *static_cast<T*>(this);
}
template<class T>
T& DurationBase<T>::fromNSec(int64_t t)
{
int64_t sec64 = t / 1000000000LL;
if (sec64 < std::numeric_limits<int32_t>::min() || sec64 > std::numeric_limits<int32_t>::max())
throw std::runtime_error("Duration is out of dual 32-bit range");
sec = static_cast<int32_t>(sec64);
nsec = static_cast<int32_t>(t % 1000000000LL);
normalizeSecNSecSigned(sec, nsec);
return *static_cast<T*>(this);
}
template<class T>
T DurationBase<T>::operator+(const T &rhs) const
{
T t;
return t.fromNSec(toNSec() + rhs.toNSec());
}
template<class T>
T DurationBase<T>::operator*(double scale) const
{
return T(toSec() * scale);
}
template<class T>
T DurationBase<T>::operator-(const T &rhs) const
{
T t;
return t.fromNSec(toNSec() - rhs.toNSec());
}
template<class T>
T DurationBase<T>::operator-() const
{
T t;
return t.fromNSec(-toNSec());
}
template<class T>
T& DurationBase<T>::operator+=(const T &rhs)
{
*this = *this + rhs;
return *static_cast<T*>(this);
}
template<class T>
T& DurationBase<T>::operator-=(const T &rhs)
{
*this += (-rhs);
return *static_cast<T*>(this);
}
template<class T>
T& DurationBase<T>::operator*=(double scale)
{
fromSec(toSec() * scale);
return *static_cast<T*>(this);
}
template<class T>
bool DurationBase<T>::operator<(const T &rhs) const
{
if (sec < rhs.sec)
return true;
else if (sec == rhs.sec && nsec < rhs.nsec)
return true;
return false;
}
template<class T>
bool DurationBase<T>::operator>(const T &rhs) const
{
if (sec > rhs.sec)
return true;
else if (sec == rhs.sec && nsec > rhs.nsec)
return true;
return false;
}
template<class T>
bool DurationBase<T>::operator<=(const T &rhs) const
{
if (sec < rhs.sec)
return true;
else if (sec == rhs.sec && nsec <= rhs.nsec)
return true;
return false;
}
template<class T>
bool DurationBase<T>::operator>=(const T &rhs) const
{
if (sec > rhs.sec)
return true;
else if (sec == rhs.sec && nsec >= rhs.nsec)
return true;
return false;
}
template<class T>
bool DurationBase<T>::operator==(const T &rhs) const
{
return sec == rhs.sec && nsec == rhs.nsec;
}
template<class T>
bool DurationBase<T>::isZero() const
{
return sec == 0 && nsec == 0;
}
// boost::posix_time conversion removed in std-only build
}
#endif

185
include/robot/impl/time.h Normal file
View File

@@ -0,0 +1,185 @@
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#ifndef ROBOT_TIME_IMPL_H_INCLUDED
#define ROBOT_TIME_IMPL_H_INCLUDED
/*********************************************************************
** Headers
*********************************************************************/
#include <robot/platform.h>
#include <iostream>
#include <cmath>
#include <limits>
#include <robot/exception.h>
#include <robot/time.h>
/*********************************************************************
** Cross Platform Headers
*********************************************************************/
#if defined(_WIN32)
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
namespace robot
{
template<class T, class D>
T& TimeBase<T, D>::fromNSec(uint64_t t)
{
uint64_t sec64 = 0;
uint64_t nsec64 = t;
normalizeSecNSec(sec64, nsec64);
sec = static_cast<uint32_t>(sec64);
nsec = static_cast<uint32_t>(nsec64);
return *static_cast<T*>(this);
}
template<class T, class D>
T& TimeBase<T, D>::fromSec(double t) {
if (t < 0)
throw std::runtime_error("Time cannot be negative.");
if (!std::isfinite(t))
throw std::runtime_error("Time has to be finite.");
constexpr double maxInt64AsDouble = static_cast<double>(std::numeric_limits<int64_t>::max());
if (t >= maxInt64AsDouble)
throw std::runtime_error("Time is out of 64-bit integer range");
int64_t sec64 = static_cast<int64_t>(floor(t));
if (sec64 > std::numeric_limits<uint32_t>::max())
throw std::runtime_error("Time is out of dual 32-bit range");
sec = static_cast<uint32_t>(sec64);
nsec = static_cast<uint32_t>(std::llround((t-sec) * 1e9));
// avoid rounding errors
sec += (nsec / 1000000000ul);
nsec %= 1000000000ul;
return *static_cast<T*>(this);
}
template<class T, class D>
D TimeBase<T, D>::operator-(const T &rhs) const
{
D d;
return d.fromNSec(toNSec() - rhs.toNSec());
}
template<class T, class D>
T TimeBase<T, D>::operator-(const D &rhs) const
{
return *static_cast<const T*>(this) + ( -rhs);
}
template<class T, class D>
T TimeBase<T, D>::operator+(const D &rhs) const
{
int64_t sec_sum = static_cast<uint64_t>(sec) + static_cast<uint64_t>(rhs.sec);
int64_t nsec_sum = static_cast<uint64_t>(nsec) + static_cast<uint64_t>(rhs.nsec);
// Throws an exception if we go out of 32-bit range
normalizeSecNSecUnsigned(sec_sum, nsec_sum);
// now, it's safe to downcast back to uint32 bits
return T(static_cast<uint32_t>(sec_sum), static_cast<uint32_t>(nsec_sum));
}
template<class T, class D>
T& TimeBase<T, D>::operator+=(const D &rhs)
{
*this = *this + rhs;
return *static_cast<T*>(this);
}
template<class T, class D>
T& TimeBase<T, D>::operator-=(const D &rhs)
{
*this += (-rhs);
return *static_cast<T*>(this);
}
template<class T, class D>
bool TimeBase<T, D>::operator==(const T &rhs) const
{
return sec == rhs.sec && nsec == rhs.nsec;
}
template<class T, class D>
bool TimeBase<T, D>::operator<(const T &rhs) const
{
if (sec < rhs.sec)
return true;
else if (sec == rhs.sec && nsec < rhs.nsec)
return true;
return false;
}
template<class T, class D>
bool TimeBase<T, D>::operator>(const T &rhs) const
{
if (sec > rhs.sec)
return true;
else if (sec == rhs.sec && nsec > rhs.nsec)
return true;
return false;
}
template<class T, class D>
bool TimeBase<T, D>::operator<=(const T &rhs) const
{
if (sec < rhs.sec)
return true;
else if (sec == rhs.sec && nsec <= rhs.nsec)
return true;
return false;
}
template<class T, class D>
bool TimeBase<T, D>::operator>=(const T &rhs) const
{
if (sec > rhs.sec)
return true;
else if (sec == rhs.sec && nsec >= rhs.nsec)
return true;
return false;
}
// boost::posix_time conversion removed in std-only build
}
#endif // ROBOT_IMPL_TIME_H_INCLUDED

23
include/robot/macros.h Normal file
View File

@@ -0,0 +1,23 @@
/*********************************************************************
* Minimal macros to decouple from ROS build system.
*********************************************************************/
#ifndef ROBOT_MINIMAL_MACROS_H
#define ROBOT_MINIMAL_MACROS_H
#if defined(_WIN32) && !defined(ROSTIME_STATIC)
#define ROBOT_HELPER_EXPORT __declspec(dllexport)
#define ROBOT_HELPER_IMPORT __declspec(dllimport)
#else
#define ROBOT_HELPER_EXPORT
#define ROBOT_HELPER_IMPORT
#endif
#if defined(_MSC_VER)
#define ROBOT_FORCE_INLINE __forceinline
#else
#define ROBOT_FORCE_INLINE inline __attribute__((always_inline))
#endif
#endif // ROBOT_MINIMAL_MACROS_H

9
include/robot/platform.h Normal file
View File

@@ -0,0 +1,9 @@
/*********************************************************************
* Minimal platform header placeholder to satisfy includes.
*********************************************************************/
#ifndef ROBOT_MINIMAL_PLATFORM_H
#define ROBOT_MINIMAL_PLATFORM_H
#endif // ROBOT_MINIMAL_PLATFORM_H

131
include/robot/rate.h Normal file
View File

@@ -0,0 +1,131 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2009, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Eitan Marder-Eppstein
*********************************************************************/
#ifndef ROBOT_LIB_RATE_H
#define ROBOT_LIB_RATE_H
#include "robot/time.h"
#include "robot_time_decl.h"
namespace robot
{
class Duration;
/**
* @class Rate
* @brief Class to help run loops at a desired frequency
*/
class ROBOT_TIME_DECL Rate
{
public:
/**
* @brief Constructor, creates a Rate
* @param frequency The desired rate to run at in Hz
*/
Rate(double frequency);
explicit Rate(const Duration&);
/**
* @brief Sleeps for any leftover time in a cycle. Calculated from the last time sleep, reset, or the constructor was called.
* @return True if the desired rate was met for the cycle, false otherwise.
*/
bool sleep();
/**
* @brief Sets the start time for the rate to now
*/
void reset();
/**
* @brief Get the actual run time of a cycle from start to sleep
* @return The runtime of the cycle
*/
Duration cycleTime() const;
/**
* @brief Get the expected cycle time -- one over the frequency passed in to the constructor
*/
Duration expectedCycleTime() const { return expected_cycle_time_; }
private:
Time start_;
Duration expected_cycle_time_, actual_cycle_time_;
};
/**
* @class WallRate
* @brief Class to help run loops at a desired frequency. This version always uses wall-clock time.
*/
class ROBOT_TIME_DECL WallRate
{
public:
/**
* @brief Constructor, creates a Rate
* @param frequency The desired rate to run at in Hz
*/
WallRate(double frequency);
explicit WallRate(const Duration&);
/**
* @brief Sleeps for any leftover time in a cycle. Calculated from the last time sleep, reset, or the constructor was called.
* @return Passes through the return value from WallDuration::sleep() if it slept, false otherwise.
*/
bool sleep();
/**
* @brief Sets the start time for the rate to now
*/
void reset();
/**
* @brief Get the actual run time of a cycle from start to sleep
* @return The runtime of the cycle
*/
WallDuration cycleTime() const;
/**
* @brief Get the expected cycle time -- one over the frequency passed in to the constructor
*/
WallDuration expectedCycleTime() const { return expected_cycle_time_; }
private:
WallTime start_;
WallDuration expected_cycle_time_, actual_cycle_time_;
};
}
#endif

View File

@@ -0,0 +1,55 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2009, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*********************************************************************/
/*
* Cross platform macros.
*
*/
#ifndef ROBOT_TIME_DECL_HPP_INCLUDED
#define ROBOT_TIME_DECL_HPP_INCLUDED
#include <robot/macros.h>
#ifdef ROBOT_BUILD_SHARED_LIBS // robot is being built around shared libraries
#ifdef robot_time_EXPORTS // we are building a shared lib/dll
#define ROBOT_TIME_DECL ROBOT_HELPER_EXPORT
#else // we are using shared lib/dll
#define ROBOT_TIME_DECL ROBOT_HELPER_IMPORT
#endif
#else // robot is being built around static libraries
#define ROBOT_TIME_DECL
#endif
#endif /* ROBOT_TIME_DECL_HPP_INCLUDED */

301
include/robot/time.h Normal file
View File

@@ -0,0 +1,301 @@
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2010, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#ifndef ROBOT_TIME_H_INCLUDED
#define ROBOT_TIME_H_INCLUDED
/*********************************************************************
** Pragmas
*********************************************************************/
#ifdef _MSC_VER
// Robot_time has some magic interface that doesn't directly include
// its implementation, this just disables those warnings.
#pragma warning(disable: 4244)
#pragma warning(disable: 4661)
#endif
/*********************************************************************
** Headers
*********************************************************************/
#include <robot/platform.h>
#include <iostream>
#include <cmath>
#include <robot/exception.h>
#include "duration.h"
#include "robot_time_decl.h"
/*********************************************************************
** Cross Platform Headers
*********************************************************************/
#if defined(_WIN32)
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
namespace robot
{
/*********************************************************************
** Exceptions
*********************************************************************/
/**
* @brief Thrown if the robot subsystem hasn't been initialised before use.
*/
class ROBOT_TIME_DECL TimeNotInitializedException : public Exception
{
public:
TimeNotInitializedException()
: Exception("Cannot use robot::Time::now() before the first NodeHandle has been created or robot::start() has been called. "
"If this is a standalone app or test that just uses robot::Time and does not communicate over ROBOT, you may also call robot::Time::init()")
{}
};
/**
* @brief Thrown if windows high perf. timestamping is unavailable.
*
* @sa getWallTime
*/
class ROBOT_TIME_DECL NoHighPerformanceTimersException : public Exception
{
public:
NoHighPerformanceTimersException()
: Exception("This windows platform does not "
"support the high-performance timing api.")
{}
};
/*********************************************************************
** Functions
*********************************************************************/
ROBOT_TIME_DECL void normalizeSecNSec(uint64_t& sec, uint64_t& nsec);
ROBOT_TIME_DECL void normalizeSecNSec(uint32_t& sec, uint32_t& nsec);
ROBOT_TIME_DECL void normalizeSecNSecUnsigned(int64_t& sec, int64_t& nsec);
ROBOT_TIME_DECL void robot_walltime(uint32_t& sec, uint32_t& nsec);
ROBOT_TIME_DECL void robot_steadytime(uint32_t& sec, uint32_t& nsec);
/*********************************************************************
** Time Classes
*********************************************************************/
/**
* \brief Base class for Time implementations. Provides storage, common functions and operator overloads.
* This should not need to be used directly.
*/
template<class T, class D>
class TimeBase
{
public:
uint32_t sec, nsec;
TimeBase() : sec(0), nsec(0) { }
TimeBase(uint32_t _sec, uint32_t _nsec) : sec(_sec), nsec(_nsec)
{
normalizeSecNSec(sec, nsec);
}
explicit TimeBase(double t) { fromSec(t); }
D operator-(const T &rhs) const;
T operator+(const D &rhs) const;
T operator-(const D &rhs) const;
T& operator+=(const D &rhs);
T& operator-=(const D &rhs);
bool operator==(const T &rhs) const;
inline bool operator!=(const T &rhs) const { return !(*static_cast<const T*>(this) == rhs); }
bool operator>(const T &rhs) const;
bool operator<(const T &rhs) const;
bool operator>=(const T &rhs) const;
bool operator<=(const T &rhs) const;
double toSec() const { return static_cast<double>(sec) + 1e-9*static_cast<double>(nsec); };
T& fromSec(double t);
uint64_t toNSec() const {return static_cast<uint64_t>(sec)*1000000000ull + static_cast<uint64_t>(nsec); }
T& fromNSec(uint64_t t);
inline bool isZero() const { return sec == 0 && nsec == 0; }
inline bool is_zero() const { return isZero(); }
// boost::posix_time conversion removed in std-only build
static const T MIN; //!< Minimum representable time
static const T MAX; //!< Maximum representable time
static const T ZERO; //!< Zero (invalid) time
static const T UNINITIALIZED; //!< Uninitialized time
};
/**
* \brief Time representation. May either represent wall clock time or ROBOT clock time.
*
* robot::TimeBase provides most of its functionality.
*/
class ROBOT_TIME_DECL Time : public TimeBase<Time, Duration>
{
public:
Time()
: TimeBase<Time, Duration>()
{}
Time(uint32_t _sec, uint32_t _nsec)
: TimeBase<Time, Duration>(_sec, _nsec)
{}
explicit Time(double t) { fromSec(t); }
/**
* \brief Retrieve the current time. If ROBOT clock time is in use, this returns the time according to the
* ROBOT clock. Otherwise returns the current wall clock time.
*/
static Time now();
/**
* \brief Sleep until a specific time has been reached.
* @return True if the desired sleep time was met, false otherwise.
*/
static bool sleepUntil(const Time& end);
static void init();
static void shutdown();
static void setNow(const Time& new_now);
static bool useSystemTime();
static bool isSimTime();
static bool isSystemTime();
/**
* \brief Returns whether or not the current time source is valid. Simulation time is valid if it is non-zero.
*/
static bool isValid();
/**
* \brief Wait for time source to become valid
*/
static bool waitForValid();
/**
* \brief Wait for time source to become valid, with timeout
*/
static bool waitForValid(const robot::WallDuration& timeout);
// boost::posix_time conversion removed in std-only build
};
extern ROBOT_TIME_DECL const Time TIME_MAX;
extern ROBOT_TIME_DECL const Time TIME_MIN;
template<> const Time TimeBase<Time, Duration>::MAX;
template<> const Time TimeBase<Time, Duration>::MIN;
template<> const Time TimeBase<Time, Duration>::ZERO;
template<> const Time TimeBase<Time, Duration>::UNINITIALIZED;
/**
* \brief Time representation. Always wall-clock time.
*
* robot::TimeBase provides most of its functionality.
*/
class ROBOT_TIME_DECL WallTime : public TimeBase<WallTime, WallDuration>
{
public:
WallTime()
: TimeBase<WallTime, WallDuration>()
{}
WallTime(uint32_t _sec, uint32_t _nsec)
: TimeBase<WallTime, WallDuration>(_sec, _nsec)
{}
explicit WallTime(double t) { fromSec(t); }
/**
* \brief Returns the current wall clock time.
*/
static WallTime now();
/**
* \brief Sleep until a specific time has been reached.
* @return True if the desired sleep time was met, false otherwise.
*/
static bool sleepUntil(const WallTime& end);
static bool isSystemTime() { return true; }
};
template<> const WallTime TimeBase<WallTime, WallDuration>::MAX;
template<> const WallTime TimeBase<WallTime, WallDuration>::MIN;
template<> const WallTime TimeBase<WallTime, WallDuration>::ZERO;
template<> const WallTime TimeBase<WallTime, WallDuration>::UNINITIALIZED;
/**
* \brief Time representation. Always steady-clock time.
*
* Not affected by ROBOT time.
*
* robot::TimeBase provides most of its functionality.
*/
class ROBOT_TIME_DECL SteadyTime : public TimeBase<SteadyTime, WallDuration>
{
public:
SteadyTime()
: TimeBase<SteadyTime, WallDuration>()
{}
SteadyTime(uint32_t _sec, uint32_t _nsec)
: TimeBase<SteadyTime, WallDuration>(_sec, _nsec)
{}
explicit SteadyTime(double t) { fromSec(t); }
/**
* \brief Returns the current steady (monotonic) clock time.
*/
static SteadyTime now();
/**
* \brief Sleep until a specific time has been reached.
* @return True if the desired sleep time was met, false otherwise.
*/
static bool sleepUntil(const SteadyTime& end);
static bool isSystemTime() { return true; }
};
template<> const SteadyTime TimeBase<SteadyTime, WallDuration>::MAX;
template<> const SteadyTime TimeBase<SteadyTime, WallDuration>::MIN;
template<> const SteadyTime TimeBase<SteadyTime, WallDuration>::ZERO;
template<> const SteadyTime TimeBase<SteadyTime, WallDuration>::UNINITIALIZED;
ROBOT_TIME_DECL std::ostream &operator <<(std::ostream &os, const Time &rhs);
ROBOT_TIME_DECL std::ostream &operator <<(std::ostream &os, const WallTime &rhs);
ROBOT_TIME_DECL std::ostream &operator <<(std::ostream &os, const SteadyTime &rhs);
}
#endif // ROBOT_TIME_H