Skip to content

Commit

Permalink
Merge branch 'main' into compressed_image
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono authored Aug 1, 2024
2 parents 4cc3dba + 5570ab7 commit aa665c6
Show file tree
Hide file tree
Showing 31 changed files with 636 additions and 7 deletions.
7 changes: 6 additions & 1 deletion device/camera_reader/camera_reader/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def __init__(self):
super().__init__("camera")
self.publisher_ = self.create_publisher(CompressedImage, "camera_image", 10)
self.timer = self.create_timer(0.1, self.timer_callback)
self.cap = cv2.VideoCapture(0)
param = self.declare_parameter("camera_id", 0)
camera_id = (
self.get_parameter_or(param.name, param).get_parameter_value().integer_value
)
assert isinstance(camera_id, int)
self.cap = cv2.VideoCapture(camera_id)
self.bridge = CvBridge()
if not self.cap.isOpened():
self.get_logger().error("Failed to open camera")
Expand Down
3 changes: 3 additions & 0 deletions device/camera_reader/config/default_param.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*:
ros__parameters:
camera_id: 0
9 changes: 9 additions & 0 deletions device/camera_reader/launch/camera_reader_launch.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
config = os.path.join(
get_package_share_directory("camera_reader"),
"config",
"default_param.yml",
)
return LaunchDescription(
[
Node(
package="camera_reader",
executable="camera",
namespace="device",
parameters=[config],
remappings=[("/device/camera_image", "/packet/camera_image")],
),
],
Expand Down
4 changes: 4 additions & 0 deletions device/camera_reader/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
os.path.join("share", package_name, "launch"),
glob(os.path.join("launch", "*")),
),
(
os.path.join("share", package_name, "config"),
glob(os.path.join("config", "*")),
),
],
install_requires=["setuptools"],
zip_safe=True,
Expand Down
2 changes: 1 addition & 1 deletion device/joystick/joystick/joystick.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Joystick(Node):
def __init__(self):
super().__init__("joystick")
self._timer = self.create_timer(0.5, self._timer_callback)
self._timer = self.create_timer(0.01, self._timer_callback)
self._joy_publisher = self.create_publisher(Joy, "joystick", 10)
# TODO: ここparameterで変えられるようにしたい
self._joystick = pygame.joystick.Joystick(0)
Expand Down
60 changes: 60 additions & 0 deletions device/nucleo_communicate/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
cmake_minimum_required(VERSION 3.8)
project(nucleo_communicate)

set(CMAKE_CXX_STANDARD 20)

if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif ()

# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(packet_interfaces REQUIRED)

add_library(channel
SHARED
src/recv_data.cpp
src/send_data.cpp
src/serial_port.cpp
src/channel.cpp
)

ament_target_dependencies(channel
rclcpp
rclcpp_components
std_msgs
packet_interfaces
)

