ScorbotController.C
00001 #include "Robots/Scorbot/ScorbotController.H"
00002
00003 const ModelOptionCateg MOC_ScorbotComponent = {
00004 MOC_SORTPRI_3, "Scorbot Related Options" };
00005
00006 const ModelOptionDef OPT_SerialDev =
00007 { MODOPT_ARG(std::string), "SerialDev", &MOC_ScorbotComponent, OPTEXP_CORE,
00008 "Serial Device",
00009 "serial-dev", '\0', "<string>", "/dev/ttyUSB0" };
00010
00011
00012
00013 ScorbotController::ScorbotController(int id, OptionManager& mgr,
00014 const std::string& descrName, const std::string& tagName) :
00015 RobotBrainComponent(mgr, descrName, tagName),
00016 itsSerialDev(&OPT_SerialDev, this, 0),
00017 itsSerial(new Serial(mgr))
00018 {
00019 addSubComponent(itsSerial)
00020 }
00021
00022
00023 ScorbotController::start1()
00024 {
00025
00026 itsSerial->configure(itsSerialDev.getVal(), 115200, "8N1", false, false, 0);
00027 }
00028
00029
00030 void ScorbotController::registerTopics()
00031 {
00032 LINFO("Registering Scorbot Messages");
00033
00034 this->registerSubscription("ScorbotControl");
00035 }
00036
00037
00038 void ScorbotController::evolve()
00039 {
00040
00041 }
00042
00043
00044 void ScorbotController::updateMessage(const RobotSimEvents::EventMessagePtr& eMsg,
00045 const Ice::Current&)
00046 {
00047
00048 if(eMsg->ice_isA("::RobotSimEvents::CameraConfigMessage"))
00049 {
00050 }
00051 }
00052
00053
00054
00055 void ScorbotController::setMotor(JOINT m, int pwm)
00056 {
00057 unsigned char cmd[6];
00058
00059 if (pwm > MAX_PWM) pwm = MAX_PWM;
00060 if (pwm < -MAX_PWM) pwm = -MAX_PWM;
00061
00062 cmd[0] = 10;
00063
00064 if (m == WRIST_ROLL || m == WRIST_PITCH)
00065 {
00066 cmd[1] = WRIST1;
00067 cmd[3] = 10;
00068 cmd[4] = WRIST2;
00069
00070 if (pwm >= 0)
00071 cmd[2] = (short int)pwm&0x7F;
00072 else
00073 cmd[2] = (abs((short int)pwm)&0x7F) | 0x80;
00074
00075 if (m == WRIST_ROLL)
00076 {
00077 if (pwm >= 0)
00078 cmd[5] = (short int)pwm&0x7F;
00079 else
00080 cmd[5] = (abs((short int)pwm)&0x7F) | 0x80;
00081 } else {
00082 if (pwm <= 0)
00083 cmd[5] = (short int)pwm&0x7F;
00084 else
00085 cmd[5] = (abs((short int)pwm)&0x7F) | 0x80;
00086 }
00087
00088
00089 for(int i=0; i<3; i++)
00090 {
00091 itsSerial->write(cmd, 6);
00092 usleep(1000);
00093 }
00094
00095 } else {
00096 cmd[1] = m;
00097 if (pwm >= 0)
00098 cmd[2] = (short int)pwm&0x7F;
00099 else
00100 cmd[2] = (abs((short int)pwm)&0x7F) | 0x80;
00101
00102 for(int i=0; i<3; i++)
00103 {
00104 itsSerial->write(cmd, 3);
00105 usleep(1000);
00106 }
00107 }
00108
00109 }
00110
00111
00112 void ScorbotController::stopAllMotors()
00113 {
00114 unsigned char cmd[1];
00115 cmd[0] = 11;
00116 itsSerial->write(cmd, 1);
00117 }
00118
00119
00120 int Scorbot::getMicroSwitch()
00121 {
00122 unsigned char cmd;
00123 unsigned char buf;
00124 cmd = 15;
00125
00126 itsSerial->write(&cmd, 1);
00127
00128 usleep(1000);
00129 itsSerial->read(&buf, 1);
00130 return buf;
00131 }
00132
00133
00134 int ScorbotController::getEncoder(JOINT m)
00135 {
00136 unsigned char cmd[2];
00137 unsigned char buf[2];
00138
00139 if (m == WRIST_ROLL || m == WRIST_PITCH)
00140 {
00141 int wrist1 = getEncoder(WRIST1);
00142 int wrist2 = getEncoder(WRIST2);
00143
00144 if (m==WRIST_PITCH)
00145 return wrist1-wrist2;
00146 else
00147 return wrist1+wrist2;
00148
00149 } else {
00150 cmd[0] = 12;
00151 cmd[1] = m;
00152
00153 short val = 0;
00154 itsSerial->write(cmd, 2);
00155
00156 usleep(1000);
00157 int i = itsSerial->read(buf, 2);
00158 if (i == 1)
00159 i += itsSerial->read(buf+1, 1);
00160
00161 if (i < 2)
00162 {
00163 LFATAL("Error reading encoder value read(%i)", i);
00164 }
00165
00166 val = (buf[0] << 8) + buf[1];
00167 return val;
00168 }
00169 return 0;
00170 }
00171
00172
00173 void ScorbotController::setSafety(bool val)
00174 {
00175 unsigned char cmd[2];
00176 cmd[0] = SAFE_MODE;
00177 if (val)
00178 cmd[1] = 1;
00179 else
00180 cmd[1] = 0;
00181
00182 itsSerial->write(cmd, 2);
00183 }
00184
00185
00186 int ScorbotController::getPWM(JOINT m)
00187 {
00188 unsigned char cmd[2];
00189 unsigned char buf[2];
00190 cmd[0] = GET_PWM;
00191 cmd[1] = m;
00192 itsSerial->write(cmd, 2);
00193
00194 int i = itsSerial->read(buf, 1);
00195 if (i < 1) return -1;
00196 return buf[0];
00197 }
00198
00199
00200
00201
00202