Gazebo和ROS通訊實現發球

2021-10-03 22:25:22 字數 2368 閱讀 1709

ros版本: ros kinetic

作業系統:ubuntu16.04 lts

1.ros的安裝和通訊機制,主要是話題和服務通訊。

這裡只需要根據根據ros wiki上的教程實現前20講,就可以實現安裝(包括gazebo_ros包)和簡單入門。

2.gazebo的一些基礎知識(非必須),這裡可以看一下gazebo的入門教程

#!/usr/bin/env python

import time

import rospy

from gazebo_msgs.srv import spawnmodel

from gazebo_msgs.srv import deletemodel

from gazebo_msgs.srv import getmodelstate

from geometry_msgs.msg import pose

from geometry_msgs.msg import wrench

#初始位置

initial_pose = pose(

)initial_pose.position.x =-4

initial_pose.position.y =-1

initial_pose.position.z =

1#所用力的大小

initial_wrench = wrench(

)initial_wrench.force.x =

0.16

initial_wrench.force.y =

0.0initial_wrench.force.z =

0.25

#ctrl+h顯示隱藏文件,將***替換為自己的目錄,從自己的目錄中找的這個檔案

f =open

('/home/***xx/.gazebo/models/robocup_spl_ball/model.sdf'

,'r'

)sdff = f.read(

)#產生小球的服務

rospy.wait_for_service(

'gazebo/spawn_sdf_model'

)spawn_model_prox = rospy.serviceproxy(

'gazebo/spawn_sdf_model'

, spawnmodel)

#施加乙個作用力

rospy.wait_for_service(

)#刪除小球

rospy.wait_for_service(

'gazebo/delete_model'

)deletesrv = rospy.serviceproxy(

'gazebo/delete_model'

, deletemodel)

#得到小球位置

rospy.wait_for_service(

'gazebo/get_model_state'

)get_m_state = rospy.serviceproxy(

'gazebo/get_model_state'

, getmodelstate)

while(1

):spawn_model_prox(

"my_ball"

, sdff,

"robotos_name_space"

, initial_pose,

"world"

) time.sleep(1)

"my_ball::ball"

,"world"

,none

, initial_wrench, rospy.time.from_sec(0)

, rospy.duration.from_sec(

0.2)

) s = get_m_state(

"my_ball"

,"world"

)while(1

):if(s.pose.position.x <6)

: s = get_m_state(

"my_ball"

,"world"

)print

(s.pose.position.x)

else

: rospy.wait_for_service(

'gazebo/delete_model'

) deletesrv = rospy.serviceproxy(

'gazebo/delete_model'

, deletemodel)

ass = deletesrv(

"my_ball"

)print

(ass)

break

關於程式中各個服務的的呼叫方法,可以直接參考相關的訊息和服務文件。

串列埠通訊實現

前段時間編寫了串列埠通訊的 用的是rs 232資料線 include 標準輸入輸出定義 include 標準函式庫定義 include unix 標準函式定義 include include include include include include define dev dev ttys1 d...

socket通訊實現

為了實現socket的通訊驗證,寫了乙個簡單的客服端向伺服器端通訊的 伺服器端 1 版本驗證 word wversionrequested makeword 2 2 wsadata result int err wsastartup wversionrequested,result if err 0...

Thrift協議通訊實現

做乙個demo感受一下整個過程,demo是簡單的計算器功能。1 目錄結構 genphp thrift r gen php server computethrift.thrift lib thrift 的 lib資料夾 computethrift.thrift 介面thrift檔案 computeha...