$search
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 strcat(filename, job); 00175 00176 // Remove the file, if it exists 00177 status = mpRemove( filename ); 00178 if (this->MP_ERROR == status) 00179 { 00180 LOG_WARN("Failed to remove job file: %s", filename); 00181 } 00182 00183 // Create the file and write the job 00184 fd = mpCreate( filename, O_WRONLY ); 00185 if (this->MP_ERROR != fd) 00186 { 00187 status = mpWrite( fd, job, strlen(job) ); 00188 if ( this->MP_ERROR != status ) 00189 { 00190 LOG_INFO("Successfully loaded file: %s, bytes written: %d", filename, status); 00191 status = mpClose(fd); 00192 if (this->MP_ERROR == status) 00193 { 00194 LOG_WARN("Failed to close file: %s, ignoring failure", filename); 00195 } 00196 rtn = true; 00197 } 00198 else 00199 { 00200 LOG_ERROR("Failed to wraite file: %s", filename); 00201 rtn = false; 00202 } 00203 } 00204 else 00205 { 00206 LOG_ERROR("Failed to create job file: %s", filename); 00207 rtn = false; 00208 } 00209 return rtn; 00210 } 00211 #undef FILE_NAM_BUFFER_SIZE 00212 00213 00214 00215 bool Controller::loadJob(char* path, char * job) 00216 { 00217 bool rtn = false; 00218 int status; 00219 status = mpLoadFile (MP_DRV_ID_DRAM, path, job ); 00220 if (this->MP_OK == status) 00221 { 00222 LOG_INFO("Loaded job file %s", job); 00223 rtn = true; 00224 } 00225 else 00226 { 00227 LOG_ERROR("Failed to load job file: %s, path: %s, returned error code: %d", 00228 job, path, status); 00229 rtn = false; 00230 } 00231 return rtn; 00232 00233 } 00234 00235 00236 void Controller::setDigitalOut(int bit_offset, bool value) 00237 { 00238 LOG_DEBUG("Setting digital out, Bit offset: %d, value: %d", bit_offset, value); 00239 if ( (bit_offset < this->UNIV_IO_DATA_SIZE_) && 00240 ( bit_offset > 0) ) 00241 { 00242 MP_IO_DATA data; 00243 data.ulAddr = this->UNIV_OUT_DATA_START_ + bit_offset; 00244 data.ulValue = value; 00245 //TODO: The return result of mpWriteIO is not checked 00246 mpWriteIO(&data, 1); 00247 } 00248 else 00249 { 00250 LOG_ERROR("Bit offset: %d, is greater than size: %d", bit_offset, this->UNIV_IO_DATA_SIZE_); 00251 } 00252 } 00253 00254 void Controller::waitDigitalIn(int bit_offset, bool wait_value) 00255 { 00256 LOG_DEBUG("Waiting for digital in, Bit offset: %d, Wait value: %d", bit_offset, wait_value); 00257 if ( (bit_offset < this->UNIV_IO_DATA_SIZE_) && 00258 ( bit_offset > 0) ) 00259 { 00260 MP_IO_INFO info; 00261 info.ulAddr = this->UNIV_IN_DATA_START_ + bit_offset; 00262 00263 USHORT readValue; 00264 do 00265 { 00266 readValue = !wait_value; 00267 //TODO: The return result of mpReadIO is not checked 00268 mpReadIO (&info, &readValue, 1); 00269 mpTaskDelay(VAR_POLL_DELAY_); 00270 } while ( ((bool)readValue) != wait_value); 00271 } 00272 else 00273 { 00274 LOG_ERROR("Bit offset: %d, is greater than size: %d", bit_offset, this->UNIV_IO_DATA_SIZE_); 00275 } 00276 } 00277 00278 00279 00280 } //controller 00281 } //motoman