View unanswered posts | View active topics It is currently Fri Nov 22, 2019 12:11 pm






Reply to topic  [ 6 posts ] 
Garbage Collector 
Author Message
Rookie

Joined: Mon Oct 05, 2015 5:03 pm
Posts: 4
Post Garbage Collector
Hello,

I am working on a program using ROBOTC for the ev3. The robot should identify obstacles inside of a rectangle that is outlined in black. When the robot spots an obstacle, the robot should push it out of the rectangle. It should repeat this process until no other obstacles are detected within the rectangle. If the color sensor detects the black border, the robot should turn and stay inside of the rectangle. I am having problems detecting obstacles with the sensor. Sometimes it picks them up, but will sometimes ignore them. In addition to this, I feel that my program is likely structured poorly. I have attempted to create this program several times, but I have encountered trouble with each iteration. I was hoping someone more knowledgeable with ROBOTC could take a quick look, and make suggestions on what I may be doing poorly in my code. Thanks in advance for any suggestions you may have.

Here is my current implementation:

Code:
tMotor LEFT_MOTOR = motorA;
tMotor RIGHT_MOTOR = motorD;

void turn(){
   setMotorSpeed(LEFT_MOTOR, 0);
   setMotorSpeed(RIGHT_MOTOR, 0);

   sleep(500);

   setMotorSpeed(LEFT_MOTOR, -15);
   setMotorSpeed(RIGHT_MOTOR, -15);

   sleep(1000);

   setMotorSpeed(LEFT_MOTOR, 15);
   setMotorSpeed(RIGHT_MOTOR, -15);

   sleep(1500);

   setMotorSpeed(LEFT_MOTOR, 15);
   setMotorSpeed(RIGHT_MOTOR, 15);

   sleep(1000);
}

task main()
{
   bool objectFound = false;
   bool lineFound = false;

   while(true){
      setMotorSpeed(LEFT_MOTOR, 0);
      setMotorSpeed(RIGHT_MOTOR, 0);

      // Turn in increments until object is found or timer reaches 10 seconds.
      time1[T1] = 0;
      while(objectFound == false && time1[T1] < 10000){
         setMotorSpeed(LEFT_MOTOR, 15);
         setMotorSpeed(RIGHT_MOTOR, -15);
         sleep(1000)

         setMotorSpeed(LEFT_MOTOR, 0);
         setMotorSpeed(RIGHT_MOTOR, 0);

         if(getUSDistance(distanceCM) < 45)
            objectFound = true;

      }

      //Move forward until line is detected or timer reaches 10 seconds.
      time1[T1] = 0;
      while(lineFound == false && time1[T1] < 10000){
         setMotorSpeed(LEFT_MOTOR, 15);
         setMotorSpeed(RIGHT_MOTOR, 15);   

         if(SensorRaw[S1] < 25)
            lineFound = true;
      }

      turn();
            objectFound = false;
            lineFound = false;
   }
}


Mon Oct 05, 2015 5:15 pm
Profile
Novice

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
Post Re: Garbage Collector
Code:
 while(objectFound == false && time1[T1] < 10000){
         setMotorSpeed(LEFT_MOTOR, 15);
         setMotorSpeed(RIGHT_MOTOR, -15);
         sleep(1000)

         setMotorSpeed(LEFT_MOTOR, 0);
         setMotorSpeed(RIGHT_MOTOR, 0);

         if(getUSDistance(distanceCM) < 45)
            objectFound = true;

      }

in this piece of code you are looking for the object with your eyes closed. While turning the us sensor is ignored. Only when you stop you open your eyes and see if there is something exact in front of you. if not you close your eyes again and turn further.
I suggest to keep your eyes open while turning and stop turning when you spot someting.
Code:
start motors
while(distance >45 and timer is <10000)
{wait 1 msec}
stop motors
if (distance is <45 )
 object found=true
else
 timeout



Wed Oct 07, 2015 6:29 am
Profile
Rookie

Joined: Mon Oct 05, 2015 5:03 pm
Posts: 4
Post Re: Garbage Collector
Thanks for you suggestion nxt! I now see what you are saying when you mentioned that I have my eyes closed when I look for the object. With the way you suggested your pseudo code though, where would the motors start again after the while loop is entered as the bot continues to make turns, continually looking for the object.

