1. Visual module architecture design
Several aspects to consider:
Mainly use the (toc) mechanism to continuously publish image information to the outside world;
There are two forms of image reception and processing, as well as sending processing result nodes. One is to use the service communication mechanism, and the other is to use the topic mechanism. Both are acceptable. I collected some information online and referred to the opinions of chatgpt, and got a good result: topics are faster and simpler to implement. Topics are generally used by default during development. If topics no longer meet our needs as development progresses, we can switch to the service mechanism. Since the sensors (camera) and models in the arm (usually only one is used in a robotic arm) are not complicated, I chose the topic communication mechanism to develop the image data reception and processing and result sending modules.
Use subscribers to receive the processing results of the CV model.
[sens publisher (camera_pub.py)] --> [scriber and publisher node (cam_sub_and_detecon_pub.py)] --> [subscriber (detection_results_sub.py)]
2. Code Writing
1. Create a new workspace
1. Create a src folder to store the source code;
2. Create cv_devel_pkg and interfaces_pkg in the src directory to store the source code of the vision development module and topic data (interface) files respectively;
2.1 interfaces_pkg writing
It should be noted that when creating a new interface pkg, build type can only be selected temporarily (information source: official document), and we need to modify cmakelists.txt and package.xml:
cmakelists.txt:
Added:
# find_package(REQUIRED) find_package(geometry_msgs REQUIRED) find_package(rosidl_default_generators REQUIRED) rosidl_genera_interfaces(${PROJECT_NAME} "msg/DetectionResults.msg" s DEPENDENCIES geometry_msgs )
package.xml:
Added:
geometry_msgs rosidl_default_generators rosidl_default_runtime rosidl_interface_packages
Among them, the information stored in DetectionResults.msg is the msg of the result coordinates:
int32 position_x
int32 position_y
At this point, the interface pkg code writing is completed.
2.2 cv_devel_pkg writing
(1) camera_pub.py
import rclpy from rclpy.node import Node from cv_bridge import CvBridge from sensor_msgs.msg import Image import cv2 class CameraPubNode(Node): def __init__(self, name): super().__init__(name) self.pub = self.create_publisher(Image, 'image_raw', 10) self.timer = self.create_timer(0.5, self.timer_callbk) self.cap = cv2.VideoCapture(0) self.cv_bridge = CvBridge() def timer_callback(self): ret = self.cap.grab() if ret: flag, frame = self.cap.retrieve() if flag: self.pub.publish(self.cv_bridge.cv2_to_imgmsg(frame, 'bgr8')) self.get_logger().info('Publish image successfully!') else: self.get_logger().info('Did not get image info!') def main(args=None): rclpy.init(args=args) node = CameraPubNode('CameraPubNode') rclpy.spin(node) node.destroy_node() rclpy.shutdown()
After writing, register the node in setup.py under the pkg directory, and execute colcon build, source install/local_setup.sh, and run cv_devel_pkg camera_pub respectively.
As shown in the figure, normal operation:
(2) cam_sub_and_detection_pub.py
import rclpy from rclpy.node import Node from sensor_msgs.msg import Image from interfaces_pkg.msg import DetectionResults from cv_bridge import CvBridge import cv2 import numpy as np class CamSubAndDetectionPubNode(Node): def __init__(self, name): super().__init__(name) self.sub = self.create_subscription(Image, 'image_raw', self.listen_callback, 10) self.pub = self.create_publisher(DetectionResults, 'detection_results', 10) self.cv_bridge = CvBridge() self.position_x = 0 self.position_y = 0 def listen_callback(self, data): self.get_logger().info('Get image! I will process it!') image = self.cv_bridge.imgmsg_to_cv2(data, 'bgr8') self.detect(image) position = DetectionResults() position.position_x = self.position_x position.position_y = self.position_y self.get_logger().info('Position is: ({}, {})'.format(self.position_x, self.position_y)) self.pub.publish(position) def detect(self, image): pass#You can embed your own or AI vision code here def main(args=None): rclpy.init(args=args) node = CamSubAndDetectionPubNode('CamSubAndDetectionPubNode') rclpy.spin(node) node.destroy_node() rclpy.shutdown()
(3) detection_results_sub.py
import rclpy from rclpy.node import Node from interfaces_pkg.msg import DetectionResults class DetectionResulubNode(Node): def __init__(self, name): super().__init__(name) self.sub = self.create_subscription(DetectionResults, 'detection_results', self.listen_callback, 10) def listen_callback(self, data): self.get_logger().info('I get the position: ({},{})'.format(data.position_x, data.position_y)) def main(args=None): rclpy.init(args=args) node = DetectionResultsSubNode('detection_results_sub_node') rclpy.spin(node) node.destroy_node() rclpy.shutdown()
3. Completion
After all the node codes in cv_devel_pkg are written, register them in setup.py, then build & run.
Results:
The three nodes are running normally:
Review editor: Liu Qing
Previous article:Explore the innovative journey of exoskeleton robots
Next article:Synabot launches an autonomous loading and unloading robot - iLoabot-M
- Popular Resources
- Popular amplifiers
- Using IMU to enhance robot positioning: a fundamental technology for accurate navigation
- Researchers develop self-learning robot that can clean washbasins like humans
- Universal Robots launches UR AI Accelerator to inject new AI power into collaborative robots
- The first batch of national standards for embodied intelligence of humanoid robots were released: divided into 4 levels according to limb movement, upper limb operation, etc.
- New chapter in payload: Universal Robots’ new generation UR20 and UR30 have upgraded performance
- Humanoid robots drive the demand for frameless torque motors, and manufacturers are actively deploying
- MiR Launches New Fleet Management Software MiR Fleet Enterprise, Setting New Standards in Scalability and Cybersecurity for Autonomous Mobile Robots
- Nidec Drive Technology produces harmonic reducers for the first time in China, growing together with the Chinese robotics industry
- DC motor driver chip, low voltage, high current, single full-bridge driver - Ruimeng MS31211
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Thank you + my son, my daughter, and my wife
- Op amp circuit noise calculation
- How to set breakpoints when debugging a program in keil4?
- 【Qinheng Trial】3. System clock and TIMER0
- XILINX DDR3 IP Core Usage Tutorial
- Award-winning live broadcast: Market environment is uncertain, protecting IoT devices Infineon is very sure to start on time at 10:00 on July 30
- How to measure the internal resistance of the battery?
- Which one costs more: 200 MSP430s or 250 MSP430s?
- Free application: ST sensor kit SensorTile.box worth 350 yuan
- EEWorld Circuit Diagram Channel has a new version and is now online, come and experience it now!