00001 #include "PostureEditor.h"
00002 #include "Motion/MMAccessor.h"
00003 #include "Motion/EmergencyStopMC.h"
00004 #include "Motion/MotionSequenceMC.h"
00005 #include "Motion/LedMC.h"
00006 #include "Sound/SoundManager.h"
00007 #include "Events/EventRouter.h"
00008 #include "ValueEditControl.h"
00009 #include "NullControl.h"
00010 #include "StringInputControl.h"
00011 #include "FileInputControl.h"
00012
00013 PostureEditor::PostureEditor(MotionManager::MC_ID estop_ID)
00014 : ControlBase("Posture Editor","Allows you to load, save, and numerically edit the posture"),
00015 pose(), reachID(MotionManager::invalid_MC_ID),
00016 estopID(estop_ID), loadPose(NULL), disabledLoadPose(NULL), savePose(NULL), pauseCalled(false)
00017 {
00018
00019 pushSlot(loadPose=new FileInputControl("Load Posture","Select a posture to open",config->portPath(config->motion.root)));
00020 loadPose->setFilter("*.pos");
00021 disabledLoadPose=new NullControl("[Load disabled by EStop]","Cannot load new postures while EStop is active");
00022 pushSlot(savePose=new StringInputControl("Save Posture","Please enter the filename to save to (in "+config->motion.root+")"));
00023
00024
00025 ControlBase * weights;
00026 pushSlot(weights=new ControlBase("Weights","Set the weights for outputs"));
00027 for(unsigned int i=0; i<NumOutputs; i++)
00028 weights->pushSlot(new ValueEditControl<float>(outputNames[i],&pose(i).weight));
00029
00030 pushSlot(NULL);
00031
00032
00033 for(unsigned int i=0; i<NumOutputs; i++)
00034 pushSlot(new ValueEditControl<float>(outputNames[i],&pose(i).value));
00035 }
00036
00037 PostureEditor::~PostureEditor() {
00038 delete loadPose;
00039 delete disabledLoadPose;
00040 options[0]=NULL;
00041 }
00042
00043 ControlBase *
00044 PostureEditor::activate(MotionManager::MC_ID disp_id, Socket * gui) {
00045
00046 if(reachID!=MotionManager::invalid_MC_ID)
00047 return ControlBase::activate(disp_id,gui);
00048
00049 pose.takeSnapshot();
00050 pose.setWeights(1);
00051
00052 for(unsigned int i=LEDOffset; i<LEDOffset+NumLEDs; i++)
00053 pose.setOutputCmd(i,0);
00054
00055 SharedObject<SmallMotionSequenceMC> reach;
00056 reachID=motman->addPersistentMotion(reach);
00057
00058 erouter->addListener(this,EventBase::estopEGID);
00059
00060 return ControlBase::activate(disp_id,gui);
00061 }
00062
00063 void
00064 PostureEditor::refresh() {
00065
00066 if(isEStopped()) {
00067 processEvent(EventBase(EventBase::timerEGID,1,EventBase::statusETID,0));
00068 erouter->addTimer(this,0,500);
00069 options[0]=disabledLoadPose;
00070 } else {
00071 options[0]=loadPose;
00072 }
00073 if(loadPose->getLastInput().size()>0) {
00074 pose.loadFile(loadPose->getLastInput().c_str());
00075 updatePose(moveTime);
00076 loadPose->clearLastInput();
00077 } else if(savePose->getLastInput().size()>0) {
00078
00079 std::string filename=savePose->getLastInput();
00080 if(filename.find(".")==std::string::npos)
00081 filename+=".pos";
00082 pose.saveFile(config->motion.makePath(filename).c_str());
00083 savePose->takeInput("");
00084 } else {
00085 updatePose(moveTime/2);
00086 }
00087 pauseCalled=false;
00088 ControlBase::refresh();
00089 }
00090
00091 void
00092 PostureEditor::pause() {
00093
00094 refresh();
00095 pauseCalled=true;
00096 erouter->removeTimer(this);
00097 ControlBase::pause();
00098 }
00099
00100 void
00101 PostureEditor::deactivate() {
00102
00103
00104 motman->removeMotion(reachID);
00105 reachID=MotionManager::invalid_MC_ID;
00106 erouter->removeListener(this);
00107 erouter->removeTimer(this);
00108 ControlBase::deactivate();
00109 }
00110
00111 void
00112 PostureEditor::processEvent(const EventBase& e) {
00113
00114 if(e.getGeneratorID()==EventBase::estopEGID) {
00115 if(e.getTypeID()==EventBase::deactivateETID) {
00116 MMAccessor<SmallMotionSequenceMC>(reachID)->play();
00117 erouter->removeTimer(this);
00118 if(!pauseCalled)
00119 refresh();
00120 } else {
00121 if(!pauseCalled) {
00122 erouter->addTimer(this,0,500);
00123 processEvent(EventBase(EventBase::timerEGID,0,EventBase::statusETID));
00124 }
00125 }
00126 } else if(e.getGeneratorID()==EventBase::timerEGID) {
00127
00128 for(unsigned int i=0; i<LEDOffset; i++)
00129 pose(i).value=state->outputs[i];
00130 for(unsigned int i=LEDOffset+NumLEDs; i<NumOutputs; i++)
00131 pose(i).value=state->outputs[i];
00132 if(e.getSourceID()==0)
00133 refresh();
00134 } else {
00135 serr->printf("WARNING: PostureEditor unexpected event: %s\n",e.getName().c_str());
00136 }
00137 }
00138
00139 bool
00140 PostureEditor::isEStopped() {
00141 return MMAccessor<EmergencyStopMC>(estopID)->getStopped();
00142 }
00143
00144 void
00145 PostureEditor::updatePose(unsigned int delay) {
00146
00147 bool paused=isEStopped();
00148 MMAccessor<SmallMotionSequenceMC> reach_acc(reachID);
00149 if(paused) {
00150 reach_acc->clear();
00151 return;
00152 }
00153 PostureEngine curpose;
00154 reach_acc->getPose(curpose);
00155 reach_acc->clear();
00156
00157 reach_acc->setTime(1);
00158 for(unsigned int i=0; i<NumOutputs; i++)
00159 if(curpose(i).weight!=0)
00160 reach_acc->setOutputCmd(i,curpose(i));
00161
00162 reach_acc->setTime(delay);
00163 reach_acc->setPose(pose);
00164 reach_acc->play();
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177