Additionally, is there still a need to use boolean variables if I set the conditionals for my first while loop to check for distance > x ? I reworked the code based on what I interpreted from your post. I do not have much time each week to actually use the robots so I will not be able to test anything until later.


The bot should now be aiming to turn in a circle while nothing is detected. If the sensor detects something or the timer runs out it will move forward until it reaches a boundary. When a boundary is reached it will turn back into the box. From here, starting to attempt to detect new obstacles. The goal is that it if it detects something in range with US, it will move forward until the object is pushed out of the arena. I have installed a large bumper on the front of the bot to push obstacles.

As a sidenote, is there a function to call that will command the bot to power down? I did not see any in my documentation.

Code:
task main()
{
   while(true){

      // Turn in increments until object is found or timer reaches 10 seconds.
      time1[T1] = 0;
      while(getUSDistance(distanceCM) > 30 && time1[T1] < 10000){
         setMotorSpeed(LEFT_MOTOR, 15);
         setMotorSpeed(RIGHT_MOTOR, -15);

         wait(1)

         setMotorSpeed(LEFT_MOTOR, 0);
         setMotorSpeed(RIGHT_MOTOR, 0);
      }

      //Move forward until line is detected
      while(SensorRaw[S1] > 20){
         setMotorSpeed(LEFT_MOTOR, 15);
         setMotorSpeed(RIGHT_MOTOR, 15);   
      }

      //Execute a 180 degree turn.
      turn();
   }
}


Wed Oct 07, 2015 8:20 am
Profile
Novice

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
Post Re: Garbage Collector
with this code you are actualy doing exactly the same thing, only smaller steps so more chance your sensor will see something.
Stopping a motor 1 ms after starting it, will probably result in not turning the motor at all. (it will stop before it actually rotated).

You can start the motors Before you enter the while loop. Stay in the while loop until sensor found something.
exit while loop. stop motors

Quote:
As a sidenote, is there a function to call that will command the bot to power down? I did not see any in my documentation.

Code:
powerOff();

Should do the trick. It is under battery and power control.


Wed Oct 07, 2015 9:57 am
Profile
Rookie

Joined: Mon Oct 05, 2015 5:03 pm
Posts: 4
Post Re: Garbage Collector
Ok I think I understand. So there is no need to stop the motors during the while loop to check the sensors (only stop the motors after the while loop)? I had it written that way before because I was unsure of the accuracy of the sensors while spinning, and if the robot would stop fast enough that it would still be lined up with the detected object.

If I start the motors before the while loop, and I stop the motors after the while loop, what do I want the robot to do during the while loop? If an object is detected, it should break out of the while loop. Because of this I shouldn't need an if conditional to check the distance correct?

Code:
task main()
{
   while(true){

      setMotorSpeed(LEFT_MOTOR, 15);
      setMotorSpeed(RIGHT_MOTOR, -15);
      
      // Turn in increments until object is found or timer reaches 10 seconds.
      time1[T1] = 0;
      while(getUSDistance(distanceCM) > 30 && time1[T1] < 10000){
         //What am I doing here?  If an object is detected, it should break out of the while loop.
      }

      setMotorSpeed(LEFT_MOTOR, 0);
      setMotorSpeed(RIGHT_MOTOR, 0);

      //Move forward until line is detected
      while(SensorRaw[S1] > 20){
         setMotorSpeed(LEFT_MOTOR, 15);
         setMotorSpeed(RIGHT_MOTOR, 15);   
      }

      //Execute a 180 degree turn.
      turn();
   }
}


Wed Oct 07, 2015 10:39 am
Profile
Novice

Joined: Sat Apr 12, 2008 11:09 am
Posts: 60
Location: holland
Post Re: Garbage Collector
You do not need to do anything in the while loop.
Although it is a good idea to put a delay in it
Code:
while( your check)
{
  delay(1);
}


If the us sensor distance is not anymore > 30 the test in the while loop will fail and you break out of the loop.


Wed Oct 07, 2015 10:49 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 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.