D:/Projekte/Super-Yano/Super-Yano/RobotControl.cpp

gehe zur Dokumentation dieser Datei
00001 #include "StdAfx.h"
00002 #include "RobotControl.h"
00003 #include <ctype.h>
00004 
00009 void RobotControl::construct(System::String^ port_name) 
00010 {
00011         blinkingState = STOPPED;
00012         earTremorState = STOPPED;
00013         visualAdjustmentThreadState = STOPPED;
00014         isMouthMovement = true;
00015         
00016         initiateServoSettings();
00017         
00018         components = (gcnew System::ComponentModel::Container());
00019 
00020         spVisemeEventHandler = gcnew SpeechLib::_ISpeechVoiceEvents_VisemeEventHandler(&RobotControl::speechVisemeEvent);
00021 
00022         isSerialPort = true;
00023                 
00024         serial_Port = (gcnew System::IO::Ports::SerialPort(components));
00025         
00026         try
00027         {
00028                 serial_Port->BaudRate = 2400;
00029                 serial_Port->PortName = port_name;
00030                 serial_Port->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(&RobotControl::serialPort_DataReceived);
00031                 serial_Port->Encoding = System::Text::Encoding::GetEncoding(28591);
00032                 serial_Port->NewLine = "\r";
00033         }
00034         catch (Exception^ e)
00035         {
00036                 serial_Port->Close();
00037                 isSerialPort = false;
00038                 Windows::Forms::MessageBox::Show( e->Message + "\nEs trat ein Fehler beim Konfigurieren des COM-Objets auf!", "Comport-Fehler "+port_name, Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00039         }
00040 
00041 
00042         //nach Roboter suchen...
00043         String^ s = findComport(port_name);
00044         if (s->CompareTo("") != 0)
00045         {
00046                 serial_Port->PortName = s;
00047                 isSerialPort = true;
00048         }
00049         else
00050         {
00051                 Windows::Forms::MessageBox::Show( "Es wurde kein Roboterinterface gefunden!\n", "Comport-Fehler", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00052         }
00053 
00054         
00055         //Baudrate auf 38400 Baud setzen
00056         if(isSerialPort == true)
00057         {
00058                 try
00059                 {
00060                         if (!serial_Port->IsOpen) serial_Port->Open();
00061                         serial_Port->WriteLine("2400 Baud");
00062                         serial_Port->WriteLine("U!SCVER?");
00063                         Threading::Thread::Sleep(1500);
00064                         serial_Port->WriteLine("U!SCSBR"+Char(0x01));
00065                         serial_Port->Close();
00066                         serial_Port->BaudRate = 38400;
00067                         serial_Port->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(&RobotControl::serialPort_DataReceived);
00068                         serial_Port->Open();
00069                         serial_Port->Encoding = System::Text::Encoding::GetEncoding(28591);
00070                         serial_Port->NewLine = "\r";    
00071                         serial_Port->WriteLine("U!SCVER?");
00072                         Threading::Thread::Sleep(500);
00073                         initiateRobot();
00074                         Threading::Thread::Sleep(500);
00075                 }
00076                 catch (Exception^ e)
00077                 {
00078                         serial_Port->Close();
00079                         isSerialPort = false;
00080                         Windows::Forms::MessageBox::Show( e->Message + "\nDie Com-Schnittstelle "+ getComPortName() +" konnte nicht initialisiert werden.\nDer Roboterkopf wird nicht benutzt.", "Comport-Fehler", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00081                 }
00082         }
00083 
00084 
00085         blinkThread = gcnew Thread( gcnew ThreadStart( &RobotControl::blink ));
00086         blinkingState = RUNNING;
00087         blinkThread->Start();
00088 
00089         earTremorThread = gcnew Thread( gcnew ThreadStart( &RobotControl::earTremorISR ));
00090         earTremorState = RUNNING;
00091         earTremorThread->Start();
00092 
00093         visualAdjustmentThread = gcnew Thread( gcnew ThreadStart( &RobotControl::visualAdjustmentThreadISR ));
00094         visualAdjustmentThreadState = RUNNING;
00095         visualAdjustmentThread->Start();
00096 
00097 
00098 }
00099 
00104 void RobotControl::setBlinkingThreadPaused(bool paused)
00105 {
00106         if((paused)&&(blinkingState == RUNNING)) blinkingState = PAUSED;
00107         else if((!paused)&&(blinkingState == PAUSED)) blinkingState = RUNNING;
00108 }
00109 
00114 void RobotControl::setEarTremorThreadPaused(bool paused)
00115 {
00116         if((paused)&&(earTremorState == RUNNING)) earTremorState = PAUSED;
00117         else if((!paused)&&(earTremorState == PAUSED)) earTremorState = RUNNING;
00118 }
00119 
00124 void RobotControl::setVisualAdjustmentThreadPaused(bool paused)
00125 {
00126         if((paused)&&(visualAdjustmentThreadState == RUNNING)) visualAdjustmentThreadState = PAUSED;
00127         else if((!paused)&&(visualAdjustmentThreadState == PAUSED)) visualAdjustmentThreadState = RUNNING;
00128 }
00129 
00134 void RobotControl::setMouthMovementEnabled(bool enabled)
00135 {
00136         isMouthMovement = enabled;
00137 }
00138 
00139 
00144 void RobotControl::setCamera(int camera)
00145 {
00146         if ( serial_Port->IsOpen)
00147         {
00148                 used_eye = camera;
00149                 serial_Port->WriteLine("!CAM"+camera);
00150         }
00151 }
00152 
00158 void RobotControl::setEyeOpen(int eye, bool eyeOpened)
00159 {
00160         if (eye == LEFT)
00161         {
00162                 if (eyeOpened)
00163                 {
00164                         setServoValue(lid_l, 0, servos[lid_l]->startValue);
00165                 }
00166                 else
00167                 {
00168                         setServoValue(lid_l, 0, servos[lid_l]->maxValue);
00169                 }
00170         }
00171         else if (eye == RIGHT)
00172         {
00173                 if (eyeOpened)
00174                 {
00175                         setServoValue(lid_r, 0, servos[lid_r]->startValue);     
00176                 }
00177                 else
00178                 {
00179                         setServoValue(lid_r, 0, servos[lid_r]->minValue);
00180                 }               
00181         }
00182 }
00183 
00189 void RobotControl::visualAdjustmentThreadISR(void)
00190 {
00191         int numberOfFramesWithoutFaces = 0;
00192         int maxNumberOfFramesWithoutFaces = 10;
00193 
00194         int numberOfFramesSinceLastEyeMove = 0;
00195         int maxNumberOfFramesSinceLastEyeMove = 5;
00196 
00197         int eyeSleepTime = 25;
00198 
00199         double currentRelativeX;
00200         double currentRelativeY;
00201 
00202         //double diffX, diffY;
00203 
00204         while(visualAdjustmentThreadState != STOPPED)
00205         {
00206 
00207                 while(visualAdjustmentThreadState == RUNNING)
00208                 {
00209                         Thread::Sleep(10);
00210 
00211                         int count = getNumberOfFaces();
00212                         double fx = getMajorFaceRelativeX(); 
00213                         double fy = getMajorFaceRelativeY(); 
00214 
00215                         if(count < 1)  // kein Gesicht gefunden
00216                         {
00217                                 if (numberOfFramesWithoutFaces >= maxNumberOfFramesWithoutFaces)
00218                                 {
00219                                         //manchmal verhackeln sich die Augenlider, darum vielleicht öffnen
00220                                         setEyeOpen(LEFT, true);
00221                                         Threading::Thread::Sleep(50);
00222                                         setEyeOpen(RIGHT, true);
00223                                 }
00224 
00225                                 if (numberOfFramesWithoutFaces < maxNumberOfFramesWithoutFaces)
00226                                 {
00227                                         numberOfFramesWithoutFaces++;
00228                                 }
00229                         }
00230                         else  //mindestens ein Gesicht gefunden
00231                         {
00232                                 numberOfFramesWithoutFaces = 0;
00233 
00234                                 numberOfFramesSinceLastEyeMove++;
00235 
00236                                 currentRelativeX = (servos[turn]->currentValue - servos[turn]->minValue) / (servos[turn]->maxValue - servos[turn]->minValue);
00237                                 currentRelativeY = (servos[pitch]->currentValue - servos[pitch]->minValue) / (servos[pitch]->maxValue - servos[pitch]->minValue);
00238                                 
00239                                 //diffX = Math::Abs(fx - currentRelativeX);
00240                                 //diffY = Math::Abs(fy - currentRelativeY);
00241 
00242                                 if ( Math::Abs(fx - 0.5) > 0.07) // minus 50%, um von 43% auf -7% bzw. von 57% auf 7% zu kommen, weil ja Abweichung vom Bildmittelpunkt (50%) gesucht ist
00243                                 {
00244                                         if(fx > 0.5)
00245                                         {
00246                                                         if(servos[eye_l]->currentValue >= (servos[eye_l]->minValue+20))
00247                                                         {
00248                                                                 setServoValue(eye_l, 0, servos[eye_l]->currentValue - 20); 
00249                                                                 numberOfFramesSinceLastEyeMove = 0;
00250                                                                 Thread::Sleep(eyeSleepTime);
00251                                                         }
00252                                                         if(servos[eye_r]->currentValue >= (servos[eye_r]->minValue+20))
00253                                                         {
00254                                                                 setServoValue(eye_r, 0, servos[eye_r]->currentValue - 20); 
00255                                                                 numberOfFramesSinceLastEyeMove = 0;
00256                                                                 Thread::Sleep(eyeSleepTime);
00257                                                         }
00258                                         }
00259                                         else // diffX <= 0.5
00260                                         {
00261                                                         if(servos[eye_l]->currentValue <= (servos[eye_l]->maxValue-20))
00262                                                         {
00263                                                                 setServoValue(eye_l, 0, servos[eye_l]->currentValue + 20); 
00264                                                                 numberOfFramesSinceLastEyeMove = 0;
00265                                                                 Thread::Sleep(eyeSleepTime);
00266                                                         }
00267                                                         if(servos[eye_r]->currentValue <= (servos[eye_r]->maxValue-20))
00268                                                         {
00269                                                                 setServoValue(eye_r, 0, servos[eye_r]->currentValue + 20); 
00270                                                                 numberOfFramesSinceLastEyeMove = 0;
00271                                                                 Thread::Sleep(eyeSleepTime);
00272                                                         }
00273                                                         
00274                                         } // fx <= 0.5
00275 
00276                                 } // fx ausserhalb toleranz?
00277 
00278                                 if (numberOfFramesSinceLastEyeMove > maxNumberOfFramesSinceLastEyeMove)
00279                                 {
00280                                         if (
00281                                                         (
00282                                                                 (used_eye == LEFT) 
00283                                                                 && 
00284                                                                 ( Math::Abs(servos[eye_l]->currentValue - servos[eye_l]->startValue) > 100 )
00285                                                         )
00286                                                         ||
00287                                                         (
00288                                                                 (used_eye == RIGHT) 
00289                                                                 && 
00290                                                                 ( Math::Abs(servos[eye_r]->currentValue - servos[eye_r]->startValue) > 100 )
00291                                                         )
00292                                                 )
00293                                         {
00294                                                 int direction;
00295                                                 if(used_eye == RIGHT)
00296                                                 {
00297                                                         if(servos[eye_r]->currentValue > servos[eye_r]->startValue) direction = RIGHT;
00298                                                         else direction = LEFT;
00299                                                 }
00300                                                 else
00301                                                 {
00302                                                         if(servos[eye_l]->currentValue > servos[eye_l]->startValue) direction = RIGHT;
00303                                                         else direction = LEFT;
00304                                                 }
00305                                                 if(direction == RIGHT)
00306                                                 {
00307                                                         if(servos[turn]->currentValue >= (servos[turn]->minValue + 5))
00308                                                         {
00309                                                                 setServoValue(turn, 0, servos[turn]->currentValue - 5); 
00310                                                                 Thread::Sleep(20);
00311                                                         }
00312                                                 }
00313                                                 else
00314                                                 {
00315                                                         if(servos[turn]->currentValue <= (servos[turn]->maxValue - 5))
00316                                                         {
00317                                                                 setServoValue(turn, 0, servos[turn]->currentValue + 5); 
00318                                                                 Thread::Sleep(20);
00319                                                         }
00320                                                 }
00321                                         } // if .x.x.x.x.x.x.x.
00322                                 } // if (numberOfFramesSinceLastEyeMove > maxNumberOfFramesSinceLastEyeMove)
00323 
00324 
00325                                 if ( Math::Abs(fy - 0.5) > 0.15)
00326                                 {
00327                                         if(fy > 0.5)
00328                                         {
00329                                                 if(servos[pitch]->currentValue >= (servos[pitch]->minValue+20))
00330                                                 {
00331                                                         setServoValue(pitch, 0, servos[pitch]->currentValue - 20); 
00332                                                         Thread::Sleep(20);
00333                                                 }
00334                                         }
00335                                         else
00336                                         {
00337                                                 if(servos[pitch]->currentValue <= (servos[pitch]->maxValue-20))
00338                                                 {
00339                                                         setServoValue(pitch, 0, servos[pitch]->currentValue + 20); 
00340                                                         Thread::Sleep(20);
00341                                                 }
00342                                         }
00343                                 } // fy ausserhalb toleanz?
00344 
00345 
00346                         }
00347 
00348                 } //while RUNNING
00349 
00350                 Thread::Sleep(100);
00351 
00352         } //while != STOPPED
00353 
00354 }
00355 
00361 void RobotControl::earTremorISR(void)
00362 {
00363 
00364         long sleepTime;
00365 
00366         Random^ rnd = gcnew Random();
00367 
00368         while(earTremorState != STOPPED)
00369         {
00370                 while(earTremorState == RUNNING)
00371                 {
00372                         sleepTime = rnd->Next(30000, 90000);
00373                         for(int i=0; i< (sleepTime/100); i++)
00374                         {
00375                                 if(earTremorState == RUNNING) Thread::Sleep(100);
00376                                 else break;
00377                         }
00378 
00379                         int ear = rnd->Next(2);
00380                         int count = rnd->Next(3)+1;
00381                         int direction = rnd->Next(2);
00382 
00383                         if (ear == LEFT) ear = ear_l;
00384                         else ear = ear_r; 
00385 
00386 
00387                         for(int i=0; i<count; i++)
00388                         {
00389                                 if(direction == 1)
00390                                 {
00391                                         //nach unten zucken
00392                                         int diff = (servos[ear]->startValue - servos[ear]->minValue) / count;
00393                                         setServoValue(ear, 0, servos[ear]->startValue-diff);
00394                                         Threading::Thread::Sleep(200);
00395                                         setServoValue(ear, 0, servos[ear]->startValue);
00396                                 }
00397                                 else
00398                                 {
00399                                         //nach oben zucken
00400                                         int diff = (servos[ear]->maxValue - servos[ear]->startValue) / count;
00401                                         setServoValue(ear, 0, servos[ear]->startValue+diff);
00402                                         Threading::Thread::Sleep(200);
00403                                         setServoValue(ear, 0, servos[ear]->startValue);
00404                                 }
00405                                 if (count>1) Threading::Thread::Sleep(200);
00406                         }
00407 
00408                 }
00409                 Thread::Sleep(100);
00410         }
00411 }
00412 
00416 void RobotControl::blink(void)
00417 {       
00418         long sleepTime;
00419         Random^ rnd = gcnew Random();
00420 
00421         while(blinkingState != STOPPED)
00422         {
00423                 while(blinkingState == RUNNING)
00424                 {
00425                         sleepTime = rnd->Next(20000, 30000);
00426                         for(int i=0; i< (sleepTime/100); i++)
00427                         {
00428                                 if(blinkingState == RUNNING) Thread::Sleep(100);
00429                                 else break;
00430                         }
00431 
00432                         setEyeOpen(LEFT, false);
00433                         Threading::Thread::Sleep(50);
00434                         setEyeOpen(RIGHT, false);
00435 
00436                         Threading::Thread::Sleep(200);
00437 
00438                         setEyeOpen(LEFT, true);
00439                         Threading::Thread::Sleep(50);
00440                         setEyeOpen(RIGHT, true);
00441                 }
00442                 Thread::Sleep(100);
00443         }
00444 }
00445 
00450 void RobotControl::setServosEnable(bool enabled)
00451 {
00452         try
00453         {
00454                 if (enabled) serial_Port->WriteLine("!ENABLE");
00455                 else serial_Port->WriteLine("!DISABLE");
00456         }
00457         catch(Exception^) {}
00458 }
00459 
00466 void RobotControl::showDisplay(String^ text)
00467 {
00468         try
00469         {
00470                 serial_Port->WriteLine(text);   
00471         }
00472         catch(Exception^) {};
00473 }
00474 
00486 void RobotControl::speechVisemeEvent(int streamNumber, System::Object^ streamPosition, int duration, SpeechLib::SpeechVisemeType nextVisemeID, SpeechLib::SpeechVisemeFeature feature, SpeechLib::SpeechVisemeType currentVisemeId)
00487 {
00488         if(isMouthMovement)
00489         {
00490                 int visemeType = (int)currentVisemeId;
00491 
00492                 if (visemeType > 0)
00493                 {
00494                         if (true) //(visemeCount >= 0)
00495                         {
00496                                 visemeCount = 0;
00497 
00498                                 if (visemeType == 21)   //m b p
00499                                 {
00500                                         setServoValue(mouth, 0, 1368);
00501                                         setServoValue(jaw,   0, 1412);
00502                                 }
00503                                 else if ((visemeType == 2) || (visemeType == 3) || (visemeType == 8)) //aa ao ow
00504                                 {
00505                                         setServoValue(mouth, 0, 1344);
00506                                         setServoValue(jaw,   0, 1678);
00507                                 }
00508                                 else if ((visemeType == 1) || (visemeType == 4) || (visemeType == 11) || (visemeType == 12)) //ih ae ah ay h
00509                                 {
00510                                         setServoValue(mouth, 0, 1456);
00511                                         setServoValue(jaw,   0, 1612);
00512                                 }
00513                                 else if (visemeType == 9) // aw
00514                                 {
00515                                         setServoValue(mouth, 0, 1122);
00516                                         setServoValue(jaw,   0, 1678);
00517                                 }
00518                                 else if ((visemeType == 15) || (visemeType == 16) || (visemeType == 17) || (visemeType == 19) || (visemeType == 20)) //n ng ch dh d g f k t z zh s sh
00519                                 {
00520                                         setServoValue(mouth, 0, 1522);
00521                                         setServoValue(jaw,   0, 1522);
00522                                 }
00523                                 else if (visemeType == 6) //ih ey y
00524                                 {
00525                                         setServoValue(mouth, 0, 1456);
00526                                         setServoValue(jaw,   0, 1612);
00527                                 }
00528                                 else if ((visemeType == 7) || (visemeType == 10)) //uw w oy
00529                                 {
00530                                         setServoValue(mouth, 0, 1412);
00531                                         setServoValue(jaw,   0, 1612);
00532                                 }
00533                                 else if (visemeType == 18) //f v
00534                                 {
00535                                         setServoValue(mouth, 0, 1368);
00536                                         setServoValue(jaw,   0, 1434);
00537                                 }
00538                                 else if (visemeType == 14) //l
00539                                 {
00540                                         setServoValue(mouth, 0, 1100);
00541                                         setServoValue(jaw,   0, 1590);
00542                                 }
00543                                 else if ((visemeType == 13) || (visemeType == 5)) //r er
00544                                 {
00545                                         setServoValue(mouth, 0, 1100);
00546                                         setServoValue(jaw,   0, 1456);
00547                                 }
00548                         }
00549                         visemeCount++;
00550                 }
00551                 else
00552                 {
00553                         setMouthSilentViseme();
00554                 }
00555 
00556         }
00557 }
00558 
00559 
00563 void RobotControl::setMouthSilentViseme(void)
00564 {
00565         setServoValue(jaw,   0, servos[jaw]->neutralValue );
00566         setServoValue(mouth, 0, servos[jaw]->neutralValue );
00567 }
00568 
00573 SpeechLib::_ISpeechVoiceEvents_VisemeEventHandler^ RobotControl::getVisemeEventHandler(void)
00574 {
00575         return spVisemeEventHandler;    
00576 }
00577 
00578 
00584 void RobotControl::initiateServoSettings(void)
00585 {
00586         servos = gcnew array<Servo^, 1>(16);
00587 
00588         for(int i=0;i<16;i++)
00589         {
00590                 servos[i] = gcnew Servo();
00591         }
00592         
00593         readServoSettings(); //from XML file
00594 
00595 }
00596 
00600 void RobotControl::initiateRobot(void)
00601 {
00602         for(int i = 0; i< 16; i++)
00603         {
00604                 System::String^ servo_name = servos[i]->servoName;
00605 
00606                 if( servo_name->CompareTo("") != 0)
00607                 {
00608                   setServoValue(i, 16, servos[i]->startValue);
00609                 }
00610         }
00611 }
00612 
00617 array<System::String^, 1>^ RobotControl::getComPortNames(void)
00618 {
00619         return serial_Port->GetPortNames();
00620 }
00621 
00629 System::String^ RobotControl::findComport(System::String^ start_port_for_search)
00630 {
00631         array<System::String^, 1>^ portnames = RobotControl::getComPortNames();
00632         System::Array::Sort(portnames);
00633         int l = portnames->Length+1;
00634 
00635         array<System::String^, 1>^ searchports = gcnew array<System::String^, 1>(l);
00636         searchports[0] = start_port_for_search;
00637         for (int i=0;i<(l-1);i++)
00638         {
00639                 searchports->SetValue(portnames->GetValue(i), i+1);
00640         }
00641 
00642 
00643         System::String^ pn;
00644         System::String^ ret = "";
00645 
00646         for(int i = 0; i < l; i++)
00647         {
00648                 try
00649                 {
00650                         serial_Port->BaudRate = 2400;
00651                         pn = (System::String^) searchports->GetValue(i);
00652                         serial_Port->PortName = pn;
00653                         
00654                         serial_port_received_data = "";
00655                         serial_Port->Open();
00656                         serial_Port->WriteLine("U!SCVER?");
00657                         Threading::Thread::Sleep(1500);
00658                         System::String^ answer = serial_port_received_data;
00659                         serial_Port->Close();
00660 
00661                         if(answer->Equals("U!SCVER?\r1.4"))
00662                         {
00663                                 ret = pn;
00664                                 break;
00665                         }
00666                 }
00667                 catch (Exception^ )
00668                 {
00669                         serial_Port->Close();
00670                 }
00671         }
00672         return ret;
00673 }
00674 
00680 void RobotControl::serialPort_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e)
00681 {
00682   try
00683   {
00684           if (serial_Port->IsOpen) serial_port_received_data += serial_Port->ReadExisting();  //serial_Port->ReadLine();
00685           if (serial_port_received_data->Length >= 255) serial_port_received_data = "";
00686   }
00687   catch (Exception^)
00688   {
00689           //nope!
00690   }
00691 }
00692 
00701 void RobotControl::setServoValue(int servo_nr, int servo_speed, int servo_value)
00702 {
00703         if ((servo_nr >= 0)&&(servo_nr < servos->Length))
00704         {
00705                 int value = servo_value / 2;
00706                 int value_h = (value & 0xFF00) / 256;
00707                 int value_l = (value & 0x00FF);
00708 
00709                 System::String^ sendString = "!SC" + Char(servo_nr) + Char(servo_speed) + Char(value_l) + Char(value_h);
00710 
00711                 try
00712                 {
00713                         serial_Port->WriteLine(sendString);
00714                 }
00715                 catch(Exception^) {}
00716                 servos[servo_nr]->currentValue = servo_value;
00717         }
00718 }
00719 
00724 System::String^ RobotControl::getComPortName(void)
00725 {
00726         return serial_Port->PortName;
00727 }
00728 
00736 System::String^ RobotControl::getServoDescription(int servoNumber)
00737 {
00738         if ((servoNumber >= 0) && (servoNumber < servos->Length))
00739         {
00740                 return servos[servoNumber]->servoName;
00741         }
00742         else return "";
00743 }
00744 
00750 int RobotControl::getServoMinValue(int servoNumber)
00751 {
00752         if ((servoNumber >= 0) && (servoNumber < servos->Length))
00753         {
00754                 return servos[servoNumber]->minValue;
00755         }
00756         else return -1;
00757 }
00758 
00764 int RobotControl::getServoMaxValue(int servoNumber)
00765 {
00766         if ((servoNumber >= 0) && (servoNumber < servos->Length))
00767         {
00768                 return servos[servoNumber]->maxValue;
00769         }
00770         else return -1;
00771 }
00772 
00778 int RobotControl::getServoStartValue(int servoNumber)
00779 {
00780         if ((servoNumber >= 0) && (servoNumber < servos->Length))
00781         {
00782                 return servos[servoNumber]->startValue;
00783         }
00784         else return -1;
00785 }
00786 
00792 void RobotControl::readServoSettings(void)
00793 {
00794         ear_l = -1;
00795         ear_r = -1;
00796         lid_l = -1;
00797         lid_r = -1;
00798         eye_l = -1;
00799         eye_r = -1;
00800         brows = -1;
00801         mouth = -1;
00802         jaw   = -1;
00803         turn  = -1;
00804         pitch = -1;
00805         
00806         XmlTextReader^ xmlTextReader = gcnew XmlTextReader("super-yano.xml");
00807 
00808         while (xmlTextReader->Read())
00809         {
00810                 if (xmlTextReader->NodeType == XmlNodeType::Element)
00811                 {
00812                         while (xmlTextReader->NodeType != XmlNodeType::EndElement)
00813                         {
00814                                 int servo_nr = -1;
00815                                 int start_value;
00816                                 int min_value;
00817                                 int max_value;
00818                                 System::String^ servo_name;
00819                                 if (xmlTextReader->Name->CompareTo("ear_l") == 0)
00820                                 {
00821                                         servo_name = "ear_l";
00822                                         try
00823                                         {
00824                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00825                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00826                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00827                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00828                                                 ear_l = servo_nr;
00829                                         }
00830                                         catch (Exception^ e)
00831                                         {
00832                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00833                                         }
00834                                 }
00835                                 else if (xmlTextReader->Name->CompareTo("ear_r") == 0)
00836                                 {
00837                                         servo_name = "ear_r";
00838                                         try
00839                                         {
00840                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00841                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00842                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00843                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00844                                                 ear_r = servo_nr;
00845                                         }
00846                                         catch (Exception^ e)
00847                                         {
00848                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00849                                         }
00850                                 }
00851                                 else if (xmlTextReader->Name->CompareTo("lid_l") == 0)
00852                                 {
00853                                         servo_name = "lid_l";
00854                                         try
00855                                         {
00856                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00857                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00858                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00859                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00860                                                 lid_l = servo_nr;
00861                                         }
00862                                         catch (Exception^ e)
00863                                         {
00864                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00865                                         }
00866                                 }
00867                                 else if (xmlTextReader->Name->CompareTo("lid_r") == 0)
00868                                 {
00869                                         servo_name = "lid_r";
00870                                         try
00871                                         {
00872                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00873                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00874                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00875                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00876                                                 lid_r = servo_nr;
00877                                         }
00878                                         catch (Exception^ e)
00879                                         {
00880                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00881                                         }
00882                                 }
00883                                 else if (xmlTextReader->Name->CompareTo("eye_l") == 0)
00884                                 {
00885                                         servo_name = "eye_l";
00886                                         try
00887                                         {
00888                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00889                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00890                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00891                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00892                                                 eye_l = servo_nr;
00893                                         }
00894                                         catch (Exception^ e)
00895                                         {
00896                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00897                                         }
00898                                 }
00899                                 else if (xmlTextReader->Name->CompareTo("eye_r") == 0)
00900                                 {
00901                                         servo_name = "eye_r";
00902                                         try
00903                                         {
00904                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00905                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00906                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00907                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00908                                                 eye_r = servo_nr;
00909                                         }
00910                                         catch (Exception^ e)
00911                                         {
00912                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00913                                         }
00914                                 }
00915                                 else if (xmlTextReader->Name->CompareTo("brows") == 0)
00916                                 {
00917                                         servo_name = "brows";
00918                                         try
00919                                         {
00920                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00921                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00922                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00923                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00924                                                 brows = servo_nr;
00925                                         }
00926                                         catch (Exception^ e)
00927                                         {
00928                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00929                                         }
00930                                 }
00931                                 else if (xmlTextReader->Name->CompareTo("mouth") == 0)
00932                                 {
00933                                         servo_name = "mouth";
00934                                         try
00935                                         {
00936                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00937                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00938                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00939                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00940                                                 mouth = servo_nr;
00941                                         }
00942                                         catch (Exception^ e)
00943                                         {
00944                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00945                                         }
00946                                 }
00947                                 else if (xmlTextReader->Name->CompareTo("jaw") == 0)
00948                                 {
00949                                         servo_name = "jaw";
00950                                         try
00951                                         {
00952                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00953                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00954                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00955                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00956                                                 jaw = servo_nr;
00957                                         }
00958                                         catch (Exception^ e)
00959                                         {
00960                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00961                                         }
00962                                 }
00963                                 else if (xmlTextReader->Name->CompareTo("turn") == 0)
00964                                 {
00965                                         servo_name = "turn";
00966                                         try
00967                                         {
00968                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00969                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00970                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00971                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00972                                                 turn = servo_nr;
00973                                         }
00974                                         catch (Exception^ e)
00975                                         {
00976                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00977                                         }
00978                                 }
00979                                 else if (xmlTextReader->Name->CompareTo("pitch") == 0)
00980                                 {
00981                                         servo_name = "pitch";
00982                                         try
00983                                         {
00984                                                 servo_nr = Convert::ToInt32( xmlTextReader->GetAttribute(0) );
00985                                                 start_value = Convert::ToInt32( xmlTextReader->GetAttribute(1) );
00986                                                 min_value = Convert::ToInt32( xmlTextReader->GetAttribute(2) );
00987                                                 max_value = Convert::ToInt32( xmlTextReader->GetAttribute(3) );
00988                                                 pitch = servo_nr;
00989                                         }
00990                                         catch (Exception^ e)
00991                                         {
00992                                                 Windows::Forms::MessageBox::Show( e->Message, "Fehler in XML-Eintrag \""+ servo_name +"\"", Windows::Forms::MessageBoxButtons::OK, Windows::Forms::MessageBoxIcon::Error);
00993                                         }
00994                                 }
00995 
00996                                 if (servo_nr > -1)
00997                                 {
00998                                         servos[servo_nr]->servoName = servo_name;
00999                                         servos[servo_nr]->minValue = min_value;
01000                                         servos[servo_nr]->maxValue = max_value;
01001                                         servos[servo_nr]->startValue = start_value;
01002                                         servos[servo_nr]->currentValue = start_value;
01003                                         servos[servo_nr]->neutralValue = start_value;
01004                                 }
01005 
01006                                 xmlTextReader->Read();
01007                         }
01008                 }
01009         }
01010         xmlTextReader->Close();
01011 }

Erzeugt am Mon Jun 4 16:39:20 2007 für Super-Yano von  doxygen 1.5.2