configurable interfaces over ros params

This commit is contained in:
Florian Zirker 2018-11-26 19:50:11 +01:00
parent a22bb36ab1
commit 2f28e926c3

View file

@ -10,52 +10,78 @@ using namespace wlan_pioneer;
string GetStdoutFromCommand(const string &cmd)
{
string data;
FILE *stream;
const int max_buffer = 256;
char buffer[max_buffer];
//cmd.append(" 2>&1");
string data;
FILE *stream;
const int max_buffer = 256;
char buffer[max_buffer];
//cmd.append(" 2>&1");
stream = popen(cmd.c_str(), "r");
if (stream)
{
while (!feof(stream))
{
if (fgets(buffer, max_buffer, stream) != NULL)
stream = popen(cmd.c_str(), "r");
if (stream)
{
while (!feof(stream))
{
data.append(buffer);
if (fgets(buffer, max_buffer, stream) != NULL)
{
data.append(buffer);
}
}
}
pclose(stream);
}
return data;
pclose(stream);
}
return data;
}
int64_t getWlanSignalStrength(const string &interface)
{
stringstream command;
command << "iwconfig " << interface << " | grep Signal "
<< "| cut -d \"=\" -f3 "
<< "| cut -d \" \" -f1";
return stol(GetStdoutFromCommand(command.str()));
}
int main(int argc, char **argv)
{
// TODO: make interface configurable
string singalLevelCmd("iwconfig wlp3s0 | grep Signal | cut -d \"=\" -f3 | cut -d \" \" -f1");
init(argc, argv, "wlanSignal");
NodeHandle node;
string wlanInterface24G;
string wlanInterface5G;
init(argc, argv, "wlanSignal");
NodeHandle node;
if (node.param<string>("wlan_interface_24G", wlanInterface24G, "wlan0"))
{
ROS_INFO("Param wlan_interface_24: %s", wlanInterface24G.c_str());
}
else
{
ROS_INFO("Param 'wlan_interface_24' not set. Using 'wlan0'");
}
// topic: wlan_signal
Publisher publisher = node.advertise<WlanSignalMsg>("wlan_signal", 1000);
if (node.param<string>("wlan_interface_5G", wlanInterface5G, "wlan0"))
{
ROS_INFO("Param wlan_interface_5G: %s", wlanInterface5G.c_str());
}
else
{
ROS_INFO("Param 'wlan_interface_5G' not set. Using 'wlan0'");
}
Rate loopRate(1); // every second
// topic: wlan_signal
Publisher publisher = node.advertise<WlanSignalMsg>("wlan_signal", 1000);
while (ros::ok())
{
WlanSignalMsg msg;
msg.timestamp = ros::Time::now();
msg.level_24G = stol(GetStdoutFromCommand(singalLevelCmd));
msg.level_5G = 0;
Rate loopRate(1); // every second
ROS_INFO("Signal strength 2.4G: %li Signal strength 5G: %li", msg.level_24G, msg.level_5G);
while (ros::ok())
{
WlanSignalMsg msg;
msg.timestamp = ros::Time::now();
msg.level_24G = getWlanSignalStrength(wlanInterface24G);
msg.level_5G = getWlanSignalStrength(wlanInterface5G);
publisher.publish(msg);
spinOnce();
loopRate.sleep();
}
return 0;
ROS_INFO("Signal strength 2.4G: %li Signal strength 5G: %li", msg.level_24G, msg.level_5G);
publisher.publish(msg);
spinOnce();
loopRate.sleep();
}
return 0;
}