Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
O AL
Published

AI Robot Arm Configuration

This project detects the mouth position and configures the robot arm using Kria KR260 Robotics Starter Kit.

33
AI Robot Arm Configuration

Things used in this project

Hardware components

Kria™ KR260 Robotics Starter Kit
AMD Kria™ KR260 Robotics Starter Kit
×1

Software apps and online services

PYNQ Framework
AMD PYNQ Framework
Robot Operating System
ROS Robot Operating System

Story

Read more

Code

threejoints.urdf

HTML
URDF to configure robot in Moveit Assistant.
<?xml version="1.0"?>
<robot name="robot_arm">
  <link name="world"/>
  <link name="base_link">
    <visual>
      <geometry>
        <cylinder radius="0.1" length="0.05"></cylinder>
      </geometry>
      <material name="cyan">
        <color rgba="0 1 1 1"/>
      </material>
      <origin xyz="0 0 0.025" rpy="0 0 0"></origin>
    </visual>

    <collision>
      <geometry>
        <cylinder radius="0.1" length="0.05"></cylinder>
      </geometry>
      <origin xyz="0 0 0.025" rpy="0 0 0"></origin>
    </collision>

    <inertial>
      <origin xyz="0 0 0.025" rpy="0 0 0"></origin>
      <mass value="5.0"></mass>
      <inertia ixx="0.0135" ixy="0.0" ixz="0.0" iyy="0.0135" iyz="0.0" izz="0.05"></inertia>
    </inertial>
  </link>

  <link name="link_1">
    <visual>
      <geometry>
        <cylinder radius="0.05" length="0.5"></cylinder>
      </geometry>
      <material name="red">
        <color rgba="1 0 0 1"/>
      </material>
      <origin xyz="0 0 0.25" rpy="0 0 0"></origin>
    </visual>

    <collision>
      <geometry>
        <cylinder radius="0.05" length="0.5"></cylinder>
      </geometry>
      <origin xyz="0 0 0.25" rpy="0 0 0"></origin>
    </collision>

    <inertial>
      <origin xyz="0 0 0.25" rpy="0 0 0"></origin>
      <mass value="5.0"></mass>
      <inertia ixx="0.107" ixy="0.0" ixz="0.0" iyy="0.107" iyz="0.0" izz="0.0125"></inertia>
    </inertial>
  </link>

  <link name="link_2">
    <inertial>
      <origin xyz="0 0 0.2" rpy="0 0 0"></origin>
        <mass value="2.0"></mass>
        <inertia ixx="0.027" ixy="0.0" ixz="0.0" iyy="0.027" iyz="0.0" izz="0.0025"></inertia>
    </inertial>
    <visual>
      <geometry>
        <cylinder radius="0.05" length="0.1"></cylinder>
      </geometry>
      <material name="Green">
        <color rgba="0 1 0 1"/>
      </material>
    </visual>

    <collision>
      <geometry>
        <cylinder radius="0.05" length="0.1"></cylinder>
      </geometry>
    </collision>
  </link>

  <link name="link_3">
    <inertial>
      <origin xyz="0 0 0.2" rpy="0 0 0"></origin>
      <mass value="0.01"></mass>
      <inertia ixx="0.027" ixy="0.0" ixz="0.0" iyy="0.027" iyz="0.0" izz="0.0025"></inertia>
    </inertial>

    <visual>
      <geometry>
        <cylinder radius="0.05" length="0.4"></cylinder>
      </geometry>
      <material name="blue">
        <color rgba="0 0 1 1"/>
      </material>
      <!-- <origin rpy="0 0 0" xyz="0 0 0.2"></origin> -->
    </visual>

    <collision>
      <geometry>
        <cylinder radius="0.05" length="0.4"></cylinder>
      </geometry>
    </collision>
  </link>

  <link name="link_4">
    <inertial>
      <origin xyz="0 0 0.2" rpy="0 0 0"></origin>
      <mass value="0.01"></mass>
      <inertia ixx="0.027" ixy="0.0" ixz="0.0" iyy="0.027" iyz="0.0" izz="0.0025"></inertia>
    </inertial>

    <visual>
      <geometry>
        <cylinder radius="0.05" length="0.1"></cylinder>
      </geometry>
      <material name="Red">
        <color rgba="1 0 0 1"/>
      </material>
    </visual>

    <collision>
      <geometry>
        <cylinder radius="0.05" length="0.1"></cylinder>
      </geometry>
    </collision>
  </link>

  <link name="link_5">
    <inertial>
      <origin xyz="0 0 0.2" rpy="0 0 0"></origin>
      <mass value="0.01"></mass>
      <inertia ixx="0.027" ixy="0.0" ixz="0.0" iyy="0.027" iyz="0.0" izz="0.0025"></inertia>
    </inertial>

    <visual>
      <geometry>
        <cylinder radius="0.05" length="0.3"></cylinder>
      </geometry>
      <material name="cyan">
        <color rgba="0 1 1 1"/>
      </material>
    </visual>

    <collision>
      <geometry>
        <cylinder radius="0.05" length="0.3"></cylinder>
      </geometry>
      <dynamics damping="0.0" friction="0.0"></dynamics>
    </collision>
  </link>

  <joint name="fixed" type="fixed">
    <parent link="world"></parent>
    <child link="base_link"></child>
  </joint>

  <joint name="joint_1" type="continuous">
    <axis xyz="0 0 1"></axis>
    <parent link="base_link"></parent>
    <child link="link_1"></child>
    <origin xyz="0.0 0.0 0.05" rpy="0 0 0"></origin>
  </joint>

  <joint name="joint_2" type="continuous">
    <axis xyz="0 0 1"></axis>
    <parent link="link_1"></parent>
    <child link="link_2"></child>
    <origin xyz="0.0 -0.005 0.58" rpy="0 1.5708 0"></origin>
    <limit lower="-0.25" upper="3.34" effort="10" velocity="0.5"></limit>
  </joint>

  <joint name="joint_3" type="fixed">
    <parent link="link_2"></parent>
    <child link="link_3"></child>
    <origin xyz="0.0 0.2 0" rpy="1.57 0 0"></origin>
  </joint>

  <joint name="joint_4" type="continuous">
    <parent link="link_3"></parent>
    <child link="link_4"></child>
    <origin xyz=" 0 0 -0.25" rpy="1.57 0 0"></origin>
    <axis xyz=" 0 0 1"></axis>
    <limit lower="-1.92" upper="1.92" effort="10" velocity="0.5"></limit>
  </joint>

  <joint name="joint_5" type="fixed">
    <parent link="link_4"></parent>
    <child link="link_5"></child>
    <origin xyz="0.0 -0.2 0 " rpy="1.57 0 0"></origin>
  </joint>
