Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

USARBot.HelicopterRobot


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
class HelicopterRobot extends AerialVehicle config(USARBot);

// Programming variables (these variables are transparent to the users)
var bool initialized;
var float currentVel, PropellerForce, currentTime, oldTime, maxPitch, maxRoll, ControllerSpeed;
var int PropellerIndex, AntiTorqueIndex;
var vector TorqueToApply;

function ProcessCarInput()
{
    // Programming variables
    local int i;
    local float maxSpeed, InputSpeed;
    local bool isCommandNormalized;
    local rotator Rotation_Desired, Rotation_Current;
    
    Super.ProcessCarInput();

    // Initializes various variables and fills up arrays. Note: this section is only executed once.
    if(!initialized)
    {
        // Here, we get the indexes of the propeller and of the anti-torque propeller
        for(i=0; i<JointParts.length; i++)
        {
            // Get main propeller
            if(InStr(Caps(string(JointParts[i].PartName)), "PRIMARY") != -1)
            {
                PropellerIndex = i;
            }
            
            // Get anti torque propeller
            if(InStr(Caps(string(JointParts[i].PartName)), "SECONDARY") != -1)
            {
                AntiTorqueIndex = i;
            }
        }

        // Initialize the controller's properties
        USARRemoteBot(Controller).Normalized = false;
        USARRemoteBot(Controller).Propeller = 0.0;
        USARRemoteBot(Controller).AircraftRotation = Rot(0,0,0);
        initialized = true;
        
        // Section used for debugging purposes to see if the correct joints have been saved
        if(bDebug)
        {
            Log("Primary Propeller: " $ PropellerIndex);
            Log("Secondary Propeller: " $ AntiTorqueIndex);
        }
    }
    
    PropellerForce = 0;
    TorqueToApply = Vect(0,0,0);
    currentTime = Level.TimeSeconds;

    // If a DRIVE command was issued
    if(USARRemoteBot(Controller).bNewThrottle)
    {
        isCommandNormalized = USARRemoteBot(Controller).Normalized;    // Get the normalized bool from the controller
        ControllerSpeed = USARRemoteBot(Controller).Propeller;         // Get the propeller's spin speed value from the controller
        Rotation_Desired = USARRemoteBot(Controller).AircraftRotation; // Get the desired aircraft rotation from the controller
        Rotation_Current = Rotation;
        if(Rotation_Current.Yaw < 0) Rotation_Current.Yaw += 65536;    // Make sure the current rotation is between 0 and +2Pi (instead of -Pi and +Pi)

        maxSpeed = Propeller(Parts[PropellerIndex]).maxSpinSpeed; // Get the maximum spin speed for this propeller

        // Here, we make sure the propeller spin speed is a valid value, depending on whether or not the command is normalized
        if(isCommandNormalized)
        {
            if(ControllerSpeed < 0)        InputSpeed = 0;                                 // If the normalized value is less than 0, we make sure the propeller's speed is 0 (it cannot be negative)
            else if(ControllerSpeed > 100) InputSpeed = maxSpeed;                          // If the normalized value is more than 100, we use the propeller's maximum spin speed
            else                           InputSpeed = (ControllerSpeed/100) * maxSpeed;  // If the normalized value is between -100 and 100, we use a percentage of the maximum spin speed
        }
        else
        {
            if(ControllerSpeed < 0)             InputSpeed = 0;               // If the absolute value is less than 0, we make sure the propeller's speed is 0 (it cannot be negative)
            else if(ControllerSpeed > maxSpeed) InputSpeed = maxSpeed;        // If the absolute value is more than the maximum speed, we use the propeller's maximum spin speed
            else                                InputSpeed = ControllerSpeed; // Otherwise, we use the controller's value for the propeller's spin speed
        }
        
        // Here, we make sure the desired rotation's pitch (from the controller) is valid
        if(Rotation_Desired.Pitch < -Converter.AngleToUU(maxPitch))     Rotation_Desired.Pitch = -Converter.AngleToUU(maxPitch);
        else if(Rotation_Desired.Pitch > Converter.AngleToUU(maxPitch)) Rotation_Desired.Pitch = Converter.AngleToUU(maxPitch);

        // Here, we make sure the desired rotation's roll (from the controller) is valid
        if(Rotation_Desired.Roll < -Converter.AngleToUU(maxRoll))     Rotation_Desired.Roll = -Converter.AngleToUU(maxRoll);
        else if(Rotation_Desired.Roll > Converter.AngleToUU(maxRoll)) Rotation_Desired.Roll = Converter.AngleToUU(maxRoll);
        
        // Here, we make sure the desired rotation's yaw (from the controller) is valid (between 0 and +2Pi)
        if(Rotation_Desired.Yaw > 65536) // If the controller's value is more than 2Pi
        {
            while(Rotation_Desired.Yaw > 65536) // Subtract 2Pi until the value is less than 2Pi
            {
                Rotation_Desired.Yaw = Rotation_Desired.Yaw - 65536;
            }
        }
        else if(Rotation_Desired.Yaw < 0) // If the controller's value is less than 0
        {
            while(Rotation_Desired.Yaw < 0) // Add 2Pi until the value is more than 0
            {
                Rotation_Desired.Yaw = Rotation_Desired.Yaw + 65536;
            }
        }

        // Physically spin the primary propeller (spin the part)
        setSpinSpeed(PropellerIndex, Converter.SpinSpeedToUU(InputSpeed));
        setSpinSpeed(AntiTorqueIndex, -Converter.SpinSpeedToUU(InputSpeed));   // Used to balance the helicopter

        // Find the velocity generated by the propeller
        currentVel = (InputSpeed/6.283185307179586476925286766559) * (Propeller(Parts[PropellerIndex]).Pitch);

        // Find the force generated by the propeller
        PropellerForce = getPropellerForce();

        // The helicopter will only pitch/roll/yaw when the force generated by the propeller is greater than the force of gravity
        if(PropellerForce >= (9.8 * ChassisMass))
        {
            // Here we deal with the helicopter rotation -> PITCH
            if(Rotation_Desired.Pitch > Rotation.Pitch)      TorqueToApply.Y = -(InputSpeed/2);
            else if(Rotation_Desired.Pitch < Rotation.Pitch) TorqueToApply.Y = (InputSpeed/2);
            else                                             TorqueToApply.Y = 0;

            // Here we deal with the helicopter rotation -> ROLL
            if(Rotation_Desired.Roll > Rotation.Roll)      TorqueToApply.X = -(InputSpeed/2);
            else if(Rotation_Desired.Roll < Rotation.Roll) TorqueToApply.X = (InputSpeed/2);
            else                                           TorqueToApply.X = 0;

            // Here, we deal with the helicopter roation -> YAW
            if(Rotation_Desired.Yaw == Rotation_Current.Yaw)             TorqueToApply.Z = 0;
            else if(RotateClockwise(Rotation_Current, Rotation_Desired)) TorqueToApply.Z = (InputSpeed/2);
            else                                                         TorqueToApply.Z = -(InputSpeed/2);

        }

        oldTime = currentTime;

        KWake(); // Make sure that the Karma Engine is "awake"
    }
}

