After checking main If condition unable to reach if and while



  • Hardware reading from scope Thread stored in beforeRightValue. Before that is coming inside void Motion::_Grid() car will stop 2 seconds. Sometimes values will not match so that time I want to calculate error value then I want to make repositioning.

      float degree_check; // calculating error value will store here.
    

    I have created 4 conditions. All condition will check first stored value from hardware (firstdataValue) and current value(beforeRightValue). If any difference happened that will calculate the error value (degree_check), then the car need to move same first degree depends upon the error degree.

    That is working fine. That is coming inside if condition. For example condition 1 if, condition 4 if printing. If that is coming inside of main if condition, this condition also true only, but that is not coming inside of this if condition, not inside of the while. I was unable to understand the mistake.

    void Motion::_Grid()
    {
      float beforeRightValue = ScopeThread::data; 
    
      if((int)beforeRightValue != (int)firstdataValue) 
        {
          float degree_check; // calculating error value will store here.
    
            if(firstdataValue > 0 && beforeRightValue > 0) 
            {
              degree_check = (firstdataValue)-(beforeRightValue); 
    
              if(degree_check > 0)
                {
                  cout<<"condition 1 if : "<<degree_check<<endl;
    
                  if(!beforeRightValue == firstYawValue) 
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
    		        cout<<"2nd while : "<<firstdataValue<<endl;
    
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(degree_check < 0)
                {
                   cout<<"condition 1 else : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"2nd while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
    
            else if(firstdataValue < 0 && beforeRightValue < 0)
            {
              degree_check = (firstdataValue) - (beforeRightValue);
    
              if(degree_check < 0)
                {
                  cout<<"condition 2 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"3rd while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(degree_check > 0)
                {
                    cout<<"condition 2 else : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"4th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
    
            else if(firstdataValue > 0 && beforeRightValue < 0)
            {
              degree_check = (firstdataValue) - (beforeRightValue);
    
              if(beforeRightValue < -90)
                {
                  cout<<"condition 3 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"5th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(beforeRightValue > -90 && beforeRightValue <= 0)
                {
                    cout<<"condition 3 else : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"6th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
    
            else if(firstdataValue < 0 && beforeRightValue > 0)
            {
              degree_check = (firstdataValue) - (beforeRightValue);
    
              if(beforeRightValue > 90)
                {
                    cout<<"condition 4 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"7th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(beforeRightValue < 90 && beforeRightValue >= 0)
                {
                  cout<<"condition 3 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"8th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
        }
    }
    

  • Lifetime Qt Champion

    Hi,

    I'd recommend splitting your function in smaller pieces. That will make it easier to understand. Your use of ScopeThread::data also isn't really clear.

    Since you are controlling hardware, did you consider using the state machine framework ? That would allow to more cleanly model your car behaviour.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.