00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "TorqueSimulationAxis.hpp"
00020 #include <rtt/Logger.hpp>
00021 #include <limits>
00022
00023 using namespace OCL;
00024
00025 TorqueSimulationEncoder::TorqueSimulationEncoder(double initial, double min, double max):
00026 _position(initial),
00027 _min(min),
00028 _max(max),
00029 _first_drive(true)
00030 {}
00031
00032 double
00033 TorqueSimulationEncoder::readSensor() const
00034 {
00035 if (_first_drive){
00036 return _position;
00037 }
00038
00039 else{
00040
00041 TimeService::Seconds _delta_time = TimeService::Instance()->secondsSince(_previous_time);
00042 return _position + (_velocity*(double)_delta_time);
00043 }
00044 }
00045
00046 int
00047 TorqueSimulationEncoder::readSensor(double& data) const
00048 {
00049 data = readSensor();
00050 return 0;
00051 }
00052
00053
00054 void
00055 TorqueSimulationEncoder::update(double position, double velocity, TimeService::ticks previous_time)
00056 {
00057 if (_first_drive)
00058 _first_drive = false;
00059
00060 _position = position;
00061 _velocity = velocity;
00062 _previous_time = previous_time;
00063 }
00064
00065 void
00066 TorqueSimulationEncoder::stop()
00067 {
00068 _velocity = 0.0;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 TorqueSimulationVelocitySensor::TorqueSimulationVelocitySensor( double maxvel):
00080 _velocity(0),
00081 _acceleration(0),
00082 _maxvel(maxvel),
00083 _first_drive(true)
00084 {}
00085
00086 double
00087 TorqueSimulationVelocitySensor::readSensor() const
00088 {
00089 if (_first_drive){
00090 return _velocity;
00091 }
00092
00093 else{
00094
00095 TimeService::Seconds _delta_time = TimeService::Instance()->secondsSince(_previous_time);
00096 return _velocity + (_acceleration*(double)_delta_time);
00097 }
00098 }
00099
00100 int
00101 TorqueSimulationVelocitySensor::readSensor(double& data) const
00102 {
00103 data = readSensor();
00104 return 0;
00105 }
00106
00107
00108 void
00109 TorqueSimulationVelocitySensor::update(double velocity, double acceleration, TimeService::ticks previous_time)
00110 {
00111 if (_first_drive)
00112 _first_drive = false;
00113
00114 _velocity = velocity;
00115 _acceleration = acceleration;
00116 _previous_time = previous_time;
00117 }
00118
00119 void
00120 TorqueSimulationVelocitySensor::stop()
00121 {
00122 _velocity = 0.0;
00123 _acceleration = 0.0;
00124 }
00125
00126 TorqueSimulationAxis::TorqueSimulationAxis(double initial, double min, double max, double velLim):
00127 _drive_value(0),
00128 _enable(false), _brake(true),
00129 _max_drive_value(std::numeric_limits<double>::max()),
00130 _encoder( new TorqueSimulationEncoder( initial, min, max) ),
00131 _velSensor( new TorqueSimulationVelocitySensor( velLim ) ),
00132 _curSensor( new TorqueSimulationCurrentSensor( this, _max_drive_value ) ),
00133 _is_locked(true),
00134 _is_stopped(false),
00135 _is_driven(false)
00136 {}
00137
00138
00139
00140 TorqueSimulationAxis::~TorqueSimulationAxis()
00141 {
00142 delete _encoder;
00143 delete _velSensor;
00144 delete _curSensor;
00145 }
00146
00147 bool
00148 TorqueSimulationAxis::drive( double current )
00149 {
00150
00151 if ( !_enable.isOn() )
00152 return false;
00153
00154 if ( _brake.isOn() )
00155 return false;
00156
00157 if (_is_stopped || _is_driven){
00158 if ( (current < -_max_drive_value) || (current > _max_drive_value) ){
00159
00160 stop();
00161 lock();
00162 return false;
00163 }
00164 else{
00165 _drive_value = current;
00166 _is_stopped = false;
00167 _is_driven = true;
00168 return true;
00169 }
00170 }
00171 else
00172 return false;
00173 }
00174
00175 bool
00176 TorqueSimulationAxis::stop()
00177 {
00178 if (_is_driven){
00179 _encoder->stop();
00180 _velSensor->stop();
00181 _drive_value = 0.0;
00182 _is_driven = false;
00183 _is_stopped = true;
00184 return true;
00185 }
00186 else if (_is_stopped)
00187 return true;
00188 else
00189 return false;
00190 }
00191
00192 bool
00193 TorqueSimulationAxis::lock()
00194 {
00195 if (_is_stopped){
00196 _is_locked = true;
00197 _is_stopped = false;
00198 _brake.switchOn();
00199 _enable.switchOff();
00200 return true;
00201 }
00202 else if (_is_locked)
00203 return true;
00204 else
00205 return false;
00206 }
00207
00208 bool
00209 TorqueSimulationAxis::unlock()
00210 {
00211 if (_is_locked){
00212 _is_locked = false;
00213 _is_stopped = true;
00214 _brake.switchOff();
00215 _enable.switchOn();
00216 return true;
00217 }
00218 else if (_is_stopped)
00219 return true;
00220 else
00221 return false;
00222 }
00223
00224 bool
00225 TorqueSimulationAxis::isLocked() const
00226 {
00227 return _is_locked;
00228 }
00229
00230 bool
00231 TorqueSimulationAxis::isStopped() const
00232 {
00233 return _is_stopped;
00234 }
00235
00236 bool
00237 TorqueSimulationAxis::isDriven() const
00238 {
00239 return _is_driven;
00240 }
00241
00242 DigitalOutput* TorqueSimulationAxis::getBrake()
00243 {
00244 return &_brake;
00245 }
00246
00247 DigitalOutput* TorqueSimulationAxis::getEnable()
00248 {
00249 return &_enable;
00250 }
00251
00252
00253
00254 SensorInterface<double>*
00255 TorqueSimulationAxis::getSensor(const std::string& name) const
00256 {
00257 if (name == "Position")
00258 return _encoder;
00259 if (name == "Velocity")
00260 return _velSensor;
00261 if (name == "Current")
00262 return _curSensor;
00263
00264 return NULL;
00265 }
00266
00267
00268 std::vector<std::string>
00269 TorqueSimulationAxis::sensorList() const
00270 {
00271 std::vector<std::string> result;
00272 result.push_back("Position");
00273 result.push_back("Velocity");
00274 result.push_back("Current");
00275 return result;
00276 }
00277
00278 double TorqueSimulationAxis::getDriveValue() const
00279 {
00280 return _drive_value;
00281 }
00282