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;
}
Asked by arya on 2018-07-10 05:14:40 UTC
Answers
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;
Asked by Hamid Didari on 2018-07-10 08:24:03 UTC
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.
Asked by arya on 2018-07-11 03:44:52 UTC
Comments
Why all those
Duration(0.25).sleep()
?Asked by Delb on 2018-07-10 09:46:02 UTC
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 .
Asked by arya on 2018-07-11 03:47:40 UTC
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)
Asked by Delb on 2018-07-11 04:03:19 UTC