Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved After checking main If condition unable to reach if and while

    Mobile and Embedded
    2
    2
    311
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ?
      A Former User last edited by

      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;
                          }
                      }
                  }
              }
          }
      }
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • First post
          Last post