identification the location of objects
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;
}
Why all those
Duration(0.25).sleep()
?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 .
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)