pid_loader_and_saver.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2011 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 
18 import rospy
19 import os
20 import yaml
21 import rospkg
22 
23 try:
24  from yaml import CLoader as Loader, CDumper as Dumper
25 except ImportError:
26  from yaml import Loader, Dumper
27 
28 
29 class PidLoader(object):
30 
31  """
32  Loads pid parameters of each controller in parameters_dict from a yaml file
33  """
34 
35  def __init__(self):
36  pass
37 
38  def get_settings(self, param_name):
39  param_dict = {}
40  if len(param_name) == 2:
41  try:
42  tmp_dict = rospy.get_param(param_name[0])
43  except KeyError:
44  return -1
45  for item in tmp_dict.items():
46  param_dict["pos/" + item[0]] = item[1]
47 
48  try:
49  tmp_dict = rospy.get_param(param_name[1])
50  except KeyError:
51  return -1
52  for item in tmp_dict.items():
53  param_dict["vel/" + item[0]] = item[1]
54  else:
55  try:
56  param_dict = rospy.get_param(param_name)
57  except KeyError:
58  return -1
59  return param_dict
60 
61 
62 class PidSaver(object):
63 
64  """
65  Saves pid parameters of each controller in parameters_dict in a yaml file
66  """
67 
68  def __init__(self, file_path):
69  self.path = file_path
70 
71  def save_settings(self, param_path, parameters_dict):
72  f = open(self.path, 'r')
73  document = ""
74  for line in f.readlines():
75  document += line
76  f.close()
77 
78  yaml_config = yaml.load(document)
79 
80  for item in parameters_dict.items():
81  if "pos/" in item[0]:
82  yaml_config[param_path[0]]["position_pid"][
83  item[0].split("pos/")[1]] = item[1]
84  elif "vel/" in item[0]:
85  yaml_config[param_path[0]]["velocity_pid"][
86  item[0].split("vel/")[1]] = item[1]
87  else:
88  yaml_config[param_path[0]][param_path[1]][item[0]] = item[1]
89 
90  full_config_to_write = yaml.dump(yaml_config, default_flow_style=False)
91 
92  f = open(self.path, 'w')
93  f.write(full_config_to_write)
94  f.close()
95 
96 if __name__ == '__main__':
97  path_to_config = "~"
98  try:
99  path_to_config = os.path.join(
100  rospkg.RosPack().get_path("sr_edc_controller_configuration"))
101 
102  pid_saver = PidSaver(
103  path_to_config + "/sr_edc_mixed_position_velocity_joint_controllers.yaml")
104  pid_saver.save_settings(
105  ["sh_wrj2_mixed_position_velocity_controller", "pid"], {"d": 1.0})
106  except:
107  rospy.logwarn(
108  "couldnt find the sr_edc_controller_configuration package")
def save_settings(self, param_path, parameters_dict)


sr_gui_controller_tuner
Author(s): Ugo Cupcic
autogenerated on Wed Oct 14 2020 03:22:49