00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <memory>
00020
00021
00022 #include "LOW_compJalousieController.h"
00023
00024 #include "LOW_netSegment.h"
00025 #include "LOW_platformMisc.h"
00026 #include "LOW_thread_Factory.h"
00027
00028
00029
00030
00031
00032
00033
00034
00035 int LOW_compJalousieController::jcCounter = 0;
00036
00037
00038
00039
00040
00041
00042
00043
00044 LOW_compJalousieController::LOW_compJalousieController( const LOW_devDS2405 &inPowerControl,
00045 const LOW_devDS2405 &inDirectionControl,
00046 const float inClosingTime, const float inOpeningTime,
00047 const float inAngleClosingTime, const float inAngleOpeningTime) :
00048 jcSerialNum( jcCounter++),
00049 powerControl(inPowerControl),
00050 directionControl(inDirectionControl),
00051 closingTime(inClosingTime),
00052 openingTime(inOpeningTime),
00053 angleClosingTime(inAngleClosingTime),
00054 angleOpeningTime(inAngleOpeningTime)
00055 {
00056 if ( powerControl.getID() == directionControl.getID() )
00057 throw compJalousieController_error( "Same device for powerControl and directionControl.", __FILE__, __LINE__);
00058
00059 if ( !powerControl.getNetSegment().getHasExternalPower() || !directionControl.getNetSegment().getHasExternalPower() )
00060 throw compJalousieController_error( "Network segment must supply power.", __FILE__, __LINE__);
00061
00062 __linkRecMutex = LOW_thread_Factory::new_mutex( LOW_thread_mutex::mutexKind_recursive);
00063
00064 try {
00065 jcLock lockIt = jcLock( *this);
00066
00067
00068 if ( powerControl.cmd_SearchActive() == true ) {
00069 powerControl.cmd_Match();
00070 if ( powerControl.cmd_SearchActive() == true )
00071 throw compJalousieController_error( "Cannot initialize powerControl.", __FILE__, __LINE__);
00072 }
00073
00074
00075 if ( directionControl.cmd_SearchActive() == true ) {
00076 directionControl.cmd_Match();
00077 if ( directionControl.cmd_SearchActive() == true )
00078 throw compJalousieController_error( "Cannot initialize directionControl.", __FILE__, __LINE__);
00079 }
00080 }
00081 catch (...) {
00082 delete __linkRecMutex;
00083 throw;
00084 }
00085
00086 powerIsOn = false;
00087 directionIsDown = false;
00088
00089 stop2upDelay = 70;
00090 up2stopDelay = 70;
00091 stop2downDelay = 140;
00092 down2stopDelay = 140;
00093 up2downDelay = 70;
00094 down2upDelay = 70;
00095 }
00096
00097
00098 LOW_compJalousieController::~LOW_compJalousieController()
00099 {
00100 try {
00101 jcLock lockIt = jcLock( *this);
00102 stopMove();
00103 }
00104 catch (...) {
00105 delete __linkRecMutex;
00106 throw;
00107 }
00108
00109 delete __linkRecMutex;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119 void LOW_compJalousieController::setPosition( float inPos, float inAngle)
00120 {
00121 jcLock lockIt = jcLock( *this);
00122
00123 if ( inPos<0.0 || inPos>1.0 )
00124 throw compJalousieController_error( "Illegal position parameter.", __FILE__, __LINE__);
00125
00126 if ( inAngle<0.0 || inAngle>1.0 )
00127 throw compJalousieController_error( "Illegal angle parameter.", __FILE__, __LINE__);
00128
00129
00130 if ( inPos > 0.5 ) {
00131
00132
00133 moveDownStop( static_cast<unsigned long>((closingTime*1000)+fullMovementAdditionMillis));
00134
00135 if ( inPos != 1.0 ) {
00136
00137 moveUpStop( static_cast<unsigned long>(((1.0-inPos)*(openingTime*1000))));
00138
00139 if ( inAngle != 0.0 ) {
00140
00141 moveDownStop( static_cast<unsigned long>(inAngle*(angleClosingTime*1000)));
00142 }
00143 }
00144 else if ( inAngle != 1.0 ) {
00145
00146 moveUpStop( static_cast<unsigned long>((1.0-inAngle)*(angleOpeningTime*1000)));
00147 }
00148 }
00149
00150
00151 else {
00152
00153
00154 moveUpStop( static_cast<unsigned long>((openingTime*1000)+fullMovementAdditionMillis));
00155
00156 if ( inPos != 0.0 ) {
00157
00158 moveDownStop( static_cast<unsigned long>(inPos*(closingTime*1000)));
00159
00160 if ( inAngle != 1.0 ) {
00161
00162 moveUpStop( static_cast<unsigned long>((1.0-inAngle)*(angleOpeningTime*1000)));
00163 }
00164 }
00165 else if ( inAngle != 0.0 ) {
00166
00167 moveDownStop( static_cast<unsigned long>(inAngle*(angleClosingTime*1000)));
00168 }
00169 }
00170 }
00171
00172
00173 float LOW_compJalousieController::getClosingTime() const
00174 {
00175 return closingTime;
00176 }
00177
00178
00179 void LOW_compJalousieController::setClosingTime( const float inClosingTime)
00180 {
00181 jcLock lockIt = jcLock( *this);
00182 closingTime = inClosingTime;
00183 }
00184
00185
00186 float LOW_compJalousieController::getOpeningTime() const
00187 {
00188 return openingTime;
00189 }
00190
00191
00192 void LOW_compJalousieController::setOpeningTime( const float inOpeningTime)
00193 {
00194 jcLock lockIt = jcLock( *this);
00195 openingTime = inOpeningTime;
00196 }
00197
00198
00199 float LOW_compJalousieController::getAngleClosingTime() const
00200 {
00201 return angleClosingTime;
00202 }
00203
00204
00205 void LOW_compJalousieController::setAngleClosingTime( const float inAngleClosingTime)
00206 {
00207 jcLock lockIt = jcLock( *this);
00208 angleClosingTime = inAngleClosingTime;
00209 }
00210
00211
00212 float LOW_compJalousieController::getAngleOpeningTime() const
00213 {
00214 return angleOpeningTime;
00215 }
00216
00217
00218 void LOW_compJalousieController::setAngleOpeningTime( const float inAngleOpeningTime)
00219 {
00220 jcLock lockIt = jcLock( *this);
00221 angleOpeningTime = inAngleOpeningTime;
00222 }
00223
00224
00225 void LOW_compJalousieController::measureTransitionDelays()
00226 {
00227 jcLock lockIt = jcLock( *this);
00228
00229 const unsigned int loopCnt = 10;
00230 std::vector<unsigned int> t_sp2up, t_up2sp, t_sp2dn, t_dn2sp, t_up2dn, t_dn2up;
00231 LOW_platformMisc::timestamp_t t_start, t_stop, t_diff;
00232
00233 for( unsigned int a=0; a<loopCnt; a++) {
00234 LOW_platformMisc::getTimestamp( t_start);
00235 moveUp();
00236 LOW_platformMisc::getTimestamp( t_stop);
00237 LOW_platformMisc::diffTimestamp( t_stop, t_start, t_diff);
00238 t_sp2up.push_back( t_diff.milSec);
00239
00240 LOW_platformMisc::getTimestamp( t_start);
00241 stopMove();
00242 LOW_platformMisc::getTimestamp( t_stop);
00243 LOW_platformMisc::diffTimestamp( t_stop, t_start, t_diff);
00244 t_up2sp.push_back( t_diff.milSec);
00245
00246 LOW_platformMisc::getTimestamp( t_start);
00247 moveDown();
00248 LOW_platformMisc::getTimestamp( t_stop);
00249 LOW_platformMisc::diffTimestamp( t_stop, t_start, t_diff);
00250 t_sp2dn.push_back( t_diff.milSec);
00251
00252 LOW_platformMisc::getTimestamp( t_start);
00253 stopMove();
00254 LOW_platformMisc::getTimestamp( t_stop);
00255 LOW_platformMisc::diffTimestamp( t_stop, t_start, t_diff);
00256 t_dn2sp.push_back( t_diff.milSec);
00257
00258 moveUp();
00259 LOW_platformMisc::getTimestamp( t_start);
00260 moveDown();
00261 LOW_platformMisc::getTimestamp( t_stop);
00262 LOW_platformMisc::diffTimestamp( t_stop, t_start, t_diff);
00263 t_up2dn.push_back( t_diff.milSec);
00264
00265 moveDown();
00266 LOW_platformMisc::getTimestamp( t_start);
00267 moveUp();
00268 LOW_platformMisc::getTimestamp( t_stop);
00269 LOW_platformMisc::diffTimestamp( t_stop, t_start, t_diff);
00270 t_dn2up.push_back( t_diff.milSec);
00271
00272 stopMove();
00273 }
00274
00275 stop2upDelay = averageMillis( t_sp2up);
00276 up2stopDelay = averageMillis( t_up2sp);
00277 stop2downDelay = averageMillis( t_sp2dn);
00278 down2stopDelay = averageMillis( t_dn2sp);
00279 up2downDelay = averageMillis( t_up2dn);
00280 down2upDelay = averageMillis( t_dn2up);
00281 }
00282
00283
00284 void LOW_compJalousieController::getTransitionDelays( unsigned int &outStop2upDelay, unsigned int &outUp2stopDelay,
00285 unsigned int &outStop2downDelay, unsigned int &outDown2stopDelay,
00286 unsigned int &outUp2downDelay, unsigned int &outDown2upDelay)
00287 {
00288 outStop2upDelay = stop2upDelay;
00289 outUp2stopDelay = up2stopDelay;
00290 outStop2downDelay = stop2downDelay;
00291 outDown2stopDelay = down2stopDelay;
00292 outUp2downDelay = up2downDelay;
00293 outDown2upDelay = down2upDelay;
00294 }
00295
00296
00297 void LOW_compJalousieController::setTransitionDelays( const unsigned int inStop2upDelay, const unsigned int inUp2stopDelay,
00298 const unsigned int inStop2downDelay, const unsigned int inDown2stopDelay,
00299 const unsigned int inUp2downDelay, const unsigned int inDown2upDelay)
00300 {
00301 jcLock lockIt = jcLock( *this);
00302
00303 stop2upDelay = inStop2upDelay;
00304 up2stopDelay = inUp2stopDelay;
00305 stop2downDelay = inStop2downDelay;
00306 down2stopDelay = inDown2stopDelay;
00307 up2downDelay = inUp2downDelay;
00308 down2upDelay = inDown2upDelay;
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318 void LOW_compJalousieController::moveUp()
00319 {
00320
00321
00322 setDirectionUp();
00323 setPowerOn();
00324 }
00325
00326
00327 void LOW_compJalousieController::moveDown()
00328 {
00329
00330
00331 setDirectionDown();
00332 setPowerOn();
00333 }
00334
00335
00336 void LOW_compJalousieController::stopMove()
00337 {
00338
00339
00340 setPowerOff();
00341 setDirectionUp();
00342 }
00343
00344
00345 void LOW_compJalousieController::moveUpStop( const unsigned long inMillis)
00346 {
00347
00348
00349 unsigned long sleepTime;
00350 if ( ( (static_cast<long>(inMillis)) - (static_cast<int>(up2stopDelay)) ) < 0 )
00351 sleepTime = 0;
00352 else
00353 sleepTime = inMillis-up2stopDelay;
00354
00355 moveUp();
00356 LOW_platformMisc::milliSleep( sleepTime);
00357 stopMove();
00358 }
00359
00360
00361 void LOW_compJalousieController::moveDownStop( const unsigned long inMillis)
00362 {
00363
00364
00365 unsigned long sleepTime;
00366 if ( ( (static_cast<long>(inMillis)) - (static_cast<int>(up2stopDelay)) ) < 0 )
00367 sleepTime = 0;
00368 else
00369 sleepTime = inMillis-down2stopDelay;
00370
00371 moveDown();
00372 LOW_platformMisc::milliSleep( sleepTime);
00373 stopMove();
00374 }
00375
00376
00377
00378
00379
00380
00381
00382
00383 void LOW_compJalousieController::setPowerOn()
00384 {
00385 if ( powerIsOn )
00386 return;
00387
00388 if ( powerControl.cmd_MatchRead() == true )
00389 throw compJalousieController_error( "Error setting powerControl.", __FILE__, __LINE__);
00390
00391 powerIsOn = true;
00392 }
00393
00394
00395 void LOW_compJalousieController::setPowerOff()
00396 {
00397 if ( ! powerIsOn )
00398 return;
00399
00400 if ( powerControl.cmd_MatchRead() == false )
00401 throw compJalousieController_error( "Error setting powerControl.", __FILE__, __LINE__);
00402
00403 powerIsOn = false;
00404 }
00405
00406
00407 void LOW_compJalousieController::setDirectionDown()
00408 {
00409 if ( directionIsDown )
00410 return;
00411
00412 if ( directionControl.cmd_MatchRead() == true )
00413 throw compJalousieController_error( "Error setting directionControl.", __FILE__, __LINE__);
00414
00415 directionIsDown = true;
00416 }
00417
00418
00419 void LOW_compJalousieController::setDirectionUp()
00420 {
00421 if ( ! directionIsDown )
00422 return;
00423
00424 if ( directionControl.cmd_MatchRead() == false )
00425 throw compJalousieController_error( "Error setting directionControl.", __FILE__, __LINE__);
00426
00427 directionIsDown = false;
00428 }
00429
00430
00431 unsigned int LOW_compJalousieController::averageMillis( const std::vector<unsigned int> &inVals)
00432 {
00433 unsigned long avg = 0;
00434 for ( unsigned int a=0; a<inVals.size(); a++)
00435 avg += inVals[a];
00436 avg /= inVals.size();
00437
00438 return avg;
00439 }
00440
00441
00442
00443
00444
00445
00446
00447
00448 LOW_compJalousieController::manualMove::manualMove( LOW_compJalousieController &inJCComp) :
00449 jcLock( inJCComp),
00450 jcComp( inJCComp)
00451 {
00452 }
00453
00454
00455 LOW_compJalousieController::manualMove::~manualMove()
00456 {
00457 stopMove();
00458 }
00459
00460
00461 void LOW_compJalousieController::manualMove::moveUp()
00462 {
00463 jcComp.moveUp();
00464 }
00465
00466
00467 void LOW_compJalousieController::manualMove::moveDown()
00468 {
00469 jcComp.moveDown();
00470 }
00471
00472
00473 void LOW_compJalousieController::manualMove::stopMove()
00474 {
00475 jcComp.stopMove();
00476 }
00477
00478