4
* Created on: Nov 18, 2008
8
#include <srActuator/screwDrive.h>
9
#include <srCore/actuator/hingeActuator.h>
13
#include <dtUtil/log.h>
15
#define MAX(X,Y) (X>Y?X:Y)
16
#define MIN(X,Y) (X<Y?X:Y)
21
ScrewDrive::ScrewDrive(const std::string name):
22
ActuatorBase(name, "ScrewDrive")
29
//speed is here the torque *gulp*
30
F_div_M = d_m/2.0 * ((l + M_PI*my_Screw*d_m) / (M_PI*d_m - my_Screw*l));
34
ScrewDrive::~ScrewDrive() {
35
// TODO Auto-generated destructor stub
42
void ScrewDrive::registerActuator() {
44
ActuatorBase::jointActuatorMap.insert(std::pair<dJointID, ActuatorBase*>(frontActuator->getHinge(), frontActuator));
45
ActuatorBase::jointActuatorMap.insert(std::pair<dJointID, ActuatorBase*>(backActuator->getHinge(), backActuator));
52
void ScrewDrive::setVel(float v, float deltaTime)
56
float speedLeft, speedRight;
58
if (secondScrewDrive != NULL) {
59
if (this->isOnRightHandSide) {
61
speedLeft = secondScrewDrive->getVel(deltaTime);
65
speedRight = secondScrewDrive->getVel(deltaTime);
68
std::ostringstream msg; msg.clear(); msg.str("");
69
msg << "Speed [left,right] = [" << speedLeft << "," << speedRight << "]";
72
float speedTrans = std::min(fabs(speedLeft), fabs(speedRight));
73
float speedRot = fabs(speedLeft - speedRight);
74
float sum = speedTrans + speedRot; // removed spurious calls to abs() below: both trans & rot speeds guaranteed positive now
76
speedTrans = speedTrans / sum;
77
speedRot = speedRot / sum;
80
//std::cout << "v right =" << speedRight << ", v left = " << speedLeft <<std::endl;
81
// actual super position of the movement
82
if (this->isOnRightHandSide) {
87
dJointID f0 = getHinge(0);
88
dJointID f1 = getHinge(1);
89
dJointID b0 = secondScrewDrive->getHinge(0);
90
dJointID b1 = secondScrewDrive->getHinge(1);
92
dBodyID frontBody = dJointGetBody( f0, 1);
93
//notused dBodyID backBody = dJointGetBody( b0, 1);
96
dJointGetHingeAnchor (f0, pos0);
98
dJointGetHingeAnchor (f1, pos1);
101
dJointGetHingeAnchor (b0, pos2);
103
dJointGetHingeAnchor (b1, pos3);
106
float F_Screw_left = speedLeft * F_div_M;
107
float F_Screw_right = -speedRight * F_div_M;
109
float F_Roll_left = 2 * speedLeft / d_m;
110
float F_Roll_right = 2 * speedRight / d_m;
113
if (F_Screw_left * F_Screw_right > 0) F_Screw = 2 * MIN(F_Screw_left, F_Screw_right);
116
if (F_Roll_left * F_Roll_right > 0) F_Roll = 2 * MIN(F_Roll_left, F_Roll_right);
118
std::ostringstream msg; msg.clear(); msg.str("");
119
msg << "F_Roll = " << F_Roll << ", F_Screw = " << F_Screw;
120
msg << "Position = " << osg::Vec3((pos0[0] + pos1[0]) / 2, (pos0[1] + pos1[1]) / 2, (pos0[2] + pos1[2]) / 2);
121
LOG_DEBUG(msg.str());
123
//dBodyAddRelForceAtPos (frontBody, 0, F_Roll, F_Screw*100, (pos0[0] + pos1[0]) / 2, (pos0[1] + pos1[1]) / 2, (pos0[2] + pos1[2]) / 2);
124
dBodyAddRelForceAtRelPos (frontBody, 0, 0, F_Roll,0,0,0);//, F_Screw*100);
125
//dBodyAddRelForceAtPos (backBody, 0, F_Roll, -F_Screw*100, (pos2[0] + pos3[0]) / 2, (pos2[1] + pos3[1]) / 2, (pos2[2] + pos3[2]) / 2);
126
//dBodyAddRelForce (backBody, 0, 0, -F_Roll);//, F_Screw*100);
129
// frontActuator->setODEParameter(dParamVel, speedRight);
130
// backActuator->setODEParameter(dParamVel, -speedRight * speedTrans + speedRot * speedLeft);
131
// secondScrewDrive->frontActuator->setODEParameter(dParamVel, speedLeft);
132
// secondScrewDrive->backActuator->setODEParameter(dParamVel, -speedLeft * speedTrans + speedRot * speedRight);
136
secondScrewDrive->setVel(secondScrewDrive->getVel(deltaTime), deltaTime);
138
// frontActuator->setODEParameter(dParamVel, speedLeft);
139
// backActuator->setODEParameter(dParamVel, -speedLeft * speedTrans + speedRot * speedRight);
140
// secondScrewDrive->frontActuator->setODEParameter(dParamVel, speedRight);
141
// secondScrewDrive->backActuator->setODEParameter(dParamVel, -speedRight * speedTrans + speedRot * speedLeft);
146
if(screwSound != NULL)
148
float speed = fabs(v);
151
screwSound->SetGain(0.1f+speed/10);
152
screwSound->SetPitch(0.5f+speed/100);
153
if (!screwSound->IsPlaying())
156
else if(screwSound->IsPlaying())