set_thruster_output_efficiency.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 from uuv_gazebo_ros_plugins_msgs.srv import SetThrusterEfficiency
19 
20 
21 if __name__ == '__main__':
22  print('Set the thruster output efficiency for vehicle, namespace=', rospy.get_namespace())
23  rospy.init_node('set_thrusters_states')
24 
25  if rospy.is_shutdown():
26  raise rospy.ROSException('ROS master not running!')
27 
28  starting_time = 0.0
29  if rospy.has_param('~starting_time'):
30  starting_time = rospy.get_param('~starting_time')
31 
32  print('Starting time= {} s'.format(starting_time))
33 
34  duration = 0.0
35  if rospy.has_param('~duration'):
36  duration = rospy.get_param('~duration')
37 
38  if duration == 0.0:
39  raise rospy.ROSException('Duration not set, leaving node...')
40 
41  print('Duration [s]=', ('Inf.' if duration < 0 else duration))
42 
43  if rospy.has_param('~efficiency'):
44  efficiency = rospy.get_param('~efficiency')
45  if efficiency < 0 or efficiency > 1:
46  raise rospy.ROSException('Invalid thruster output efficiency, leaving node...')
47  else:
48  raise rospy.ROSException('Thruster output efficiency not set, leaving node...')
49 
50  if rospy.has_param('~thruster_id'):
51  thruster_id = rospy.get_param('~thruster_id')
52  else:
53  raise rospy.ROSException('Thruster ID not given')
54 
55  if thruster_id < 0:
56  raise rospy.ROSException('Invalid thruster ID')
57 
58  print('Setting thruster output efficiency #{} to {}'.format(thruster_id, 100 * efficiency))
59 
60  vehicle_name = rospy.get_namespace().replace('/', '')
61 
62  srv_name = '/%s/thrusters/%d/set_thrust_force_efficiency' % (vehicle_name, thruster_id)
63 
64  try:
65  rospy.wait_for_service(srv_name, timeout=2)
66  except rospy.ROSException:
67  raise rospy.ROSException('Service not available! Closing node...')
68 
69  try:
70  set_eff = rospy.ServiceProxy(srv_name, SetThrusterEfficiency)
71  except rospy.ServiceException as e:
72  raise rospy.ROSException('Service call failed, error=' + e)
73 
74  rate = rospy.Rate(100)
75  while rospy.get_time() < starting_time:
76  rate.sleep()
77 
78  success = set_eff(efficiency)
79 
80  if success:
81  print('Time={} s'.format(rospy.get_time()))
82  print('Current thruster output efficiency #{}={}'.format(thruster_id, efficiency * 100))
83 
84  if duration > 0:
85  rate = rospy.Rate(100)
86  while rospy.get_time() < starting_time + duration:
87  rate.sleep()
88 
89  success = set_eff(1.0)
90 
91  if success:
92  print('Time={} s'.format(rospy.get_time()))
93  print('Returning to previous thruster output efficiency #{}={}'.format(thruster_id, efficiency * 100))
94 
95  print('Leaving node...')


uuv_control_utils
Author(s): Musa Morena Marcusso Manhaes , Sebastian Scherer , Luiz Ricardo Douat
autogenerated on Thu Jun 18 2020 03:28:47