Will camera keep running if i have a while statement going

I have a while statement in Default_Routine that starts when a button is pressed, keeps moving the drive motors until the target is strait ahead of the robot (the pan servo = 127), and then leaves the while loop.
Now here is my question…
during the while loop will the camera keep tracking the target because it is interupt driven or will the while loop overide the camera’s tracking interupts and just get me stuck in the loop because the camera wont update.

i am trying to have my robot aim with it’s drive wheels when i press a button

you cannot use while or for loops in the RC code.

The main loop of the code does something like this:

  1. read all the sensor and OI inputs (including the camera)
  2. execute all your code
  3. update all the output (to the motors and servos…)
    goto 1

as you can see, a while loop in step 2 will hold up the entire program, and stop any further inputs or outputs

to make matters worse, there is another processor in the RC that you do not have access to. If it sees the code is not updating the outputs at the normal rate it assumes your code has gone out to lunch, and it shuts the robot down.

you need to use IF statements to check and see if the condition you are looking for is true. If not then let the code continue, and it will check again on its next loop through.

Well, you can use while or for loops - looping over an array is a good use, for example.

i am just going to post what i have mabye someone can tell me haow i would do this without stopping my entire program. This is my first year programming for first and i have promised my team some nice “features”. any help would be greatly appreciated



  if (p1_sw_trig == 1)
  {  
    Switch1_LED = 1        // aiming light turns on when aiming
    locked = 0
	while (locked == 0)
    {
      if (PAN_SERVO > PAN_CENTER_PWM_DEFAULT + 20)
      {
         pwm01 = 127 + aim_gain
         pwm02 = 127 + aim_gain
      } 
      else if (PAN_SERVO < PAN_CENTER_PWM_DEFAULT - 20)
      {
         pwm01 = 127 - aim_gain
         pwm02 = 127 - aim_gain
      } 
	  else if (PAN_SERVO < PAN_CENTER_PWM_DEFAULT + 21 & PAN_SERVO > PAN_CENTER_PWM_DEFAULT)
      {
        pwm01 = 127 + aim_soft_gain
        pwm02 = 127 + aim_soft_gain
      } 
	  else if (PAN_SERVO > PAN_CENTER_PWM_DEFAULT - 21 & PAN_SERVO < PAN_CENTER_PWM_DEFAULT)
      {
        pwm01 = 127 - aim_soft_gain
        pwm02 = 127 - aim_soft_gain
      } 
      else 
      {
        Switch1_LED = 0  // really cool light scrolling that i had to put somewhere in the code
        Switch2_LED = 0
        Switch3_LED = 0
        Switch1_LED = 1
        Switch1_LED = 0
        Switch2_LED = 1
        Switch2_LED = 0
        Switch3_LED = 1
        Switch3_LED = 0
        Switch2_LED = 1
        Switch2_LED = 0
        Switch1_LED = 1
        Switch1_LED = 0
		printf("\TARGET ACQUIRED");
        locked = 1
      }
    }
  }


yes, if you have a while loop that executes quickly, and that is not looking for a change in inputs, esp if its trying to change the outputs (motor speed) to make the inputs change.

using while loops to process data will also have the adverse effect of altering your code-loop time.

whats going to happen with your code, as soon as

WHILE locked = 0

becomes true, your while loop becomes an infinite loop - it never exits, so your code will never see new input values from the operator interface - it will act like the trigger never gets released

but the supervisor uC will shut the robot down within a second or two anyway, and your bot will act like its been disabled.

Also: your light scrolling- the code needs to flow though to the end before any outputs get updated, so the only output that would happen would be the last values you assign to each output variable.

wouldn’t the last line in the final else statement break it out of the loop once it centers on the traget (the locked = 1). of course then i would still have the “Supervisor Uc” thing yall keep talking about to worry about so i guess it wouldn’t work anyway…(not too mention the camera wont work in the loop Sorry for my confusion. I have programmed in a lot of other langauges besides C . I am just new to C and still trying to understand some of the stuff.

What if i put this code into the beginning of the while loop


Getdata(&rxdata);
Tracking_Info_Terminal();
Camera_Handler();
Servo_Track();
Putdata(&txdata);

also if i add a counter to 1000 and when it reached it it would run the program once but before it did it set a variable that had an if statement near the beginning of the run (but after “Supervisor Uc” checked) that put it back into the while loop. I hope i am not being to vague

it would almost be like making a super high priority interupt

Remove your while statement. Why? Because your code is already running inside of a while loop.

The loop your code runs inside of runs at 26.2ms per loop.

Setting pwms and LEDs does not immediately change the actual hardware. That occurs later in the loop by the default code after your code is left. So making your light scrolling work needs delays between setting successive lights.

It’s not really a C question, it’s about how the default code is set up (that you don’t normally see).

so how would i do this without a while loop???
I understand what you are saying now…
i just dont understand how i can fix it.
if anyone could point me in the right direction or mabye even give me a little default code it would be greatly appreciated. I just dont understand how it would keep the tracking proccess going in the next loop of the “master while statement” but not have it happen every loop. I need it to only hapeen after the trigger is pressed and keep tracking till it is locked on

Servo_track() is the function which does the camera tracking. In your section of code you can set a global variable to turn camera tracking on/off. Then either add checking that variable before the call to Servo_Track() or add the check inside of Servo_Track() itself.

How about instead of putting the if statements inside a while(locked==0), put them inside a giant if(locked==0)?

I think you should be able to make your “locked” variable a static and change your “while” to an “if”. As long as the trigger is being held, this code will be executed every time through the main loop. (I think this is approximately what DHoizner was suggesting.)

static int locked = 0;
static int prev_trig = 0;

  if (p1_sw_trig == 1)
  {  
    if (prev_trig == 0) locked = 0; // Clear flag if trigger just pulled.

    Switch1_LED = 1        // aiming light turns on when aiming
    if (locked == 0)
    {
      if (PAN_SERVO > PAN_CENTER_PWM_DEFAULT + 20)
      {
         pwm01 = 127 + aim_gain
         pwm02 = 127 + aim_gain
      } 
      else if (PAN_SERVO < PAN_CENTER_PWM_DEFAULT - 20)
      {
         pwm01 = 127 - aim_gain
         pwm02 = 127 - aim_gain
      } 
	  else if (PAN_SERVO < PAN_CENTER_PWM_DEFAULT + 21 & PAN_SERVO > PAN_CENTER_PWM_DEFAULT)
      {
        pwm01 = 127 + aim_soft_gain
        pwm02 = 127 + aim_soft_gain
      } 
	  else if (PAN_SERVO > PAN_CENTER_PWM_DEFAULT - 21 & PAN_SERVO < PAN_CENTER_PWM_DEFAULT)
      {
        pwm01 = 127 - aim_soft_gain
        pwm02 = 127 - aim_soft_gain
      } 
      else 
      {
        Switch1_LED = 0  // really cool light scrolling that i had to put somewhere in the code
        Switch2_LED = 0
        Switch3_LED = 0
        Switch1_LED = 1
        Switch1_LED = 0
        Switch2_LED = 1
        Switch2_LED = 0
        Switch3_LED = 1
        Switch3_LED = 0
        Switch2_LED = 1
        Switch2_LED = 0
        Switch1_LED = 1
        Switch1_LED = 0
		printf("TARGET ACQUIRED");
        locked = 1
      }
    }
  }
  prev_trig = p1_sw_trig;

Oops, Dan and Greg are right. I stand corrected. :slight_smile:

thanks to all the people who helped explain this too me. Yall have no idea how much this helped me. Everything just clicked in my head. I get it now. thanks again.