Libbarrett
1.2.4
|
00001 /* 00002 * control_mode_switcher.h 00003 * 00004 * Created on: Jul 24, 2012 00005 * Author: dc 00006 */ 00007 00008 #ifndef CONTROL_MODE_SWITCHER_H_ 00009 #define CONTROL_MODE_SWITCHER_H_ 00010 00011 00012 #include <iostream> 00013 #include <cassert> 00014 00015 #include <libconfig.h++> 00016 00017 #include <barrett/os.h> 00018 #include <barrett/units.h> 00019 #include <barrett/math/utils.h> 00020 #include <barrett/products/product_manager.h> 00021 #include <barrett/products/puck.h> 00022 #include <barrett/systems/helpers.h> 00023 #include <barrett/systems/wam.h> 00024 #include <barrett/systems/gain.h> 00025 00026 00027 template<size_t DOF> 00028 class ControlModeSwitcher { 00029 public: 00030 BARRETT_UNITS_TEMPLATE_TYPEDEFS(DOF); 00031 00032 enum ControlMode { CURRENT, VOLTAGE }; 00033 00034 ControlModeSwitcher(barrett::ProductManager& pm_, barrett::systems::Wam<DOF>& wam_, const libconfig::Setting& setting_) : 00035 pm(pm_), wam(wam_), setting(setting_), mode(VOLTAGE), cGain(setting["current_gain"]), vGain(setting["voltage_gain"]), torqueGainSys(vGain) 00036 { 00037 for (size_t i = 0; i < DOF; ++i) { 00038 assert(wam.getLowLevelWam().getPucks()[i]->getVers() >= 200); 00039 } 00040 00041 barrett::systems::connect(wam.jtSum.output, torqueGainSys.input); 00042 barrett::systems::reconnect(torqueGainSys.output, wam.llww.input); 00043 currentControl(); 00044 } 00045 ~ControlModeSwitcher() { 00046 currentControl(); // Revert back to current control. 00047 barrett::systems::reconnect(wam.jtSum.output, wam.llww.input); 00048 } 00049 00050 enum ControlMode getMode() const { return mode; } 00051 double getCurrentGain() const { return cGain; } 00052 double getVoltageGain() const { return vGain; } 00053 00054 void currentControl() { 00055 if (getMode() == CURRENT) { 00056 return; 00057 } 00058 00059 for (size_t i = 0; i < DOF; ++i) { 00060 wam.getLowLevelWam().getPucks()[i]->resetProperty(barrett::Puck::IKCOR); 00061 wam.getLowLevelWam().getPucks()[i]->resetProperty(barrett::Puck::IKP); 00062 wam.getLowLevelWam().getPucks()[i]->resetProperty(barrett::Puck::IKI); 00063 } 00064 00065 const libconfig::Setting& wamSetting = pm.getConfig().lookup(pm.getWamDefaultConfigPath()); 00066 00067 wam.jpController.setFromConfig(wamSetting["joint_position_control"]); 00068 wam.jpController.resetIntegrator(); 00069 00070 wam.tpController.setFromConfig(wamSetting["tool_position_control"]); 00071 wam.tpController.resetIntegrator(); 00072 wam.tpoTpController.setFromConfig(wamSetting["tool_position_control"]); 00073 wam.tpoTpController.resetIntegrator(); 00074 00075 wam.toController.setFromConfig(wamSetting["tool_orientation_control"]); 00076 wam.tpoToController.setFromConfig(wamSetting["tool_orientation_control"]); 00077 00078 torqueGainSys.setGain(cGain); 00079 00080 mode = CURRENT; 00081 } 00082 void voltageControl() { 00083 if (getMode() == VOLTAGE) { 00084 return; 00085 } 00086 00087 wam.getLowLevelWam().getPuckGroup().setProperty(barrett::Puck::IKI, 0); 00088 wam.getLowLevelWam().getPuckGroup().setProperty(barrett::Puck::IKP, 8000); 00089 wam.getLowLevelWam().getPuckGroup().setProperty(barrett::Puck::IKCOR, 0); 00090 00091 wam.jpController.setFromConfig(setting["voltage_joint_position_control"]); 00092 wam.jpController.resetIntegrator(); 00093 00094 wam.tpController.setFromConfig(setting["voltage_tool_position_control"]); 00095 wam.tpController.resetIntegrator(); 00096 wam.tpoTpController.setFromConfig(setting["voltage_tool_position_control"]); 00097 wam.tpoTpController.resetIntegrator(); 00098 00099 wam.toController.setFromConfig(setting["voltage_tool_orientation_control"]); 00100 wam.tpoToController.setFromConfig(setting["voltage_tool_orientation_control"]); 00101 00102 torqueGainSys.setGain(vGain); 00103 00104 mode = VOLTAGE; 00105 } 00106 00107 00108 static const double MAX_SCALE = 4.0; 00109 static const double MIN_SCALE = 0.25; 00110 00111 void calculateTorqueGain() { 00112 libconfig::Config config; 00113 config.readFile("/etc/barrett/calibration.conf"); 00114 libconfig::Setting& setting = config.lookup("gravitycal")[pm.getWamDefaultConfigPath()]; 00115 00116 int scaleCount = 0; 00117 double scaleSum = 0.0; 00118 double maxScale = MIN_SCALE; 00119 double minScale = MAX_SCALE; 00120 00121 for (int i = 0; i < setting.getLength(); ++i) { 00122 wam.moveTo(jp_type(setting[i])); 00123 barrett::btsleep(1.0); 00124 00125 jt_type gravity = wam.jtSum.getInput(barrett::systems::Wam<DOF>::GRAVITY_INPUT).getValue(); 00126 jt_type supporting = wam.llww.input.getValue(); 00127 double scale = (gravity.dot(supporting) / gravity.norm()) / gravity.norm(); 00128 jt_type error = supporting - scale*gravity; 00129 00130 printf("Calibration Pose - %d of %d", i+1, setting.getLength()); 00131 00132 bool good = true; 00133 if (scale < MIN_SCALE || scale > MAX_SCALE) { 00134 printf("Scale is out of range.\n"); 00135 good = false; 00136 } 00137 if (error.norm() > 0.4 * supporting.norm()) { 00138 //printf("Error is too big.\n"); 00139 good = false; 00140 } 00141 00142 if (good) { 00143 scaleCount++; 00144 scaleSum += scale; 00145 maxScale = barrett::math::max(maxScale, scale); 00146 minScale = barrett::math::min(minScale, scale); 00147 } 00148 00149 printf("\n"); 00150 } 00151 00152 assert(scaleCount >= 3); 00153 double meanScale = scaleSum/scaleCount; 00154 printf("SCALE: %f (+%f, -%f)\n", meanScale, maxScale - meanScale, meanScale - minScale); 00155 00156 switch (getMode()) { 00157 case CURRENT: 00158 cGain = meanScale; 00159 torqueGainSys.setGain(cGain); 00160 break; 00161 case VOLTAGE: 00162 vGain = meanScale; 00163 torqueGainSys.setGain(vGain); 00164 break; 00165 default: 00166 assert(false); 00167 break; 00168 } 00169 } 00170 00171 protected: 00172 barrett::ProductManager& pm; 00173 barrett::systems::Wam<DOF>& wam; 00174 const libconfig::Setting& setting; 00175 00176 enum ControlMode mode; 00177 double cGain, vGain; 00178 barrett::systems::Gain<jt_type, double> torqueGainSys; 00179 }; 00180 00181 00182 #endif /* CONTROL_MODE_SWITCHER_H_ */