DataTrace.cpp
Go to the documentation of this file.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
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #include "youbot/DataTrace.hpp"
00052 namespace youbot {
00053
00054 DataTrace::DataTrace(YouBotJoint& youBotJoint, const std::string Name, const bool overwriteFiles):joint(youBotJoint) {
00055
00056
00057 roundsPerMinuteSetpoint.rpm = 0;
00058 PWMSetpoint.pwm = 0;
00059 encoderSetpoint.encoderTicks = 0;
00060
00061 InverseMovementDirection invertDirectionParameter;
00062 joint.getConfigurationParameter(invertDirectionParameter);
00063 bool inverted = false;
00064 invertDirectionParameter.getParameter(inverted);
00065 if(inverted){
00066 invertDirection = -1;
00067 }else{
00068 invertDirection = 1;
00069 }
00070
00071 this->name = Name;
00072 if(Name != ""){
00073 this->path = Name;
00074 this->path.append("/");
00075 }
00076 char input = 0;
00077
00078 if(boost::filesystem::exists((path+"jointDataTrace").c_str())){
00079 while(input != 'y' && input != 'n' && overwriteFiles == false){
00080 std::cout << "Do you want to overwrite the existing files? [n/y]" << std::endl;
00081
00082 input = getchar();
00083
00084 if(input == 'n'){
00085 throw std::runtime_error("Will not overwrite files!");
00086 }
00087 }
00088
00089 }else{
00090 boost::filesystem::path rootPath (this->path);
00091
00092 if ( !boost::filesystem::create_directories( rootPath ))
00093 throw std::runtime_error("could not create folder!");
00094
00095 }
00096
00097
00098 }
00099
00100 DataTrace::~DataTrace() {
00101
00102
00103 }
00104
00105 void DataTrace::startTrace() {
00106
00107
00108 timeDurationMicroSec = 0;
00109
00110
00111 file.open((path+"jointDataTrace").c_str(), std::fstream::out | std::fstream::trunc);
00112
00113 ptime today;
00114 today = second_clock::local_time();
00115
00116 file << "# Name: " << this->name << std::endl;
00117
00118 file << "# Date: " << boost::posix_time::to_simple_string(today) << std::endl;
00119
00120 JointName jointName;
00121 FirmwareVersion firmwareParameter;
00122 std::string parameterString;
00123 joint.getConfigurationParameter(firmwareParameter);
00124 joint.getConfigurationParameter(jointName);
00125 jointName.toString(parameterString);
00126 file << "# " << parameterString << std::endl;
00127 firmwareParameter.toString(parameterString);
00128 file << "# " << parameterString << std::endl;
00129
00130
00131 file << "# time [milliseconds]"
00132 << " " << "angle setpoint [rad]"
00133 << " " << "velocity setpoint [rad/s]"
00134 << " " << "RPM setpoint"
00135 << " " << "current setpoint [A]"
00136 << " " << "torque setpoint [Nm]"
00137 << " " << "ramp generator setpoint [rad/s]"
00138 << " " << "encoder setpoint"
00139
00140 << " " << "sensed angle [rad]"
00141 << " " << "sensed encoder ticks"
00142 << " " << "sensed velocity [rad/s]"
00143 << " " << "sensed RPM"
00144 << " " << "sensed current [A]"
00145 << " " << "sensed torque [Nm]"
00146 << " " << "actual PWM"
00147
00148 << " " << "OVER_CURRENT" << " "
00149 << "UNDER_VOLTAGE" << " "
00150 << "OVER_VOLTAGE" << " "
00151 << "OVER_TEMPERATURE" << " "
00152 << "MOTOR_HALTED" << " "
00153 << "HALL_SENSOR_ERROR" << " "
00154 << "PWM_MODE_ACTIVE" << " "
00155 << "VELOCITY_MODE" << " "
00156 << "POSITION_MODE" << " "
00157 << "TORQUE_MODE" << " "
00158 << "POSITION_REACHED" << " "
00159 << "INITIALIZED" << " "
00160 << "TIMEOUT" << " "
00161 << "I2T_EXCEEDED" << " "
00162 << std::endl;
00163
00164 parametersBeginTraceFile.open((path+"ParametersAtBegin").c_str(), std::fstream::out | std::fstream::trunc);
00165
00166
00167
00168 parameterVector.push_back(new ActualMotorVoltage);
00169
00170 parameterVector.push_back(new ActualMotorDriverTemperature);
00171 parameterVector.push_back(new I2tSum);
00172 parameterVector.push_back(new PositionError);
00173 parameterVector.push_back(new PositionErrorSum);
00174 parameterVector.push_back(new RampGeneratorSpeed);
00175 parameterVector.push_back(new VelocityError);
00176 parameterVector.push_back(new VelocityErrorSum);
00177
00178 parameterVector.push_back(new DParameterFirstParametersPositionControl);
00179 parameterVector.push_back(new DParameterFirstParametersSpeedControl);
00180 parameterVector.push_back(new DParameterCurrentControl);
00181 parameterVector.push_back(new DParameterSecondParametersPositionControl);
00182 parameterVector.push_back(new DParameterSecondParametersSpeedControl);
00183
00184 parameterVector.push_back(new IClippingParameterFirstParametersPositionControl);
00185 parameterVector.push_back(new IClippingParameterFirstParametersSpeedControl);
00186 parameterVector.push_back(new IClippingParameterCurrentControl);
00187 parameterVector.push_back(new IClippingParameterSecondParametersPositionControl);
00188 parameterVector.push_back(new IClippingParameterSecondParametersSpeedControl);
00189
00190
00191 parameterVector.push_back(new IParameterFirstParametersPositionControl);
00192 parameterVector.push_back(new IParameterFirstParametersSpeedControl);
00193 parameterVector.push_back(new IParameterCurrentControl);
00194 parameterVector.push_back(new IParameterSecondParametersPositionControl);
00195 parameterVector.push_back(new IParameterSecondParametersSpeedControl);
00196
00197 parameterVector.push_back(new MaximumPositioningVelocity);
00198 parameterVector.push_back(new MotorAcceleration);
00199 parameterVector.push_back(new PositionControlSwitchingThreshold);
00200 parameterVector.push_back(new PParameterFirstParametersPositionControl);
00201 parameterVector.push_back(new PParameterFirstParametersSpeedControl);
00202 parameterVector.push_back(new PParameterCurrentControl);
00203 parameterVector.push_back(new PParameterSecondParametersPositionControl);
00204 parameterVector.push_back(new PParameterSecondParametersSpeedControl);
00205 parameterVector.push_back(new RampGeneratorSpeedAndPositionControl);
00206 parameterVector.push_back(new SpeedControlSwitchingThreshold);
00207
00208 parameterVector.push_back(new ActivateOvervoltageProtection);
00209 parameterVector.push_back(new ActualCommutationOffset);
00210
00211 parameterVector.push_back(new BEMFConstant);
00212
00213
00214 parameterVector.push_back(new CommutationMode);
00215 parameterVector.push_back(new CommutationMotorCurrent);
00216 parameterVector.push_back(new CurrentControlLoopDelay);
00217 parameterVector.push_back(new EncoderResolution);
00218 parameterVector.push_back(new EncoderStopSwitch);
00219 parameterVector.push_back(new HallSensorPolarityReversal);
00220 parameterVector.push_back(new I2tExceedCounter);
00221 parameterVector.push_back(new I2tLimit);
00222 parameterVector.push_back(new InitializationMode);
00223 parameterVector.push_back(new InitSineDelay);
00224 parameterVector.push_back(new MassInertiaConstant);
00225 parameterVector.push_back(new MaximumMotorCurrent);
00226 parameterVector.push_back(new MaximumVelocityToSetPosition);
00227 parameterVector.push_back(new MotorCoilResistance);
00228 parameterVector.push_back(new MotorControllerTimeout);
00229 parameterVector.push_back(new MotorPoles);
00230 parameterVector.push_back(new OperationalTime);
00231 parameterVector.push_back(new PIDControlTime);
00232 parameterVector.push_back(new PositionTargetReachedDistance);
00233 parameterVector.push_back(new ReversingEncoderDirection);
00234 parameterVector.push_back(new SetEncoderCounterZeroAtNextNChannel);
00235 parameterVector.push_back(new SetEncoderCounterZeroAtNextSwitch);
00236 parameterVector.push_back(new SetEncoderCounterZeroOnlyOnce);
00237 parameterVector.push_back(new SineInitializationVelocity);
00238 parameterVector.push_back(new StopSwitchPolarity);
00239 parameterVector.push_back(new ThermalWindingTimeConstant);
00240 parameterVector.push_back(new VelocityThresholdForHallFX);
00241 parameterVector.push_back(new MotorHaltedVelocity);
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 parametersBeginTraceFile << "Name: " << this->name << std::endl;
00259 parametersBeginTraceFile << "Date: " << boost::posix_time::to_simple_string(today) << std::endl;
00260
00261 jointName.toString(parameterString);
00262
00263 parametersBeginTraceFile << parameterString << std::endl;
00264 firmwareParameter.toString(parameterString);
00265 parametersBeginTraceFile << parameterString << std::endl;
00266
00267 TorqueConstant torqueconst;
00268 joint.getConfigurationParameter(torqueconst);
00269 torqueconst.toString(parameterString);
00270
00271 parametersBeginTraceFile << parameterString << std::endl;
00272
00273 JointLimits jointLimits;
00274 joint.getConfigurationParameter(jointLimits);
00275 jointLimits.toString(parameterString);
00276
00277 parametersBeginTraceFile << parameterString << std::endl;
00278
00279 EncoderTicksPerRound encoderTicksPerRound;
00280 joint.getConfigurationParameter(encoderTicksPerRound);
00281 encoderTicksPerRound.toString(parameterString);
00282
00283 parametersBeginTraceFile << parameterString << std::endl;
00284
00285 GearRatio gearRatio;
00286 joint.getConfigurationParameter(gearRatio);
00287 gearRatio.toString(parameterString);
00288
00289 parametersBeginTraceFile << parameterString << std::endl;
00290
00291 InverseMovementDirection inverseMovementDirection;
00292 joint.getConfigurationParameter(inverseMovementDirection);
00293 inverseMovementDirection.toString(parameterString);
00294
00295 parametersBeginTraceFile << parameterString << std::endl;
00296
00297
00298 for (unsigned int i = 0; i < parameterVector.size(); i++) {
00299 joint.getConfigurationParameter(*(parameterVector[i]));
00300 parameterVector[i]->toString(parameterString);
00301
00302 parametersBeginTraceFile << parameterString << std::endl;
00303 }
00304 parametersBeginTraceFile.close();
00305
00306
00307 traceStartTime = microsec_clock::local_time();
00308
00309 }
00310
00311 void DataTrace::stopTrace() {
00312
00313 file.close();
00314
00315 parametersEndTraceFile.open((path+"ParametersAfterTrace").c_str(), std::fstream::out | std::fstream::trunc);
00316 std::string parameterString;
00317
00318 parametersEndTraceFile << "Name: " << this->name << std::endl;
00319 ptime today;
00320 today = second_clock::local_time();
00321 parametersEndTraceFile << "Date: " << boost::posix_time::to_simple_string(today) << std::endl;
00322
00323 JointName jointName;
00324 joint.getConfigurationParameter(jointName);
00325 jointName.toString(parameterString);
00326
00327 parametersEndTraceFile << parameterString << std::endl;
00328
00329 FirmwareVersion firmwareParameter;
00330 joint.getConfigurationParameter(firmwareParameter);
00331 firmwareParameter.toString(parameterString);
00332 parametersEndTraceFile << parameterString << std::endl;
00333
00334 TorqueConstant torqueconst;
00335 joint.getConfigurationParameter(torqueconst);
00336 torqueconst.toString(parameterString);
00337
00338 parametersEndTraceFile << parameterString << std::endl;
00339
00340 JointLimits jointLimits;
00341 joint.getConfigurationParameter(jointLimits);
00342 jointLimits.toString(parameterString);
00343
00344 parametersEndTraceFile << parameterString << std::endl;
00345
00346 EncoderTicksPerRound encoderTicksPerRound;
00347 joint.getConfigurationParameter(encoderTicksPerRound);
00348 encoderTicksPerRound.toString(parameterString);
00349
00350 parametersEndTraceFile << parameterString << std::endl;
00351
00352 GearRatio gearRatio;
00353 joint.getConfigurationParameter(gearRatio);
00354 gearRatio.toString(parameterString);
00355
00356 parametersEndTraceFile << parameterString << std::endl;
00357
00358 InverseMovementDirection inverseMovementDirection;
00359 joint.getConfigurationParameter(inverseMovementDirection);
00360 inverseMovementDirection.toString(parameterString);
00361
00362 parametersEndTraceFile << parameterString << std::endl;
00363
00364 for (unsigned int i = 0; i < parameterVector.size(); i++) {
00365 joint.getConfigurationParameter(*(parameterVector[i]));
00366 parameterVector[i]->toString(parameterString);
00367 parametersEndTraceFile << parameterString << std::endl;
00368 delete parameterVector[i];
00369 }
00370
00371
00372 parametersEndTraceFile.close();
00373
00374 }
00375
00376 void DataTrace::plotTrace() {
00377
00378
00379 std::string executeString = "cd ";
00380 executeString.append(path);
00381 executeString.append("; gnuplot ../../gnuplotconfig > /dev/null 2>&1");
00382 std::system(executeString.c_str());
00383
00384 }
00385
00386 void DataTrace::updateTrace(const JointAngleSetpoint& setpoint) {
00387
00388 angleSetpoint = setpoint;
00389 controllerMode = POSITION_CONTROL_RAD;
00390 this->update();
00391
00392 }
00393
00394 void DataTrace::updateTrace(const JointVelocitySetpoint& setpoint) {
00395
00396 velocitySetpoint = setpoint;
00397 controllerMode = VELOCITY_CONTROL_RAD_SEC;
00398 this->update();
00399
00400 }
00401
00402 void DataTrace::updateTrace(const JointRoundsPerMinuteSetpoint& setpoint) {
00403
00404 roundsPerMinuteSetpoint = setpoint;
00405 controllerMode = VELOCITY_CONTROL_RPM;
00406 this->update();
00407
00408 }
00409
00410 void DataTrace::updateTrace(const JointCurrentSetpoint& setpoint) {
00411
00412 currentSetpoint = setpoint;
00413 controllerMode = CURRENT_CONTROL_MODE;
00414 this->update();
00415
00416 }
00417
00418 void DataTrace::updateTrace(const JointTorqueSetpoint& setpoint) {
00419
00420 torqueSetpoint = setpoint;
00421 controllerMode = TORQUE_CONTROL_MODE;
00422 this->update();
00423
00424 }
00425
00426 void DataTrace::updateTrace(const JointEncoderSetpoint& setpoint) {
00427
00428 encoderSetpoint = setpoint;
00429 controllerMode = POSITION_CONTROL_ENC;
00430 this->update();
00431
00432 }
00433
00434 void DataTrace::updateTrace() {
00435
00436 YouBotSlaveMsg message;
00437 joint.getData(message);
00438 switch (message.stctOutput.controllerMode) {
00439 case MOTOR_STOP:
00440 controllerMode = NOT_DEFINED;
00441 break;
00442 case POSITION_CONTROL:
00443 encoderSetpoint.encoderTicks = message.stctOutput.value * invertDirection;
00444 controllerMode = POSITION_CONTROL_ENC;
00445 break;
00446 case VELOCITY_CONTROL:
00447 roundsPerMinuteSetpoint.rpm = message.stctOutput.value * invertDirection;
00448 controllerMode = VELOCITY_CONTROL_RPM;
00449 break;
00450 case NO_MORE_ACTION:
00451 controllerMode = NOT_DEFINED;
00452 break;
00453 case SET_POSITION_TO_REFERENCE:
00454 controllerMode = NOT_DEFINED;
00455 break;
00456 case CURRENT_MODE:
00457 currentSetpoint.current = (double)message.stctOutput.value /1000.0 * invertDirection* ampere;
00458 controllerMode = CURRENT_CONTROL_MODE;
00459 break;
00460 default:
00461 controllerMode = NOT_DEFINED;
00462 break;
00463 };
00464
00465
00466 this->update();
00467
00468 }
00469
00470 unsigned long DataTrace::getTimeDurationMilliSec() {
00471
00472 return timeDurationMicroSec;
00473
00474 }
00475
00476 void DataTrace::update() {
00477
00478 timeDuration = microsec_clock::local_time() - traceStartTime;
00479 timeDurationMicroSec = timeDuration.total_milliseconds();
00480 unsigned int statusFlags;
00481 joint.getStatus(statusFlags);
00482 joint.getData(sensedAngle);
00483 joint.getData(sensedEncoderTicks);
00484 joint.getData(sensedVelocity);
00485 joint.getData(sensedRoundsPerMinute);
00486 joint.getData(sensedCurrent);
00487 joint.getData(sensedTorque);
00488 joint.getData(targetAngle);
00489 joint.getData(targetVelocity);
00490 joint.getData(targetCurrent);
00491 joint.getData(rampGenSetpoint);
00492
00493 std::stringstream angleSet, angleEncSet, velSet, velRPMSet, currentSet, pwmSet, torqueSet;
00494
00495 switch (controllerMode) {
00496 case POSITION_CONTROL_RAD:
00497 angleSet << angleSetpoint.angle.value();
00498 angleEncSet << "NaN";
00499 velSet << targetVelocity.angularVelocity.value();
00500 velRPMSet << "NaN";
00501 currentSet << targetCurrent.current.value();
00502 pwmSet << "NaN";
00503 torqueSet << "NaN";
00504 break;
00505 case POSITION_CONTROL_ENC:
00506 angleSet << "NaN";
00507 angleEncSet << encoderSetpoint.encoderTicks;
00508 velSet << targetVelocity.angularVelocity.value();
00509 velRPMSet << "NaN";
00510 currentSet << targetCurrent.current.value();
00511 pwmSet << "NaN";
00512 torqueSet << "NaN";
00513 break;
00514 case VELOCITY_CONTROL_RAD_SEC:
00515 angleSet << "NaN";
00516 angleEncSet << "NaN";
00517 velSet << velocitySetpoint.angularVelocity.value();
00518 velRPMSet << "NaN";
00519 currentSet << targetCurrent.current.value();
00520 pwmSet << "NaN";
00521 torqueSet << "NaN";
00522 break;
00523 case VELOCITY_CONTROL_RPM:
00524 angleSet << "NaN";
00525 angleEncSet << "NaN";
00526 velSet << "NaN";
00527 velRPMSet << roundsPerMinuteSetpoint.rpm;
00528 currentSet << targetCurrent.current.value();
00529 pwmSet << "NaN";
00530 torqueSet << "NaN";
00531 break;
00532 case CURRENT_CONTROL_MODE:
00533 angleSet << "NaN";
00534 angleEncSet << "NaN";
00535 velSet << "NaN";
00536 velRPMSet << "NaN";
00537 currentSet << currentSetpoint.current.value();
00538 pwmSet << "NaN";
00539 torqueSet << "NaN";
00540 break;
00541 case TORQUE_CONTROL_MODE:
00542 angleSet << "NaN";
00543 angleEncSet << "NaN";
00544 velSet << "NaN";
00545 velRPMSet << "NaN";
00546 currentSet << "NaN";
00547 pwmSet << "NaN";
00548 torqueSet << torqueSetpoint.torque.value();
00549 break;
00550 case NOT_DEFINED:
00551 angleSet << "NaN";
00552 angleEncSet << "NaN";
00553 velSet << "NaN";
00554 velRPMSet << "NaN";
00555 currentSet << "NaN";
00556 pwmSet << "NaN";
00557 torqueSet << "NaN";
00558 break;
00559 };
00560
00561
00562
00563 file << timeDurationMicroSec
00564 << " " << angleSet.str()
00565 << " " << velSet.str()
00566 << " " << velRPMSet.str()
00567 << " " << currentSet.str()
00568 << " " << torqueSet.str()
00569 << " " << rampGenSetpoint.angularVelocity.value()
00570 << " " << angleEncSet.str()
00571
00572 << " " << sensedAngle.angle.value()
00573 << " " << sensedEncoderTicks.encoderTicks
00574 << " " << sensedVelocity.angularVelocity.value()
00575 << " " << sensedRoundsPerMinute.rpm
00576 << " " << sensedCurrent.current.value()
00577 << " " << sensedTorque.torque.value()
00578 << " " << "0"
00579
00580 << " " << bool(statusFlags & OVER_CURRENT) << " "
00581 << bool(statusFlags & UNDER_VOLTAGE) << " "
00582 << bool(statusFlags & OVER_VOLTAGE) << " "
00583 << bool(statusFlags & OVER_TEMPERATURE) << " "
00584 << bool(statusFlags & MOTOR_HALTED) << " "
00585 << bool(statusFlags & HALL_SENSOR_ERROR) << " "
00586 << "0" << " "
00587 << bool(statusFlags & VELOCITY_MODE) << " "
00588 << bool(statusFlags & POSITION_MODE) << " "
00589 << bool(statusFlags & TORQUE_MODE) << " "
00590 << bool(statusFlags & POSITION_REACHED) << " "
00591 << bool(statusFlags & INITIALIZED) << " "
00592 << bool(statusFlags & TIMEOUT) << " "
00593 << bool(statusFlags & I2T_EXCEEDED)
00594 << std::endl;
00595
00596
00597
00598 }
00599
00600
00601 }