AccelerationControl.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 numpy
18 import rospy
19 
20 from dynamic_reconfigure.server import Server
21 from geometry_msgs.msg import Accel
22 from geometry_msgs.msg import Wrench
23 from rospy.numpy_msg import numpy_msg
24 
26  def __init__(self):
27  print('AccelerationControllerNode: initializing node')
28 
29  self.ready = False
30  self.mass = 1.
31  self.inertial_tensor = numpy.identity(3)
32  self.mass_inertial_matrix = numpy.zeros((6, 6))
33 
34  # ROS infrastructure
35  self.sub_accel = rospy.Subscriber(
36  'cmd_accel', numpy_msg(Accel), self.accel_callback)
37  self.sub_force = rospy.Subscriber(
38  'cmd_force', numpy_msg(Accel), self.force_callback)
39  self.pub_gen_force = rospy.Publisher(
40  'thruster_manager/input', Wrench, queue_size=1)
41 
42  if not rospy.has_param("pid/mass"):
43  raise rospy.ROSException("UUV's mass was not provided")
44 
45  if not rospy.has_param("pid/inertial"):
46  raise rospy.ROSException("UUV's inertial was not provided")
47 
48  self.mass = rospy.get_param("pid/mass")
49  self.inertial = rospy.get_param("pid/inertial")
50 
51  # update mass, moments of inertia
52  self.inertial_tensor = numpy.array(
53  [[self.inertial['ixx'], self.inertial['ixy'], self.inertial['ixz']],
54  [self.inertial['ixy'], self.inertial['iyy'], self.inertial['iyz']],
55  [self.inertial['ixz'], self.inertial['iyz'], self.inertial['izz']]])
56  self.mass_inertial_matrix = numpy.vstack((
57  numpy.hstack((self.mass*numpy.identity(3), numpy.zeros((3, 3)))),
58  numpy.hstack((numpy.zeros((3, 3)), self.inertial_tensor))))
59 
60  print(self.mass_inertial_matrix)
61  self.ready = True
62 
63  def force_callback(self, msg):
64  if not self.ready:
65  return
66 
67  # extract 6D force / torque vector from message
68  force = numpy.array((
69  msg.accel.linear.x, msg.accel.linear.y, msg.accel.linear.z))
70  torque = numpy.array((
71  msg.accel.angular.x, msg.accel.angular.y, msg.accel.angular.z))
72  force_torque = numpy.hstack((force, torque)).transpose()
73 
74  force_msg = Wrench()
75  force_msg.force.x = force[0]
76  force_msg.force.y = force[1]
77  force_msg.force.z = force[2]
78 
79  force_msg.torque.x = torque[0]
80  force_msg.torque.y = torque[1]
81  force_msg.torque.z = torque[2]
82 
83  self.pub_gen_force.publish(force_msg)
84 
85  def accel_callback(self, msg):
86  if not self.ready:
87  return
88 
89  # extract 6D accelerations (linear, angular) from message
90  linear = numpy.array((msg.linear.x, msg.linear.y, msg.linear.z))
91  angular = numpy.array((msg.angular.x, msg.angular.y, msg.angular.z))
92  accel = numpy.hstack((linear, angular)).transpose()
93 
94  # convert acceleration to force / torque
95  force_torque = self.mass_inertial_matrix.dot(accel)
96 
97  force_msg = Wrench()
98  force_msg.force.x = force_torque[0]
99  force_msg.force.y = force_torque[1]
100  force_msg.force.z = force_torque[2]
101 
102  force_msg.torque.x = force_torque[3]
103  force_msg.torque.y = force_torque[4]
104  force_msg.torque.z = force_torque[5]
105 
106  self.pub_gen_force.publish(force_msg)
107 
108 if __name__ == '__main__':
109  print('starting AccelerationControl.py')
110  rospy.init_node('acceleration_control')
111 
112  try:
114  rospy.spin()
115  except rospy.ROSInterruptException:
116  print('caught exception')
117  print('exiting')


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