View unanswered posts | View active topics It is currently Wed Aug 27, 2014 9:46 am






Reply to topic  [ 8 posts ] 
Counting with Ultrasonic 
Author Message
Rookie

Joined: Thu May 17, 2012 3:35 pm
Posts: 35
Post Counting with Ultrasonic
Hello
I want to do my Robot should count objekts and then do something.

Image

This is the Code:

Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)

#include "hitechnic-sensormux.h"
#include "lego-ultrasound.h"

const tMUXSensor LEGOUSL = msensor_S2_2; //ultra L

task main()
{
int countValue = 0;
int wert1;
int distL = 0;   // sonar L


wert1 = 30;

while(true)
   {
eraseDisplay();
distL = USreadDist(LEGOUSL);
ClearTimer(T1);
 //driving forward

while (time1[T1] < 3000)
      {
if (distL < wert1)  //   30 cm..
       {
    countValue = countValue + 1;
    nxtDisplayTextLine(4, "Zahl:  %d", countValue); //Display Counter
    wait1Msec(500);
       }
      else
        {
       //do nothing
       }
   nxtDisplayTextLine(2, "LinkUltra:  %d", distL); //Display Ultrasonic left
   wait1Msec(500);
   
   if (countValue > 5) //5 Reihen - Objekte
      {
     //go right motors.....
      }
    else
        {
       //do nothing
       }
      }
   }
}


But the counter counts 2 numbers at once. I don´t know.....where is the mistake?


Fri Nov 01, 2013 12:20 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Counting with Ultrasonic
First, you need to indent your code (like below). It makes it easier to spot problems. If that's your actual code, I can't find any line that moves the robot. In other words, the robot just sits there. If it happens to sit in front of the obstacle that's less than 30cm away, it will keeping counting up one every second until 3 seconds past and then erase the display, read a new distance and do another 3 seconds while continue to count up.

Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)

#include "hitechnic-sensormux.h"
#include "lego-ultrasound.h"

const tMUXSensor LEGOUSL = msensor_S2_2; //ultra L

task main()
{
    int countValue = 0;
    int wert1;
    int distL = 0;   // sonar L


    wert1 = 30;

    while(true)
    {
        eraseDisplay();
        distL = USreadDist(LEGOUSL);
        ClearTimer(T1);
        //driving forward

        while (time1[T1] < 3000)
        {
            if (distL < wert1)  //   30 cm..
            {
                countValue = countValue + 1;
                nxtDisplayTextLine(4, "Zahl:  %d", countValue); //Display Counter
                wait1Msec(500);
            }
            else
            {
                //do nothing
            }
            nxtDisplayTextLine(2, "LinkUltra:  %d", distL); //Display Ultrasonic left
            wait1Msec(500);

            if (countValue > 5) //5 Reihen - Objekte
            {
                //go right motors.....
            }
            else
            {
                //do nothing
            }
        }
    }
}


Fri Nov 01, 2013 3:15 pm
Profile
Rookie

Joined: Thu May 17, 2012 3:35 pm
Posts: 35
Post Re: Counting with Ultrasonic
Sorry

Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)

#include "hitechnic-sensormux.h"
#include "lego-ultrasound.h"

const tMUXSensor LEGOUSL = msensor_S2_2; //ultra L

task main()
{
    int countValue = 0;
    int wert1;
    int distL = 0;   // sonar L


    wert1 = 30;

    while(true)
    {
        eraseDisplay();
        distL = USreadDist(LEGOUSL);
        ClearTimer(T1);

        //driving forward
       motor[motorD] = 75;   
       motor[motorE] = 75;   

        while (time1[T1] < 3000)
        {
            if (distL < wert1)  //   30 cm..
            {
                countValue = countValue + 1;
                nxtDisplayTextLine(4, "Zahl:  %d", countValue); //Display Counter
                wait1Msec(500);
            }
            else
            {
                //do nothing
            }
            nxtDisplayTextLine(2, "LinkUltra:  %d", distL); //Display Ultrasonic left
            wait1Msec(500);

            if (countValue > 5) //5 Reihen - Objekte
            {
                  motor[motorD] = 0;   
                  motor[motorE] = 100;   
                  wait1Msec(2000);
            }
            else
            {
                //do nothing
            }
        }
    }
}


Fri Nov 01, 2013 3:52 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Counting with Ultrasonic
Okay, let's list the goals you want to achieve.
1. move the robot forward at a certain speed.
2. while moving, examine the value of the ultrasonic sensor pointing to the right.
3. if there is an obstacle less than 30cm away, increment the count.
4. if we have counted 5 obstacles, we have completed the mission.
5. Then what? Turn right and stop? Or turn right and drive for another 2 seconds?

Once you have goals listed. Start to translate the goals into code. For example:
1. move the robot forward at a certain speed.
Code:
task main()
{
    while (true)
    {
        motor[motorD] = 75;
        motor[motorE] = 75;
        wait1Msec(100);
    }
}

Once you have that working, move on to the next goal.
2. while moving, examine the value of the ultrasonic sensor pointing to the right.
Code:
task main()
{
    int distL = 0;

    while (true)
    {
        motor[motorD] = 75;
        motor[motorE] = 75;
        distL = USreadDist(LEGOUSL);
        nxtDisplayTextLine(2, "LinkUltra:  %d", distL); //Display Ultrasonic left
        wait1Msec(100);
    }
}

