/*! \file Coupler.H c++/Coupler.H \brief C++ Train Coupler/Uncoupler Controller Class Interface \author Zhengrong.Zang@gmail.com \date 2013.10.01 Defines interface to control Train Coupler/Uncoupler */ // The contents of this file are subject to the Mozilla Public License // Version 1.0 (the "License"); you may not use this file except in // compliance with the License. You may obtain a copy of the License // at http://www.mozilla.org/MPL/ // // Software distributed under the License is distributed on an "AS IS" // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See // the License for the specific language governing rights and // limitations under the License. #ifndef _Couplert_H_ #define _Couplert_H_ #include #include #include #include #include TouchSensor *touchSensor; wakeup_t touched(wakeup_t data) { return touchSensor->pressed() || (remote_key & LRKEY_STOP); } wakeup_t untouched(wakeup_t data) { return !touchSensor->pressed() || (remote_key & LRKEY_STOP); } class Coupler { public: enum CouplerState {Coupling, Uncoupling}; static const int COUPLING_VELOCITY = 2; static const int COUPLING_SPEED = 5; static const int COUPLING_TIME = 10*400; static const int UNCOUPLING_TIME1 = 10*800; static const int UNCOUPLING_TIME2 = 10*15; private: ConstantMotor *train; Motor *couplerMotor; CouplerState mstate; public: Coupler(ConstantMotor *train, Motor::Port motorPort, Sensor::Port sensorPort, CouplerState state) { this->train = train; couplerMotor = new Motor(motorPort); touchSensor = new TouchSensor(sensorPort); mstate = state; } ~Coupler() { delete touchSensor; delete couplerMotor; } const CouplerState state() const { return mstate; } const void uncoupling() { // train->motor()->lock(); // train->motor()->brake(); train->brake(); couplerMotor->forward(Motor::max); delay(UNCOUPLING_TIME1); couplerMotor->brake(); train->forward(ConstantMotor::FAST_VELOCITY, Motor::max); // train->velocity(ConstantMotor::FAST_VELOCITY); // train->motor()->forward(Motor::max); delay(UNCOUPLING_TIME2); train->stop(); // train->motor()->stop(); // train->motor()->unlock(); couplerMotor->backward(Motor::max); delay(UNCOUPLING_TIME1); couplerMotor->stop(); mstate = Uncoupling; } const void coupling() { // train->motor()->brake(); train->brake(); couplerMotor->forward(Motor::max); delay(COUPLING_TIME); couplerMotor->brake(); // train->motor()->lock(); // train->velocity(COUPLING_VELOCITY); // train->motor()->backward(COUPLING_SPEED); train->backward(COUPLING_VELOCITY, COUPLING_SPEED); wait_event(&touched, 0); train->brake(); // train->motor()->brake(); // train->motor()->unlock(); couplerMotor->backward(Motor::max); wait_event(&untouched, 0); delay(COUPLING_TIME); couplerMotor->stop(); mstate = Coupling; } }; #endif // _Couplert_H_