00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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
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
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];
00170
00171 memset(filename, '\0', FILE_NAM_BUFFER_SIZE);
00172 strcpy(filename, "MPRAM1:\\");
00173 strcat(filename, path);
00174
00175 LOG_DEBUG("writeJob: %s", filename);
00176
00177
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
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
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
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
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
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 }
00330 }