User Tools

Site Tools


avatar_furo_source_codes

This is an old revision of the document!


Programming

<Face Tracking>

The findface_publisher.py is a publisher that detects people's face and transfer the x,y coordinates of people's face to the face_tracks.py script. Directory: facedetects/nodes/findface_publisher.py

| findface_publisher.py
#!/usr/bin/env python
import cv2 as cv # opencv library name to cv
import rospy #ros python
 
from sensor_msgs.msg import Image
from geometry_msgs.msg import Point
 
rec_point=Point()
 
rospy.init_node("furo_find")
coordinates_pub = rospy.Publisher("target_coordinate", Point, queue_size=10) #publish x,y coordinate to the node
## Trained XML file for detecting face with its path
face_cascade = cv.CascadeClassifier('/home/dasl/opencv/opencv-4.2.0/data/haarcascades/haarcascade_frontalface_alt.xml')
# Trained XML file for detecting eyes
eye_cascade = cv.CascadeClassifier('/home/dasl/opencv/opencv-4.2.0/data/haarcascades/haarcascade_eye.xml')
 
# Capture frames from a camera
cap = cv.VideoCapture(0,cv.CAP_V4L)
while True:
	ret, img = cap.read()
	# Convert to gray scale of each frames
	gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
 
	# Detects faces of different sizes in the input image
	faces = face_cascade.detectMultiScale(gray, 1.3, 5)
 
	for (x,y,w,h) in faces:
		# To draw a rectangle in a face
		cv.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
		roi_gray = gray[y:y+h, x:x+w]
		roi_color = img[y:y+h, x:x+w]
		target_x=(w/2.0)+x
		target_y=(h/2.0)+y
 
		target_direction_x = target_x/640
		target_direction_y = target_y/480
 
		target_direction_x -= 0.5
		target_direction_y -= 0.5
 
		target_direction_x *= 2.0
		target_direction_y *= 2.0
 
		rec_point.x = target_direction_x
		rec_point.y = target_direction_y
 
		# Detects eyes of different sizes in the input image
	#	eyes = eye_cascade.detectMultiScale(roi_gray)
 
		#To draw a rectangle in eyes
		#for (ex,ey,ew,eh) in eyes:
		#	cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,127,255),2)
 
	##Use the below comment if you need to display an image in a window
	screen_res=2560,1440
	window_width=2560
	window_height=1440
	cv.namedWindow('img',cv.WINDOW_NORMAL)
	resized=cv.resize(img,screen_res)
	cv.resizeWindow('img',window_width, window_height)
	flipped =cv.flip(resized,1)
	cv.imshow('img', flipped)
	h,w,c=img.shape
	#print(w)
	#print('width:',img.width)
	#print('height:',img.height)
 
 
	coordinates_pub.publish(rec_point)
	rospy.loginfo(rec_point)
	rate = rospy.Rate(200)
	#rate.sleep()
 
	# Wait for Esc key to stop
	if cv.waitKey(5) ==27:
	    break

The face_tracks.py script is a subscriber that receives the x,y coordinate of a person's face and enable Furo to move toward the person.
Directory: head_motor/src/face_tracks.py

| face_tracks.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import Float32
from geometry_msgs.msg import Point
 
import RPi.GPIO as GPIO
from time import sleep 
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
AN2 =33  #pitch 
AN1 = 32  #roll
DIG2 = 18 #pitch
DIG1 = 37 #roll
GPIO.setup(AN2,GPIO.OUT)
GPIO.setup(AN1, GPIO.OUT)
GPIO.setup(DIG2, GPIO.OUT)
GPIO.setup(DIG1, GPIO.OUT)
sleep(1)
p1=GPIO.PWM(AN1,100)
p2=GPIO.PWM(AN2,100)
 
__author__= "4dimentional@kau.kr"
 
 
target_x = 0;
target_y = 0;
error_x = 0;
error_y = 0;
diff_x = 0;
diff_y = 0;
max_output_x = 76.7;
min_output_x = -76.7;
max_output_y = 76.7;
min_output_y = -76.7;
 
 
def head_motion_callback(rec_point):
	global target_y,target_x
	global error_x,error_y
	global diff_x,diff_y
	global max_output_x, min_output_x
	global max_output_y, min_output_y
 
	Px,Dx =20, 30
	Py,Dy = 20 , 30
 
	prev_x = 0
	prev_y = 0
 
	target_x = rec_point.x
	target_y = rec_point.y
 
	error_x = 0 - target_x
	error_y = 0 - target_y
 
	diff_x = prev_x - error_x
	diff_y = prev_y - error_y
 
	prev_x = error_x
	prev_y = error_y
 
	output_x = Px*error_x + Dx*diff_x
	output_y = Py*error_y + Dy*diff_y
 
	if output_x > max_output_x:
		output_x = max_output_x
	elif output_x < min_output_x:
		output_x = min_output_x
 
	if output_x < 0:
		left_speed = abs(output_x)
		GPIO.output(DIG2,GPIO.LOW)
		p2.start(left_speed)
	elif output_x > 0:
		right_speed = abs(output_x)
		GPIO.output(DIG2,GPIO.HIGH)
		p2.start(right_speed)
 
	if output_y > max_output_y:
		output_y = max_output_y
	elif output_y < min_output_y:
		output_y = min_output_y
 
	if output_y < 0:
		upward_speed = abs(output_y)
		GPIO.output(DIG1,GPIO.LOW)
		p1.start(upward_speed)
	elif output_y > 0:
		downward_speed = abs(output_y)
		GPIO.output(DIG1,GPIO.HIGH)
		p1.start(downward_speed)
 
 
	print(output_y)


<Furo's Mobility using Joystick>

The joy_node script is to convert joystick value to digital value.
Directory: joy/src/joy_node.cpp

The furo script is a topic is both a publisher as well as subscriber at the same time. Obtaining value from the joystick, converting it to a float data type and passes it to the motor controller.
Directory: learning_joy/src/main.cpp

CMakeList

| CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(learning_joy)
 
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
 
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  joy
  roscpp
  turtlesim
)
 
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
 
 
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
 
################################################
## Declare ROS messages, services and actions ##
################################################
 
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
 
## Generate messages in the 'msg' folder
# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )
 
## Generate services in the 'srv' folder
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )
 
## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )
 
## Generate added messages and services with any dependencies listed here
# generate_messages(
#   DEPENDENCIES
#   std_msgs  # Or other packages containing msgs
# )
 
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
 
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed
 
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
#   cfg/DynReconf1.cfg
#   cfg/DynReconf2.cfg
# )
 
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES learning_joy
#  CATKIN_DEPENDS joy roscpp turtlesim
#  DEPENDS system_lib
)
 
###########
## Build ##
###########
 
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)
 
## Declare a C++ library
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/learning_joy.cpp
# )
 
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
 
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/learning_joy_node.cpp)
 
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
 
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
 
## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
#   ${catkin_LIBRARIES}
# )
 
#############
## Install ##
#############
 
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
 
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
#   scripts/my_python_script
#   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
 
## Mark executables and/or libraries for installation
# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
 
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )
 
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
 
#############
## Testing ##
#############
 
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_learning_joy.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
 
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
add_executable(furo src/main.cpp)  #Calls furo instead of main.cpp when launching the script from ROS
target_link_libraries(furo ${catkin_LIBRARIES}) 
avatar_furo_source_codes.1656317315.txt.gz · Last modified: 2022/06/27 01:08 by glee