plot_states.py
Go to the documentation of this file.
1 #*********************************************************************
2 # Copyright (c) 2017 Robert Bosch GmbH.
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 # *********************************************************************/
17 
18 import os.path
19 import numpy as np
20 import matplotlib.pyplot as plt
21 
22 CSV_HEADER = "x,y,theta,kappa,d".split(",")
23 
24 def read_csv(filename):
25  with open(filename, 'r') as f:
26  next(f)
27  lines = [line.strip().split(",") for line in f.readlines()]
28  f.close
29  return lines
30 
31 
32 def distance(x1, y1, x2, y2):
33  return np.sqrt((x2 - x1)**2 + (y2 - y1)**2)
34 
35 
36 class State:
37  def __init__(self, x, y, theta, kappa, d):
38  self.x = x
39  self.y = y
40  self.theta = theta
41  self.kappa = kappa
42  self.d = d
43 
44 
45 class Path:
46  def __init__(self):
47  self.states = []
48  self.x = None
49  self.y = None
50  self.theta = None
51  self.kappa = []
52  self.d = []
53  self.s = []
54 
55  def load(self, fpath, fname):
56  if not os.path.exists(fpath + fname):
57  return 0
58  else:
59  output = read_csv(fpath + fname)
60  for line in output:
61  x = np.float(line[CSV_HEADER.index("x")])
62  y = np.float(line[CSV_HEADER.index("y")])
63  theta = np.float(line[CSV_HEADER.index("theta")])
64  kappa = np.float(line[CSV_HEADER.index("kappa")])
65  d = np.float(line[CSV_HEADER.index("d")])
66  self.states.append(State(x, y, theta, kappa, d))
67  return 1
68 
69  def postprocess(self):
70  self.x = [state.x for state in self.states]
71  self.y = [state.y for state in self.states]
72  self.theta = [state.theta for state in self.states]
73  self.kappa = [state.kappa for state in self.states]
74  self.d = [state.d for state in self.states]
75 
76  s = 0.0
77  state1 = self.states[0]
78  for state2 in self.states:
79  s += distance(state1.x, state1.y, state2.x, state2.y)
80  self.s.append(s)
81  state1 = state2
82 
83 
84 if __name__ == "__main__":
85  # load data
86  filepath = "./"
87 
88  path = Path()
89  path.load(filepath, "path.csv")
90  path.postprocess()
91 
92  # plot
93  f, axarr = plt.subplots(2, 2, figsize=(16, 8))
94 
95  axarr[0, 0].plot(path.x, path.y)
96  axarr[0, 0].set_xlabel('$x$ [m]')
97  axarr[0, 0].set_ylabel('$y$ [m]')
98  axarr[0, 0].set_aspect('equal')
99 
100  axarr[0, 1].plot(path.s, path.theta)
101  axarr[0, 1].set_xlabel('$s$ [m]')
102  axarr[0, 1].set_ylabel('$theta$ [rad]')
103 
104  ax1 = axarr[1, 0]
105  ax2 = axarr[1, 0].twinx()
106  handle1, = ax1.plot(path.s, path.kappa, label='$kappa$')
107  handle2, = ax2.plot(path.s, path.d, label='$d$', color='red', alpha=0.4)
108  axarr[1, 0].set_xlabel('$s$ [m]')
109  ax1.set_ylabel('$kappa$ [1/m]')
110  ax2.set_ylabel('$d$ [-]')
111  axarr[1, 0].legend(handles=[handle1, handle2])
112 
113  plt.show()
plot_states.Path.load
def load(self, fpath, fname)
Definition: plot_states.py:55
plot_states.Path.kappa
kappa
Definition: plot_states.py:51
plot_states.State
Definition: plot_states.py:36
plot_states.State.__init__
def __init__(self, x, y, theta, kappa, d)
Definition: plot_states.py:37
plot_states.State.kappa
kappa
Definition: plot_states.py:41
plot_states.Path
Definition: plot_states.py:45
plot_states.Path.y
y
Definition: plot_states.py:49
plot_states.Path.__init__
def __init__(self)
Definition: plot_states.py:46
plot_states.Path.s
s
Definition: plot_states.py:53
plot_states.Path.states
states
Definition: plot_states.py:47
plot_states.State.x
x
Definition: plot_states.py:38
plot_states.read_csv
def read_csv(filename)
Definition: plot_states.py:24
plot_states.State.y
y
Definition: plot_states.py:39
plot_states.distance
def distance(x1, y1, x2, y2)
Definition: plot_states.py:32
plot_states.State.d
d
Definition: plot_states.py:42
plot_states.Path.x
x
Definition: plot_states.py:48
plot_states.Path.postprocess
def postprocess(self)
Definition: plot_states.py:69
plot_states.Path.theta
theta
Definition: plot_states.py:50
plot_states.Path.d
d
Definition: plot_states.py:52
plot_states.State.theta
theta
Definition: plot_states.py:40


steering_functions
Author(s): Holger Banzhaf
autogenerated on Mon Dec 11 2023 03:27:43