apply_body_wrench.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2016 The UUV Simulator Authors.
3 # All rights reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 from __future__ import print_function
17 import rospy
18 import sys
19 from gazebo_msgs.srv import ApplyBodyWrench
20 from geometry_msgs.msg import Point, Wrench, Vector3
21 
22 if __name__ == '__main__':
23  print('Apply programmed perturbation to vehicle', rospy.get_namespace())
24  rospy.init_node('set_body_wrench')
25 
26  if rospy.is_shutdown():
27  print('ROS master not running!')
28  sys.exit(-1)
29 
30  starting_time = 0.0
31  if rospy.has_param('~starting_time'):
32  starting_time = rospy.get_param('~starting_time')
33 
34  print('Starting time= {} s'.format(starting_time))
35 
36  duration = 0.0
37  if rospy.has_param('~duration'):
38  duration = rospy.get_param('~duration')
39 
40  if duration == 0.0:
41  print('Duration not set, leaving node...')
42  sys.exit(-1)
43 
44  print('Duration [s]=', ('Inf.' if duration < 0 else duration))
45 
46  force = [0, 0, 0]
47  if rospy.has_param('~force'):
48  force = rospy.get_param('~force')
49  print(force)
50  if len(force) != 3:
51  raise rospy.ROSException('Invalid force vector')
52 
53  print('Force [N]=', force)
54 
55  torque = [0, 0, 0]
56  if rospy.has_param('~torque'):
57  torque = rospy.get_param('~torque')
58  if len(torque) != 3:
59  raise rospy.ROSException('Invalid torque vector')
60 
61  print('Torque [N]=', torque)
62 
63  try:
64  rospy.wait_for_service('/gazebo/apply_body_wrench', timeout=10)
65  except rospy.ROSException:
66  print('Service not available! Closing node...')
67  sys.exit(-1)
68 
69  try:
70  apply_wrench = rospy.ServiceProxy('/gazebo/apply_body_wrench', ApplyBodyWrench)
71  except rospy.ServiceException as e:
72  print('Service call failed, error=', e)
73  sys.exit(-1)
74 
75  ns = rospy.get_namespace().replace('/', '')
76 
77  body_name = '%s/base_link' % ns
78 
79  if starting_time >= 0:
80  rate = rospy.Rate(100)
81  while rospy.get_time() < starting_time:
82  rate.sleep()
83 
84  wrench = Wrench()
85  wrench.force = Vector3(*force)
86  wrench.torque = Vector3(*torque)
87  success = apply_wrench(
88  body_name,
89  'world',
90  Point(0, 0, 0),
91  wrench,
92  rospy.Time().now(),
93  rospy.Duration(duration))
94 
95  if success:
96  print('Body wrench perturbation applied!')
97  print('\tFrame: ', body_name)
98  print('\tDuration [s]: ', duration)
99  print('\tForce [N]: ', force)
100  print('\tTorque [Nm]: ', torque)
101  else:
102  print('Failed!')


uuv_control_utils
Author(s): Musa Morena Marcusso Manhaes , Sebastian Scherer , Luiz Ricardo Douat
autogenerated on Mon Jul 1 2019 19:39:33