taskCtrl.c
Go to the documentation of this file.00001
00010 #include "taskCtrl.h"
00011 #include "packetHandleTarget.h"
00012 #include "sh7045lib.h"
00013
00014
00015 static void watchDogTimer(runCtrl_t *run) {
00016 if (run->watch_dog_msec > 0) {
00017 if (--run->watch_dog_msec == 0) {
00018 run->mode.ctrl = MODE_SERVO_CTRL;
00019 run->mode.straight_ctrl = MODE_STOP_FIRST;
00020 run->mode.rotate_ctrl = MODE_NO_CTRL;
00021 }
00022 }
00023 }
00024
00025
00026 static void resetWatchDogTimer(runCtrl_t *run) {
00027 run->watch_dog_msec = run->watch_dog_msec_max;
00028 }
00029
00030
00031 void update_tRunCtrlState(runCtrl_t *run) {
00032 int straight_mm_vel, rotate_div16_vel;
00033 int cnt[2];
00034 int i;
00035
00036 if (++run->msec >= 1000) {
00037 run->msec = 0;
00038 ++(run->sec);
00039 }
00040 watchDogTimer(run);
00041
00042 for (i = 0; i < 2; ++i) {
00043 updateEncDiff(&run->enc[i]);
00044 }
00045 for (i = 0; i < 2; ++i) {
00046 cnt[i] =
00047 run->bodyInfo.mtr_direction[i] * run->bodyInfo.whl[i]->enc->diff;
00048 }
00049 updateBodyPosition(&run->bodyPos, cnt[WHL_RIGHT], cnt[WHL_LEFT]);
00050 updateCoordinatePosition(&run->gl_crd, &run->bodyPos);
00051 updateVelocityInfo(&run->velInfo, &run->bodyPos);
00052 convertCoordinateInfo(&run->gl_crd, &run->gl_crd_offset);
00053
00054 if (run->mode.ctrl != MODE_SERVO_CTRL) {
00055 run->mode.straight_ctrl = MODE_NO_CTRL;
00056 run->mode.rotate_ctrl = MODE_NO_CTRL;
00057 }
00058 switch (run->mode.ctrl) {
00059 case MODE_SERVO_CTRL:
00060 run->run_crd = run->gl_crd;
00061 convertCoordinateInfo(&run->run_crd, &run->run_crd_offset);
00062 straight_mm_vel = calcStraightRefVel(&run->straight, &run->mode,
00063 &run->bodyPos, &run->velInfo,
00064 &run->run_crd);
00065 rotate_div16_vel = calcRotateRefVel(&run->rotate, &run->mode,
00066 &run->bodyPos, &run->velInfo,
00067 &run->run_crd);
00068 setBodyMove(&run->bodyInfo, straight_mm_vel, rotate_div16_vel);
00069 break;
00070
00071 case MODE_DIRECT_CTRL:
00072 for (i = 0; i < 2; ++i) {
00073 set_pwm(i, run->direct[i].pwm);
00074 if (run->direct[i].mode != DIRECT_MODE_NONE) {
00075 set_mode(i, run->direct[i].mode);
00076 }
00077 }
00078 break;
00079
00080 case MODE_DIRECT_WHEEL_CTRL:
00081 for (i = 0; i < 2; ++i) {
00082 setWheelMoveVelocity(run->directWhl[i].ref_mm_sec_vel, &run->whl[i]);
00083 }
00084 break;
00085
00086 case MODE_SERVO_FREE:
00087 for (i = 0; i < 2; ++i) {
00088 setWheelFree(run->bodyInfo.whl[i]);
00089 }
00090 break;
00091 }
00092 getRawCount(&run->used_msec);
00093 }
00094
00095
00096 void recv_tRunCtrlPacket(runCtrl_t *run) {
00097 if (packetRecvHandler(run) > 0) {
00098 resetWatchDogTimer(run);
00099 }
00100 }
00101
00102
00103 void init_tRunCtrlState(runCtrl_t *run, long version) {
00104 int i;
00105
00106 run->version = version;
00107 run->msec = 0;
00108 run->sec = 0;
00109 run->watch_dog_msec = 0;
00110
00111 initEnc();
00112 initMotor();
00113 for (i = 0; i < 2; ++i) {
00114 initEncInfo(i, &run->enc[i]);
00115 initMotorInfo(i, &run->mtr[i]);
00116 initDirectInfo(&run->direct[i]);
00117 initWheelInfo(&run->whl[i], &run->mtr[i], &run->enc[i], i);
00118 initDirectWheelCtrlInfo(&run->directWhl[i]);
00119 }
00120 initBodyInfo(&run->bodyInfo, &run->whl[WHL_RIGHT], &run->whl[WHL_LEFT]);
00121 initBodyPositionInfo(&run->bodyPos, &run->bodyInfo);
00122 initVelocityCtrlInfo(&run->straight);
00123 initVelocityCtrlInfo(&run->rotate);
00124 initVelocityInfo(&run->velInfo);
00125
00126 initCoordinateInfo(&run->gl_crd);
00127 initCoordinateInfo(&run->gl_crd_offset);
00128 initCoordinateInfo(&run->run_crd);
00129 initCoordinateInfo(&run->run_crd_offset);
00130 initModeInfo(&run->mode);
00131 }
00132
00133
00134 void reset_tRunCtrlPosition(runCtrl_t *run) {
00135 initBodyPositionInfo(&run->bodyPos, &run->bodyInfo);
00136 }
00137