propagate_baseline.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2008, Willow Garage, Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of Willow Garage, Inc. nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 # author: Vijay Pradeep
36 
37 import roslib; roslib.load_manifest('pr2_calibration_launch')
38 import sensor_msgs.msg
39 import sensor_msgs.srv
40 import rospy
41 import rosbag
42 
43 import math
44 import sys
45 import yaml
46 
48 
49 def usage():
50  print("Usage: ./baseline_updater camera:=[camera_namespace] [calibration_bagfile] [system_filename] [config_camera_name]")
51  print(" camera_namespace: Ros name to prepend onto the call to set_calibration")
52  print(" config_filename: yaml file that stores the baseline shift")
53  print(" config_camera_name: Name of the camera in the configuration file. Tells script which baseline shift to use")
54 
55 def main():
56  rospy.init_node('baseline_updater', anonymous=True)
57 
58  argv = rospy.myargv()
59 
60  if len(argv) != 4:
61  usage()
62  sys.exit(0)
63 
64  bag_filename = argv[1]
65  system_filename = argv[2]
66  config_cam_name = argv[3]
67 
68  cam_info = get_cam_info(bag_filename, config_cam_name)
69  if cam_info == None:
70  print("Could not find a camera of the name [%s]" % config_cam_name)
71  sys.exit(-1)
72 
73  print("Original Projection Matrix:")
74  for k in range(3):
75  print(" %s" % " ".join(["% 12.5f" % p for p in cam_info.P[k*4:k*4+4]]))
76  original_baseline = cam_info.P[3]
77 
78  # ****** Get the baseline shift from the yaml ******
79  system_dict = yaml.load(open(system_filename))
80  cam_dict = system_dict['sensors']['rectified_cams']
81  target_cam_dict = cam_dict[config_cam_name]
82  baseline_shift = target_cam_dict['baseline_shift']
83 
84  print("Baseline Shift:")
85  print(" % 12.5f" % baseline_shift)
86 
87  # ****** Update the baseline ******
88  updated_baseline = original_baseline + baseline_shift
89 
90  #import code; code.interact(local=locals())
91  updated_P = list(cam_info.P)
92  updated_P[3] = updated_baseline
93  cam_info.P = updated_P
94 
95  print("Updated Projection Matrix:")
96  for k in range(3):
97  print(" %s" % " ".join(["% 12.5f" % p for p in cam_info.P[k*4:k*4+4]]))
98 
99  # ****** Load new camera info onto camera eeprom ******
100  cam_name = rospy.resolve_name("camera")
101  set_cal_service_name = cam_name + "/set_camera_info"
102 
103  print("Waiting for service [%s] to be available" % set_cal_service_name)
104  rospy.wait_for_service(set_cal_service_name)
105  print("Writing camera info to camera memory...")
106  set_cal_srv = rospy.ServiceProxy(set_cal_service_name, sensor_msgs.srv.SetCameraInfo)
107  resp = set_cal_srv(cam_info)
108  if resp.success:
109  print("Done writing to camera")
110  else:
111  print("**************** ERROR WRITING TO CAMERA. PLEASE RETRY ***********************")
112  sys.exit(-1)
113 
114 if __name__ == "__main__":
115  main()


pr2_calibration_launch
Author(s): Vijay Pradeep
autogenerated on Tue Jun 1 2021 02:50:59