public override void InitializeAgent() { ammoInf = true; base.InitializeAgent(); roboState = GetComponent <RoboState>(); roboMovement = GetComponent <RoboMovement>(); roboShooter = GetComponent <RoboShooter>(); rayPer = GetComponent <RoboRayPerception>(); if (gameObject.CompareTag("redAgent")) { myTeam = tag; myShield = "redShield"; myReload = "redReload"; enemyTeam = "blueAgent"; enemyShield = "blueShield"; enemyReload = "blueReload"; } else if (gameObject.CompareTag("blueAgent")) { myTeam = tag; myShield = "blueShield"; myReload = "blueReload"; enemyTeam = "redAgent"; enemyShield = "redShield"; enemyReload = "redReload"; } }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { Vector3 ray = main_cam.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero, LayerMask.GetMask("Robot")); if (hit.collider != null && RobotsList.Contains(hit.transform.gameObject)) { hold_state = hit.transform.gameObject.GetComponent <RoboState>(); //clone a robot gettheClone(hit.transform.gameObject); hold_state.setState(2); } } else if (Input.GetMouseButtonUp(0) && hold_state != null) { int num = getRobotNum(hold_state.gameObject); home_point = new Vector3(3.01f + num * 0.78f, -3.26f, 0f); if (roboClone.transform.position.y < homeline && roboClone.transform.position != home_point) { roboClone.transform.position = home_point; } hold_state.setState(4); //send command to serial port. moving robot. //Update send info send_id = System.Convert.ToInt16(num); send_x = roboClone.transform.position.x; send_y = roboClone.transform.position.y; send_v = 5f; isSend = true; hold_state = null; } }
private Bitmap blueF(Bitmap image) { //Create Filter and set value ranges AForge.Imaging.Filters.HSLFiltering filter = new AForge.Imaging.Filters.HSLFiltering(); filter.Hue = new AForge.IntRange(200, 240); filter.Saturation = new AForge.Range(0.3f, 1); filter.Luminance = new AForge.Range(0.1f, 1); filter.ApplyInPlace(image); AForge.Imaging.BlobCounter extractor = new AForge.Imaging.BlobCounter(); extractor.MinHeight = 5; extractor.MinWidth = 5; extractor.FilterBlobs = true; extractor.ProcessImage(image); //Create a blob counter to search through the image Rectangle[] rects = extractor.GetObjectsRectangles(); if (rects.Length > 0) { Rectangle objectRect = rects[0]; Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 2)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); //Get the direction and change state. if (currentState != RoboState.FOLLOW_RED && currentState != RoboState.FOLLOW_GREEN) { currentState = RoboState.SEARCH; } } return(image); }
//Filter to find red private Bitmap redF(Bitmap image) { //Create Filter and set value ranges AForge.Imaging.Filters.HSLFiltering filter = new AForge.Imaging.Filters.HSLFiltering(); filter.Hue = new AForge.IntRange(350, 25); filter.Saturation = new AForge.Range(0.5f, 1); filter.Luminance = new AForge.Range(0.15f, 1); filter.ApplyInPlace(image); //Create a blob counter to search through the image AForge.Imaging.BlobCounter extractor = new AForge.Imaging.BlobCounter(); extractor.MinHeight = 5; extractor.MinWidth = 5; extractor.FilterBlobs = true; extractor.ProcessImage(image); //Gets the rectangles so that we can draw around them Rectangle[] rects = extractor.GetObjectsRectangles(); if (rects.Length > 0) { Rectangle objectRect = rects[0]; Graphics g = Graphics.FromImage(image); using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 2)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); //Get the direction and change state. objDirectionRed(objectRect, image.Width); currentState = RoboState.FOLLOW_RED; } return(image); }
private int maxAmmoCapacity = 200; // 탄창 용량 private void OnEnable() { currentHeat = 0f; roboState = GetComponent <RoboState>(); ammoRemain = roboState.ammoRemain; moveInput = GetComponent <MoveInput>(); vGimbalPivot = transform.Find("Gimbal/V Gimbal Pivot"); }
//Controller for the state machine when it is in the SEARCH state private void searchSpace() { while (currentState == RoboState.SEARCH) { this.Drive.Forward(1); this.Drive.RotateLeft(10); currentState = RoboState.FIND; } }
//Controller for the state machine when it is in the FOLLOW_RED state private void followRED() { while (currentState == RoboState.FOLLOW_RED) { switch (redPos) { case ObjectPosition.OBJ_INFRONT: if (!this.IRSensor.Detection) { this.Drive.Forward(10); //this.Drive.Stop(); redPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; } else { redPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; } case ObjectPosition.OBJ_LEFT: this.API.Movement.ManualDrive.RotateLeft(10); //this.Drive.Stop(); redPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; case ObjectPosition.OBJ_RIGHT: this.API.Movement.ManualDrive.RotateRight(10); //this.Drive.Stop(); redPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; case ObjectPosition.OBJ_UNKNOWN: redPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; default: currentState = RoboState.FIND; break; } } }
//Controller for the state machine when it is in the FOLLOW_GREEN state private void followGREEN() { while (currentState == RoboState.FOLLOW_GREEN) { switch (greenPos) { case ObjectPosition.OBJ_INFRONT: if (!this.IRSensor.Detection) { this.Drive.Forward(10); greenPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; } else { greenPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; } case ObjectPosition.OBJ_LEFT: this.Drive.StraightLeft(10); greenPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; case ObjectPosition.OBJ_RIGHT: this.Drive.StraightRight(10); greenPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; case ObjectPosition.OBJ_UNKNOWN: greenPos = ObjectPosition.OBJ_UNKNOWN; currentState = RoboState.FIND; break; default: currentState = RoboState.FIND; break; } } }
private void objDirectionGreen(Rectangle rect, int imWidth) { if (imWidth * 0.3 > (rect.X + rect.Width)) { greenPos = ObjectPosition.OBJ_LEFT; } else if (imWidth * 0.6 < (rect.X + rect.Width)) { greenPos = ObjectPosition.OBJ_RIGHT; } else { greenPos = ObjectPosition.OBJ_INFRONT; if (rect.Height > 90) { currentState = RoboState.CAPTURED; } } }
void OnEnable() { roboState = transform.parent.parent.GetComponent <RoboState>(); }