Now you have a moving robot that will display the distance of any obstacles to the right. The next goal is to count them. Let's stop for a moment and think about how to do that. Would it work if you increment the count when the sensor reads less than 30cm? No it won't. Why? Because the obstacle may be wide and it takes time for the robot to move past it. Before the robot is able to move past it, the sensor will give you a value < 30, so every 100 msec, the code will increment 1. Depending on the speed of the robot, it may count a lot before the robot past the obstacle and give you a value > 30. Now your original code did 2 waits for 500 msec each. So you won't count until a second later. This is like having the robot closes its eyes for one second while moving and then have it look again for obstacles. Depending on the speed of the robot and how wide the obstacle is, you are either still not quite clear the first obstacle therefore counting the same obstacle again or you may have skipped the second obstacle altogether and miss one. So that's not a reliable way to count. One way to do it reliably is to detect the edge of the obstacle. For example, you are moving along with a sensor reading > 30. If all the sudden you have a reading < 30, you have crossed the edge of the obstacle (the entering edge). You may increment the count by one. If you make a note of the fact that you have detected an obstacle then you should not count any more until the sensor reading is > 30 again meaning that it has detected the other edge of the obstacle (the exiting edge). Here is a translation of the algorithm we described here.
Code:
task main()
{
    int distL = 0;
    bool detectedObject = false;
    int countValue = 0;

    while (true)
    {
        motor[motorD] = 75;
        motor[motorE] = 75;
        distL = USreadDist(LEGOUSL);
        nxtDisplayTextLine(2, "LinkUltra:  %d", distL); //Display Ultrasonic left
        if (!detectedObject && distL <= 30)
        {
            // detected the entering edge of the obstacle.
            detectedObject = true;
            countValue++;
        }
        else if (detectedObject && distL > 30)
        {
            // detected the exiting edge of the obstacle.
            detectedObject = false;
        }
        nxtDisplayTextLine(4, "Zahl:  %d", countValue); //Display Counter
        wait1Msec(100);
    }
}

I will leave the rest of the goals for you to finish. Post your finished code.


Fri Nov 01, 2013 4:54 pm
Profile
Rookie

Joined: Thu May 17, 2012 3:35 pm
Posts: 35
Post Re: Counting with Ultrasonic
Hello

Thank you so much, you´re great. I installed with a right turn (void goRight()) after the program has counted 4 properties (while (countValue < 5)). goRight will start when the program counts 4.

Greetings from Germany!


Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)

#include "hitechnic-sensormux.h"
#include "lego-ultrasound.h"

const tMUXSensor LEGOUSL = msensor_S2_2; //ultra L

void goRight()
{
motor[motorD] = 0;
motor[motorE] = 75;   
wait1Msec(1000);
//here comes the next StartTask to go forward
}

task main()
{
    int distL = 0;
    bool detectedObject = false;
    int countValue = 0;

    while (countValue < 5)
    {
        motor[motorD] = 75;
        motor[motorE] = 75;
        distL = USreadDist(LEGOUSL);
        nxtDisplayTextLine(2, "LinkUltra:  %d", distL); //Display Ultrasonic left
        if (!detectedObject && distL <= 30)
        {
            // detected the entering edge of the obstacle.
            detectedObject = true;
            countValue++;
        }
        else if (detectedObject && distL > 30)
        {
            // detected the exiting edge of the obstacle.
            detectedObject = false;
        }
        nxtDisplayTextLine(4, "Zahl:  %d", countValue); //Display Counter
        wait1Msec(100);
           
    }
    goRight();
}


Sat Nov 02, 2013 5:08 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Counting with Ultrasonic
That's certainly one way to do it. BTW, your code is actually counting 5 obstacles, not 4. Think carefully.
Also, your code doesn't show motorD and motorE in the pragmas. Are you sure you have tested the code and it worked? Mindstorms has only 3 motor ports motorA, B, and C. Unless you are using Textrix/Matrix motors, you don't have motorD and E. Also, which motor is the left wheel?


Sat Nov 02, 2013 7:07 am
Profile
Rookie

Joined: Thu May 17, 2012 3:35 pm
Posts: 35
Post Re: Counting with Ultrasonic
Hi

I use Tetrix.
It´s better to use this:
Code:
   
..................
   int n;
   
    n = 4; //Number of objects

    while (countValue < n)
    {
..........

After the counter I have built a wait command, so that the program not be too quick counts at different size of the objects

Code:
 
..............
           countValue++;
          wait1Msec(800);
...................



It works. Great! Thank you so much!!!


Sat Nov 02, 2013 9:05 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Counting with Ultrasonic
If you add a delay when detecting the entering edge, it is like having the robot to close its eyes again. It will affect not just the last obstacle but every obstacle. A better way to do it is to add code after detecting the exiting edge and then compare whether you already have counted 4 obstacles. If you do, then exit the loop. For example:
Code:

#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)

#include "hitechnic-sensormux.h"
#include "lego-ultrasound.h"

const tMUXSensor LEGOUSL = msensor_S2_2; //ultra L

void goRight()
{
motor[motorD] = 0;
motor[motorE] = 75;   
wait1Msec(1000);
//here comes the next StartTask to go forward
}

task main()
{
    int distL = 0;
    bool detectedObject = false;
    int countValue = 0;

    while (true)
    {
        motor[motorD] = 75;
        motor[motorE] = 75;
        distL = USreadDist(LEGOUSL);
        nxtDisplayTextLine(2, "LinkUltra:  %d", distL); //Display Ultrasonic left
        if (!detectedObject && distL <= 30)
        {
            // detected the entering edge of the obstacle.
            detectedObject = true;
            countValue++;
        }
        else if (detectedObject && distL > 30)
        {
            // detected the exiting edge of the obstacle.
            detectedObject = false;
            if (countValue == 4)
            {
                break;
            }
        }
        nxtDisplayTextLine(4, "Zahl:  %d", countValue); //Display Counter
        wait1Msec(100);
    }
    goRight();
}


Sat Nov 02, 2013 5:18 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: JohnWatson 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.