lightsensors.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 #include <ros/package.h>
3 #include "raspicat/LightSensorValues.h"
4 #include <fstream>
5 using namespace ros;
6 
7 int getFrequency(int old, NodeHandle *n)
8 {
9  int f;
10  if(n->getParam("frequency",f) and f > 0)
11  return f;
12 
13  return old;
14 }
15 
16 int main(int argc, char **argv)
17 {
18  init(argc,argv,"lightsensors");
19  NodeHandle n("~");
20 
21  Publisher pub = n.advertise<raspicat::LightSensorValues>("/lightsensors", 5);
22 
23  int freq = 10;
24 
25  ros::Rate loop_rate(freq);
26  raspicat::LightSensorValues msg;
27 
28  unsigned int c = 0;
29  while(ok()){
30  if(c++ % freq == 0){ // check the parapeter every 1[s]
31  unsigned int old = freq;
32  freq = getFrequency(freq,&n);
33  if(old != freq){
34  loop_rate = ros::Rate(freq);
35  ROS_INFO("Lightsensor frequency: %d", freq);
36  }
37  }
38 
39  std::ifstream ifs("/dev/rtlightsensor0");
40  ifs >> msg.right_forward >> msg.right_side
41  >> msg.left_side >> msg.left_forward;
42 
43  msg.sum_forward = msg.left_forward + msg.right_forward;
44  msg.sum_all = msg.sum_forward + msg.left_side + msg.right_side;
45 
46  pub.publish(msg);
47  spinOnce();
48  loop_rate.sleep();
49  }
50  exit(0);
51 }
52 
void publish(const boost::shared_ptr< M > &message) const
f
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int getFrequency(int old, NodeHandle *n)
Definition: lightsensors.cpp:7
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
int main(int argc, char **argv)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void spinOnce()


raspicat
Author(s): Ryuichi Ueda , Daisuke Sato
autogenerated on Mon Jun 10 2019 14:27:50