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

USARBot.EncoderSensor


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
class EncoderSensor extends sensor config (USARBot);

/**
    This class simulates the encoder sensor.

    The added config params are:
      Resolution:   The resoulution of the encoder defined in UU.

    The returned value is:
      Tick: The data range is (-65535/uuResolution)~(65535/uuResolution).
            Positive value means clockwise direction. It's the controller's
            resposible to count how many circles the part has rotated.
*/

var USARRemoteBot rBot;

var config float Resolution;

var int uuResolution;
var string tickData;
var int myTick;
var vector axis1,axis2;
var int lastAng, curAng;
var int lastBaseAng, curBaseAng;
var float totalAng, myAng, baseAng;
var Actor parBase;

function Init(String SName, Actor parent, vector position, rotator direction, KVehicle platform, name mount)
{
    Super.Init(SName, parent, position, direction, platform, mount);

    axis1 = vector(direction); // the turning axis
    axis2 = vector(direction+rot(16384,0,0));

    if (parent.Owner!=None)
        parBase = parent.Owner;
    else if (parent.Base!=None)
        parBase = parent.Base;
    else
        parBase = None;
}

function ConvertParam(USARConverter converter)
{
    if (converter!=None)
        uuResolution = converter.AngleToUU(Resolution);
    else
        uuResolution = Resolution;
}

function int getTick()
{
    local vector newAxis1,newAxis2,lastAxis;
    local Quat relQ, curQ;
    local float difAng,difCos,difSign;

    // my angle
    curQ = base.KGetRBQuaternion();
    newAxis1 = QuatRotateVector(curQ,axis1);
    newAxis2 = QuatRotateVector(curQ,axis2);

    relQ = QuatFindBetween(newAxis1,axis1);
    lastAxis = QuatRotateVector(relQ,newAxis2);

    difCos = lastAxis Dot axis2;
    difCos = int((difCos*10000+5))/10000.0;
    if (difCos>1.0) difCos = 1.0;
    if (difCos<-1.0) difCos = -1.0;

    difSign = (lastAxis Cross axis2) Dot axis1;
    if (difSign<0) difSign=-1.0;
    else difSign=1.0;

    curAng = difSign * ACos(difCos)*32768/PI;
    difAng = curAng - lastAng;
    lastAng = curAng;
    if (difAng>=32768)
        difAng = difAng - 65535;
    else if (difAng<-32768)
        difAng = difAng + 65535;

    myAng += difAng;

    // base's angle
    if (parBase!=None) {
        curQ = parBase.KGetRBQuaternion();
        newAxis1 = QuatRotateVector(curQ,axis1);
        newAxis2 = QuatRotateVector(curQ,axis2);

        relQ = QuatFindBetween(newAxis1,axis1);
        lastAxis = QuatRotateVector(relQ,newAxis2);

        difCos = lastAxis Dot axis2;
        difCos = int((difCos*10000+5))/10000.0;
        if (difCos>1.0) difCos = 1.0;
        if (difCos<-1.0) difCos = -1.0;

        difSign = (lastAxis Cross axis2) Dot axis1;
        if (difSign<0) difSign=-1.0;
        else difSign=1.0;

        curBaseAng = difSign * ACos(difCos)*32768/PI;
        difAng = curBaseAng - lastBaseAng;
        lastBaseAng = curBaseAng;
        if (difAng>=32768)
            difAng = difAng - 65535;
        else if (difAng<-32768)
            difAng = difAng + 65535;

        baseAng += difAng;
}

    totalAng = normalAngle(myAng - baseAng);
    if (totalAng!=(myAng - baseAng)) {
        myAng = normalAngle(myAng);
        baseAng = normalAngle(baseAng);
    }

    //log(ItemName@"dif:"$myAng@baseAng@totalAng@base.Rotation@base.Owner.Rotation);

    totalAng += RandRange(-Noise, Noise)*totalAng;
    myTick = int(normalAngle(totalAng)/uuResolution);
    return myTick;
}

function float normalAngle(float ang) {
    if (ang>65536) ang-=65536;
    if (ang<-65536) ang+=65536;
    return ang;
}

function string Set(String opcode, String args)
{
    if(Caps(opcode)=="RESET") {
        myAng = 0;
        baseAng = 0;
        totalAng = 0;
        myTick = 0;
        return "OK";
    }
    return "Failed";
    }

function String GetData()
{
    local string outstring;

    outstring = "{Name "$ItemName$" Tick "$getTick()$"}";

    return outstring;
}

function String GetConfData()
{
    local string outstring;
    outstring = Super.GetConfData();
    outstring = outstring@"{Resolution "$converter.FloatString(Resolution)$"}";
    return outstring;
}

defaultproperties
{
//Scaled with 4.762 at Mon Sep 25 14:21:52 EDT 2006
    bUseGroup=True
    ItemType="Encoder"
    HiddenSensor=false
    Resolution=0.01745 // in rad. It equals 1 deg
    DrawScale=0.0047620004
    DrawScale3D=(X=0.001)
    StaticMesh=StaticMesh'USARSim_VehicleParts_Meshes.Sensors.Sensor'
    Noise=0.001
    Mass=0.001
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: Fr 12.1.2007 20:47:52.000 - Creation time: Mo 16.4.2007 11:20:46.937 - Created with UnCodeX