Skip to content

Commit

Permalink
add back ros_observer for as
Browse files Browse the repository at this point in the history
  • Loading branch information
MishkaMN committed Aug 29, 2024
1 parent ff9ad81 commit 0a299ff
Show file tree
Hide file tree
Showing 10 changed files with 905 additions and 0 deletions.
93 changes: 93 additions & 0 deletions common/ros_observer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
cmake_minimum_required(VERSION 3.1)
project(ros_observer)

find_package(carma_cmake_common REQUIRED)
carma_check_ros_version(1)

set(CMAKE_CXX_STANDARD 14)

find_package(
catkin REQUIRED COMPONENTS
roscpp
roslint
)

find_package(Boost REQUIRED COMPONENTS
thread
)

set(ROSLINT_CPP_OPTS "--filter=-build/c++14")
roslint_cpp()

catkin_package(
INCLUDE_DIRS include
LIBRARIES lib_ros_observer
)

include_directories(
include
${catkin_INCLUDE_DIRS}
)

add_executable(
ros_observer
src/ros_observer.cpp
)

target_link_libraries(
ros_observer
rt ${Boost_LIBRARIES}
pthread
)

add_dependencies(
ros_observer
${catkin_EXPORTED_TARGETS}
)

add_library(
lib_ros_observer
lib/lib_ros_observer.cpp
)

target_link_libraries(
lib_ros_observer
${catkin_LIBRARIES}
rt
${Boost_LIBRARIES}
)

add_dependencies(
lib_ros_observer
${catkin_EXPORTED_TARGETS}
)

# include header files
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)

# Install library
install(TARGETS ros_observer lib_ros_observer
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

# Install launch
install(DIRECTORY launch/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
)

if (CATKIN_ENABLE_TESTING)
roslint_add_test()
find_package(rostest REQUIRED)
add_rostest_gtest(test_ros_observer
test/test_ros_observer.test
test/src/test_ros_observer.cpp
)
target_link_libraries(test_ros_observer
lib_ros_observer
${catkin_LIBRARIES}
)
endif()
71 changes: 71 additions & 0 deletions common/ros_observer/include/ros_observer/lib_ros_observer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef ROS_OBSERVER_LIB_ROS_OBSERVER_H
#define ROS_OBSERVER_LIB_ROS_OBSERVER_H

/*
* Copyright 2019 Autoware Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/thread.hpp>

#include <ros_observer/ros_observer.h>

enum class VitalMonitorMode
{
CNT_CLEAR,
CNT_MON
};

class ShmVitalMonitor
{
public:
ShmVitalMonitor(std::string mod_name, const double loop_rate, VitalMonitorMode mode = VitalMonitorMode::CNT_CLEAR);

void set_mode(VitalMonitorMode mode) {mode_ = mode;}
void run(void);
bool is_error_detected(void);

protected:
std::string name_, shm_name_, mut_name_;
VitalMonitorMode mode_;
bool is_opened_;
const unsigned int polling_interval_msec_;

bool attempt_to_open(void);
void init_vital_counter(void);
void update_vital_counter(void);
};

class ShmDRStopRequest
{
public:
ShmDRStopRequest() :
is_opened_(false), name_("DRStopRequest"), shm_name_("SHM_" + name_), mut_name_("MUT_" + name_) {}

void clear_request(void);
bool is_request_received(void);

protected:
bool is_opened_;
std::string name_, shm_name_, mut_name_;

bool attempt_to_open(void);
};

#endif // ROS_OBSERVER_LIB_ROS_OBSERVER_H
53 changes: 53 additions & 0 deletions common/ros_observer/include/ros_observer/ros_observer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef ROS_OBSERVER_ROS_OBSERVER_H
#define ROS_OBSERVER_ROS_OBSERVER_H

/*
* Copyright 2019 Autoware Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <iostream>
#include <string>
#include <functional>
#include <utility>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/thread.hpp>

static constexpr const char* SHM_NAME = "SharedMemoryForVitalMonitor";
static constexpr int SHM_SIZE = 65536;
static constexpr unsigned int SHM_TH_COUNTER = 3;
static constexpr unsigned int SHM_COUNTER_MAX = 10000;

enum class ModuleStatus
{
Normal,
ErrorDetected
};

struct ShmVitalCounter
{
ModuleStatus modstatus;

bool activated;
unsigned int thresh;
unsigned int value;

ShmVitalCounter() :
modstatus(ModuleStatus::Normal), activated(false), thresh(0), value(0) {}
};

#endif // ROS_OBSERVER_ROS_OBSERVER_H
4 changes: 4 additions & 0 deletions common/ros_observer/launch/ros_observer.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<launch>
<node pkg="ros_observer" type="ros_observer" name="ros_observer" output="log" respawn="false" />
</launch>
6 changes: 6 additions & 0 deletions common/ros_observer/launch/test.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<launch>
<node pkg="ros_observer" type="ros_observer" name="ros_observer" output="screen" respawn="false" respawn_delay="0" />
<arg name="shm_vmon_enabled" default="false"/>
<param name="shm_vmon_enabled" value="$(arg shm_vmon_enabled)"/>
</launch>
Loading

0 comments on commit 0a299ff

Please sign in to comment.