//*********************************************************************************************************************
// RotateClockwise Function
// --------------------------
//     This function returns 'true' when it is faster to rotate clockwise to get from the current yaw to the deisred
//   yaw.  It returns 'false' when it is faster to rotate counter-clockwise to get from the current yaw to the desired
//   yaw.
//*********************************************************************************************************************
function bool RotateClockwise(rotator current, rotator desired)
{
    local int DistanceToCover;

    // Here, we calculate the distance to cover, in UU angle, to go from the current yaw to the desired yaw
    if(current.Yaw > desired.Yaw) DistanceToCover = 65536 - current.Yaw + desired.Yaw;
    else                          DistanceToCover = desired.Yaw - current.Yaw;

    return (DistanceToCover <= 32768); // If the distance to cover is less than Pi, we rotate the helicopter clockwise
}

//*********************************************************************************************************************
// getPropellerForce Function
// --------------------------
//     Function that calculates and returns the force generated by the helicopter's propeller.
//*********************************************************************************************************************
function float getPropellerForce()
{
    return (1.1 * currentVel * ChassisMass) / (currentTime - oldTime);
}

//*********************************************************************************************************************
// KApplyForce Event
// -----------------
//     Event called at the same frequency of the Tick function, as long as the karma engine is "awake".
//     The event calculates the appropriate forces and torques, based on the helicopter's rotation, which
//   are then automatically applied by the Karma Engine.
//*********************************************************************************************************************
event KApplyForce(out vector Force, out vector Torque)
{
    local float GravityForce;

    GravityForce = -9.8 * ChassisMass;

    // Set the Force to be applied by the Karma Engine, based on the helicopter's rotation
    Force = (PropellerForce * Vect(0,0,1)) >> Rotation;
    Force.Z += GravityForce;

    // Set the Linear Damping (the linear resistance), depending on whether or not the helicopter has enough power
    if(ControllerSpeed >= 7)     KarmaParamsRBFull(KParams).KLinearDamping = 5;
    else if(ControllerSpeed < 7) KarmaParamsRBFull(KParams).KLinearDamping = 0.5;

    // Set the Torque to be applied by the Karma Engine, based on the helicopter's rotation
    Torque = TorqueToApply >> Rotation;
}

//*********************************************************************************************************************
// DEFAULT PROPERTIES
// DO NOT change these properties since they are used to initialize programming variables
//*********************************************************************************************************************
defaultproperties
{
    initialized = false
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: Fr 19.1.2007 22:41:30.000 - Creation time: Mo 16.4.2007 11:20:48.265 - Created with UnCodeX