ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

identification the location of objects

asked 2018-07-10 05:14:40 -0600

arya gravatar image

updated 2018-07-10 07:22:34 -0600

Hello dears, i am new in ROS and i have tried to detect objects by laser fortunately i could do some things, i mean if i want to find objects in Left, Right or Front separately, i am done, but when i am going to mix all, consider each of them as a function, unfortunately just one of them is working. may i ask you please take look at my code and help me. (my question find location of the object)

 int check_Left()
{

    double total_index = (laser.angle_max + abs(laser.angle_min))/laser.angle_increment;

    for (int i =680 ; i < total_index  ; i++)
        {
            if( laser.ranges[i] < 8)
            {
                state = 9;
            }
            else
            {
                state =0;
                break;
            }
        }

    return state;
}




int check_Right()
{   for (int i =0 ; i < 50 ; i++)
        {
            if( laser.ranges[i] < 9)
            {
                state = 3;
            }
            else
            {
                state =0;
                break;
            }
        }

    return state;
}



int check_fornt()
{

    for (int i = 320; i < 400 ; i++)
    {
        if( laser.ranges[i] < 9)
        {

            state = 12;
        }
         else
        {
            state = 0;
        }
    }

    return state;
}





void scanCallback(const sensor_msgs::LaserScan scan)
{

    laser = scan;

     state = check_Right();
     Duration(0.25).sleep();
     state = check_Left();

     Duration(0.25).sleep();
     state = check_fornt();
     Duration(0.25).sleep();
     cout << state << endl;
    /* switch (state) {
        case 12:
            cout << "object is in Front"<< endl;
            break;
        case 3:
            cout << "object is in Right"<< endl;
            break;
        case 9:
            cout << "object is in Left"<< endl;
            break;

    }
*/

}




 int main(int argc, char **argv)
 {
     init(argc ,argv , "Obstacle_avoidance");
     NodeHandle n;
     Com_sub = n.subscribe("scan",10,scanCallback);
     Com_pub =n.advertise<geometry_msgs::Twist>("cmd_vel",10);

     spin();

     return 0;
}
edit retag flag offensive close merge delete

Comments

Why all those Duration(0.25).sleep() ?

Delb gravatar image Delb  ( 2018-07-10 09:46:02 -0600 )edit

just to be sure it has enough time up to catch the value and return to main function. i have thought that may be it would be a good solution, but i had a simple BUG and fortunately solved and it works. thank you .

arya gravatar image arya  ( 2018-07-11 03:47:40 -0600 )edit

You can get rid of them since once you receieve a LaserScan message the callback has to end to be called again (so the new messages would be stored in the queue, which you have set to 10 so by waiting all this time you will lose a lot of messages, depending on the rate of you laser)

Delb gravatar image Delb  ( 2018-07-11 04:03:19 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2018-07-10 08:24:03 -0600

Hamid Didari gravatar image

updated 2018-07-10 08:27:37 -0600

Hi

in your scanCallback function you only show the state result after check_fornt()

try this:

     state = check_Right();
     Duration(0.25).sleep();
     cout << state << endl;
     state = check_Left();
     Duration(0.25).sleep();
     cout << state << endl;
     state = check_fornt();
     Duration(0.25).sleep();
     cout << state << endl;
edit flag offensive delete link more

Comments

merci hamid jan, i solved the problem, i had just simple BUG, really it has stolen my time. anyway thank you so much man.

arya gravatar image arya  ( 2018-07-11 03:44:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-10 05:14:40 -0600

Seen: 170 times

Last updated: Jul 10 '18