00001
00002
00003
00004
00005
00006
00007 #include "mRunCtrl.h"
00008 #include "parseArgs.h"
00009 #include "screenTask.h"
00010
00011
00012 void mRunCtrl::writeLocalPosition(const VXV::Position3D& ret_pos,
00013 unsigned long ticks) {
00014 mon->log->lock();
00015 mon->log->writeTag("run", "getLocalPosition", ticks);
00016 fprintf(mon->log->fd, " timestamp=%lu x=%d y=%d div16=%d",
00017 crd_ticks, ret_pos.x, ret_pos.y, to_div16(ret_pos.zt));
00018 mon->log->writeTagEnd();
00019 mon->log->unlock();
00020 }
00021
00022
00023 void mRunCtrl::readLocalPosition(VXV::Position& ret_pos) {
00024 mon->log->lock();
00025 unsigned long ticks = mon->log->readTag("run", "getLocalPosition");
00026
00027 const char* line = mon->log->getLineBuffer();
00028 long div16 = 0;
00029 sscanf(line, "%*s %*s timestamp=%lu x=%d y=%d div16=%ld",
00030 &crd_ticks, &ret_pos.x, &ret_pos.y, &div16);
00031 ret_pos.zt = rad(2.0 * M_PI * div16 / 0x10000);
00032 crd_position = ret_pos;
00033 mon->log->unlock();
00034
00035 mon->task->waitToTicks(ticks, cond, mutex);
00036 }
00037
00038
00039 void mRunCtrl::writeIsStable(bool ret_value, unsigned long ticks) {
00040 mon->log->lock();
00041 mon->log->writeTag("run", "isStable", ticks);
00042 fprintf(mon->log->fd, " ret_value=%d", (ret_value) ? 1 : 0);
00043 mon->log->writeTagEnd();
00044 mon->log->unlock();
00045 }
00046
00047
00048 bool mRunCtrl::readIsStable(void) {
00049 mon->log->lock();
00050 unsigned long ticks = mon->log->readTag("run", "isStable");
00051
00052 const char* line = mon->log->getLineBuffer();
00053 int ret_value = 0;
00054 sscanf(line, "%*s %*s ret_value=%d", &ret_value);
00055 mon->log->unlock();
00056 mon->task->waitToTicks(ticks, cond, mutex);
00057
00058 return (ret_value == 0) ? false : true;
00059 }
00060
00061
00062 VXV::Position3D mRunCtrl::getLocalPosition(void) {
00063 VXV::Position ret_pos;
00064
00065 if (MonitorMode == Monitor::Playback) {
00066
00067 readLocalPosition(ret_pos);
00068
00069 } else {
00070
00071 ret_pos = RunCtrl::getLocalPosition();
00072 writeLocalPosition(ret_pos, mon->getTicks());
00073 }
00074 if (MonitorMode != Monitor::Simulator) {
00075 simulator->setLocalPosition(ret_pos - local_offset);
00076 }
00077
00078 return ret_pos;
00079 }
00080
00081
00082 void mRunCtrl::stop(void) {
00083
00084
00085
00086 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex, "run", "stop");
00087 RunCtrl::stop();
00088
00089
00090 mon->scr->commandLogPrintf("stop();\n");
00091 }
00092
00093
00094 void mRunCtrl::followLine(const VXV::Position& position,
00095 const CoordinateCtrl* crd) {
00096
00097
00098
00099 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex,
00100 "run", "followLine");
00101 RunCtrl::followLine(position, crd);
00102
00103
00104 mon->scr->commandLogPrintf("followLine(%d, %d, %d);\n",
00105 position.x, position.y, position.zt.to_deg());
00106 }
00107
00108
00109 void mRunCtrl::followCircle(const VXV::Grid& center, int radius,
00110 const CoordinateCtrl* crd) {
00111
00112
00113
00114 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex,
00115 "run", "followCircle");
00116 RunCtrl::followCircle(center, radius, crd);
00117
00118
00119 mon->scr->commandLogPrintf("followCircle((%d, %d), %d);\n",
00120 center.x, center.y, radius);
00121 }
00122
00123
00124 void mRunCtrl::followCircleOnTangent(const VXV::Position& position, int radius,
00125 const CoordinateCtrl* crd) {
00126 RunCtrl::followCircleOnTangent(position, radius, crd);
00127 }
00128
00129
00130 void mRunCtrl::stopToLine(const VXV::Position& position,
00131 const CoordinateCtrl* crd) {
00132
00133
00134
00135 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex,
00136 "run", "stopToLine");
00137 RunCtrl::stopToLine(position, crd);
00138
00139
00140 mon->scr->commandLogPrintf("stopToLine(%d, %d, %d);\n",
00141 position.x, position.y, position.zt.to_deg());
00142 }
00143
00144
00145 void mRunCtrl::rotateToDirection(const VXV::Direction& direction,
00146 const CoordinateCtrl* crd) {
00147
00148
00149
00150 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex,
00151 "run", "rotateDirection");
00152 RunCtrl::rotateToDirection(direction, crd);
00153
00154
00155 mon->scr->commandLogPrintf("rotateToDirection(%d);\n",direction.to_deg());
00156 }
00157
00158
00159 void mRunCtrl::rotateAngle(const VXV::Direction& direction) {
00160
00161
00162
00163 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex,
00164 "run", "rotateAngle");
00165 RunCtrl::rotateAngle(direction);
00166
00167
00168 mon->scr->commandLogPrintf("rotateAngle(%d);\n", direction.to_deg());
00169 }
00170
00171
00172 void mRunCtrl::spin(const VXV::Direction& velocity) {
00173
00174
00175
00176 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex, "run", "spin");
00177 RunCtrl::spin(velocity);
00178
00179
00180 mon->scr->commandLogPrintf("spin(%d);\n", velocity.to_deg());
00181 }
00182
00183
00184 void mRunCtrl::lastMoveCommand(const CoordinateCtrl* crd) {
00185 mon->task->waitOnlyCommand(mon, MonitorMode, cond, mutex,
00186 "run", "lastMoveCommand");
00187 RunCtrl::lastMoveCommand(crd);
00188
00189
00190 mon->scr->commandLogPrintf("lastMoveCommand()\n");
00191 }
00192
00193
00194 bool mRunCtrl::isStable(void) {
00195
00196 if (MonitorMode == Monitor::Playback) {
00197
00198 return readIsStable();
00199
00200 } else {
00201
00202 bool ret_value = RunCtrl::isStable();
00203 writeIsStable(ret_value, mon->getTicks());
00204
00205 return ret_value;
00206 }
00207 }
00208
00209
00210 void mRunCtrl::pauseLock(void) {
00211 if (MonitorMode == Monitor::Playback) {
00212 SDL_SemWait(mon->pause_sem);
00213 }
00214 }
00215
00216
00217 void mRunCtrl::pauseUnlock(void) {
00218 if (MonitorMode == Monitor::Playback) {
00219 SDL_SemPost(mon->pause_sem);
00220 }
00221 }
00222
00223
00224 void mRunCtrl::servoCtrl(bool on) {
00225 pauseLock();
00226 RunCtrl::servoCtrl(on);
00227 pauseUnlock();
00228
00229
00230 mon->scr->commandLogPrintf("servoCtrl(%s)\n", on ? "true" : "false");
00231 }
00232
00233
00234 void mRunCtrl::setMotorPwm(int id, unsigned char duty) {
00235 pauseLock();
00236 RunCtrl::setMotorPwm(id, duty);
00237 pauseUnlock();
00238 }
00239
00240
00241 void mRunCtrl::setMotorPwm(int id, unsigned char duty[], int nums) {
00242 pauseLock();
00243 RunCtrl::setMotorPwm(id, duty, nums);
00244 pauseUnlock();
00245 }
00246
00247
00248 void mRunCtrl::setMotorMode(int id, unsigned char mode) {
00249 pauseLock();
00250 RunCtrl::setMotorPwm(id, mode);
00251 pauseUnlock();
00252 }
00253
00254
00255 void mRunCtrl::setMotorMode(int id, unsigned char mode[], int nums) {
00256 pauseLock();
00257 RunCtrl::setMotorPwm(id, mode, nums);
00258 pauseUnlock();
00259 }
00260
00261
00262 #if 0
00263 void mRunCtrl::getEncoderVel(int id, int *cnt) {
00264 RunCtrl::getEncoderVel(id, cnt);
00265 }
00266
00267
00268 void mRunCtrl::getEncoderVel(int id, int cnt[], int nums) {
00269 RunCtrl::getEncoderVel(id, cnt, nums);
00270 }
00271
00272
00273 void mRunCtrl::getEncoderValue(int id, unsigned short *cnt) {
00274 RunCtrl::getEncoderValue(id, cnt);
00275 }
00276
00277
00278 void mRunCtrl::getEncoderValue(int id, unsigned short cnt[], int nums) {
00279 RunCtrl::getEncoderValue(id, cnt, nums);
00280 }
00281 #endif
00282
00283
00284 void mRunCtrl::setWheelVel(int id, int mm_vel) {
00285 pauseLock();
00286 RunCtrl::setWheelVel(id, mm_vel);
00287 pauseUnlock();
00288 }
00289
00290
00291 void mRunCtrl::setWheelVel(int id, int mm_vel[], int nums) {
00292 pauseLock();
00293 RunCtrl::setWheelVel(id, mm_vel, nums);
00294 pauseUnlock();
00295 }
00296