MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Instruments/Ocean/iActuation/MOOSSAILDriver.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite
00004 //
00005 //   A suit of Applications and Libraries for Mobile Robotics Research
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and
00007 //   Oxford University.
00008 //
00009 //   This software was written by Paul Newman and others
00010 //   at MIT 2001-2002 and Oxford University 2003-2005.
00011 //   email: pnewman@robots.ox.ac.uk.
00012 //
00013 //   This file is part of a  MOOS Instrument.
00014 //
00015 //   This program is free software; you can redistribute it and/or
00016 //   modify it under the terms of the GNU General Public License as
00017 //   published by the Free Software Foundation; either version 2 of the
00018 //   License, or (at your option) any later version.
00019 //
00020 //   This program is distributed in the hope that it will be useful,
00021 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00023 //   General Public License for more details.
00024 //
00025 //   You should have received a copy of the GNU General Public License
00026 //   along with this program; if not, write to the Free Software
00027 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00028 //   02111-1307, USA.
00029 //
00031 // MOOSSAILDriver.cpp: implementation of the CMOOSSAILDriver class.
00032 //
00034 #include <MOOSLIB/MOOSLib.h>
00035 #include <iostream>
00036 #include "MOOSSAILDriver.h"
00037 #include <math.h>
00038 
00040 // Construction/Destruction
00042 
00043 #define SAIL_MAX 255
00044 #define SAIL_GB_RATIO 825
00045 #define SAIL_ENCODER_RESOLUTION 12
00046 
00047 CMOOSSAILDriver::CMOOSSAILDriver()
00048 {
00049     MOOSTrace("Creating SAIL Driver\n");
00050 }
00051 
00052 CMOOSSAILDriver::~CMOOSSAILDriver()
00053 {
00054 
00055 }
00056 
00057 
00058 bool CMOOSSAILDriver::Initialise()
00059 {
00060 
00061 
00062     list<CSailCommand> List;
00063 
00064 
00065     if(m_pPort != NULL )
00066     {
00067 
00068 
00069 
00070 
00071     List.push_back(CSailCommand("#THR:",true));
00072 
00073         //enable hardware kill...
00074     List.push_back(CSailCommand("#THB:",true));
00075 //        List.push_back(CSailCommand("#THG:",true));
00076 
00077 //        List.push_back("#FRR:");
00078 //        List.push_back("#FRO:");
00079     List.push_back(CSailCommand("#FRG:",false));
00080 //        List.push_back("#FRZ:");
00081 //        List.push_back("#FRL000000");
00082 
00083 //        List.push_back("#FER:");
00084 //        List.push_back("#FEO:");
00085     List.push_back(CSailCommand("#FEG:",false));
00086 
00087     list<CSailCommand>::iterator p;
00088 
00089     for(p = List.begin();p!=List.end();p++)
00090     {
00091         string sCmd = p->m_sCmd;
00092 
00093         MOOSTrace("iActuation Init() : Sending %s\n",sCmd.c_str());
00094         string sReply;
00095         SendAndAck(sCmd,sReply,p->m_bWaitForReply);
00096 
00097         MOOSPause(100);
00098     }
00099 
00100 
00101     }
00102 
00103     MOOSTrace("Sail Init complete...\n\n\n");
00104     return true;
00105 }
00106 
00107 
00108 bool CMOOSSAILDriver::SetElevator(double dfAng)
00109 {
00110     return DoFinControl("#FEL",dfAng);
00111 }
00112 
00113 bool CMOOSSAILDriver::SetRudder(double dfAng)
00114 {
00115 
00116     return DoFinControl("#FRL",dfAng);
00117 
00118 }
00119 
00120 bool CMOOSSAILDriver::SetThrust(double dfPercent)
00121 {
00122 
00123 
00124     int nSign = 1;
00125 
00126     if(dfPercent!=0)
00127     {
00128     nSign= (int)(dfPercent/(fabs(dfPercent)));
00129     }
00130 
00131     dfPercent = fabs(dfPercent);
00132 
00133 
00134     //map dfPercent to 127...
00135     if(dfPercent>100)
00136     {
00137     dfPercent = 100.0;
00138     }
00139 
00140     unsigned int nFull = 0xE0;
00141 
00142     unsigned int nStart = 0x0;
00143 
00144     //look after asymetry...
00145     if(nSign>0)
00146     {
00147         nStart =0xA8;
00148     }
00149     else
00150     {
00151         nStart = 0xD0;
00152     }
00153 
00154     //y =mx+x////
00155     unsigned char nLevel = (unsigned char)((nFull-nStart)/100.0*dfPercent+nStart);
00156 
00157     //but zero is REALLY ZERO!
00158     if(dfPercent==0)
00159     {
00160         nLevel=0;
00161     }
00162 
00163 
00164     unsigned char byLow = nLevel & 0xF;
00165     unsigned char byHigh = (nLevel>>4) & 0xF;
00166 
00167     const char* cHEX = "0123456789ABCDEF";
00168 
00169     if(m_pPort!=NULL)
00170     {
00171     char sCmd[20];
00172 
00173     sprintf(sCmd,"#TH%c%c%c:",
00174         nSign==1?'F':'V',
00175         cHEX[byHigh],
00176         cHEX[byLow]);
00177 
00178     string sReply;
00179 
00180     if(SendAndAck(sCmd,sReply))
00181     {
00182 
00183         //now here we look for watchdog time out...
00184 
00185         if(sReply.find('M')!=string::npos)
00186         {
00187         MOOSTrace("SAIL Bus Monitor timeout detected\nResetting...\n");
00188         SendAndAck("#THR:",sReply);
00189         SendAndAck("#THB:",sReply);
00190         }
00191     }
00192 
00193 
00194 
00195     }
00196 
00197     return true;
00198 }
00199 
00200 
00201 bool CMOOSSAILDriver::SetZeroElevator()
00202 {
00203     MOOSTrace("Setting home for Elevator\n");
00204     string sReply;
00205     return SendAndAck("#FEZ",sReply);
00206 }
00207 
00208 bool CMOOSSAILDriver::SetZeroRudder()
00209 {
00210     MOOSTrace("Setting home for Rudder\n");
00211     string sReply;
00212     return SendAndAck("#FRZ",sReply);
00213 }
00214 
00215 
00216 
00217 
00218 
00219 bool CMOOSSAILDriver::DoFinControl(const char *sAddress, double dfAng)
00220 {
00221     int nCount =(int)( (dfAng/360.0*SAIL_GB_RATIO)*SAIL_ENCODER_RESOLUTION);
00222 
00223     nCount = ~nCount;
00224     nCount +=1;
00225 
00226     const char* cHEX = "0123456789ABCDEF";
00227 
00228     if(m_pPort!=NULL)
00229     {
00230     char sCmd[20];
00231 
00232     sprintf(sCmd,"%s%c%c%c%c%c%c",
00233         sAddress,
00234         cHEX[(nCount>>20)&0xF],
00235         cHEX[(nCount>>16)&0xF],
00236         cHEX[(nCount>>12)&0xF],
00237         cHEX[(nCount>>8)&0xF],
00238         cHEX[(nCount>>4)&0xF],
00239         cHEX[(nCount>>0)&0xF]);
00240 
00241 
00242     //    sprintf(sCmd,"#FRL000000");
00243 
00244 
00245     string sReply;
00246     return     SendAndAck(sCmd,sReply);
00247 
00248 
00249 
00250 
00251     }
00252     else
00253     {
00254     return false;
00255     }
00256 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines