Guest User

Untitled

a guest
Oct 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. void WorkerThreadBase::run()
  2. {
  3.     if(!spineSerialComm_.IsOpen() && !spineSerialComm_.Open(SerialPortNumber))
  4.     {
  5.         state_ = NO_SPINE;
  6.         emit workerThreadStateChanged(state_);
  7.  
  8.         return;
  9.     }
  10.  
  11.     state_ = SPINE_OK;
  12.     emit workerThreadStateChanged(state_);
  13.  
  14.     algorithmRunning_   = false;
  15.     startAlgorithm_     = false;
  16.  
  17.     bool prevStartButtonState = false;
  18.  
  19.     do
  20.     {
  21.         spineSerialComm_.Communicate();
  22.  
  23.         newSpineData_ = spineSerialComm_.HasNewData();
  24.  
  25.         if(newSpineData_)
  26.         {
  27.             spineSerialComm_.GetData(&spineData_);
  28.             spineSerialComm_.ClearNewData();
  29.         }      
  30.  
  31.         //
  32.         // Detects algorithm start command from WorkerThreadBase::StartAlgorithm()
  33.         // or rising edge of spineData.GetStartButton()
  34.         //     
  35.         if(!algorithmRunning_ && (startAlgorithm_ || (!prevStartButtonState && spineData_.GetStartButton())))
  36.         {
  37.             prevStartButtonState= spineData_.GetStartButton();
  38.             algorithmRunning_   = true;
  39.             startAlgorithm_     = false;
  40.  
  41.             if(imageProcessor_ != NULL)
  42.             {
  43.                 connect(imageProcessor_, SIGNAL(NewVisionDataReady(VisionData*)), SLOT(newVisionDataReady(VisionData*)));
  44.             }
  45.  
  46.             emit workerThreadStateChanged(ALGORITHM_RUNNING);
  47.  
  48.             algorithm_->Init();
  49.             algorithm_->SetAttackGoal(spineData_.GetAttackGoal());
  50.         }
  51.  
  52.         //
  53.         // Detects algorithm stop from WorkerThreadBase::StopAlgorithm()
  54.         // or falling edge of spineData.GetStartButton()
  55.         //
  56.         if(algorithmRunning_ && (prevStartButtonState && !spineData_.GetStartButton()))
  57.         {
  58.             prevStartButtonState    = spineData_.GetStartButton();
  59.             algorithmRunning_       = false;
  60.             spineCmd_.Stop();
  61.         }
  62.  
  63.  
  64.         if(algorithmRunning_)
  65.         {
  66.             //
  67.             // Do Algorithm::Execute() only if new data
  68.             //
  69.             if(newSpineData_ || newVisionData_)
  70.             {
  71.                 bool stateChanged   = algorithm_->Execute(0, newVisionData_, newSpineData_, &visionData_, &spineData_);
  72.                
  73.                 if(stateChanged)
  74.                 {
  75.                     emit algorithmStateChanged(algorithm_->GetState(), (char*)algorithm_->GetStateName());
  76.                 }
  77.             }
  78.  
  79.             newVisionData_  = false;
  80.             newSpineData_   = false;
  81.         }
  82.  
  83.         //
  84.         // emit requested speed (to UI?)
  85.         //
  86.         if(spineCmd_.NewCommands())
  87.         {
  88.             emit speedCmdChanged(spineCmd_.GetAbsSpeed(), spineCmd_.GetRadAngle(), spineCmd_.GetRadTurnRate());
  89.         }
  90.  
  91.         //
  92.         // Send requested speed to motor controller
  93.         //
  94.         if(spineCmd_.NewCommands() && !spineSerialComm_.SendControllerCommands(&spineCmd_))
  95.         {
  96.             break;
  97.         }
  98.  
  99.         spineCmd_.ClearCommands();
  100.     }
  101.     while(spineSerialComm_.IsOpen() && !threadStopCmd_);
  102.  
  103.     algorithmRunning_   = false;
  104.     state_              = NO_SPINE;
  105.  
  106.     if(spineSerialComm_.IsOpen())
  107.     {
  108.         spineCmd_.Stop();
  109.         spineSerialComm_.SendControllerCommands(&spineCmd_);
  110.     }
  111.  
  112.     emit workerThreadStateChanged(state_);
  113.  
  114.     if(imageProcessor_ != NULL)
  115.     {
  116.         disconnect(imageProcessor_, SIGNAL(NewVisionDataReady(VisionData*)), this, SLOT(newVisionDataReady(VisionData*)));
  117.     }
  118. }
Add Comment
Please, Sign In to add comment