View unanswered posts | View active topics It is currently Fri Aug 29, 2014 12:14 pm






Reply to topic  [ 2 posts ] 
having trouble with sonar not updating 
Author Message
Rookie

Joined: Tue Oct 29, 2013 11:13 am
Posts: 12
Post having trouble with sonar not updating
I'm attempting to code a tetrix sumo bot. The hoipe is that itll search for opponent by making a small left turn, then a small right turn, and if the opponent is not seen, then increasing the turns and sweeping again. Im trying to perform these multiple turns in a while loop, and at the same time check for ultra sonic value and quit the loop as soon as an object is seen, but the sonar only seems to update at the beginning of the loops.

Code:
  while(true)
    {
    while(SensorValue[sonar] > 40) // search for opponent  **I want the robot to quit this loop as soon as sonar < 40**
        {
        resetEncoders(); //turn 1: checking if opponent is left
        while (abs(nMotorEncoder[motorE] - nMotorEncoder[motorD])/2 < abs(x))
          {
          if (SensorValue[sonar] <= 40)
            break;
          motor[motorE] = -50;
          motor[motorD] = 50;
          wait1Msec(50);
          }

        resetEncoders(); //turn 2: checking if opponent is right
        while (abs(nMotorEncoder[motorE] - nMotorEncoder[motorD])/2 < abs(x*2))
          {
          if (SensorValue[sonar] <= 40)
            break;
          motor[motorE] = 50;
          motor[motorD] = -50;
          wait1Msec(50);
          }

        resetEncoders(); //turn 3: moving back to original position
        while (abs(nMotorEncoder[motorE] - nMotorEncoder[motorD])/2 < abs(x))
          {
          if (SensorValue[sonar] <= 40)
            break;
          motor[motorE] = -50;
          motor[motorD] = 50;
          wait1Msec(50);
          }

        x+=480; //increase the value of each turn for a larger sweep

        }

    while(SensorValue(sonar) < 40) // bot has seen opponent, so attack
       {
       motor[motorD] = -100;
       motor[motorE] = -100;
       }

    wait10Msec(100);
    }


Tue Dec 10, 2013 11:52 am
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: having trouble with sonar not updating
Hi kevtastic,
The sonar check is only happening at the beginning of each loop because that's the only place where you've programmed the robot to do the check.

If you would like the check to happen continuously while the search behavior is also happening, the simplest approach would probably be to have two tasks running simultaneously, one checking the sonar and the other doing the search behavior. When the sonar task finds the opponent, it stops the search task.

The other approach would be to include the sonar condition in each of the internal while loops as well, so they only continue if they haven't completed the movement and the opponent hasn't been found. That way, if the opponent is located, any of the movement behaviors will be immediately stopped, and the others will be skipped until the end of the outer loop, which will also detect the presence of the opponent, and exit the entire behavior.

Cheers,
--Ryan

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Tue Dec 10, 2013 2:39 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.