Exemple #1
0
    // Use this for initialization
    public SpiderBody()
    {
        legs     = new SpiderLeg[6];
        futures  = new Queue <Future <object> >();
        executor = new ImmediateExecutor();
        SharedParams sharedParams = new SharedParams();

        for (int i = 0; i < legs.Length; i++)
        {
            legs[i] = new SpiderLeg(ref executor, ref sharedParams, (i * 3) + 1);
        }
    }
Exemple #2
0
    /// <summary>
    /// Überprüft alle RobotParts auf ihre TerrainCompatability.
    /// Wenn die TerrainTypen mit dem TerrainTypen des übergebenen tiles übereinstimmen, wird true, sonst wird false zurückgegeben.
    /// </summary>
    /// <param name="tile"></param>
    /// <returns></returns>
    public bool CanWalkOn(GroundTile tile)
    {
        bool canWalk = false;

        foreach (RobotPart part in parts)
        {
            if (part is BasicLeg)
            {
                BasicLeg leg = part as BasicLeg;
                if (leg.TerrainCompatability == tile.terrainType)
                {
                    canWalk = true;
                    break;
                }
            }
            else if (part is SpiderLeg)
            {
                SpiderLeg leg = part as SpiderLeg;
                if (leg.TerrainCompatability == tile.terrainType)
                {
                    canWalk = true;
                    break;
                }
            }
            else if (part is Boat)
            {
                Boat leg = part as Boat;
                if (leg.TerrainCompatability == tile.terrainType)
                {
                    canWalk = true;
                    break;
                }
            }
        }
        return(canWalk);
    }
    /// <summary>
    /// Erstellt die Für den Test nötigen Objekte.
    /// </summary>
    public void TestSolve()
    {
        //Robot1
        GameObject robot1Object = robotManager.CreateRobot(-6, 3);

        robotManager.TurnRobotStartingDirection(robot1Object);
        robotManager.TurnRobotStartingDirection(robot1Object);
        ShreddingTool tool1      = new ShreddingTool(robot1Object.GetComponent <Robot>());
        BasicLeg      leg1       = new BasicLeg(robot1Object.GetComponent <Robot>());
        TextAsset     text       = Resources.Load <TextAsset>("Texts/Robot 1Actions");
        string        scriptCode = text.text;

        robot1Object.GetComponent <Robot>().ChangeScriptCode(scriptCode);

        //Robot2
        GameObject  robot2Object = robotManager.CreateRobot(-7, 5);
        BasicArm    tool2        = new BasicArm(robot2Object.GetComponent <Robot>());
        SpiderLeg   leg2         = new SpiderLeg(robot2Object.GetComponent <Robot>());
        BasicSensor sensor2      = new BasicSensor(robot2Object.GetComponent <Robot>());

        text       = Resources.Load <TextAsset>("Texts/Robot 2Actions");
        scriptCode = text.text;
        robot2Object.GetComponent <Robot>().ChangeScriptCode(scriptCode);

        //Robot3
        GameObject robot3Object = robotManager.CreateRobot(-7, 2);

        robotManager.TurnRobotStartingDirection(robot3Object);
        robotManager.TurnRobotStartingDirection(robot3Object);
        BasicArm     tool3     = new BasicArm(robot3Object.GetComponent <Robot>());
        Boat         leg3      = new Boat(robot3Object.GetComponent <Robot>());
        BasicSensor  sensor3   = new BasicSensor(robot3Object.GetComponent <Robot>());
        GroundSensor sensor3_2 = new GroundSensor(robot3Object.GetComponent <Robot>());

        text       = Resources.Load <TextAsset>("Texts/Robot 3Actions");
        scriptCode = text.text;
        robot3Object.GetComponent <Robot>().ChangeScriptCode(scriptCode);

        //Robot4
        GameObject robot4Object = robotManager.CreateRobot(-7, -2);

        robotManager.TurnRobotStartingDirection(robot4Object);
        robotManager.TurnRobotStartingDirection(robot4Object);
        BasicArm    tool4   = new BasicArm(robot4Object.GetComponent <Robot>());
        BasicLeg    leg4    = new BasicLeg(robot4Object.GetComponent <Robot>());
        BasicSensor sensor4 = new BasicSensor(robot4Object.GetComponent <Robot>());

        text       = Resources.Load <TextAsset>("Texts/Robot 4Actions");
        scriptCode = text.text;
        robot4Object.GetComponent <Robot>().ChangeScriptCode(scriptCode);

        //Robot5
        GameObject robot5Object = robotManager.CreateRobot(-2, -3);

        robotManager.TurnRobotStartingDirection(robot5Object);
        BasicArm    tool5   = new BasicArm(robot5Object.GetComponent <Robot>());
        SpiderLeg   leg5    = new SpiderLeg(robot5Object.GetComponent <Robot>());
        BasicSensor sensor5 = new BasicSensor(robot5Object.GetComponent <Robot>());

        text       = Resources.Load <TextAsset>("Texts/Robot 5Actions");
        scriptCode = text.text;
        robot5Object.GetComponent <Robot>().ChangeScriptCode(scriptCode);

        //Robot6
        GameObject  robot6Object = robotManager.CreateRobot(4, -1);
        WeldingTool tool6        = new WeldingTool(robot6Object.GetComponent <Robot>());
        SpiderLeg   leg6         = new SpiderLeg(robot6Object.GetComponent <Robot>());
        BasicSensor sensor6      = new BasicSensor(robot6Object.GetComponent <Robot>());
        Scanner     sensor6_2    = new Scanner(robot6Object.GetComponent <Robot>());

        text       = Resources.Load <TextAsset>("Texts/Robot 6Actions");
        scriptCode = text.text;
        robot6Object.GetComponent <Robot>().ChangeScriptCode(scriptCode);

        //Robot7
        GameObject robot7Object = robotManager.CreateRobot(0, -1);

        robotManager.TurnRobotStartingDirection(robot7Object);
        robotManager.TurnRobotStartingDirection(robot7Object);
        robotManager.TurnRobotStartingDirection(robot7Object);
        BasicArm    tool7   = new BasicArm(robot7Object.GetComponent <Robot>());
        SpiderLeg   leg7    = new SpiderLeg(robot7Object.GetComponent <Robot>());
        BasicSensor sensor7 = new BasicSensor(robot7Object.GetComponent <Robot>());

        text       = Resources.Load <TextAsset>("Texts/Robot 7Actions");
        scriptCode = text.text;
        robot7Object.GetComponent <Robot>().ChangeScriptCode(scriptCode);
    }
    /**
    **  Sequence parsing logic, use executeSequence() to run a sequence file
    **/
    IEnumerator parseSequence(string filecontent, bool validate, int repeat, float speedModifier)
    {
        bool hasHeader = false;
        int  lineNr    = 0;

        string[] lines = filecontent.Split('\n');
        Dictionary <int, SpiderLeg.LegMovement> tmpFrame = null;
        int tmpFrameMillis = -1;

        for (int i = 0; i < repeat; i++)
        {
            foreach (string line in lines)
            {
                lineNr += 1;
                if (line.TrimStart(' ').StartsWith("#"))
                {
                    continue;
                }
                string   cleanline = line.Replace("\r", string.Empty);
                string[] words     = cleanline.Split(' ');
                string   command   = words[0];


                if (!hasHeader && !words[0].ToLower().Equals("sequence"))
                {
                    Debug.Log("Sequencefile has an invalid header, it should start with 'Sequence <sequencename>'");
                    yield break;
                }
                else if (lineNr == 1)
                {
                    hasHeader = true;
                    if (words.Length > 3)
                    {
                        this.offset = int.Parse(words[3]);
                    }
                    else
                    {
                        this.offset = 0;
                    }
                    continue;
                }

                if (speedModifier < 0)
                {
                    if (command == "framebegin")
                    {
                        command = "frameend";
                    }
                    else if (command == "frameend")
                    {
                        command = "framebegin";
                    }
                }

                if (words[0].ToLower().Equals("framebegin"))
                {
                    if (words.Length > 1)
                    {
                        tmpFrameMillis = int.Parse(words[1]);
                    }

                    tmpFrame = new Dictionary <int, SpiderLeg.LegMovement>();
                }
                else
                if (words[0].ToLower().Equals("frameend"))
                {
                    if (tmpFrame == null)
                    {
                        continue;
                    }

                    SpiderLeg.SequenceFrame sf = SpiderLeg.newSequenceFrame(tmpFrame, tmpFrameMillis);
                    tmpFrameMillis = -1;

                    for (int x = 1; x < 7; x++)
                    {
                        SpiderLeg.LegMovement mov;
                        bool hasMov = sf.movements.TryGetValue(x, out mov);
                        if (!hasMov)
                        {
                            mov       = new SpiderLeg.LegMovement();
                            mov.empty = true;
                        }
                        mov.maxExecTime = sf.maxMaxExecTime;
                        legs[x - 1].addMove(mov);
                    }

                    tmpFrame.Clear();
                    tmpFrame = null;
                }
                else
                if (words[0].ToLower().Equals("delay"))
                {
                    if (words.Length != 2)
                    {
                        Debug.Log("No argument given for delay at line: " + lineNr);
                        yield break;
                    }

                    float seconds = int.Parse(words[1]) / 1000f;


                    if (!validate)
                    {
                        Debug.Log("Will delay " + seconds + " seconds");
                        yield return(new WaitForSeconds(seconds * delayModifier));
                    }
                }
                else
                if (words[0].ToLower().StartsWith("waitlegs"))
                {
                    //bool shouldBlock = true;

                    bool allEmpty = false;

                    while (!allEmpty)
                    {
                        float maxExec = 0f;
                        foreach (SpiderLeg leg in legs)
                        {
                            allEmpty = true;
                            maxExec  = Mathf.Max(maxExec, leg.maxExecTime);
                            if (leg.hasMovesOnQueue())
                            {
                                allEmpty = false;
                                yield return(new WaitForSeconds(maxExec));
                            }
                        }
                    }
                }

                if (words[0].ToLower().StartsWith("s:"))
                {
                    if (words.Length < 2 || words.Length > 3)
                    {
                        Debug.Log("Wrong amount of arguments for servo control: " + words.Length + " at line: " + lineNr);
                        yield break;
                    }

                    int      servoID = int.Parse(words[0].Split(':')[1]);
                    string[] coords  = words[1].Split(',');

                    int speed = -1;
                    if (words.Length == 3)
                    {
                        speed = int.Parse(words[2]);
                    }

                    speed = (int)(speed * speedModifier);

                    if (!validate)
                    {
                        float newAngle = float.Parse(coords[0]);
                        //Debug.Log("Will control servo " + servoID + ", coords: " + newAngle + ", speed: " + speed);
                        foreach (SpiderLeg leg in legs)
                        {
                            leg.moveByServoID(servoID, newAngle, speed, false);
                        }
                        //#hier de servocontroller aanroepen met de variabelen wanneer not validate
                    }
                }

                else
                //Controll legs
                if (words[0].ToLower().StartsWith("l:"))
                {
                    if (words.Length < 2 || words.Length > 3)
                    {
                        Debug.Log("Wrong amount of arguments for servo control: " + words.Length + " at line: " + lineNr);
                        yield break;
                    }

                    int      legID  = int.Parse(words[0].Split(':')[1]);
                    string[] coords = words[1].Split(",".ToCharArray());
                    if (coords.Length != 3)
                    {
                        Debug.Log("Wrong amount of coords: " + coords.Length + " at line: " + lineNr);
                        yield break;
                    }



                    int speed = -1;
                    if (words.Length == 3)
                    {
                        speed = (int)(int.Parse(words[2]));
                    }

                    speed = (int)(speed * speedModifier);

                    if (!validate)
                    {
                        //Debug.Log("Will control leg " + legID + ", coords: " + words[1] + ", speed: " + speed);

                        SpiderLeg.LegMovement lm = setServoPos(float.Parse(coords[0]), float.Parse(coords[1]), float.Parse(coords[2]), legID, speed);
                        if (tmpFrame == null)
                        {
                            this.legs[legID - 1].addMove(lm);
                        }
                        else
                        {
                            tmpFrame.Add(legID, lm);
                        }
                    }
                }
            }
        }
    }