set_timed_current_perturbation.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 numpy import pi
21 
22 if __name__ == '__main__':
23  print('Starting current perturbation node')
24  rospy.init_node('set_timed_current_perturbation')
25 
26  print('Programming the generation of a current perturbation')
27  if rospy.is_shutdown():
28  print('ROS master not running!')
29  sys.exit(-1)
30 
31  starting_time = 0.0
32  if rospy.has_param('~starting_time'):
33  starting_time = rospy.get_param('~starting_time')
34  if starting_time < 0.0:
35  print('Negative starting time, setting it to 0.0')
36  starting_time = 0.0
37 
38  print('Starting time=', starting_time)
39 
40  end_time = -1
41  if rospy.has_param('~end_time'):
42  end_time = rospy.get_param('~end_time')
43  if end_time != -1 and end_time <= starting_time:
44  raise rospy.ROSException('End time is smaller than the starting time')
45 
46  print('End time=', (end_time if end_time != -1 else 'Inf.'))
47 
48  vel = 0.0
49  if rospy.has_param('~current_velocity'):
50  vel = rospy.get_param('~current_velocity')
51 
52  print('Current velocity [m/s]=', vel)
53 
54  horz_angle = 0.0
55  if rospy.has_param('~horizontal_angle'):
56  horz_angle = rospy.get_param('~horizontal_angle')
57  horz_angle *= pi / 180
58 
59  print('Current horizontal angle [deg]=', horz_angle * 180 / pi)
60 
61  vert_angle = 0.0
62  if rospy.has_param('~vertical_angle'):
63  vert_angle = rospy.get_param('~vertical_angle')
64  vert_angle *= pi / 180
65 
66  print('Current vertical angle [deg]=', horz_angle * 180 / pi)
67 
68  try:
69  rospy.wait_for_service('/hydrodynamics/set_current_velocity', timeout=20)
70  except rospy.ROSException:
71  print('Current velocity services not available! Closing node...')
72  sys.exit(-1)
73 
74  try:
75  set_current = rospy.ServiceProxy('/hydrodynamics/set_current_velocity', SetCurrentVelocity)
76  except rospy.ServiceException as e:
77  print('Service call failed, error=', e)
78  sys.exit(-1)
79 
80  # Wait to set the current model
81  rate = rospy.Rate(100)
82  if rospy.get_time() < starting_time:
83  while rospy.get_time() < starting_time:
84  rate.sleep()
85 
86  print('Applying current model...')
87  if set_current(vel, horz_angle, vert_angle):
88  print('Current velocity changed successfully at %f s! vel= %f m/s' % (rospy.get_time(), vel))
89  else:
90  print('Failed to change current velocity')
91 
92  if end_time != -1:
93  while rospy.get_time() < end_time:
94  rate.sleep()
95 
96  print('TIMEOUT, setting current velocity to zero...')
97  set_current(0, horz_angle, vert_angle)
98 
99  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