</robot>

ImagePublisher.py

Python
It will publish the video stream when the node is run and the camera is connected.
import rclpy 
from rclpy.node import Node 
from sensor_msgs.msg import Image
from sensor_msgs.msg import RegionOfInterest, CameraInfo
from std_msgs.msg import Float64
import geometry_msgs
from cv_bridge import CvBridge
import cv2 
import numpy as np

from rclpy.qos import qos_profile_sensor_data
coordinates = []

class mouthPublisher(Node):

# Create an FaceDetection class, which is a subclass of the Node class.
  def __init__(self):
# Class constructor to set up the node
# Initiate the Node class's constructor and give it a name
    super().__init__('face_detection')
    self.subscription = self.create_subscription(
    Image,
    'video_frames',
    self.listener_callback,
    qos_profile_sensor_data)
    self.subscription 
    self.br = CvBridge()
    self.running = True
    ROI =  RegionOfInterest()
    self.roi_pub =self.create_publisher(RegionOfInterest, '/roi', 10)
  def listener_callback(self, data):
    self.get_logger().info('Receiving image')
# Convert ROS Image message to OpenCV image
    if self.running:
      current_frame = self.br.imgmsg_to_cv2(data, "8UC3")
# Convert to grayscale
      gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
# Detect the faces
      face_cascade = cv2.CascadeClassifier('/home/ubuntu/KR260-Robotics-AI-Challenge/files/scripts/ros2_ws/src/haarcascade_frontalface_default.xml')
