00001 /************************************************************************* 00002 * * 00003 * Open Physics Abstraction Layer * 00004 * Copyright (C) 2004-2005 * 00005 * Alan Fischer alan.fischer@gmail.com * 00006 * Andres Reinot andres@reinot.com * 00007 * Tyler Streeter tylerstreeter@gmail.com * 00008 * All rights reserved. * 00009 * Web: opal.sourceforge.net * 00010 * * 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of EITHER: * 00013 * (1) The GNU Lesser General Public License as published by the Free * 00014 * Software Foundation; either version 2.1 of the License, or (at * 00015 * your option) any later version. The text of the GNU Lesser * 00016 * General Public License is included with this library in the * 00017 * file license-LGPL.txt. * 00018 * (2) The BSD-style license that is included with this library in * 00019 * the file license-BSD.txt. * 00020 * * 00021 * This library is distributed in the hope that it will be useful, * 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * 00024 * license-LGPL.txt and license-BSD.txt for more details. * 00025 * * 00026 *************************************************************************/ 00027 00028 #include "GearedMotor.h" 00029 #include "Joint.h" 00030 00031 namespace opal 00032 { 00033 GearedMotor::GearedMotor() 00034 : Motor() 00035 { 00036 // "mData" will be initialized in its own constructor. 00037 } 00038 00039 GearedMotor::~GearedMotor() 00040 { 00041 } 00042 00043 //void GearedMotor::init(Joint* joint, int axisNum) 00044 //{ 00045 // Motor::init(); 00046 // assert(joint->isRotational(axisNum)); 00047 // mJoint = joint; 00048 // mJointAxisNum = axisNum; 00049 //} 00050 00051 void GearedMotor::init(const GearedMotorData& data) 00052 { 00053 assert(data.joint); 00054 Motor::init(); 00055 assert(data.joint->isRotational(data.jointAxisNum)); 00056 mData = data; 00057 } 00058 00059 const GearedMotorData& GearedMotor::getData()const 00060 { 00061 return mData; 00062 } 00063 00064 MotorType GearedMotor::getType()const 00065 { 00066 return mData.getType(); 00067 } 00068 00069 void GearedMotor::setName(const std::string& name) 00070 { 00071 mData.name = name; 00072 } 00073 00074 const std::string& GearedMotor::getName()const 00075 { 00076 return mData.name; 00077 } 00078 00079 bool GearedMotor::isEnabled()const 00080 { 00081 return mData.enabled; 00082 } 00083 00084 void GearedMotor::setEnabled(bool e) 00085 { 00086 //if (!mInitCalled) 00087 //{ 00088 // return; 00089 //} 00090 00091 mData.enabled = e; 00092 } 00093 00094 void GearedMotor::internal_update() 00095 { 00096 if (mData.enabled && mData.joint) 00097 { 00098 // Global/local direction makes no difference here since 00099 // we simply use the joint axis. 00100 00101 // TODO: Fix the following case: if throttle is zero, there 00102 // will be an opposing torque at any velocity. 00103 00104 real normalizedVel = 00105 mData.joint->getVelocity(mData.jointAxisNum) / 00106 mData.maxVelocity; 00107 real magnitude = mData.maxTorque * (mData.throttle - 00108 normalizedVel); 00109 mData.joint->addTorque(mData.jointAxisNum, magnitude, 0, true); 00110 } 00111 } 00112 00113 void GearedMotor::setMaxTorque(real max) 00114 { 00115 mData.maxTorque = max; 00116 } 00117 00118 real GearedMotor::getMaxTorque()const 00119 { 00120 return mData.maxTorque; 00121 } 00122 00123 void GearedMotor::setMaxVelocity(real max) 00124 { 00125 assert(max != 0); 00126 mData.maxVelocity = max; 00127 } 00128 00129 real GearedMotor::getMaxVelocity()const 00130 { 00131 return mData.maxVelocity; 00132 } 00133 00134 void GearedMotor::setThrottle(real throttle) 00135 { 00136 assert(throttle >= -1.0 && throttle <= 1.0); 00137 mData.throttle = throttle; 00138 } 00139 00140 real GearedMotor::getThrottle()const 00141 { 00142 return mData.throttle; 00143 } 00144 00145 bool GearedMotor::internal_dependsOnJoint(Joint* j) 00146 { 00147 if (j == mData.joint) 00148 { 00149 return true; 00150 } 00151 else 00152 { 00153 return false; 00154 } 00155 } 00156 }