advantech_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, LABUST, UNIZG-FER
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the LABUST nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  *  Author : Dula Nad
00035  *  Created: 23.07.2013.
00036  *********************************************************************/
00037 #include <REL_Linux_SUSI.h>
00038 #include <diagnostic_msgs/DiagnosticStatus.h>
00039 #include <std_msgs/UInt16.h>
00040 
00041 #include <ros/ros.h>
00042 #include <boost/bind.hpp>
00043 
00044 struct SharedData
00045 {
00046         SharedData():
00047           pins(0),
00048           status(0){};
00049         enum {tcpu,tsys,taux};
00050         uint16_t tempTypes;
00051         float temp[3];
00052         bool hasIO;
00053         DWORD pins, status;
00054 };
00055 
00056 int init_susi(SharedData& shared)
00057 {
00058         if (!SusiDllInit())
00059         {
00060                 ROS_ERROR("Cannot initialize advantech SUSI drivers.");
00061                 return 1;
00062         }
00063 
00064         bool hasHwm(SusiHWMAvailable());
00065         shared.tempTypes = 0;
00066         if (!hasHwm)
00067         {
00068                 ROS_WARN("No HWM available");
00069         }
00070         else
00071         {
00072                 float retVal;
00073                 SusiHWMGetTemperature(0, &retVal, &shared.tempTypes);
00074         }
00075 
00076         shared.hasIO = SusiIOAvailable();
00077         if (!shared.hasIO)
00078         {
00079                 ROS_WARN("No IO available");
00080         }
00081         else
00082         {
00083                 //Put all pins as outgoing
00084                 DWORD mask(0);
00085                 DWORD pins(0xFF);
00086                 //if (!SusiIOSetDirectionMulti(pins,&mask))
00087                 {
00088                   ROS_ERROR("Unable to set pins to output.");
00089                 }
00090         }
00091 
00092         return 0;
00093 }
00094 
00095 void getTemperature(SharedData& shared)
00096 {
00097         if (TCPU & shared.tempTypes)
00098         {
00099                 SusiHWMGetTemperature(TCPU, &shared.temp[SharedData::tcpu], NULL);
00100         }
00101         if (TSYS & shared.tempTypes)
00102         {
00103                 SusiHWMGetTemperature(TSYS, &shared.temp[SharedData::tsys], NULL);
00104         }
00105         if (TAUX & shared.tempTypes)
00106         {
00107                 SusiHWMGetTemperature(TAUX, &shared.temp[SharedData::taux], NULL);
00108         }
00109 }
00110 
00111 void handleGPIO(SharedData& shared, const std_msgs::UInt16::ConstPtr& data)
00112 {
00113         if (shared.hasIO)
00114         {
00115                 DWORD pins = (data->data & 0xFF00) >> 8;
00116                 DWORD status = (data->data & 0x00FF);
00117                 shared.pins = pins;
00118                 shared.status = status;
00119                 ROS_INFO("Set gpio: %d, %d",pins, status);
00120                 //int result = SusiIOWriteMulti(pins, status);
00121                 //if (result == FALSE) 
00122                 //      ROS_INFO("SusiIOWriteMulti() failed\n");
00123         }
00124 }
00125 
00126 int main(int argc, char* argv[])
00127 {
00128         ros::init(argc,argv,"advantech_io");
00129 
00130         SharedData shared;
00131   if (init_susi(shared))
00132   {
00133         SusiDllUnInit();
00134         return -1;
00135   };
00136 
00137         ros::NodeHandle nh;
00138         //Publishers
00139         ros::Publisher diagnostics = nh.advertise<diagnostic_msgs::DiagnosticStatus>("advantech_diagnostic",1);
00140         ros::Subscriber gpio = nh.subscribe<std_msgs::UInt16>("gpio", 1, boost::bind(&handleGPIO,boost::ref(shared),_1));
00141 
00142         ros::Rate rate(10);
00143 
00144         std::string temps[3] = {"TCPU","TSYS","TAUX"};
00145         diagnostic_msgs::DiagnosticStatus status;
00146         status.level = status.OK;
00147         status.name = "AdvantechBoard";
00148         status.message = "Temperature report";
00149         status.hardware_id = "AdvantechSUSI";
00150         for(int i=0;i<3;++i) status.values.push_back(diagnostic_msgs::KeyValue());
00151 
00152         DWORD mask(0x0);
00153         while (ros::ok())
00154         {
00155                 getTemperature(shared);
00156 
00157                 //Publish temperature
00158                 std::ostringstream out;
00159                 for(int i=0;i<3;++i)
00160                 {
00161                         out.str("");
00162                         out<<shared.temp[i];
00163                         status.values[i].key = temps[i];
00164                         status.values[i].value = out.str();
00165                 }
00166                 diagnostics.publish(status);
00167                 DWORD pins(0xFF);
00168                 SusiIOSetDirectionMulti(pins,&mask);
00169                 if (!SusiIOSetDirectionMulti(pins,&mask))
00170                 {
00171                   ROS_ERROR("Unable to set pins to output.");
00172                 }
00173                 
00174                 ROS_INFO("Set gpio: %d, %d",shared.pins, shared.status);
00175                 int result = SusiIOWriteMulti(0xFF, 0x0);
00176                 if (result != 0) 
00177                         ROS_INFO("SusiIOWriteMulti() failed\n");
00178 
00179                 rate.sleep();
00180                 ros::spinOnce();
00181         }
00182 
00183         SusiDllUnInit();
00184         return 0;
00185 }
00186 


advantech
Author(s):
autogenerated on Fri Feb 7 2014 11:36:51