ir_controller_node.cpp
Go to the documentation of this file.
00001 
00024 #include "ir_controller_node.h"
00025 
00027 
00028 IRControllerNode::IRControllerNode(IrDriverInterface *ir_driver) :
00029     _nh_private("~"),
00030     _publish_rate(10),
00031     _ir_driver(ir_driver),
00032     _is_on(false)
00033 {
00034     // connect with the driver
00035     if (_ir_driver->connect() != 0) {
00036         ROS_ERROR("[IR_CONTROLLER_NODE] Error connecting to driver");
00037     }
00038 }
00039 
00041 
00042 IRControllerNode::~IRControllerNode()
00043 {
00044     // disconnect the driver
00045     _ir_driver->disconnect();
00046 }
00047 
00049 
00050 void IRControllerNode::init()
00051 {
00052     _action_pub = _nh_private.advertise<ir_controller_msgs::GetTvAction>("tv_action", 1);
00053     _action_srv = _nh_private.advertiseService("send_command", &IRControllerNode::send_command, this);
00054 }
00055 
00057 
00058 void IRControllerNode::spin()
00059 {
00060     while(_nh.ok()) {
00061         ros::spinOnce();
00062         _publish_rate.sleep();
00063     }
00064 }
00065 
00067 
00068 bool IRControllerNode::send_command(ir_controller_msgs::SetTvAction::Request & req,
00069                                     ir_controller_msgs::SetTvAction::Response & resp)
00070 {
00071     ROS_DEBUG("[IR_CONTROLLER_NODE] Sending IR command...");
00072 
00073     if (_ir_driver->send_remote_command(req.remote, req.command) != 0) {
00074         ROS_ERROR("[IR_CONTROLLER_NODE] Error sending remote command");
00075         return false;
00076     }
00077 
00078     // publish
00079     publish(req.command);
00080 
00081     return true;
00082 }
00083 
00085 
00086 void IRControllerNode::publish(std::string command)
00087 {
00088     ir_controller_msgs::GetTvAction msg;
00089 
00090     msg.last_update = ros::Time::now();
00091 
00092     if (command == "OnOff") {
00093         // check if TV was ON or OFF
00094         if (_is_on) {
00095             msg.action = ir_controller_msgs::TvActions::OFF;
00096             _is_on = false;
00097         }
00098         else {
00099             msg.action = ir_controller_msgs::TvActions::ON;
00100             _is_on = true;
00101         }
00102     }
00103     else if (command == "v+") {
00104         msg.action = ir_controller_msgs::TvActions::VOL_UP;
00105     }
00106     else if (command == "v-") {
00107         msg.action = ir_controller_msgs::TvActions::VOL_DOWN;
00108     }
00109     else if (command == "up") {
00110         msg.action = ir_controller_msgs::TvActions::CH_UP;
00111     }
00112     else if (command == "down") {
00113         msg.action = ir_controller_msgs::TvActions::CH_DOWN;
00114     }
00115     else if (command == "DTV") {
00116         msg.action = ir_controller_msgs::TvActions::TV;
00117     }
00118     else if (command == "FM") {
00119         msg.action = ir_controller_msgs::TvActions::RADIO;
00120     }
00121 
00122     _action_pub.publish(msg);
00123 }
00124 


maggie_ir_controller
Author(s): Raul Perula-Martinez
autogenerated on Thu Apr 23 2015 22:19:07