controller.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Southwest Research Institute
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 are met:
00009  *
00010  *      * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *      * Redistributions in binary form must reproduce the above copyright
00013  *      notice, this list of conditions and the following disclaimer in the
00014  *      documentation and/or other materials provided with the distribution.
00015  *      * Neither the name of the Southwest Research Institute, nor the names
00016  *      of its contributors may be used to endorse or promote products derived
00017  *      from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031  
00032  #include "controller.h"
00033  #include "log_wrapper.h"
00034  
00035  namespace motoman
00036 {
00037 namespace controller
00038 {
00039 
00040 Controller::Controller()
00041 {
00042   this->jobStarted = false;
00043   this->motionEnabled = false;
00044 }
00045 
00046 Controller::~Controller()
00047 {
00048   this->disableMotion();
00049   // TODO: The current job should probably be unloaded.
00050 }
00051 
00052 void Controller::setInteger(int index, int value)
00053 {
00054         MP_VAR_DATA data;
00055         
00056         data.usType = MP_RESTYPE_VAR_I;
00057         data.usIndex = index;
00058         data.ulValue = value;
00059         
00060         while (mpPutVarData ( &data, 1 ) == MP_ERROR) 
00061         {
00062         LOG_ERROR("Failed to set integer varaible, index: %d, value: %d, retrying...", 
00063             data.usIndex, data.ulValue);
00064         mpTaskDelay(VAR_POLL_DELAY_);
00065     }
00066 }
00067 
00068 int Controller::getInteger(int index)
00069 {
00070     
00071         MP_VAR_INFO info;
00072         LONG rtn;
00073         
00074         info.usType = MP_RESTYPE_VAR_I;
00075         info.usIndex = index;
00076         
00077         while (mpGetVarData ( &info, &rtn, 1 ) == MP_ERROR) 
00078         {
00079         LOG_ERROR("Failed to retreive integer variable index: %d, retrying...", info.usIndex);
00080         mpTaskDelay(VAR_POLL_DELAY_);
00081     }
00082     return rtn;
00083 }
00084 
00085 void Controller::enableMotion(void)
00086 {
00087   
00088   LOG_INFO("Enabling motion");
00089   this->motionEnabled = false;
00090 
00091   servo_power_data.sServoPower = ON;
00092   while(mpSetServoPower(&servo_power_data, &servo_power_error) == MP_ERROR)
00093   {
00094     LOG_ERROR("Failed to turn on servo power, error: %d, retrying...", servo_power_error.err_no);
00095     mpTaskDelay(this->VAR_POLL_DELAY_);
00096   };
00097   
00098   hold_data.sHold = OFF;
00099   while(mpHold(&hold_data, &hold_error) == MP_ERROR)
00100   {
00101     LOG_ERROR("Failed to turn off hold, error: %d, retrying...", hold_error.err_no);
00102     mpTaskDelay(this->VAR_POLL_DELAY_);
00103   };
00104   
00105   this->motionEnabled = true;
00106 }
00107 
00108 
00109 void Controller::disableMotion(void)
00110 {
00111   LOG_INFO("Disabling motion");
00112   servo_power_data.sServoPower = OFF;
00113   while(mpSetServoPower(&servo_power_data, &servo_power_error) == MP_ERROR)
00114   {
00115     LOG_ERROR("Failed to turn off servo power, error: %d, retrying...", servo_power_error.err_no);
00116     mpTaskDelay(this->VAR_POLL_DELAY_);
00117   };
00118   
00119   this->motionEnabled = false;
00120 }
00121 
00122 void Controller::startMotionJob(char* job_name)
00123 {
00124 
00125   this->jobStarted = false;
00126   
00127   this->enableMotion();
00128   
00129   // Set up job variables
00130   job_start_data.sTaskNo = 0;
00131   strcpy(job_start_data.cJobName, job_name);
00132  
00133   
00134   LOG_INFO("Starting motion job");
00135   while(mpStartJob(&job_start_data, &job_error) == ERROR)
00136   {
00137     LOG_ERROR("Failed to start job, error: %d, retrying...", job_error.err_no);
00138     mpTaskDelay(this->VAR_POLL_DELAY_);
00139   }; 
00140   
00141   this->jobStarted = true;
00142 }
00143 
00144 
00145 void Controller::stopMotionJob(char* job_name)
00146 {  
00147   LOG_INFO("Stopping motion job");
00148   this->disableMotion();
00149   
00150   // delete task
00151   strcpy(job_delete_data.cJobName, job_name);
00152   
00153   while(mpDeleteJob(&job_delete_data, &job_error) == MP_ERROR)
00154   {
00155     LOG_ERROR("Failed to delete job, error: %d, retrying...", job_error.err_no);
00156     mpTaskDelay(this->VAR_POLL_DELAY_);
00157   };
00158   
00159   this->jobStarted = false;
00160 }       
00161 
00162 
00163 #define FILE_NAM_BUFFER_SIZE 100
00164 bool Controller::writeJob(char* path, char* job)
00165 {
00166     bool rtn = false;
00167     int fd = this->MP_ERROR;
00168     int status = this->MP_ERROR;
00169     char filename[FILE_NAM_BUFFER_SIZE];  //should be big enough to hold a file name and DRAM drive name
00170     
00171     memset(filename, '\0', FILE_NAM_BUFFER_SIZE);  //not sure this is needed, strcpy below also does this.
00172     strcpy(filename, "MPRAM1:\\");
00173     strcat(filename, path);
00174     
00175     LOG_DEBUG("writeJob: %s", filename);
00176     
00177     // Remove the file, if it exists
00178     LOG_DEBUG("Trying to remove file, if it exists");
00179     status = mpRemove( filename );
00180     if (this->MP_ERROR == status)
00181     {
00182         LOG_WARN("Failed to remove job file: %s", filename);
00183     }
00184     
00185     
00186     // Create the file and write the job
00187     fd = mpCreate( filename, O_WRONLY );
00188     if (this->MP_ERROR != fd)
00189     {
00190         status = mpWrite( fd, job, strlen(job) );
00191         if ( this->MP_ERROR != status )
00192         {
00193             LOG_INFO("Successfully loaded file: %s, bytes written: %d", filename, status);
00194             rtn = true;
00195         }
00196         else
00197         {
00198             LOG_ERROR("Failed to write file: %s", filename);
00199             rtn = false;
00200         }
00201         
00202         
00203         // Checking file status
00204         /*
00205             struct stat pStat;
00206             status = mpFstat(fd, &pStat);
00207             {
00208                         LOG_DEBUG("mpFstat Complete");
00209                         LOG_DEBUG("st_dev = %u", pStat.st_dev);
00210                         LOG_DEBUG("st_ino = %u", pStat.st_ino);
00211                         LOG_DEBUG("st_mode = %u", pStat.st_mode);
00212                         LOG_DEBUG("st_nlink = %d", pStat.st_nlink);
00213                         LOG_DEBUG("st_uid = %d", pStat.st_uid);
00214                         LOG_DEBUG("st_gid = %d", pStat.st_gid);
00215                         LOG_DEBUG("st_rdev = %u", pStat.st_rdev);
00216                         LOG_DEBUG("st_size = %u", pStat.st_size);
00217                         LOG_DEBUG("st_atime = %u", pStat.st_atime);
00218                         LOG_DEBUG("st_mtime = %u", pStat.st_mtime);
00219                         LOG_DEBUG("st_ctime = %u", pStat.st_ctime);
00220                         LOG_DEBUG("st_blksize = %u", pStat.st_blksize);
00221                         LOG_DEBUG("st_blocks = %u", pStat.st_blocks);
00222                         LOG_DEBUG("st_attrib = %u", pStat.st_attrib);
00223                 }
00224                 */
00225                 
00226         // close file descriptor
00227             status = mpClose(fd);
00228             if (this->MP_ERROR == status)
00229             {
00230                 LOG_WARN("Failed to close file: %s, ignoring failure", filename);
00231             }
00232     }
00233     else
00234     {
00235         LOG_ERROR("Failed to create job file: %s", filename);
00236         rtn = false;
00237     }
00238     
00239     
00240     return rtn;
00241 }
00242 #undef FILE_NAM_BUFFER_SIZE
00243 
00244 
00245 bool Controller::loadJob(char* path, char * job)
00246 {
00247     bool rtn = false;
00248     int status;    
00249     
00250     LOG_DEBUG("Refreshing file list");
00251     status = mpRefreshFileList(MP_EXT_ID_JBI);
00252     if (this->MP_OK != status)
00253     {
00254         LOG_WARN("Failed to refresh file list: %d, ignoring failure", status);
00255     }
00256     LOG_DEBUG("File count before file load: %d", mpGetFileCount());
00257     
00258     LOG_DEBUG("Attempting to load file, path: %s, job: %s", path, job);
00259     status = mpLoadFile (MP_DRV_ID_DRAM, path, job );
00260     if (this->MP_OK == status)
00261     {
00262         LOG_INFO("Loaded job file %s", job);
00263         rtn = true;
00264     }
00265     else
00266     {
00267         LOG_ERROR("Failed to load job file: %s, path: %s, returned error code: %d",
00268                     job, path, status);
00269         rtn = false;
00270     }
00271     
00272     LOG_DEBUG("Refreshing file list");
00273     status = mpRefreshFileList(MP_EXT_ID_JBI);
00274     if (this->MP_OK != status)
00275     {
00276         LOG_WARN("Failed to refresh file list: %d, ignoring failure", status);
00277     }
00278     LOG_DEBUG("File count after file load: %d", mpGetFileCount());
00279     
00280     return rtn;
00281 
00282 }
00283 
00284 
00285 void Controller::setDigitalOut(int bit_offset, bool value)
00286 {
00287   LOG_DEBUG("Setting digital out, Bit offset: %d, value: %d", bit_offset, value);
00288   if ( (bit_offset < this->UNIV_IO_DATA_SIZE_) && 
00289        ( bit_offset > 0) )
00290   {  
00291     MP_IO_DATA data;
00292     data.ulAddr = this->UNIV_OUT_DATA_START_ + bit_offset;
00293     data.ulValue = value;
00294     //TODO: The return result of mpWriteIO is not checked
00295     mpWriteIO(&data, 1);
00296   }
00297   else
00298   {
00299     LOG_ERROR("Bit offset: %d, is greater than size: %d", bit_offset, this->UNIV_IO_DATA_SIZE_);
00300   }
00301 }
00302 
00303  void Controller::waitDigitalIn(int bit_offset, bool wait_value)
00304  {
00305    LOG_DEBUG("Waiting for digital in, Bit offset: %d, Wait value: %d", bit_offset, wait_value);
00306    if ( (bit_offset < this->UNIV_IO_DATA_SIZE_) && 
00307        ( bit_offset > 0) )
00308   { 
00309     MP_IO_INFO info;
00310     info.ulAddr = this->UNIV_IN_DATA_START_ + bit_offset;
00311     
00312     USHORT readValue;
00313     do
00314     {
00315       readValue = !wait_value;  
00316       //TODO: The return result of mpReadIO is not checked
00317       mpReadIO (&info, &readValue, 1);
00318       mpTaskDelay(VAR_POLL_DELAY_);
00319     } while ( ((bool)readValue) != wait_value);
00320   }
00321   else
00322   {
00323     LOG_ERROR("Bit offset: %d, is greater than size: %d", bit_offset, this->UNIV_IO_DATA_SIZE_);
00324   }
00325  }
00326 
00327 
00328 
00329 } //controller
00330 } //motoman


dx100
Author(s): Shaun Edwards (Southwest Research Institute)
autogenerated on Thu Jan 2 2014 11:29:36