Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Simulation Interface Draft Stubs #49

Merged
merged 24 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a176a07
Updated can_transceiver.h and .cpp according to subscribe to /Simulat…
colinli02 Mar 3, 2023
d7ffd62
can_transceiver_ros_intf.cpp: Moved /Simulator sub
colinli02 Mar 25, 2023
eb304e5
Removed duplicate code (was placed in wrong cmake)
colinli02 Mar 25, 2023
873f389
Merge remote-tracking branch 'origin/main' into user/colinli02/16-CAN…
colinli02 Jul 27, 2023
d1f710e
Cleaned up comments within functions
colinli02 Jul 27, 2023
a4d6eb7
Updated based on custom_interfaces topics
colinli02 Jul 27, 2023
9e31613
Added ability to publish to multiple ROS topics
colinli02 Sep 16, 2023
b73db93
Add custom_interfaces dependency to CMake
hhenry01 Sep 27, 2023
a6c532c
Added subscription for mock_gps, mock_wind_sensors
colinli02 Oct 4, 2023
f603726
Added back removed RCLCPP::spin, commented out
colinli02 Oct 7, 2023
63e5444
Fixed rclcpp::spin error
colinli02 Oct 7, 2023
17c122f
Added functionality to spin pub & sub
colinli02 Oct 7, 2023
fcbbfe4
Added print for all pubsub
colinli02 Oct 7, 2023
9b4b08e
Fixed print statements related to subbing mocks
colinli02 Oct 7, 2023
1f0089f
Fixed generic sensors data issue
colinli02 Oct 28, 2023
051d80b
- Removed RCLCPP_INFO
colinli02 Nov 1, 2023
e6dee05
- Removed autogenerated file from running ros2
colinli02 Nov 1, 2023
a15109d
- Resolved branch conflicts
colinli02 Nov 1, 2023
cd5fc16
- Fixed formatting and removed unnecessary headers
colinli02 Nov 8, 2023
0dd4d7d
Merge branch 'main' into user/colinli02/16-CAN-Sim-Interface-Stubs
colinli02 Nov 8, 2023
03e4e65
- Replaced with constants defined under ros_info.h
colinli02 Nov 8, 2023
45edfb6
- Updated formatting to match ROS guidelines
colinli02 Nov 11, 2023
7c43629
- Removed un-used headers
colinli02 Nov 11, 2023
e190e61
Split into two nodes CanTrxRosIntf, CanSimIntf
colinli02 Dec 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,54 @@ endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Configure build flags
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs \
-ftest-coverage -fprofile-instr-generate -fcoverage-mapping")
add_definitions(-DDEBUG)
set(coverage_flags "-fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS_DEBUG}"
# ${coverage_flags} Needs to be implemented
)
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif()
# Enable all warnings as errors except unused parameter because auto-generated protobuf files trigger it,
# and clang-tidy can cover it without false-flagging the protobuf files.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wextra -Wpedantic -Werror -Wno-unused-parameter -pthread")
message("Building Network Systems with build type '${CMAKE_BUILD_TYPE}' "
message(WARNING "Building Network Systems with build type '${CMAKE_BUILD_TYPE}' "
"and flags: '${CMAKE_CXX_FLAGS}'")

# ROS dependencies
set(ROS_DEPS
rclcpp
std_msgs
custom_interfaces
)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
foreach(dep IN LISTS ROS_DEPS)
find_package(${dep} REQUIRED)
endforeach()


# Boost
find_package(Boost 1.74.0 COMPONENTS REQUIRED
# Insert desired libraries below (see /usr/include/boost/ for available options)
serialization
thread
)

# MongoDB
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)

# Protobuf
find_package(Protobuf REQUIRED)
set(PROTOBUF_LINK_LIBS
${Protobuf_LIBRARIES}
protofiles
)

# Googletest (installed by sailbot_workspace Docker config)
if(UNIT_TEST)
execute_process(COMMAND arch OUTPUT_VARIABLE ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GTEST_LINK_LIBS "/usr/lib/${ARCH}-linux-gnu/libgtest.a" "/usr/lib/${ARCH}-linux-gnu/libgtest_main.a")
find_package(GTest CONFIG REQUIRED)
set(GTEST_LINK_LIBS gtest gtest_main)
endif()

