MHVLib  20111227
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_StepperMotor.cpp
Go to the documentation of this file.
00001 /* Copyright (c) 2011, Make, Hack, Void Inc
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *  * Redistributions of source code must retain the above copyright
00007  *    notice, this list of conditions and the following disclaimer.
00008  *  * Redistributions in binary form must reproduce the above copyright
00009  *    notice, this list of conditions and the following disclaimer in the
00010  *    documentation and/or other materials provided with the distribution.
00011  *  * Neither the name of the Make, Hack, Void nor the
00012  *    names of its contributors may be used to endorse or promote products
00013  *    derived from this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00016  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00018  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00020  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00021  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00022  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 
00027 #include <MHV_StepperMotor.h>
00028 
00033 MHV_StepperMotor::MHV_StepperMotor(MHV_RTC &rtc) :
00034                 _position(0),
00035                 _rtc(rtc),
00036                 _moving(false),
00037                 _stepperListener(NULL) {}
00038 
00045 void MHV_StepperMotor::setPosition(int32_t      position) {
00046                 _position = position;
00047 }
00048 
00053 int32_t MHV_StepperMotor::getPosition() {
00054         return _position;
00055 }
00056 
00061 bool MHV_StepperMotor::isMoving() {
00062         return _moving;
00063 }
00064 
00071 void MHV_StepperMotor::rotate(bool forward, float speed, int32_t until) {
00072         _forward = forward;
00073         _speed = speed;
00074         _until = until;
00075 
00076 // Remove any current alarms
00077         _rtc.removeAlarm(this);
00078 
00079 // Register the alarm with the RTC
00080         MHV_ALARM newAlarm;
00081         newAlarm.listener = this;
00082         _rtc.current(&(newAlarm.when));
00083         _started.milliseconds = newAlarm.when.milliseconds;
00084         _started.timestamp = newAlarm.when.timestamp;
00085         newAlarm.repeat.milliseconds = 1;
00086         newAlarm.repeat.timestamp = 0;
00087 
00088         _startPosition = _position;
00089 
00090         _moving = true;
00091 
00092         _rtc.addAlarm(&newAlarm);
00093 }
00094 
00098 void MHV_StepperMotor::alarm(const MHV_ALARM &alarm) {
00099         if (!_moving) {
00100                 return;
00101         }
00102 
00103         MHV_TIMESTAMP elapsed;
00104         _rtc.elapsed(&_started, &elapsed);
00105 // Reuse elapsed.timestamp to represent milliseconds elapsed
00106         elapsed.timestamp *= 1000;
00107         elapsed.timestamp += elapsed.milliseconds;
00108 
00109         int32_t expectedPosition = _startPosition + elapsed.timestamp * _speed / ((_forward) ? 1000 : -1000);
00110 
00111         if (expectedPosition != _position) {
00112                 step(_forward);
00113                 _position += (_forward) ? 1 : -1;
00114         }
00115 
00116         if (_until == _position) {
00117 // We are done
00118                 _speed = 0;
00119                 _moving = false;
00120                 _rtc.removeAlarm(this);
00121                 if (_stepperListener) {
00122                         _stepperListener->moveComplete(_position);
00123                 }
00124         }
00125 }
00126 
00131 void MHV_StepperMotor::registerListener(MHV_StepperListener &listener) {
00132         _stepperListener = &listener;
00133 }
00134 
00138 void MHV_StepperMotor::deregisterListener() {
00139         _stepperListener = NULL;
00140 }