SensorNet.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2010, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita and Pedro Sousa on 25/10/2010
00036 *********************************************************************/
00037 #define NODE_VERSION 0.01
00038 
00039 #include <ros/ros.h>
00040 #include <lse_sensor_msgs/Nostril.h>
00041 #include <lse_sensor_msgs/Anemometer.h>
00042 
00043 #include <vector>
00044 #include <string>
00045 
00046 #include "cereal_port/CerealPort.h"
00047 
00048 #define DEBUG                   0
00049 
00050 #define CR      13
00051 #define SN_START                ':'
00052 #define SN_SEP                  ','
00053 
00054 #define SN_BROADCAST    'Z'
00055 
00056 #define SN_SYNC_TIME    'a'
00057 #define SN_SET_TIME             'b'
00058 #define SN_READ_SENSORS 'c'     
00059 #define SN_GET_POWER    'd'
00060 #define SN_GET_TIME             'e'
00061 #define SN_SET_ALL_PWM  'f'
00062 #define SN_SET_PWM              'g'
00063 #define SN_SET_ADDRESS  'h'
00064 #define SN_SET_SENSORS  'i'
00065 #define SN_GET_SENSORS  'j'
00066 #define SN_GET_INFO             'x'
00067 
00068 #define SN_SENSOR               's'
00069 #define SN_ACTUATOR             'a'
00070 
00071 #define SN_VERSION              10
00072 
00073 class SensorNet
00074 {
00075         public:
00076         SensorNet(std::string port_name, int baud_rate);
00077         ~SensorNet();
00078         
00079         class Node
00080         {
00081                 public:
00082                 Node(){}
00083                 Node(char address, char type, int period)
00084                 {
00085                         this->node_address=address;
00086                         this->node_type=type;
00087                         this->node_period=period;
00088                 }
00089                 ~Node(){}
00090                 
00091                 char address(){return node_address;}
00092                 char type(){return node_type;}
00093                 int period(){return node_period;}
00094                 bool sensors(int i){return node_sensors[i];}
00095                 
00096                 void setAddress(char address){node_address=address;}
00097                 void setType(char type){node_type=type;}
00098                 void setPeriod(int period){node_period=period;}
00099                 void setSensors(int s1, int s2, int s3, int s4, int w1, int w2)
00100                 {
00101                         node_sensors[0] = s1==0 ? false : true;
00102                         node_sensors[1] = s2==0 ? false : true;
00103                         node_sensors[2] = s3==0 ? false : true;
00104                         node_sensors[3] = s4==0 ? false : true;
00105                         node_sensors[4] = w1==0 ? false : true;
00106                         node_sensors[5] = w2==0 ? false : true;
00107                 }
00108                 
00109                 bool sensor(){return node_type==SN_SENSOR;}
00110                 bool actuator(){return node_type==SN_ACTUATOR;}
00111                 
00112                 private:
00113                 char node_address;
00114                 char node_type;
00115                 int node_period;                //dd
00116                 
00117                 bool node_sensors[6];   //S1 S2 S3 S4 W1 W2
00118         };
00119         
00120         std::vector<Node> nodes;
00121         
00122         bool scanNodes(int period);
00123         bool syncNodes();
00124         
00125         bool syncTime(SensorNet::Node * node);
00126         bool setTime(SensorNet::Node * node, ros::Time start_time);
00127         bool getTime(SensorNet::Node * node, ros::Time * current_time, ros::Time * next_reading);
00128         bool getSensorReadings(SensorNet::Node * node, std::vector<lse_sensor_msgs::Nostril> * mox, std::vector<lse_sensor_msgs::Anemometer> * termistor);
00129         bool setAllPWM(SensorNet::Node * node, int pwm1, int pwm2, int pwm3, int pwm4);
00130         bool setPWM(SensorNet::Node * node, ros::Time time, unsigned int id, int pwm);
00131         bool getInfo(SensorNet::Node * node);
00132         bool setSensors(SensorNet::Node * node, int s1, int s2, int s3, int s4, int w1, int w2);
00133         bool getSensors(SensorNet::Node * node);
00134         bool setAddress(SensorNet::Node * node, char new_address);
00135         
00136         int timeout;
00137         
00138         private:
00139         cereal::CerealPort * serial_port;
00140         
00141         ros::Time start_time;
00142         
00143         bool sendCommand(char address, char command);
00144         bool waitForIt(char address);
00145         
00146         void appendTime(std::string * data, ros::Time time);
00147         void appendInt(std::string * data, int value);
00148         ros::Time extractTime(std::string data);
00149         int extractInt(std::string data);
00150 };
00151 
00152 // EOF


sensornet
Author(s): Gonçalo Cabrita and Pedro Sousa
autogenerated on Mon Jan 6 2014 11:27:58