Skip to content

Commit

Permalink
【MA基礎開発】 ドローン同士の通信をクラスオブジェクトで扱えるように変更。避難所位置伝送時のエラー修正 #1 #7 #15
Browse files Browse the repository at this point in the history
  • Loading branch information
tsyu12345 committed Mar 13, 2024
1 parent 8a0c975 commit bbac30a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 27 deletions.
10 changes: 4 additions & 6 deletions MAEasySimulator/Assets/DroneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DroneController : MonoBehaviour {
public Rigidbody Rbody;

/**Events*/
public delegate void OnReceiveMessage(string message);
public delegate void OnReceiveMessage(Types.MessageData Data);
public OnReceiveMessage onReceiveMsg;
public delegate void OnCrash(Vector3 crashPos);
public OnCrash onCrash;
Expand Down Expand Up @@ -142,18 +142,16 @@ public void FlyingCtrl(ActionBuffers actions) {
public bool Communicate(Types.MessageData messageData, GameObject target) {
var result = false;
//一旦距離制限は考えない
target.GetComponent<DroneController>().ReceiveMessage(messageData.ToJson());
target.GetComponent<DroneController>().ReceiveMessage(messageData);
result = true;
return result;
}

/// <summary>
/// 他のドローンからメッセージを受信する(させる)。
/// </summary>
public void ReceiveMessage(string json) {
var messageData = Types.MessageData.FromJson(json);
Debug.Log($"Message received: Type={messageData.type}, Content={messageData.content}");
onReceiveMsg?.Invoke(json);
public void ReceiveMessage(Types.MessageData Data) {
onReceiveMsg?.Invoke(Data);
}


Expand Down
4 changes: 2 additions & 2 deletions MAEasySimulator/Assets/Scenes/test0.4.unity
Git LFS file not shown
4 changes: 2 additions & 2 deletions MAEasySimulator/Assets/SpyAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public override void OnEpisodeBegin() {
/// <param name="sensor"></param>
public override void CollectObservations(VectorSensor sensor) {
sensor.AddObservation(_controller.Rbody.velocity);
var findShelterCount = ShelterScan();
var findShelterCount = ShelterScan(); //レイキャストによる避難所検出
//TODO:複数の避難所を検出する場合の対応
sensor.AddObservation(targetPos);
sensor.AddObservation(isFindTarget);
sensor.AddObservation(findShelterCount);
}

Expand Down
16 changes: 7 additions & 9 deletions MAEasySimulator/Assets/SurpplieAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ public override void Heuristic(in ActionBuffers actionsOut) {
/// ShelterのXYZ座標
/// 例:(10.0, 0.0, 10.0)
/// </param>
private void OnReceiveMessage(string message) {
private void OnReceiveMessage(Types.MessageData data) {
//Vector3に変換
var shelterPos = message.Trim('(', ')').Split(',');
var x = float.Parse(shelterPos[0]);
var y = float.Parse(shelterPos[1]);
var z = float.Parse(shelterPos[2]);

//観察に追加
shelterPosition = new Vector3(x, y, z);
isGetShelterPos = true;
var detectType = data.type;
if(detectType == "Shelter") {
Vector3 pos = Utils.ConvertStringToVector3(data.content);
shelterPosition = new Vector3(pos.x, pos.y, pos.z);
isGetShelterPos = true;
}
}

/// <summary>
Expand Down
8 changes: 0 additions & 8 deletions MAEasySimulator/Assets/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,5 @@ public class Types : MonoBehaviour {
public class MessageData {
public string type;
public string content;

public string ToJson() {
return JsonUtility.ToJson(this);
}

public static MessageData FromJson(string json) {
return JsonUtility.FromJson<MessageData>(json);
}
}
}
27 changes: 27 additions & 0 deletions MAEasySimulator/Assets/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// オブジェクトの操作には関係ないが、汎用的な処理をまとめたクラス
/// </summary>
public class Utils : MonoBehaviour {

/// <summary>
/// 文字列データをVector3に変換します
/// </summary>
/// <param name="vectorString">(x, y, z)のVector3文字列</param>
/// <returns>Vector3 データ</returns>
public static Vector3 ConvertStringToVector3(string vectorString) {
vectorString = vectorString.TrimStart('(').TrimEnd(')');
string[] sArray = vectorString.Split(',');

Vector3 result = new Vector3(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2])
);

return result;
}
}
11 changes: 11 additions & 0 deletions MAEasySimulator/Assets/Utils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bbac30a

Please sign in to comment.