網上有很多關于一種pos機顯示屏旋轉結構, 阿克曼結構移動機器人的gazebo仿真的知識,也有很多人為大家解答關于一種pos機顯示屏旋轉結構的問題,今天pos機之家(www.tonybus.com)為大家整理了關于這方面的知識,讓我們一起來看下吧!
本文目錄一覽:
一種pos機顯示屏旋轉結構
第三章、讓小車動起來
1配置controller
在tianracer_description功能包新建config文件夾,通過一個yaml文件smart_control_config.yaml來聲明我們所需要的controller,以及對應的參數,PID增益和 控制器設置必須保存在yaml文件中,然后通過 launch文件加載到param服務器上:
tianracer:# controls the rear two tires based on individual motors # Publish all joint States ----------------------------------- joint_state_controller: type: joint_state_controller/JointStateController publish_rate: 50 rear_right_velocity_controller: type: velocity_controllers/JointVelocityController joint: right_rear_wheel_joint pid: {p: 100.0, i: 0.01, d: 10.0} rear_left_velocity_controller: type: velocity_controllers/JointVelocityController joint: left_rear_wheel_joint pid: {p: 100.0, i: 0.01, d: 10.0} front_right_steering_position_controller: type: effort_controllers/JointPositionController joint: right_steering_hinge_joint pid: {p: 4.0, i: 2.0, d: 1.0} front_left_steering_position_controller: type: effort_controllers/JointPositionController joint: left_steering_hinge_joint pid: {p: 4.0, i: 2.0, d: 1.0} gazebo_ros_control: pid_gains: right_rear_wheel_joint: p: 100.0 i: 0.5 d: 0.0 left_rear_wheel_joint: p: 100.0 i: 0.5 d: 0.0
2配置launch文件
創建一個 rosLaunch 文件以啟動 ros_control controllers,使用launch文件control.launch,運行controller_manager中的spawner,加載并運行這些上邊這些controller:
<?xml version="1.0" encoding="UTF-8"?><launch> <!-- Load joint controller configurations from YAML file to parameter server --> <rosparam file="$(find tianracer_description)/config/smart_control_config.yaml" command="load"/> <!-- load controllers --> <node name="controller_spawner" pkg="controller_manager" type="spawner" respawn="false" output="screen" ns="/tianracer" args="joint_state_controller rear_right_velocity_controller rear_left_velocity_controller front_right_steering_position_controller front_left_steering_position_controller"/> <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" respawn="true" output="screen"> <remap from="/joint_states" to="/tianracer/joint_states" /> </node> <node name="cmdvel2gazebo" pkg="tianracer_description" type="cmdvel2gazebo.py" respawn="true" output="screen"/></launch>
接著創建一個可以將小車模型在gazeboi中打開的launch文件tianracer.launch,并且能夠加載上述control.launch文件
<?xml version="1.0" encoding="UTF-8"?><launch> <!-- 設置launch文件的參數 --> <arg name="paused" default="false"/> <arg name="use_sim_time" default="true"/> <arg name="gui" default="true"/> <arg name="headless" default="false"/> <arg name="debug" default="false"/> <!--模型車的起點放置位置--> <arg name="x_pos" default="0"/> <arg name="y_pos" default="0"/> <arg name="z_pos" default="0"/> <arg name="R_pos" default="0"/> <arg name="P_pos" default="0"/> <arg name="Y_pos" default="0"/> <!--運行gazebo仿真環境--> <include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="debug" value="$(arg debug)" /> <arg name="gui" value="$(arg gui)" /> <arg name="paused" value="$(arg paused)"/> <arg name="use_sim_time" value="$(arg use_sim_time)"/> <arg name="headless" value="$(arg headless)"/> <!-- <arg name="world_name" value="$(find racebot_description)/world/tianracer_race.world"/> --> <!-- .world文件的地址--> </include> <!-- 加載機器人模型描述參數 --> <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find tianracer_description)/urdf/tianracer_description.xacro'"/> <!-- 在gazebo中加載機器人模型--> <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen" args="-urdf -model tianracer -param robot_description -x $(arg x_pos) -y $(arg y_pos) -z $(arg z_pos) -R $(arg R_pos) -P $(arg P_pos) -Y $(arg Y_pos)"/> <!-- ros_control racecar launch file --> <include file="$(find tianracer_description)/launch/control.launch"> </include> <!--Launch the simulation joystick control--> <!-- <rosparam command="load" file="$(find tianracer_gazebo)/config/keyboard_teleop.yaml" /> <node pkg="tianracer_gazebo" type="keyboard_teleop.py" name="keyboard_teleop" /> --></launch>
3配置運動學模型
在功能包中新建scripts文件夾,并加入cmdvei2gazebo.py文件,該文件用的是
【從零開始自動駕駛】07 用 ROS topic 控制車輪_嗶哩嗶哩_bilibili中的開源代碼,其中代碼的解析這里不做展開復述,各位請自行到原地址觀看,up主講的很好。該文件定義了小車的運動學模型,并且訂閱了一個Twist消息類型的話題tianracer/cmd_vel,我們可以通過編寫一個接收該消息類型話題的節點來控制小車運動。代碼如下:
#!/usr/bin/env pythonimport rospyfrom std_msgs.msg import Float64from geometry_msgs.msg import Twistimport mathclass CmdVel2Gazebo: def __init__(self): rospy.init_node('cmdvel2gazebo', anonymous=True) rospy.Subscriber('/tianracer/cmd_vel', Twist, self.callback, queue_size=1) self.pub_steerL = rospy.Publisher('/tianracer/front_left_steering_position_controller/command', Float64, queue_size=1) self.pub_steerR = rospy.Publisher('/tianracer/front_right_steering_position_controller/command', Float64, queue_size=1) self.pub_rearL = rospy.Publisher('/tianracer/rear_left_velocity_controller/command', Float64, queue_size=1) self.pub_rearR = rospy.Publisher('/tianracer/rear_right_velocity_controller/command', Float64, queue_size=1) # initial velocity and tire angle are 0 self.x = 0 self.z = 0 # car Wheelbase (in m) self.L = 0.261 # car Tread self.T_front = 0.1632 self.T_rear = 0.1632 # how many seconds delay for the dead man's switch self.timeout=rospy.Duration.from_sec(0.2); self.lastMsg=rospy.Time.now() # maximum steer angle of the "inside" tire self.maxsteerInside=0.6; # turning radius for maximum steer angle just with the inside tire # tan(maxsteerInside) = wheelbase/radius --> solve for max radius at this angle rMax = self.L/math.tan(self.maxsteerInside); # radius of inside tire is rMax, so radius of the ideal middle tire (rIdeal) is rMax+treadwidth="360px",height="auto" />
msgs_too_old = delta_last_msg_time > self.timeout if msgs_too_old: self.x = 0 msgRear = Float64() msgRear.data = self.x self.pub_rearL.publish(msgRear) self.pub_rearR.publish(msgRear) msgSteer = Float64() msgSteer.data = 0 self.pub_steerL.publish(msgSteer) self.pub_steerR.publish(msgSteer) return # The self.z is the delta angle in radians of the imaginary front wheel of ackerman model. if self.z != 0: T_rear = self.T_rear T_front = self.T_front L=self.L # self.v is the linear *velocity* r = L/math.fabs(math.tan(self.z)) rL_rear = r-(math.copysign(1,self.z)*(T_rear/2.0)) rR_rear = r+(math.copysign(1,self.z)*(T_rear/2.0)) rL_front = r-(math.copysign(1,self.z)*(T_front/2.0)) rR_front = r+(math.copysign(1,self.z)*(T_front/2.0)) msgRearR = Float64() # the right tire will go a little faster when we turn left (positive angle) # amount is proportional to the radius of the outside/ideal msgRearR.data = self.x*rR_rear/r; msgRearL = Float64() # the left tire will go a little slower when we turn left (positive angle) # amount is proportional to the radius of the inside/ideal msgRearL.data = self.x*rL_rear/r; self.pub_rearL.publish(msgRearL) self.pub_rearR.publish(msgRearR) msgSteerL = Float64() msgSteerR = Float64() # the left tire's angle is solved directly from geometry msgSteerL.data = math.atan2(L,rL_front)*math.copysign(1,self.z) self.pub_steerL.publish(msgSteerL) # the right tire's angle is solved directly from geometry msgSteerR.data = math.atan2(L,rR_front)*math.copysign(1,self.z) self.pub_steerR.publish(msgSteerR) else: # if we aren't turning msgRear = Float64() msgRear.data = self.x; self.pub_rearL.publish(msgRear) # msgRear.data = 0; self.pub_rearR.publish(msgRear) msgSteer = Float64() msgSteer.data = self.z self.pub_steerL.publish(msgSteer) self.pub_steerR.publish(msgSteer)if __name__ == '__main__': try: CmdVel2Gazebo() except rospy.ROSInterruptException: pass將代碼中的話題名以及參數進行修改,改為自己小車模型的尺寸參數。并將該python代碼作為一個節點添加到control .launch文件中。
4讓小車動起來
通過rostopic pub話題讓小車動起來
到此為止已經可以通過pub話題讓小車動起來了。
roslaunch tianracer_description tianracer.launch
再開一個終端,使用rostopic list來看一下話題列表
rostopic list
可以看到發布了單目攝像頭,深度攝像頭,激光雷達,以及讓小車運動的tianracer/cmd_vel話題,將這個話題pub一下:
rostopic pub /tianracer/cmd_vel -r 10 geometry_msgs/Twist "linear: x: 1.0 y: 0.0 z: 0.0angular: x: 0.0 y: 0.0 z: 1.0"
此時小車便會繞著Z軸轉動起來。
使用鍵盤控制節點控制小車運動
在tianracer_description/scripts目錄下新建teleop.py
#!/usr/bin/env python# -*- coding: utf-8 -*-import rospyfrom geometry_msgs.msg import Twistimport sys, select, termios, ttymsg = """Control mbot!---------------------------Moving around: u i o j k l m , .q/z : increase/decrease max speeds by 10%w/x : increase/decrease only linear speed by 10%e/c : increase/decrease only angular speed by 10%space key, k : force stopanything else : stop smoothlyCTRL-C to quit"""moveBindings = { 'i':(1,0), 'o':(1,-1), 'j':(0,1), 'l':(0,-1), 'u':(1,1), ',':(-1,0), '.':(-1,1), 'm':(-1,-1), }speedBindings={ 'q':(1.1,1.1), 'z':(.9,.9), 'w':(1.1,1), 'x':(.9,1), 'e':(1,1.1), 'c':(1,.9), }def getKey(): tty.setraw(sys.stdin.fileno()) rlist, _, _ = select.select([sys.stdin], [], [], 0.1) if rlist: key = sys.stdin.read(1) else: key = '' termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings) return keyspeed = 10turn = 1def vels(speed,turn): return "currently:\speed %surn %s " % (speed,turn)if __name__=="__main__": settings = termios.tcgetattr(sys.stdin) rospy.init_node('carsmart_teleop') pub = rospy.Publisher('/tianracer/cmd_vel', Twist, queue_size=5) x = 0 th = 0 status = 0 count = 0 acc = 0.1 target_speed = 0 target_turn = 0 control_speed = 0 control_turn = 0 try: print msg print vels(speed,turn) while(1): key = getKey() # 運動控制方向鍵(1:正方向,-1負方向) if key in moveBindings.keys(): x = moveBindings[key][0] th = moveBindings[key][1] count = 0 # 速度修改鍵 elif key in speedBindings.keys(): speed = speed * speedBindings[key][0] # 線速度增加0.1倍 turn = turn * speedBindings[key][1] # 角速度增加0.1倍 count = 0 print vels(speed,turn) if (status == 14): print msg status = (status + 1) % 15 # 停止鍵 elif key == ' ' or key == 'k' : x = 0 th = 0 control_speed = 0 control_turn = 0 else: count = count + 1 if count > 4: x = 0 th = 0 if (key == '\\x03'): break # 目標速度=速度值*方向值 target_speed = speed * x target_turn = turn * th # 速度限位,防止速度增減過快 if target_speed > control_speed: control_speed = min( target_speed, control_speed + 0.02 ) elif target_speed < control_speed: control_speed = max( target_speed, control_speed - 0.02 ) else: control_speed = target_speed if target_turn > control_turn: control_turn = min( target_turn, control_turn + 0.1 ) elif target_turn < control_turn: control_turn = max( target_turn, control_turn - 0.1 ) else: control_turn = target_turn # 創建并發布twist消息 twist = Twist() twist.linear.x = control_speed; twist.linear.y = 0; twist.linear.z = 0 twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = control_turn pub.publish(twist) except: print e finally: twist = Twist() twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0 twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0 pub.publish(twist) termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
該節點發布了一個Twist消息類型的話題tianracer/cmd_vel,可以通過鍵盤來控制小車運動。
roslaunch tianracer_description tianracer.launch rosrun tianracer_description teleop.py
此時按u,i,o等按鍵即可控制小車運動了,通過rqt_graph看一下各個節點之間的關系:
rqt_graph
5小結
至此,已經成功讓小車動起來了,而且也添加了激光雷達傳感器,這也就意味著接下去就能做建圖導航了,然而由于從solidworks導出的模型慣性參數等方面的一些問題,導致小車運動起來多多少少有一些問題,所以我手擼了小車的1:1簡易模型的代碼,并且準備用ackermann消息控制小車運動,并使用該模型完成接下去的建圖導航等內容。
參考資料:
1.古月老師的《ROS機器人開發實踐》
2.[從零駕駛自動駕駛] https://www.bilibili.com/video/BV1ZJ41187tS?spm_id_from=333.999.0.0
以上就是關于一種pos機顯示屏旋轉結構, 阿克曼結構移動機器人的gazebo仿真的知識,后面我們會繼續為大家整理關于一種pos機顯示屏旋轉結構的知識,希望能夠幫助到大家!
