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