runDeviceSimulate.cpp
00001 #include "encCtrl.h"
00002 #include "motorCtrl.h"
00003 #include "directDeviceCtrl.h"
00004 #include "wheelCtrl.h"
00005 #include "sh7045lib.h"
00006 #include <math.h>
00007 #include <stdlib.h>
00008
00009
00010 enum {
00011 MAX_STRAIGHT_VELOCITY = 650,
00012 };
00013
00014 static int EncRef[2] = { 0, 0 };
00015 static int MtrMode[2] = { DIRECT_MODE_NONE, DIRECT_MODE_NONE };
00016
00017
00018 void initEnc(void) {
00019 for (int i = 0; i < 2; ++i) {
00020 EncRef[i] = 0;
00021 }
00022 }
00023
00024
00025 void initEncInfo(unsigned char id, encInfo_t *enc) {
00026 enc->id = id;
00027 enc->prev = 0;
00028 enc->diff = 0;
00029 enc->enc_direction = ENC_CW_TO_PLUSE;
00030 }
00031
00032
00033 void updateEncDiff(encInfo_t *enc) {
00034
00035 enc->diff = EncRef[enc->id];
00036 }
00037
00038
00039 void set_mode(const unsigned char id, const int mode) {
00040 MtrMode[id] = mode;
00041 }
00042
00043
00044 static void setDirectPwmVel(int id, int duty, int mode) {
00045 int enc_ref = duty - 5 + static_cast<int>(7.0 * rand()/(RAND_MAX+1.0) -3);
00046 enc_ref = (enc_ref < 0) ? 0 : enc_ref;
00047
00048 EncRef[id] = ((mode == DIRECT_MODE_CW) ? +1 : -1) * enc_ref;
00049 }
00050
00051
00052 void set_pwm(const unsigned char id, const int duty) {
00053 switch (MtrMode[id]) {
00054
00055 case DIRECT_MODE_CW:
00056 setDirectPwmVel(id, duty, DIRECT_MODE_CW);
00057 break;
00058
00059 case DIRECT_MODE_CCW:
00060 setDirectPwmVel(id, duty, DIRECT_MODE_CCW);
00061 break;
00062
00063 case DIRECT_MODE_FREE:
00064 EncRef[id] = (EncRef[id] * 3) >> 2;
00065 break;
00066 }
00067 }
00068
00069
00070 void initMotor(void) {
00071 for (int i = 0; i < 2; ++i) {
00072 EncRef[i] = 0;
00073 }
00074 }
00075
00076
00077 void initMotorInfo(unsigned char id, motorInfo_t *mtr) {
00078 mtr->id = id;
00079 }
00080
00081
00082 void setMotorFree(motorInfo_t *mtr) {
00083 EncRef[mtr->id] = (EncRef[mtr->id] * 3) >> 2;
00084 }
00085
00086
00087 int setMotorRevolution(const int ref_cnt, const int cnt_diff,
00088 motorInfo_t *mtr) {
00089 const static int max_cnt =
00090 (int)(2 * MAX_STRAIGHT_VELOCITY * WHL_GEAR_RATIO
00091 / (M_PI * WHL_DIAMETER_RIGHT));
00092 int cnt;
00093
00094
00095 if (abs(ref_cnt) > max_cnt) {
00096
00097 cnt = (ref_cnt < 0) ? -max_cnt : +max_cnt;
00098 } else {
00099 cnt = ref_cnt;
00100 }
00101 EncRef[mtr->id] = cnt;
00102 return 0;
00103 }
00104
00105
00106 void setDirectMotorMode(unsigned char id, int mode) {
00107 }
00108
00109 void setDirectMotorPwm(unsigned char id, int duty) {
00110 }
00111
00112 void getRawCount(unsigned short *cnt) {
00113 *cnt = 0;
00114 }
00115