buttons.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 #include <ros/package.h>
3 #include "raspicat/ButtonValues.h"
4 #include <fstream>
5 using namespace ros;
6 
7 bool readButton(const char *name)
8 {
9  std::ifstream ifs(name);
10  char c;
11  ifs >> c;
12  return c == '0';
13 }
14 
15 int main(int argc, char **argv)
16 {
17  init(argc,argv,"buttons");
18  NodeHandle n;
19 
20  Publisher pub = n.advertise<raspicat::ButtonValues>("buttons", 5);
21 
22  ros::Rate loop_rate(10);
23  raspicat::ButtonValues msg;
24  int c[3] = {0,0,0};
25  while(ok()){
26  msg.front = readButton("/dev/rtswitch0");
27  msg.mid = readButton("/dev/rtswitch1");
28  msg.rear = readButton("/dev/rtswitch2");
29 
30  c[0] = msg.front ? 1+c[0] : 0;
31  c[1] = msg.mid ? 1+c[1] : 0;
32  c[2] = msg.rear ? 1+c[2] : 0;
33 
34  if(c[0] > 4){
35  msg.front_toggle = not msg.front_toggle;
36  c[0] = 0;
37  }
38  if(c[1] > 4){
39  msg.mid_toggle = not msg.mid_toggle;
40  c[1] = 0;
41  }
42  if(c[2] > 4){
43  msg.rear_toggle = not msg.rear_toggle;
44  c[2] = 0;
45  }
46 
47  pub.publish(msg);
48  spinOnce();
49  loop_rate.sleep();
50  }
51  exit(0);
52 }
53 
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
Definition: buttons.cpp:15
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool readButton(const char *name)
Definition: buttons.cpp:7
ROSCPP_DECL void spinOnce()


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