Controller.cpp
Go to the documentation of this file.
00001 /*
00002 * Controller.cpp defines all the necessary functionality for the diagnosis board controller.
00003 *
00004 * Copyright (c).2012. OWNER: Institute for Software Technology, TU Graz Austria.
00005 * Authors: Safdar Zaman, Gerald Steinbauer. (szaman@ist.tugraz.at, steinbauer@ist.tugraz.at)
00006 * All rights reserved.
00007 *    This program is free software: you can redistribute it and/or modify
00008 *    it under the terms of the GNU General Public License as published by
00009 *    the Free Software Foundation, either version 3 of the License, or
00010 *    (at your option) any later version.
00011 *
00012 *    This program is distributed in the hope that it will be useful,
00013 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 *    GNU General Public License for more details.
00016 *
00017 *    You should have received a copy of the GNU General Public License
00018 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "Controller.h"
00022 
00023 Controller::Controller(unsigned char frq, string ip, int port)
00024 {  
00025     host = gethostbyname(ip.c_str());
00026     server_addr.sin_family = AF_INET;     
00027     server_addr.sin_port = htons(port);   
00028     server_addr.sin_addr = *((struct in_addr *)host->h_addr);
00029     bzero(&(server_addr.sin_zero),8);
00030     pub_board_msr_ = n_.advertise<tug_ist_diagnosis_msgs::DBoardMeasurments>("/board_measurments",1);
00031     initFrq = frq;
00032     
00033 }
00034 
00035 Controller::~Controller()
00036 {
00037 
00038 }
00039 
00040 void Controller::initController()
00041 {
00042   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00043          perror("Socketerror:");
00044          exit(1);
00045     }
00046   if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
00047             perror("Connecterror:");
00048             exit(1);
00049     }
00050   create_threads();
00051   chnl2dev_mapping();
00052   CallMessageBroadCasting(initFrq);
00053 }
00054 
00055 void Controller::chnl2dev_mapping()
00056 {
00057 
00058   chnl2dev_map[0] = "sensor_head";
00059   chnl2dev_map[1] = "laser_alignment";
00060   chnl2dev_map[2] = "pc";
00061   chnl2dev_map[3] = "hokuyo";
00062   chnl2dev_map[4] = "kinect";
00063   chnl2dev_map[5] = "router";
00064   chnl2dev_map[6] = "thermal_camera";
00065   chnl2dev_map[7] = "jaguar";
00066   chnl2dev_map[8] = "no_dev1";
00067   chnl2dev_map[9] = "no_dev2";
00068 
00069 
00070 }
00071 
00072 char Controller::get_chnl_from_map(string dev)
00073 {
00074    map<char,string>::const_iterator it;
00075    char key = -1;
00076    for (it = chnl2dev_map.begin(); it != chnl2dev_map.end(); ++it)
00077    {
00078     if (it->second == dev)
00079      {
00080       key = it->first;
00081       break;
00082      }
00083    }
00084   return key;
00085 }
00086 
00087 void* run_recv_Thread(void* contrl_ptr){
00088     static_cast<Controller*>(contrl_ptr)->recv_Thread();
00089 }
00090 
00091 void Controller::create_threads(){
00092     pthread_t r_thread;
00093     r_thread=pthread_create(&r_thread, NULL, run_recv_Thread, this);
00094 }  
00095 
00096 void Controller::processBuffer(unsigned char *buf,char command)
00097 {      
00098      std::vector<float> curr;
00099      int channels = *buf;
00100      buf++;
00101      board_msr.o_time = ros::Time::now().toSec();;
00102      tug_ist_diagnosis_msgs::Channel channel;
00103      std::vector<tug_ist_diagnosis_msgs::Channel> msr_vector;
00104      for(int chnl=0;chnl<channels;chnl++)
00105       { 
00106         if(command==2)
00107          { ushort status;
00108            channel.id = chnl;
00109            channel.dev_connected = chnl2dev_map[(char) chnl];
00110            status = *buf;
00111            channel.status =(int) status;
00112            channel.current = *((float *)(buf+1));
00113            channel.voltage = *((float *)(buf+5));
00114            msr_vector.push_back(channel);
00115            buf+=9;
00116          }
00117         else if(command==0)
00118          {
00119            buf+=8;
00120          }
00121       }
00122     board_msr.channel = msr_vector;
00123     msr_vector.clear();
00124     pub_board_msr_.publish(board_msr);
00125     
00126 }
00127 
00128 void Controller::recv_Thread()
00129 {
00130 while(1)
00131   {
00132    int header_length=4,counter=0;
00133    while(counter<header_length)
00134      {
00135        bytes_recieved=recv(sock,buffer,4-counter,0);
00136        counter+=bytes_recieved;
00137      }
00138      char delim = buffer[0];
00139      char command = buffer[1];
00140      ushort data_length = buffer[2];
00141      counter = 0;
00142   while(counter<data_length)
00143     {  
00144        bytes_recieved=recv(sock,buffer,data_length-counter,0);
00145        counter+=bytes_recieved;
00146      }
00147     unsigned char * bufPtr;
00148     bufPtr = buffer;
00149     processBuffer(bufPtr,command);
00150     switch(command)
00151     {
00152       case 0:
00153             msg = new MessageSpefications(delim,command,data_length);
00154             msg->parseBuffer(bufPtr);
00155       break;
00156       case 2:
00157             msg = new MessageMeasurments(delim,command,data_length);
00158             msg->parseBuffer(bufPtr);
00159       break;
00160       case 5:
00161             msg = new MessageAcknowledgment(delim,command,data_length);
00162             msg->parseBuffer(bufPtr);
00163       break;
00164 
00165     } // switch
00166       
00167   }//while(1)
00168 }// recv_Thread
00169 
00170 
00171 void Controller::CallMessageBroadCasting(unsigned char frq)
00172 {
00173      unsigned char *p;
00174      msg = new MessageBroadCasting(frq);
00175      int buf_len;
00176      p = msg->getBuffer(buf_len);
00177      send(sock,p,buf_len, 0);
00178      delete p;
00179 }
00180 
00181 void Controller::CallMessageRequest()
00182 {
00183      unsigned char *p;
00184      msg = new MessageRequest();
00185      int buf_len;
00186      p = msg->getBuffer(buf_len);
00187      send(sock,p,buf_len, 0);
00188      printf("d=%i,c=%i,l=%i,size=%d",*p,*(p+1),*(p+2),buf_len);
00189      delete p;
00190 }
00191 
00192 void Controller::CallMessageChannelOnOff(char chnl, char status)
00193 {
00194     unsigned char *p;
00195     msg = new MessageChannelOnOff(chnl,status);
00196     int buf_len;
00197     p = msg->getBuffer(buf_len);
00198     send(sock,p,buf_len, 0);
00199     printf("d=%i,c=%i,l=%i,channel=%i, state=%i, size=%d",*p,*(p+1),*(p+2),*(p+4),*(p+5),buf_len);
00200     delete p;
00201 }
00202 


tug_ist_diagnosis_board
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:23