00001 #include "EmergencyStopMC.h"
00002 #include "Shared/WorldState.h"
00003 #include "Shared/get_time.h"
00004 #include "Motion/MotionManager.h"
00005 #include "SoundPlay/SoundManager.h"
00006 #include "Shared/Config.h"
00007 #include "Events/EventRouter.h"
00008 #include "Shared/ERS210Info.h"
00009 #include "Shared/ERS220Info.h"
00010 #include "Shared/ERS7Info.h"
00011 #include "Wireless/Wireless.h"
00012
00013 EmergencyStopMC::EmergencyStopMC()
00014 : PostureMC(), paused(false), stilldown(false), active(true), period(2000),
00015 timeoflastbtn(0), timeofthisbtn(0), timeoflastfreeze(0), duration(600),
00016 pidcutoff(0.2), ledengine()
00017 {
00018 for(unsigned int i=0; i<NumPIDJoints; i++)
00019 piddutyavgs[i]=0;
00020 if(state->robotDesign&WorldState::ERS210Mask) {
00021 ledengine.cycle(ERS210Info::TlRedLEDMask,period,1,0,period/2);
00022 ledengine.cycle(ERS210Info::TlBluLEDMask,period,1);
00023 } else if(state->robotDesign&WorldState::ERS220Mask) {
00024 ledengine.cycle(ERS220Info::TailCenterLEDMask, period, 2.0f, -.5f, (int)(period * 0/5.5));
00025 ledengine.cycle(ERS220Info::TailLeftLEDMask|ERS220Info::TailRightLEDMask, period, 2.0f, -.5f, (int)(period * 1/5.5));
00026 ledengine.cycle(ERS220Info::BackLeft3LEDMask|ERS220Info::BackRight1LEDMask, period, 2.0f, -.5f, (int)(period * 2/5.5));
00027 ledengine.cycle(ERS220Info::BackLeft2LEDMask|ERS220Info::BackRight2LEDMask, period, 2.0f, -.5f, (int)(period * 3/5.5));
00028 ledengine.cycle(ERS220Info::BackLeft1LEDMask|ERS220Info::BackRight3LEDMask, period, 2.0f, -.5f, (int)(period * 4/5.5));
00029 } else if(state->robotDesign&WorldState::ERS7Mask) {
00030 ledengine.cycle(1<<(ERS7Info::MdBackColorLEDOffset-LEDOffset),2*period/3,.15,.15/2-.5,0);
00031 } else {
00032 serr->printf("Emergency Stop: unknown model\n");
00033 ledengine.cycle(1<<(NumLEDs-1),period,1,0,period/2);
00034 ledengine.cycle(1<<(NumLEDs-2),period,1);
00035 }
00036 defaultMaxSpeed(.15);
00037 takeSnapshot();
00038 }
00039
00040
00041 int EmergencyStopMC::updateOutputs() {
00042 if(trigger()) {
00043 if(!stilldown) {
00044 stilldown=true;
00045 timeoflastbtn=timeofthisbtn;
00046 timeofthisbtn=get_time();
00047
00048 }
00049
00050 } else if(stilldown) {
00051
00052 stilldown=false;
00053 if((get_time()-timeoflastbtn)<duration)
00054 setStopped(!paused);
00055 }
00056 if(!paused)
00057 return 0;
00058
00059 if(get_time()-timeoflastfreeze>FrameTime*NumFrames*5) {
00060
00061 for(unsigned int i=0; i<NumPIDJoints; i++) {
00062
00063 piddutyavgs[i]=piddutyavgs[i]*.9f+state->pidduties[i]*.1f;
00064
00065 if(fabsf(state->outputs[PIDJointOffset+i]-cmds[PIDJointOffset+i].value)>.15f) {
00066 curPositions[PIDJointOffset+i]=cmds[PIDJointOffset+i].value=state->outputs[PIDJointOffset+i];
00067 dirty=true;
00068 targetReached=false;
00069 }
00070
00071 if(fabsf(piddutyavgs[i])>pidcutoff) {
00072 cmds[PIDJointOffset+i].value-=piddutyavgs[PIDJointOffset+i];
00073 dirty=true;
00074 targetReached=false;
00075 }
00076 }
00077 }
00078 ledengine.updateLEDs(&cmds[LEDOffset]);
00079 if(state->robotDesign&WorldState::ERS7Mask) {
00080
00081 static float acts[5];
00082 static bool first=true;
00083 if(first) {
00084 for(int i=0; i<5; i++)
00085 acts[i]=0;
00086 first=false;
00087 }
00088 float t=get_time();
00089 t/=period;
00090 t=(((int)t)&1)?(int)t+1-t:(t-(int)t);
00091 t*=8;
00092 const float invsigma=-6;
00093 const float gamma=.83;
00094 const float amp=.5;
00095 float imp[5];
00096 for(int i=0; i<5; i++) {
00097 imp[i]=exp(invsigma*(t-i-2)*(t-i-2));
00098 acts[i]+=amp*imp[i];
00099 acts[i]*=gamma;
00100 }
00101 cmds[ERS7Info::FaceLEDPanelOffset+6].value=acts[0]/2+imp[0];
00102 cmds[ERS7Info::FaceLEDPanelOffset+7].value=acts[4]/2+imp[4];
00103 cmds[ERS7Info::FaceLEDPanelOffset+8].value=acts[1]/2+imp[1];
00104 cmds[ERS7Info::FaceLEDPanelOffset+9].value=acts[3]/2+imp[3];
00105 cmds[ERS7Info::FaceLEDPanelOffset+10].value=acts[2]/2+imp[2];
00106 }
00107 return PostureMC::updateOutputs();
00108 }
00109
00110 void EmergencyStopMC::setActive(bool a) {
00111 if(paused) {
00112 if(!a && active)
00113 releaseJoints();
00114 else if(a && !active)
00115 freezeJoints();
00116 }
00117 active=a;
00118 }
00119
00120
00121 void EmergencyStopMC::setStopped(bool p, bool sound) {
00122 if(p!=paused) {
00123 paused=p;
00124 if(active) {
00125 if(paused) {
00126 freezeJoints();
00127 if(sound)
00128 sndman->PlayFile(config->motion.estop_on_snd);
00129 cout << "*** PAUSED ***" << endl;
00130 } else {
00131 releaseJoints();
00132 if(sound)
00133 sndman->PlayFile(config->motion.estop_off_snd);
00134 cout << "*** UNPAUSED ***" << endl;
00135 }
00136 }
00137 }
00138 }
00139
00140 void EmergencyStopMC::freezeJoints() {
00141 dirty=true;
00142 targetReached=false;
00143 for(unsigned int i=0; i<NumOutputs; i++)
00144 curPositions[i]=cmds[i].value=motman->getOutputCmd(i).value;
00145 for(unsigned int i=0; i<NumPIDJoints; i++)
00146 piddutyavgs[i]=0;
00147 for(unsigned int i=0; i<LEDOffset; i++)
00148 cmds[i].weight=1;
00149 for(unsigned int i=LEDOffset; i<LEDOffset+NumLEDs; i++)
00150 cmds[i].unset();
00151 for(unsigned int i=LEDOffset+NumLEDs; i<NumOutputs; i++)
00152 cmds[i].weight=1;
00153 if(state->robotDesign&WorldState::ERS210Mask) {
00154 cmds[ERS210Info::TlRedLEDOffset].set(0,.5);
00155 cmds[ERS210Info::TlBluLEDOffset].set(0,.5);
00156 } else if(state->robotDesign&WorldState::ERS220Mask) {
00157 for(unsigned int i = 0; i < NumLEDs; i++)
00158 if((ERS220Info::TailLEDMask|ERS220Info::BackLEDMask) & (1 << i))
00159 cmds[LEDOffset + i].set(0, .5);
00160 } else if(state->robotDesign&WorldState::ERS7Mask) {
00161 cmds[ERS7Info::MdBackColorLEDOffset].set(0,.5);
00162 for(int i=6; i<6+5; i++)
00163 cmds[ERS7Info::FaceLEDPanelOffset+i].set(0,0.5);
00164 } else {
00165 cmds[LEDOffset+NumLEDs-1].set(0,.5);
00166 cmds[LEDOffset+NumLEDs-2].set(0,.5);
00167 }
00168 postEvent(EventBase(EventBase::estopEGID,getID(),EventBase::activateETID,0));
00169 timeoflastfreeze=get_time();
00170 }
00171
00172 void EmergencyStopMC::releaseJoints() {
00173 dirty=true;
00174 targetReached=false;
00175 for(unsigned int i=0; i<NumOutputs; i++)
00176 cmds[i].unset();
00177
00178 if(state->robotDesign&WorldState::ERS210Mask) {
00179 motman->setOutput(this,ERS210Info::TlRedLEDOffset,0.f);
00180 motman->setOutput(this,ERS210Info::TlBluLEDOffset,0.f);
00181 } else if(state->robotDesign&WorldState::ERS220Mask) {
00182 for(unsigned int i = 0; i < NumLEDs; i++)
00183 if((ERS220Info::TailLEDMask|ERS220Info::BackLEDMask) & (1 << i))
00184 motman->setOutput(this,LEDOffset + i,0.f);
00185 } else if(state->robotDesign&WorldState::ERS7Mask) {
00186 cmds[ERS7Info::MdBackColorLEDOffset].set(0,0.f);
00187 for(int i=6; i<6+5; i++)
00188 cmds[ERS7Info::FaceLEDPanelOffset+i].set(0,0.f);
00189 } else {
00190 cmds[LEDOffset+NumLEDs-1].set(0,0.f);
00191 cmds[LEDOffset+NumLEDs-2].set(0,0.f);
00192 }
00193 postEvent(EventBase(EventBase::estopEGID,getID(),EventBase::deactivateETID,get_time()-timeoflastfreeze));
00194 }
00195
00196 bool EmergencyStopMC::trigger() {
00197 if(state->robotDesign&WorldState::ERS210Mask)
00198 return state->button_times[ERS210Info::BackButOffset];
00199 if(state->robotDesign&WorldState::ERS220Mask)
00200 return state->button_times[ERS220Info::BackButOffset];
00201 if(state->robotDesign&WorldState::ERS7Mask)
00202 return state->button_times[ERS7Info::FrontBackButOffset]+state->button_times[ERS7Info::MiddleBackButOffset]+state->button_times[ERS7Info::RearBackButOffset];
00203 serr->printf("EmergencyStopMC: unsupported model!\n");
00204 return false;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217