# Add src directories
Expand Down
11 changes: 7 additions & 4 deletions functions.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Create module library
function(make_lib module srcs link_libs inc_dirs)
function(make_lib module srcs link_libs inc_dirs compile_defs)
add_library(${module} ${srcs})
target_compile_definitions(${module} PUBLIC ${compile_defs})
target_link_libraries(${module} PUBLIC ${link_libs})
target_include_directories(
${module} PUBLIC
Expand All @@ -12,10 +13,11 @@ function(make_lib module srcs link_libs inc_dirs)
endfunction()

# Create module ROS executable
function(make_ros_exe module srcs link_libs inc_dirs)
function(make_exe module srcs link_libs inc_dirs ${compile_defs})
set(bin_module bin_${module})
add_executable(${bin_module} ${srcs})
ament_target_dependencies(${bin_module} PUBLIC rclcpp std_msgs)
target_compile_definitions(${bin_module} PUBLIC ${compile_defs})
ament_target_dependencies(${bin_module} PUBLIC ${ROS_DEPS})
target_link_libraries(${bin_module} PUBLIC ${link_libs})
target_include_directories(
${bin_module} PUBLIC
Expand All @@ -30,10 +32,11 @@ function(make_ros_exe module srcs link_libs inc_dirs)
endfunction()

# Create unit test
function(make_unit_test module srcs link_libs inc_dirs)
function(make_unit_test module srcs link_libs inc_dirs compile_defs)
if(UNIT_TEST)
set(test_module test_${module})
add_executable(${test_module} ${srcs})
target_compile_definitions(${test_module} PUBLIC ${compile_defs})
target_include_directories(
${test_module} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/inc
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<depend>custom_interfaces</depend>
<exec_depend>ros2launch</exec_depend>

<export>
Expand Down
5 changes: 4 additions & 1 deletion projects/can_transceiver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ set(link_libs
set(inc_dirs
)

set(compile_defs
)

set(srcs
${CMAKE_CURRENT_LIST_DIR}/src/can_transceiver.cpp
${CMAKE_CURRENT_LIST_DIR}/src/can_frame_parser.cpp
Expand All @@ -16,4 +19,4 @@ set(bin_srcs
${srcs}
${CMAKE_CURRENT_LIST_DIR}/src/can_transceiver_ros_intf.cpp
)
make_ros_exe(${module} "${bin_srcs}" "${link_libs}" "${inc_dirs}")
make_exe(${module} "${bin_srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")
10 changes: 1 addition & 9 deletions projects/can_transceiver/inc/can_transceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <linux/can.h>

#include <std_msgs/msg/detail/string__struct.hpp>
colinli02 marked this conversation as resolved.
Show resolved Hide resolved
#include <string>

#include "can_frame_parser.h"
Expand Down Expand Up @@ -99,12 +100,3 @@ class CanbusIntf : public CanTransceiver
*/
void send(const CanFrame & frame) const;
};

colinli02 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Implementation of CAN Transceiver that interfaces with the simulator
*
*/
class CanSimIntf : public CanTransceiver, public rclcpp::Node
{
void receive();
};
13 changes: 10 additions & 3 deletions projects/can_transceiver/src/can_transceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <chrono>
#include <cstring>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
#include <thread>

#include "can_frame_parser.h"
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
colinli02 marked this conversation as resolved.
Show resolved Hide resolved

using namespace std::chrono_literals;

using IFreq = struct ifreq;
using SockAddr = struct sockaddr;
using SockAddrCan = struct sockaddr_can;

#define QUEUE_SIZE 10
colinli02 marked this conversation as resolved.
Show resolved Hide resolved
namespace
{

/**
* @brief Format a CAN frame into a string for debugging and logging
*
*
* @param frame CAN frame to format
* @return String representation of frame
*/
Expand Down Expand Up @@ -114,5 +123,3 @@ void CanbusIntf::send(const CanFrame & frame) const
std::cerr << fmtCanFrameDbgStr(frame) << std::endl;
}
}

void CanSimIntf::receive() {}
146 changes: 145 additions & 1 deletion projects/can_transceiver/src/can_transceiver_ros_intf.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
#include <cstdio>
colinli02 marked this conversation as resolved.
Show resolved Hide resolved
#include <custom_interfaces/msg/detail/batteries__struct.hpp>
#include <custom_interfaces/msg/detail/generic_sensors__struct.hpp>
#include <custom_interfaces/msg/detail/gps__struct.hpp>
#include <custom_interfaces/msg/detail/wind_sensor__struct.hpp>
#include <custom_interfaces/msg/detail/wind_sensors__struct.hpp>
colinli02 marked this conversation as resolved.
Show resolved Hide resolved
#include <memory>
#include <chrono>
#include <functional>
#include <string>
#include <thread>

#include "can_frame_parser.h"
#include "can_transceiver.h"

#include "custom_interfaces/msg/gps.hpp"
#include "custom_interfaces/msg/wind_sensors.hpp"
#include "custom_interfaces/msg/batteries.hpp"
#include "custom_interfaces/msg/generic_sensors.hpp"
#include "custom_interfaces/msg/helper_generic_sensor.hpp"
#include "custom_interfaces/msg/wind_sensor.hpp"

#include "rclcpp/publisher.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp/subscription.hpp"
#include "rclcpp/timer.hpp"
#include "ros_info.h"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;
colinli02 marked this conversation as resolved.
Show resolved Hide resolved

#define QUEUE_SIZE 10
#define WIND_SENSOR_BOUND 2
#define PLACEHOLDER_VALUE 42 // Placeholder value for debugging or testing
colinli02 marked this conversation as resolved.
Show resolved Hide resolved

class CanTransceiverIntf : public rclcpp::Node
{
public:
Expand Down Expand Up @@ -43,13 +69,131 @@ class CanTransceiverIntf : public rclcpp::Node

/**
* @brief Callback function to subscribe to the onboard ROS network
*
*
*/
void sub_cb(std_msgs::msg::String /* placeholder */) { can_trns_->onNewCmd(Placeholder0); }
};


//===========================================================================================
// Simulation Interface
//===========================================================================================
/* Refer to https://ubcsailbot.atlassian.net/wiki/spaces/prjt22/pages/1768849494/Simulation+Interface#Interfaces
* Handles publishing and subscribing to certain ROS topics
*/
class CanSimIntf : public rclcpp::Node
{
public:
CanSimIntf() //Our node which subs to topics
: Node("CanSimIntf")
{
// Subscriber
// There is no timer because subscriber will respond to whatever data is published to the topic /Simulator
// Topic: mock_gps
subscriptionGPS_ = this->create_subscription<custom_interfaces::msg::GPS>(
"mock_gps", QUEUE_SIZE, std::bind(&CanSimIntf::gps_callback, this, std::placeholders::_1));

// Topic: mock_wind_sensors
subscriptionWindSensor_ = this->create_subscription<custom_interfaces::msg::WindSensors>(
"mock_wind_sensors", QUEUE_SIZE, std::bind(&CanSimIntf::wind_callback, this, std::placeholders::_1));

// Publisher
publisherWindSensors_ = this->create_publisher<custom_interfaces::msg::WindSensors>("wind_sensors", QUEUE_SIZE);
publisherWindSensor_ = this->create_publisher<custom_interfaces::msg::WindSensor>("filtered_wind_sensor", QUEUE_SIZE);
publisherGPS_ = this->create_publisher<custom_interfaces::msg::GPS>("gps", QUEUE_SIZE);
colinli02 marked this conversation as resolved.
Show resolved Hide resolved
publisherGenericSensors_ = this->create_publisher<custom_interfaces::msg::GenericSensors>("data_sensors", QUEUE_SIZE);
publisherBatteries_ = this->create_publisher<custom_interfaces::msg::Batteries>("batteries", QUEUE_SIZE);
// Timer with 500ms delay
timer_ = this->create_wall_timer(500ms, std::bind(&CanSimIntf::timer_callback, this));
}

private:

// Subscriber
void gps_callback(const custom_interfaces::msg::GPS::SharedPtr msg) const
{

}
void wind_callback(const custom_interfaces::msg::WindSensors::SharedPtr msg) const
{

}

// Publisher
void timer_callback()
{
// ** GPS **
custom_interfaces::msg::GPS gpsPubData = custom_interfaces::msg::GPS();
gpsPubData.heading.heading = PLACEHOLDER_VALUE;
gpsPubData.speed.speed = PLACEHOLDER_VALUE;
gpsPubData.lat_lon.latitude = PLACEHOLDER_VALUE;
gpsPubData.lat_lon.longitude = PLACEHOLDER_VALUE;
// Publishes msg
publisherGPS_->publish(gpsPubData);

// ** Wind Sensor (WSensor)**
custom_interfaces::msg::WindSensor WSensorData = custom_interfaces::msg::WindSensor();
WSensorData.speed.speed = PLACEHOLDER_VALUE;
WSensorData.direction = PLACEHOLDER_VALUE;
// Publishes msg
publisherWindSensor_->publish(WSensorData);

// ** Wind Sensors (WSensors)**
custom_interfaces::msg::WindSensors WSensorsData = custom_interfaces::msg::WindSensors();
WSensorsData.wind_sensors[0].direction = PLACEHOLDER_VALUE;
WSensorsData.wind_sensors[0].speed.speed = PLACEHOLDER_VALUE;
// Publishes msg
publisherWindSensors_->publish(WSensorsData);

// ** Batteries **
custom_interfaces::msg::Batteries WBatteriesData = custom_interfaces::msg::Batteries();
WBatteriesData.batteries[0].voltage = PLACEHOLDER_VALUE;
colinli02 marked this conversation as resolved.
Show resolved Hide resolved
WBatteriesData.batteries[0].current = PLACEHOLDER_VALUE;
// Publishes msg
publisherBatteries_->publish(WBatteriesData);

// ** Generic Sensors **
custom_interfaces::msg::HelperGenericSensor HelperGenSensorData = custom_interfaces::msg::HelperGenericSensor();
custom_interfaces::msg::GenericSensors GenSensorData = custom_interfaces::msg::GenericSensors();
HelperGenSensorData.id = 0x0; //uint8
HelperGenSensorData.data = 0x0; //uint64
// Publishes msg
GenSensorData.generic_sensors.push_back(HelperGenSensorData);
publisherGenericSensors_->publish( GenSensorData );

}

// Field Operations

// Publisher Field Declarations
rclcpp::Publisher<custom_interfaces::msg::WindSensors>::SharedPtr publisherWindSensors_;
rclcpp::Publisher<custom_interfaces::msg::WindSensor>::SharedPtr publisherWindSensor_;
rclcpp::Publisher<custom_interfaces::msg::GPS>::SharedPtr publisherGPS_;
rclcpp::Publisher<custom_interfaces::msg::GenericSensors>::SharedPtr publisherGenericSensors_;
colinli02 marked this conversation as resolved.
Show resolved Hide resolved
rclcpp::Publisher<custom_interfaces::msg::Batteries>::SharedPtr publisherBatteries_;

// Subscriber Field Declarations
rclcpp::Subscription<custom_interfaces::msg::GPS>::SharedPtr subscriptionGPS_;
rclcpp::Subscription<custom_interfaces::msg::WindSensors>::SharedPtr subscriptionWindSensor_;

// Variable to count number of msgs published
//size_t count_;
colinli02 marked this conversation as resolved.
Show resolved Hide resolved

// Timer object to allow our CamSimIntfFeedback node to perform action at x rate
rclcpp::TimerBase::SharedPtr timer_;

};


//===========================================================================================
// Main
//===========================================================================================
// Run with:
//$ ros2 run network_systems can_transceiver
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<CanSimIntf>());
rclcpp::shutdown();
return 0;
}
9 changes: 6 additions & 3 deletions projects/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ set(link_libs
set(inc_dirs
)

set(compile_defs
)

# Create module library
set(srcs
${CMAKE_CURRENT_LIST_DIR}/src/cached_fib.cpp
)
make_lib(${module} "${srcs}" "${link_libs}" "${inc_dirs}")
make_lib(${module} "${srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")

# Create module ROS executable
set(bin_srcs
${srcs}
${CMAKE_CURRENT_LIST_DIR}/src/cached_fib_subscriber.cpp
)
make_ros_exe(${module} "${bin_srcs}" "${link_libs}" "${inc_dirs}")
make_exe(${module} "${bin_srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")

# Create unit test
set(test_srcs
${srcs}
${CMAKE_CURRENT_LIST_DIR}/test/test_cached_fib.cpp
)
make_unit_test(${module} "${test_srcs}" "${link_libs}" "${inc_dirs}")
make_unit_test(${module} "${test_srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")
Loading