0
$\begingroup$

Just to give u a little background of what I am doing, I am currently programming my vehicle to wall-hug a maze. My idea is that my vehicle does a check at start whether or not it will move in clockwise or anti clockwise direction. The thing I am not so sure about is the placement of my flag in my codes. Some of my friends questioned why I have 2 flags in my code, this made me unsure of whether my codes/logic is right. Refer to my codes for more info. If it is wrong, let me know how I can correct it. Thanks.

 if( g_NavState == NAV_NONE ){ //if vehicle is not moving

    if(state.order.bLeft==TRUE){ //left sensor detects obstacle, moves clockwise.
        g_NavState = NAV_FRONT;
        g_MoveStateCmd = MOVE_FWD;
        flag1 = 1; //Unsure about this flag
    }
    else if(state.order.bRight==TRUE){ //right sensor detects obstacle, moves anti clockwise
        g_NavState = NAV_FRONT;
        g_MoveStateCmd = MOVE_FWD;
        flag2 = 1;//Unsure about this flag
    }
}
else if(g_NavState==NAV_FRONT){ //while going front
    if(state.order.bFrontLeft==FALSE && state.order.bLeft==FALSE && state.order.bBackLeft==FALSE && flag1 == 1){ 
       for(int iter=0;iter<1000000;iter++){
        } 
        g_NavState = NAV_FL;
        g_MoveStateCmd = MOVE_FRONT_LEFT;

    }
    else if(state.order.bFrontRight==FALSE && state.order.bRight==FALSE && state.order.bBackRight==FALSE && flag2 == 1){ /*Anti-Clockwise*/
       for(int iter=0;iter<1000000;iter++){
        }
        g_NavState = NAV_FR;
        g_MoveStateCmd = MOVE_FRONT_RIGHT;

    }
$\endgroup$
2
  • $\begingroup$ According to the posted code, the robotics behavior is programmed directly, similar to a behavior tree. Even if the language has a C syntax, it has much in common with ingame-scripting with lua-like languages. The flags in the code are perhaps part of the algorithm which should traverse the maze. The first thing to do is to separate the maze traversal from the low level behaviors. $\endgroup$ Apr 1, 2019 at 11:59
  • $\begingroup$ Hi @ManuelRodriguez The flags are indeed part of the algorithm that should traverse the maze. However, are the placement of my flags in the codes correct? Do I need 2 flags or can I just reduce to one? $\endgroup$
    – Coolo
    Apr 1, 2019 at 12:03

1 Answer 1

0
$\begingroup$

You can just use one flag, if the purpose of the flag is solely to indicate whether to turn clockwise or anti-clockwise. Name the flag turn_clockwise, make sure it's true when checking whether to turn clockwise, make sure it's false when checking whether to turn anti-clockwise.

You could also not have a flag and check the left or right sensor in the moment if that fits your application.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.