# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# 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.
# Copyright 2022 robot-creation-diary.com
#
# 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.
# 整数とテキストを送信するチュートリアルの内容から、ステレオカメラデータを取得し
# 変換・送信するように変更してある。
import rclpy
from rclpy.node import Node
from cpp_pubsub.msg import CameraMsg
#from cpp_pubsub.msg import VideoMsg
from hmdcontroller_sub.CustomMjpeg import CustomMjpeg
import cv2
import time
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
print("init start")
self.publisher = self.create_publisher(CameraMsg, 'topic', 10)
timer_period = 0.10 # seconds
print("init 0")
self.st = time.time()
# カメラ関連
# 0はカメラを指定する番号。状況に応じて変更のこと
self.video_capture_l = CustomMjpeg(1,1280,720,10)
# 1はカメラを指定する番号。状況に応じて変更のこと
self.video_capture_r = CustomMjpeg(0,1280,720,10)
self.timer = self.create_timer(timer_period, self.timer_callback)
def __del__(self):
pass
def timer_callback(self):
# カメラ関連
frame_data_l = self.video_capture_l.get_jpeg_data()
frame_data_r = self.video_capture_r.get_jpeg_data()
if None is not frame_data_l and None is not frame_data_r:
# データ送信関連
msg = CameraMsg()
# 送信用データの代入単純に=で代入しようとすると型が違うと怒られる
msg.img_data_l.frombytes(frame_data_l)
msg.img_data_r.frombytes(frame_data_r)
self.publisher.publish(msg)
# # コンソールに何も表示されないと不安なのでFPSを表示
print("FPS", 1/(time.time() - self.st ))
self.st = time.time()
pass
else:
if None is not frame_data_l:
print("l", len(frame_data_l))
if None is not frame_data_r:
print("r", len(frame_data_r))
pass
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
minimal_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()