1、安装
http://wiki.ros.org/kinetic/Installation/Ubuntu
ROS wiki是最好的教程
sudo rosdep init
rosdep update
问题:无法连接网络
解决方法:
https://blog.csdn.net/qq_41644087/article/details/115014268
2、创建workspace
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws
catkin_make
source devel/setup.bash
3、创建package
cd ~/catkin_ws/src
catkin_create_pkg basic rospy std_msgs message_generation
cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash
4、创建message:
roscd basic
mkdir msg
cd msg
vim Complex.msg
float32 real
float32 imaginary
修改package.xml
<?xml version="1.0"?>
<package format="2">
<name>basic</name>
<version>0.1.0</version>
<description> basic package</description>
<maintainer email="you@yourdomain.tld">Your Name</maintainer>
<license>BSD</license>
<url type="website">http://wiki.ros.org/beginner_tutorials</url>
<author email="you@yourdomain.tld">Jane Doe</author>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
</package>
修改CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs
message_generation
)
add_message_files(
FILES
Complex.msg
)
generate_messages(
DEPENDENCIES
std_msgs # Or other packages containing msgs
)
catkin_package(
CATKIN_DEPENDS rospy message_runtime
)
编译
cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash
5、创建topic
roscd basic
cd src
vim topic_publisher.py
#!/usr/bin/env python
import rospy
from basic.msg import Complex
from std_msgs.msg import Int32
from random import random
rospy.init_node('topic_publisher')
pub=rospy.Publisher('counter',Int32)
pub2=rospy.Publisher('complex',Complex)
rate=rospy.Rate(2)
count=0
while not rospy.is_shutdown():
pub.publish(count)
cp=Complex()
cp.real=random()
cp.imaginary=random()
pub2.publish(cp)
count+=1
rate.sleep()
chmod u+x topic_publisher.py
vim topic_subscriber.py
#!/usr/bin/env python
import rospy
from basic.msg import Complex
from std_msgs.msg import Int32
from random import random
rospy.init_node('topic_publisher')
pub=rospy.Publisher('counter',Int32)
pub2=rospy.Publisher('complex',Complex)
rate=rospy.Rate(2)
count=0
while not rospy.is_shutdown():
pub.publish(count)
cp=Complex()
cp.real=random()
cp.imaginary=random()
pub2.publish(cp)
count+=1
rate.sleep()
chmod u+x topic_subscriber.py
运行
roscore
rosrun basic topic_publisher.py
rosrun basic topic_subscriber.py
rostopic echo /counter
rostopic echo /complex
6、创建service
roscd basic
mkdir srv
cd srv
vim WordCountFilter.srv
string words
uint32 min_word_length
---
uint32 count
uint32 ignored
修改CMakeLists.txt
add_service_files(
FILES
WordCountFilter.srv
)
编译
cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash
建立服务端
roscd basic
cd src
vim service_server_fliter.py
#!/usr/bin/env python
import rospy
from basic.srv import WordCountFilter,WordCountFilterResponse
def count_words_filter(request):
words_list=request.words.split()
words_filter_list=[i for i in words_list if len(i)>=request.min_word_length]
count=len(words_filter_list)
ignored=len(words_list)-count
return WordCountFilterResponse(count,ignored)
rospy.init_node('service_server_filter')
service=rospy.Service('word_count_filter',WordCountFilter,count_words_filter)
rospy.spin()
chmod u+x service_server_fliter.py
建立客户端
vim service_client_fliter.py
#!/usr/bin/env python
import rospy
from basic.srv import WordCountFilter,WordCountFilterRequest
import sys
rospy.init_node('service_client_filter')
rospy.wait_for_service('word_count_filter')
word_counter=rospy.ServiceProxy('word_count_filter',WordCountFilter)
min_word_length=int(sys.argv[1])
words=' '.join(sys.argv[2:])
#request=WordCountFilterRequest(words,min_word_length)
#response=word_counter(request)
response=word_counter(words,min_word_length)
print('{} have {} words legth > {},{} words are ignored.'.format(words,response.count,min_word_length,response.ignored))
chmod u+x service_client_fliter.py
6、创建action
roscd basic
mkdir action
vim Timer.action
# This is an action definition file, which has three parts: the goal, the
# result, and the feedback.
#
# Part 1: the goal, to be sent by the client
#
# The amount of time we want to wait
duration time_to_wait
---
# Part 2: the result, to be sent by the server upon completion
#
# How much time we waited
duration time_elapsed
# How many updates we provided along the way
uint32 updates_sent
---
# Part 3: the feedback, to be sent periodically by the server during
# execution.
#
# The amount of time that has elapsed from the start
duration time_elapsed
# The amount of time remaining until we're done
duration time_remaining
修改CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs
actionlib_msgs
message_generation
)
add_action_files(
FILES
Timer.action
)
generate_messages(
DEPENDENCIES
actionlib_msgs
std_msgs # Or other packages containing msgs
)
catkin_package(
CATKIN_DEPENDS
rospy
message_runtime
actionlib_msgs
)
修改package.xml
<buildtool_depend>catkin</buildtool_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_depend>actionlib_msgs</build_depend>
<build_depend>actionlib</build_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>actionlib</build_export_depend>
<build_export_depend>actionlib_msgs</build_export_depend>
<exec_depend>message_runtime</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>rospy</exec_depend>
编译
cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash
建立服务端
roscd basic
cd src
vim fancy_action_server.py
#!/usr/bin/env python
import rospy
import time
import actionlib
from basic.msg import TimerAction,TimerGoal,TimerFeedback,TimerResult
def do_timer(goal):
start_time=time.time()
update_count=0
if goal.time_to_wait.to_sec()>60:
result=TimerResult()
result.time_elapsed=rospy.Duration.from_sec(time.time()-start_time)
result.updates_sent=update_count
server.set_aborted(result,"Timer aborted due to too-long wait")
return
print(time.time()-start_time)
while (time.time()-start_time)<goal.time_to_wait.to_sec():
if server.is_preempt_requested():
result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
result.updates_sent = update_count
server.set_preempted(result,"Timer preepted")
feedback=TimerFeedback()
feedback.time_elapsed=rospy.Duration.from_sec(time.time()-start_time)
feedback.time_remaining=goal.time_to_wait-feedback.time_elapsed
print(feedback)
server.publish_feedback(feedback)
update_count+=1
time.sleep(1)
result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
result.updates_sent = update_count
server.set_succeeded(result,'Timer compled successfully')
rospy.init_node('timer_action_server')
server=actionlib.SimpleActionServer('timer',TimerAction,do_timer,False)
server.start()
rospy.spin()
chmod u+x fancy_action_server.py
创建客户端
vim fancy_action_client.py
#!/usr/bin/env python
import rospy
import time
import actionlib
from basic.msg import TimerAction,TimerGoal,TimerFeedback,TimerResult
def feedback_cb(feedback):
print('feedback time elapsed:{}'.format(feedback.time_elapsed.to_sec()))
print('feedback time remaining:{}'.format(feedback.time_remaining.to_sec()))
rospy.init_node('timer_action_client')
client=actionlib.SimpleActionClient('timer',TimerAction)
client.wait_for_server()
goal=TimerGoal()
goal.time_to_wait=rospy.Duration.from_sec(5)
client.send_goal(goal,feedback_cb=feedback_cb)
#time.sleep(3)
#client.cancle_goal()
client.wait_for_result()
print('result state:{}'.format(client.get_state()))
print('result status:{}'.format(client.get_goal_status_text()))
print('result time_elapsed:{}'.format(client.get_result().time_elapsed.to_sec()))
print('result update sent:{}'.format(client.get_result().updates_sent))
chmod u+x fancy_action_client.py