light_controller/main.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <igvc/SerialPort.h>
3 #include <igvc_msgs/lights.h>
4 #include <std_msgs/Bool.h>
5 #include <std_msgs/Int8.h>
6 
7 igvc_msgs::lights state;
8 
9 void lights_callback(const igvc_msgs::lightsConstPtr &msg)
10 {
11  state = *msg;
12 }
13 
14 int main(int argc, char** argv)
15 {
16  ros::init(argc, argv, "light_controller");
17 
18  ros::NodeHandle nh;
19 
20  SerialPort port{"/dev/igvc_light_arduino", 9600};
21 
22  if(!port.isOpen())
23  {
24  ROS_ERROR_STREAM("Failed to open port: " << port.devicePath());
25  return -1;
26  }
27 
28  ros::Subscriber lights_sub = nh.subscribe("/lights", 1, lights_callback);
29 
30  ros::Publisher enabled_pub = nh.advertise<std_msgs::Bool>("/robot_enabled", 1);
31 
32  ros::Publisher battery_pub = nh.advertise<std_msgs::Int8>("/battery", 1);
33 
34  ROS_INFO_STREAM("Light controller ready.");
35 
36  sleep(2); // Give the arduino time to boot.
37 
38  ros::Rate rate(10);
39  while(ros::ok() && port.isOpen())
40  {
41  unsigned char msg[12] = {2,
42  state.safety_flashing,
43  state.underglow_color[0],
44  state.underglow_color[1],
45  state.underglow_color[2],
46  state.underglow_brightness[0],
47  state.underglow_brightness[1],
48  state.underglow_brightness[2],
49  state.underglow_brightness[3],
50  state.underglow_brightness[4],
51  state.underglow_brightness[5],
52  4};
53 
54  port.write(msg, 12);
55 
56  unsigned char *ret = (unsigned char*)port.read(4);
57 
58  if(ret[0] != 2 || ret[3] != 4)
59  ROS_ERROR_STREAM("Bad format for return packet.");
60  else if(ret[1] == 21)
61  ROS_ERROR_STREAM("Light controller gave error code " << ret[2]);
62  else
63  {
64  std_msgs::Bool estop_msg;
65  estop_msg.data = !(bool)(ret[1]);
66 
67  enabled_pub.publish(estop_msg);
68 
69  std_msgs::Int8 battery_msg;
70  battery_msg.data = ret[2];
71 
72  battery_pub.publish(battery_msg);
73  }
74 
75  rate.sleep();
76  }
77 
78  return 0;
79 }
void lights_callback(const igvc_msgs::lightsConstPtr &msg)
igvc_msgs::lights state
int main(int argc, char **argv)
Helper class to simplify interfacing with serial port hardware.
Definition: SerialPort.h:14


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45