Problem with IR Sensor
Hi everybody,
I have a problem here. I try to control the turtlebot speed based on the IR sensors reading. Unfortunately, the turtlebot only slow down the object is at it's very right side. However, the IR sensors shows all reading without any problem. Here is my code:
#include <ros/ros.h>
#include <roomba_500_series/RoombaIR.h>
#include <geometry_msgs/Twist.h>
#include <ros/rate.h>
#include <iostream>
using namespace std;
ros::Publisher pub_vel;
geometry_msgs::Twist cmdvel;
int reading, condition;
double speed;
//define the irsensorCallback function
void irsensorCallback(const roomba_500_series::RoombaIR::ConstPtr& msg)
{
reading = msg->signal;
condition = msg->state;
cout<<"IR State = "<<reading<<endl;
cout<<"IR Signal = "<<condition<<endl;
if(condition!=0 || reading=>100)
{
cmdvel.linear.x=0.08;
cmdvel.amgular.z=0.0;
}
else
{
cmdvel.linear.x=0.2;
cmdvel.amgular.z=0.0;
}
pub_vel.publish(cmdvel);
}
//ROS node entry point
int main(int argc, char **argv)
{
ros::init(argc, argv, "turtlebot_test");
ros::NodeHandle n;
ros::Subscriber irsensorSubscriber = n.subscribe("/ir_bumper", 1000, irsensorCallback);
ros::Publisher velocityPublisher = n.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
cmdvel.linear.x = 0.0;
cmdvel.angular.z = 0.0;
ros::spin();
return 0;
}
Hopefully somebody could advise me on this. Thank you.
Your code looks pretty reasonable. Are you sure you're interpreting the IR readings from the Roomba correctly?
Yes I am sure. Because IR readings display all readings from all sensors. However, the turtlebot only slow down if the IR reading from very right side is less than 100. Any idea?
Is each sensor reading published as a separate message? Is it possible that you're publishing a command for each sensor reading, and only the most recent command is being used?
yes, each sensor reading published as a separate message. And yes, the sensor reading from very right is the last sensor reading published and only this sensor reading is being used. that's why I wonder how I could make sure all other sensor reading are being used as well.