Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ENSTAR
formation_ros_2020
Commits
74da89e5
Commit
74da89e5
authored
Oct 10, 2017
by
Zhang Xuan
Browse files
skelet for the tutorial
parent
1d766a01
Changes
2
Show whitespace changes
Inline
Side-by-side
src/find_color.py
View file @
74da89e5
...
@@ -22,12 +22,13 @@ def nothing(x):
...
@@ -22,12 +22,13 @@ def nothing(x):
class
FindColor
:
class
FindColor
:
def
__init__
(
self
,
debug
=
False
):
def
__init__
(
self
,
debug
=
False
):
# Converter of ros image to opencv image
self
.
bridge
=
CvBridge
()
self
.
bridge
=
CvBridge
()
# Subscribe to the camera
# Subscribe to the camera
self
.
image_sub
=
rospy
.
Subscriber
(
"/camera/rgb/image_raw"
,
Image
,
self
.
callback
)
#TODO
# Create the center_deviation topic where we will publish the error
# Create the center_deviation topic where we will publish the error
self
.
center_pub
=
rospy
.
Publisher
(
'center_deviation'
,
Float32
,
queue_size
=
10
)
#TODO
# Create the ball_radius topic where we will publish the radius of the ball
# Create the ball_radius topic where we will publish the radius of the ball
self
.
ball_radius_pub
=
rospy
.
Publisher
(
'ball_radius'
,
Float32
,
queue_size
=
10
)
self
.
ball_radius_pub
=
rospy
.
Publisher
(
'ball_radius'
,
Float32
,
queue_size
=
10
)
# Dummy images
# Dummy images
...
@@ -121,13 +122,15 @@ class FindColor:
...
@@ -121,13 +122,15 @@ class FindColor:
max_deviation
=
image_width
//
2
max_deviation
=
image_width
//
2
center_deviation
=
(
center
[
0
]
-
max_deviation
)
/
max_deviation
center_deviation
=
(
center
[
0
]
-
max_deviation
)
/
max_deviation
# print("Center deviation : {}".format(center_deviation))
# print("Center deviation : {}".format(center_deviation))
# Publish the error to the corresponding topic
# Publish the error to the corresponding topic
self
.
center_pub
.
publish
(
center_deviation
)
#TODO
# Publish the ball_radius
# Publish the ball_radius
if
radius
>
MIN_RADIUS_SIZE
:
if
radius
>
MIN_RADIUS_SIZE
:
self
.
ball_radius_pub
.
publish
(
radius
)
self
.
ball_radius_pub
.
publish
(
radius
)
if
self
.
debug
:
if
self
.
debug
:
# Show the images
# Show the images
cv2
.
imshow
(
self
.
image_name
,
self
.
cv_image
)
cv2
.
imshow
(
self
.
image_name
,
self
.
cv_image
)
...
...
src/follow_color.py
View file @
74da89e5
...
@@ -8,70 +8,20 @@ import numpy as np
...
@@ -8,70 +8,20 @@ import numpy as np
from
geometry_msgs.msg
import
Twist
from
geometry_msgs.msg
import
Twist
from
std_msgs.msg
import
Float32
from
std_msgs.msg
import
Float32
vel_pub
=
rospy
.
Publisher
(
"/mobile_base/commands/velocity"
,
Twist
,
queue_size
=
10
)
# Publisher of velocity
Kp_w
=
2
#TODO
Kd
=
1
Ki
=
0
last_time
=
None
Kp_x
=
0.2
vel_x
=
0
omega_z
=
0
# callback of the Subscriber
def
turnCallback
(
data
):
def
turnCallback
(
data
):
#TODO
pass
global
errorD
,
errorI
,
last_time
,
omega_z
# Error (deviation to the center rescaled to [-1, 1])
error
=
data
.
data
# print("Error: {}".format(error))
if
last_time
is
not
None
:
errorD
=
error
-
last_error
dt
=
time
.
time
()
-
last_time
last_time
=
time
.
time
()
else
:
errorI
,
errorD
=
0
,
0
dt
=
1
# PID Control
omega_z
=
-
(
Kp_w
*
error
+
Kd
*
(
errorD
/
dt
)
+
Ki
*
dt
*
errorI
)
# Update integral error
errorI
+=
error
# Init twist message (speed + orientation command)
vel_msg
=
Twist
()
vel_msg
.
linear
.
x
,
vel_msg
.
linear
.
y
,
vel_msg
.
linear
.
z
=
vel_x
,
0
,
0
vel_msg
.
angular
.
x
,
vel_msg
.
angular
.
y
,
vel_msg
.
angular
.
z
=
0
,
0
,
omega_z
# Publish the Twist message
vel_pub
.
publish
(
vel_msg
)
def
translateCallback
(
raw_data
):
data
=
np
.
mean
(
raw_data
.
data
)
global
vel_x
# Error (deviation to the center rescaled to [-1, 1])
error
=
(
data
-
45
)
/
20
# print("Error: {}".format(error))
# PID Control
vel_x
=
-
Kp_x
*
error
# Init twist message (speed + orientation command)
vel_msg
=
Twist
()
vel_msg
.
linear
.
x
,
vel_msg
.
linear
.
y
,
vel_msg
.
linear
.
z
=
vel_x
,
0
,
0
vel_msg
.
angular
.
x
,
vel_msg
.
angular
.
y
,
vel_msg
.
angular
.
z
=
0
,
0
,
omega_z
# Publish the Twist message
vel_pub
.
publish
(
vel_msg
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
print
(
"Starting follow_color node"
)
print
(
"Starting follow_color node"
)
rospy
.
init_node
(
'follow_color'
,
anonymous
=
True
)
rospy
.
init_node
(
'follow_color'
,
anonymous
=
True
)
turn_sub
=
rospy
.
Subscriber
(
"center_deviation"
,
Float32
,
turnCallback
)
# Declare the Subscriber to center_deviationturn
translate_sub
=
message_filters
.
Subscriber
(
"ball_radius"
,
Float32
)
#TODO
translate_cache
=
message_filters
.
Cache
(
translate_sub
,
10
,
allow_headerless
=
True
)
translate_cache
.
registerCallback
(
translateCallback
)
try
:
try
:
rospy
.
spin
()
rospy
.
spin
()
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment