Files
rtabmap_ros/rtabmap/src/InputNode.cpp
T

115 lines
2.5 KiB
C++
Raw Normal View History

/*
* CameraNode.cpp
*
* Author: labm2414
*/
#include <ros/ros.h>
#include "rtabmap/SensoryMotorState.h"
#include <geometry_msgs/Twist.h>
#include <utilite/UMutex.h>
2011-08-23 15:36:43 +00:00
#include <utilite/UTimer.h>
UMutex commandMutex;
std::list<std::vector<float> > commands;
ros::Publisher rosPublisher;
2011-08-23 15:36:43 +00:00
UTimer timer;
rtabmap::SensoryMotorStatePtr state;
2011-08-23 15:36:43 +00:00
bool stateUpdated = false;
bool actionsUpdated = false;
int nbCommands = 10;
2011-08-23 15:36:43 +00:00
void publish()
{
2011-08-23 15:36:43 +00:00
if(stateUpdated && actionsUpdated)
{
2011-08-23 15:36:43 +00:00
std::vector<float> actions;
int sizeActions = -1;
for(std::list<std::vector<float> >::iterator iter = commands.begin(); iter!=commands.end();++iter)
{
2011-08-23 15:36:43 +00:00
actions.insert(actions.end(), iter->begin(), iter->end());
}
sizeActions = commands.size();
commands.clear();
2011-08-23 15:36:43 +00:00
state->actuators = actions;
2011-08-23 15:36:43 +00:00
rosPublisher.publish(state);
float elapsed = timer.ticks();
ROS_INFO("Sensorimotor state sent (sizeActions=%d, %f Hz)", sizeActions, elapsed>0?1/elapsed:0);
stateUpdated = false;
actionsUpdated = false;
}
}
void smReceivedCallback(const rtabmap::SensoryMotorStateConstPtr & msg)
{
ROS_INFO("Received camera data");
state = rtabmap::SensoryMotorStatePtr(new rtabmap::SensoryMotorState);
state->sensors = msg->sensors;
state->sensorStep = msg->sensorStep;
state->actuatorStep = 6;
state->image = msg->image;
state->keypoints = msg->keypoints;
2011-08-23 15:36:43 +00:00
stateUpdated = true;
publish();
}
void velocityReceivedCallback(const geometry_msgs::TwistConstPtr & msg)
{
ROS_INFO("Received command velocity linear=(%f,%f,%f) angular=(%f,%f,%f)",
msg->linear.x,
msg->linear.y,
msg->linear.z,
msg->angular.x,
msg->angular.y,
msg->angular.z);
2011-08-23 15:36:43 +00:00
std::vector<float> v(6);
v[0] = msg->linear.x;
v[1] = msg->linear.y;
v[2] = msg->linear.z;
v[3] = msg->angular.x;
v[4] = msg->angular.y;
v[5] = msg->angular.z;
commands.push_back(v);
// 10 Hz max
while(commands.size() > nbCommands)
{
ROS_WARN("Too many commands (%zu) > %d, removing the oldest...", commands.size(), nbCommands);
2011-08-23 15:36:43 +00:00
//remove the oldest
commands.pop_front();
}
if(commands.size() == nbCommands)
2011-08-23 15:36:43 +00:00
{
actionsUpdated = true;
publish();
}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "input_node");
ros::NodeHandle n("~");
n.param("nb_commands", nbCommands, nbCommands);
ROS_INFO("nb_commands=%d", nbCommands);
2011-08-23 15:36:43 +00:00
rosPublisher = n.advertise<rtabmap::SensoryMotorState>("/sm_state", 1);
ros::Subscriber image_sub = n.subscribe("/camera_data", 1, smReceivedCallback);
ros::Subscriber velocity_sub = n.subscribe("/cmd_vel", 1, velocityReceivedCallback);
timer.start();
ros::spin();
2011-08-23 15:36:43 +00:00
return 0;
}