#      eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
      mouth_cascade = cv2.CascadeClassifier('/home/ubuntu/KR260-Robotics-AI-Challenge/files/scripts/ros2_ws/src/haarcascade_mouth.xml')
      faces = face_cascade.detectMultiScale(gray, 1.3, 5)
      mouth = mouth_cascade.detectMultiScale(gray,1.5,11)
# Draw the rectangle around each face
      for (x,y,w,h) in faces:
        current_frame = cv2.rectangle(current_frame,(x,y),(x+w,y+h),(0,0,255),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = current_frame[y:y+h, x:x+w]
        for(mx, my, mw, mh) in mouth:
          cv2.rectangle(current_frame, (mx, my), (mx+mw, my+mh), (255, 0, 0), 2)
          coordinates = [mx, my]
          self.publisher_ =self.create_publisher(RegionOfInterest, '/roi', 10)
          timer_period = 1.5
          self.timer = self.create_timer(timer_period, self.timer_callback)
          print('xPosition of mouth:', mx)
          print('yPosition of mouth:', my)
      cv2.imshow("Camera", current_frame)
      cv2.waitKey(1)
  def timer_callback(self):
    self.roi_pub.publish(x_offset, y_offset)
def main(args=None):
  print('Hi from detectmouth_package.')
# Initialize the rclpy library
  rclpy.init(args=args)
# Create the node
  mouth_detection = mouthPublisher()
# Spin the node so the callback function is called.
  rclpy.spin(mouth_detection)
  mouth_detection.destroy_node()
  rclpy.shutdown()
if __name__ == '__main__':
  main()

mouthPublisher.py

Python
It will draw bounding boxes around the face and the mouth in a pop up window.
import rclpy 
from rclpy.node import Node 
from sensor_msgs.msg import Image 
from sensor_msgs.msg import RegionOfInterest, CameraInfo
from std_msgs.msg import Float64
import geometry_msgs
from cv_bridge import CvBridge 
import cv2 
import numpy as np

from rclpy.qos import qos_profile_sensor_data
coordinates = []

class mouthPublisher(Node):

# Create an FaceDetection class, which is a subclass of the Node class.
  def __init__(self):
    super().__init__('face_detection')
    self.subscription = self.create_subscription(
    Image,
    'video_frames',
    self.listener_callback,
    qos_profile_sensor_data)
    self.subscription 
    self.br = CvBridge()
    self.running = True
    ROI =  RegionOfInterest()
    self.roi_pub =self.create_publisher(RegionOfInterest, '/roi', 10)
  def listener_callback(self, data):
    self.get_logger().info('Receiving image')
# Convert ROS Image message to OpenCV image
    if self.running:
      current_frame = self.br.imgmsg_to_cv2(data, "8UC3")
# Convert to grayscale
      gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
# Detect the faces
      face_cascade = cv2.CascadeClassifier('/home/ubuntu/KR260-Robotics-AI-Challenge/files/scripts/ros2_ws/src/haarcascade_frontalface_default.xml')
#      eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
      mouth_cascade = cv2.CascadeClassifier('/home/ubuntu/KR260-Robotics-AI-Challenge/files/scripts/ros2_ws/src/haarcascade_mouth.xml')
      faces = face_cascade.detectMultiScale(gray, 1.3, 5)
      mouth = mouth_cascade.detectMultiScale(gray,1.5,11)
# Draw the rectangle around each face
      for (x,y,w,h) in faces:
        current_frame = cv2.rectangle(current_frame,(x,y),(x+w,y+h),(0,0,255),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = current_frame[y:y+h, x:x+w]
        for(mx, my, mw, mh) in mouth:
          cv2.rectangle(current_frame, (mx, my), (mx+mw, my+mh), (255, 0, 0), 2)
          coordinates = [mx, my]
          self.publisher_ =self.create_publisher(RegionOfInterest, '/roi', 10)
          timer_period = 1.5
          self.timer = self.create_timer(timer_period, self.timer_callback)
          print('xPosition of mouth:', mx)
          print('yPosition of mouth:', my)
      cv2.imshow("Camera", current_frame)
      cv2.waitKey(1)
  def timer_callback(self):
    self.roi_pub.publish(x_offset, y_offset)

def main(args=None):
  print('Hi from detectmouth_package.')
# Initialize the rclpy library
  rclpy.init(args=args)
# Create the node
  mouth_detection = mouthPublisher()
# Spin the node so the callback function is called.
  rclpy.spin(mouth_detection)
  mouth_detection.destroy_node()
  rclpy.shutdown()
if __name__ == '__main__':
  main()

robot_arm.srdf

XML
This is the SRDF file generated by MoveIt Assistant
<?xml version="1.0" encoding="UTF-8"?>
<!--This does not replace URDF, and is not an extension of URDF.
    This is a format for representing semantic information about the robot structure.
    A URDF file must exist for this robot as well, where the joints and the links that are referenced are defined
-->
<robot name="robot_arm">
    <!--GROUPS: Representation of a set of joints and links. This can be useful for specifying DOF to plan for, defining arms, end effectors, etc-->
    <!--LINKS: When a link is specified, the parent joint of that link (if it exists) is automatically included-->
    <!--JOINTS: When a joint is specified, the child link of that joint (which will always exist) is automatically included-->
    <!--CHAINS: When a chain is specified, all the links along the chain (including endpoints) are included in the group. Additionally, all the joints that are parents to included links are also included. This means that joints along the chain and the parent joint of the base link are included in the group-->
    <!--SUBGROUPS: Groups can also be formed by referencing to already defined group names-->
    <group name="KS">
        <chain base_link="base_link" tip_link="link_5"></chain>
    </group>
    <!--GROUP STATES: Purpose: Define a named state for a particular group, in terms of joint values. This is useful to define states like 'folded arms'-->
    <group_state name="home" group="KS">
        <joint name="joint_1" value="1.0472"></joint>
        <joint name="joint_2" value="0"></joint>
        <joint name="joint_4" value="1.5708"></joint>
    </group_state>
    <!--END EFFECTOR: Purpose: Represent information about an end effector.-->
    <end_effector name="tool" parent_link="link_5" group="KS"></end_effector>
    <!--VIRTUAL JOINT: Purpose: this element defines a virtual joint between a robot link and an external frame of reference (considered fixed with respect to the robot)-->
    <virtual_joint name="virtual_joint" type="fixed" parent_frame="world" child_link="base_link"></virtual_joint>
    <!--DISABLE COLLISIONS: By default it is assumed that any link of the robot could potentially come into collision with any other link in the robot. This tag disables collision checking between a specified pair of links. -->
    <disable_collisions link1="base_link" link2="link_1" reason="Adjacent"></disable_collisions>
    <disable_collisions link1="base_link" link2="link_2" reason="Never"></disable_collisions>
    <disable_collisions link1="base_link" link2="link_3" reason="Never"></disable_collisions>
    <disable_collisions link1="base_link" link2="link_4" reason="Never"></disable_collisions>
    <disable_collisions link1="base_link" link2="link_5" reason="Never"></disable_collisions>
    <disable_collisions link1="link_1" link2="link_2" reason="Adjacent"></disable_collisions>
    <disable_collisions link1="link_1" link2="link_3" reason="Never"></disable_collisions>
    <disable_collisions link1="link_1" link2="link_4" reason="Never"></disable_collisions>
    <disable_collisions link1="link_1" link2="link_5" reason="Never"></disable_collisions>
    <disable_collisions link1="link_2" link2="link_3" reason="Adjacent"></disable_collisions>
    <disable_collisions link1="link_2" link2="link_4" reason="Never"></disable_collisions>
    <disable_collisions link1="link_2" link2="link_5" reason="Never"></disable_collisions>
    <disable_collisions link1="link_3" link2="link_4" reason="Adjacent"></disable_collisions>
    <disable_collisions link1="link_3" link2="link_5" reason="Never"></disable_collisions>
    <disable_collisions link1="link_4" link2="link_5" reason="Adjacent"></disable_collisions>
</robot>

Credits

O AL
1 project • 0 followers

Comments