test_thrusters.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2016-2019 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 import roslib
17 import unittest
18 import numpy as np
19 import random
20 from tf_quaternion import transformations
21 from uuv_thrusters import ThrusterManager
22 from uuv_thrusters.models import Thruster
23 
24 PKG = 'uuv_thruster_manager'
25 roslib.load_manifest(PKG)
26 
27 IDX = 0
28 TOPIC = '/thruster'
29 AXES = [
30  np.array([1, 0, 0, 0]),
31  np.array([0, 1, 0, 0]),
32  np.array([0, 0, 1, 0])
33 ]
34 
35 def get_force_vector(pos, orientation, axis):
36  thrust_body = transformations.quaternion_matrix(orientation).dot(
37  axis.transpose())[0:3]
38  torque_body = np.cross(pos, thrust_body)
39  return np.hstack((thrust_body, torque_body)).transpose()
40 
41 class TestThrusters(unittest.TestCase):
42  def test_thruster(self):
43  # Use random positions and quaternions
44  for axis in AXES:
45  pos = np.random.rand(3)
46  q = transformations.random_quaternion()
47 
48  thruster = Thruster(
49  index=IDX,
50  topic=TOPIC,
51  pos=pos,
52  orientation=q,
53  axis=axis)
54 
55  self.assertEqual(thruster.index, IDX)
56  self.assertEqual(thruster.topic, TOPIC)
57  self.assertTrue((thruster.tam_column == get_force_vector(pos, q, axis)).all())
58 
60  # Use random positions and quaternions
61  for axis in AXES:
62  pos = np.random.rand(3)
63  q = transformations.random_quaternion()
64  gain = random.random()
65 
66  thruster = Thruster.create_thruster(
67  'proportional',
68  IDX,
69  TOPIC,
70  pos,
71  q,
72  axis,
73  gain=gain)
74 
75  self.assertEqual(thruster.index, IDX)
76  self.assertEqual(thruster.topic, TOPIC)
77  self.assertTrue((thruster.tam_column == get_force_vector(pos, q, axis)).all())
78 
79  self.assertEqual(thruster.get_thrust_value(0), 0)
80  self.assertEqual(thruster.get_command_value(0), 0)
81 
82  command = np.linspace(-100, 100, 10)
83  for x in command:
84  y = thruster.get_thrust_value(x)
85  self.assertEqual(y, gain * np.abs(x) * x)
86 
87  thrust = np.linspace(-50000, 50000, 10)
88  for x in thrust:
89  y = thruster.get_command_value(x)
90  self.assertEqual(y, np.sign(x) * np.sqrt(np.abs(x) / gain))
91 
93  input_values = np.linspace(-50, 50, 10)
94  output_values = np.linspace(-10000, 10000, 10)
95 
96  gain = 20000.0 / 100.0
97 
98  # Use random positions and quaternions
99  for axis in AXES:
100  pos = np.random.rand(3)
101  q = transformations.random_quaternion()
102 
103  thruster = Thruster.create_thruster(
104  'custom',
105  IDX,
106  TOPIC,
107  pos,
108  q,
109  axis,
110  input=input_values,
111  output=output_values)
112 
113  self.assertEqual(thruster.index, IDX)
114  self.assertEqual(thruster.topic, TOPIC)
115  self.assertTrue((thruster.tam_column == get_force_vector(pos, q, axis)).all())
116 
117  self.assertTrue(np.isclose(thruster.get_thrust_value(0), 0))
118  self.assertTrue(np.isclose(thruster.get_command_value(0), 0))
119 
120  x = random.random() * 10
121  self.assertTrue(np.isclose(thruster.get_thrust_value(x), gain * x))
122  y = random.random() * 10000
123  self.assertTrue(np.isclose(thruster.get_command_value(y), y / gain))
124 
125 if __name__ == '__main__':
126  import rosunit
127  rosunit.unitrun(PKG, 'test_thrusters', TestThrusters)
def get_force_vector(pos, orientation, axis)


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