counterbalance_training.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2010, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the Willow Garage nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 ##\author Eric Berger, Kevin Watts
00036 ##\brief Determines counterbalance adjustments based on CB data
00037 
00038 PKG = 'pr2_counterbalance_check'
00039 import roslib; roslib.load_manifest(PKG)
00040 
00041 from pr2_counterbalance_check.counterbalance_analysis import *
00042 
00043 from joint_qualification_controllers.msg import CounterbalanceTestData
00044 
00045 from optparse import OptionParser
00046 
00047 import sys, os
00048 
00049 from ros import rosrecord
00050 
00051 import numpy
00052 
00053 CB_MSG_TYPE = 'joint_qualification_controllers/CounterbalanceTestData'
00054 
00055 def get_data(bag):
00056     for topic, msg, t in rosrecord.logplayer(bag):
00057         return CounterbalanceAnalysisData(msg)
00058 
00059 def print_usage(code = 0):
00060     print "./counterbalance_training cb_bag1 cb_bag2 cb_bag3 ..."
00061     print "Determines counterbalance adjustments necessary to tune CB"
00062     print "Bags must have one message of type %s" % CB_MSG_TYPE
00063     sys.exit(code)
00064 
00065 if __name__ == '__main__':
00066     if len(sys.argv) < 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
00067         print_usage(1)
00068 
00069     bags = sys.argv[1:]
00070 
00071     adjustments = {}
00072     efforts = {}
00073     print 'Enter CB adjustments from "zero" in turns CW for each bag'
00074     for b in bags:
00075         if not os.path.exists(b):
00076             print >> sys.stderr, "Bag %s does not exist. Check filename and retry" % b
00077             print_usage(1)
00078 
00079         #Ask for adjustments when this bag was taken
00080         try:
00081             adj_secondary = float(raw_input("Please enter secondary adjustment (turns CW) for bag %s: "%b))
00082             adj_cb_bar = float(raw_input("Please enter CB bar adjustment (turns CW) for bag %s: "%b))
00083             adjustments[b] = (adj_secondary, adj_cb_bar, 1)
00084         except:
00085             print "Invalid input for adjustment.  Floating point values expected."
00086             sys.exit(1)
00087 
00088         data = get_data(b)
00089         efforts[b] = get_efforts(data, True) + get_efforts(data, False)
00090     
00091     B = numpy.array([efforts[b] for b in bags])
00092     A = numpy.array([adjustments[b] for b in bags])
00093     X = numpy.linalg.lstsq(A, B)
00094     model = X[0]
00095     model.dump('counterbalance_model.dat')
00096     print '\"counterbalance_model.dat\" contains CB adjustment values'


pr2_counterbalance_check
Author(s): Kevin Watts
autogenerated on Mon Sep 14 2015 14:39:10