target_include_directories(channel
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

rclcpp_components_register_node(
channel
PLUGIN "channel::Channel"
EXECUTABLE nucleo-channel
)

install(
TARGETS channel
EXPORT channel_component
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)

install(
DIRECTORY launch
DESTINATION share/${PROJECT_NAME}
)

ament_package()
15 changes: 15 additions & 0 deletions device/nucleo_communicate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# nucleo_communicate

Nucleoとの通信周り

## Nodes

- `Channel`: データのやり取り(TX/RX両方)

## Executables

- `nucleo-chanel`: `Channel`Nodeをspin

## Launches

- `channel_launch.py`: `Channel`Nodeを`device`namespaceでspin
45 changes: 45 additions & 0 deletions device/nucleo_communicate/include/nucleo_communicate/channel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef NUCLEO_COMMUNICATE_CHANNEL_HPP
#define NUCLEO_COMMUNICATE_CHANNEL_HPP

#include <mutex>

#include <packet_interfaces/msg/current.hpp>
#include <packet_interfaces/msg/flex.hpp>
#include <packet_interfaces/msg/power.hpp>
#include <packet_interfaces/msg/voltage.hpp>
#include <rclcpp/node.hpp>
#include <rclcpp/node_options.hpp>
#include <rclcpp/subscription.hpp>
#include <std_msgs/msg/empty.hpp>

#include "nucleo_communicate/serial_port.hpp"

namespace channel {

class Channel : public rclcpp::Node {
private:
nucleo_com::SerialPort serial;
std::mutex serial_mutex;

rclcpp::Subscription<std_msgs::msg::Empty>::SharedPtr quit_subscription;
rclcpp::Subscription<packet_interfaces::msg::Power>::SharedPtr power_subscription;

rclcpp::Publisher<packet_interfaces::msg::Flex>::SharedPtr flex1_publisher;
rclcpp::Publisher<packet_interfaces::msg::Flex>::SharedPtr flex2_publisher;
rclcpp::Publisher<packet_interfaces::msg::Current>::SharedPtr current_publisher;
rclcpp::Publisher<packet_interfaces::msg::Voltage>::SharedPtr voltage_publisher;

rclcpp::TimerBase::SharedPtr timer;

void quit_subscription_callback(const std_msgs::msg::Empty& _msg);
void power_subscription_callback(const packet_interfaces::msg::Power& msg);

void timer_callback();

public:
Channel(const rclcpp::NodeOptions& options = rclcpp::NodeOptions());
};

}

#endif // NUCLEO_COMMUNICATE_CHANNEL_HPP
25 changes: 25 additions & 0 deletions device/nucleo_communicate/include/nucleo_communicate/recv_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef NUCLEO_COMMUNICATE_RECV_DATA_HPP
#define NUCLEO_COMMUNICATE_RECV_DATA_HPP

#include <array>
#include <cstdint>
#include <std_msgs/msg/detail/header__struct.hpp>

namespace nucleo_com {

struct RecvData {
using BufferType = std::array<std::uint8_t, 8>;

BufferType buffer;

explicit RecvData(BufferType&& buf);

auto flex1_value() const -> std::uint16_t;
auto flex2_value() const -> std::uint16_t;
auto current_value() const -> std::uint16_t;
auto voltage_value() const -> std::uint16_t;
};

}

#endif // NUCLEO_COMMUNICATE_RECV_DATA_HPP
25 changes: 25 additions & 0 deletions device/nucleo_communicate/include/nucleo_communicate/send_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef NUCLEO_COMMUNICATE_SEND_DATA_HPP
#define NUCLEO_COMMUNICATE_SEND_DATA_HPP

#include <array>
#include <cstdint>

#include "packet_interfaces/msg/power.hpp"

namespace nucleo_com {

struct SendData {
using BufferType = std::array<std::uint8_t, 16>;

BufferType buffer;

SendData() = delete;

explicit SendData(BufferType&& buf);

static auto from_msg(const packet_interfaces::msg::Power& msg) -> SendData;
};

}

#endif // NUCLEO_COMMUNICATE_SEND_DATA_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef NUCLEO_COMMUNICATE_SERIAL_PORT_HPP
#define NUCLEO_COMMUNICATE_SERIAL_PORT_HPP

#include <stdio.h>
#include <string>
#include <termios.h>

#include "nucleo_communicate/recv_data.hpp"
#include "nucleo_communicate/send_data.hpp"

namespace nucleo_com {

class SerialPort {
private:
FILE* serial;

public:
explicit SerialPort(const std::string& path = "/dev/ttyACM0");

~SerialPort();

// same as
// https://os.mbed.com/docs/mbed-os/v6.16/apis/serial-uart-apis.html#configuration
void setup();

// https://github.com/rogy-AquaLab/2024_umiusi_nucleo
void send(const nucleo_com::SendData& data);

auto receive() -> nucleo_com::RecvData;

void quit();
};

}

#endif // NUCLEO_COMMUNICATE_SERIAL_PORT_HPP
20 changes: 20 additions & 0 deletions device/nucleo_communicate/launch/channel_launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
channel = Node(
package="nucleo_communicate",
executable="nucleo-channel",
namespace="device",
remappings=[
("/device/flex_1", "/packet/sensors/flex_1"),
("/device/flex_2", "/packet/sensors/flex_2"),
("/device/current", "/packet/sensors/current"),
("/device/voltage", "/packet/sensors/voltage"),
# FIXME: /packet namespaceにremap
("/device/power", "/device/order/power"),
("/device/quit", "/device/order/quit")
],
)
return LaunchDescription([channel])
19 changes: 19 additions & 0 deletions device/nucleo_communicate/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>nucleo_communicate</name>
<version>0.1.0</version>
<description>Nucleoとの通信周り</description>
<maintainer email="[email protected]">h1rono</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
<depend>std_msgs</depend>
<depend>packet_interfaces</depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading

0 comments on commit aa665c6

Please sign in to comment.