節電鯖民の遊び場

Solar

最終更新:

mcforum

- view
メンバー限定 登録/ログイン

ソーラーパネルの消費電力表示、効率表示、あとざっくりとしたバー表示化、ついでに生産20000W以下になったらアラームなるようにした


  • ソーラーパネルの値はまともに取れないので詳細情報から分解して持ってくる必要あり。
    • この手法、他のモジュールに転用できまくるのでは
  • 復帰値は意味ない。 throw new Exception("異常内容コメ"); で異常終了させたほうが、コントロール画面に出力されて便利だぞ
  • アラームは予め流す音選択して、時間MAXにしてブロックの電源OFFにしておく。
  • これをコマンドブロックに書いて、タイマーブロックから適当な秒周期で呼び出す。タイマーブロックに自身のタイマーブロックのstartアクション登録しておいてループ化。
  • 作成:ecolight

第二版ソース

  • Timerブロックから、Run指定でProgrammable blockを登録し、カンマ区切りでパラメータを渡してやると動作。
    • 第一パラメタ: ソーラーパネル名
    • 第二パラメタ: テキストパネル名(LCD,WideLCDも可)
    • 第三パラメタ: サウンドブロック名
    • 第四パラメタ: 適当な数値。この値以下の出力ワット数以下になった場合、第三パラメタで指定したサウンドブロックをON制御、ワット数以上になった場合はOFF制御する。
- ...
//------------------------------------------------------------------------------
// Main Function
//------------------------------------------------------------------------------
void Main(string argument)
{
    //--------------------------------------------------------------------------
    // Config (argument paramater)
    //--------------------------------------------------------------------------
    string[] settings = argument.Split(new string[] {","}, StringSplitOptions.None);
    // Solar Panel Name
    string solarName = settings[0];         // 1st param
    // Text Panel Name
    string panelName = settings[1];         // 2nd param
    // Buzzer
    string soundName = settings[2];         // 3rd param
    // Alart wat
    int threshold = int.Parse(settings[3]); // 4th param

    //--------------------------------------------------------------------------
    // Load text panel
    //--------------------------------------------------------------------------
    IMyTextPanel p = GetPanel(panelName);

    //--------------------------------------------------------------------------
    // Get solar power
    //--------------------------------------------------------------------------
    float max = GetSolarMaxPowerByName(solarName);
    float cur = GetSolarPowerByName(solarName);
    p.WritePublicText(" [INFO] Solar Power Monitoring\n", false);  
    p.WritePublicText(" [INFO] MAX: " + max + " W\n", true);  
    p.WritePublicText(" [INFO] CUR: " + cur + " W\n", true);  

    //--------------------------------------------------------------------------
    // Power generating efficiency
    //-------------------------------------------------------------------------- 
    DrawIndicator(p, cur, 120000, "Generating efficiency");

    //-------------------------------------------------------------------------- 
    // Power consumption
    //-------------------------------------------------------------------------- 
    DrawIndicator(p, cur, max, "Power consumption");

    //-------------------------------------------------------------------------- 
    // Load sound
    //--------------------------------------------------------------------------
    IMySoundBlock sound = GetSound(soundName); 

    //-------------------------------------------------------------------------- 
    // Threshold alert
    //-------------------------------------------------------------------------- 
    if (cur < threshold) {
        On(sound); 
    } else {
        Off(sound); 
    }

    return;
}


//------------------------------------------------------------------------------
// Text Panel Cntrol Function
//------------------------------------------------------------------------------
public IMyTextPanel GetPanel(string panelName) {
    // Load TextPanel
    IMyTextPanel p = GridTerminalSystem.GetBlockWithName(panelName) as IMyTextPanel;

    // check result
    if (p == null) { throw new Exception("Panel [ " + panelName + " ] not found." ); }

    // check type
    if (!(p is IMyTextPanel)) { throw new Exception("Panel [ " + panelName + " ] is invalid type."); }

    // result
    return p;
}

public void DrawIndicator(IMyTextPanel panel, float cur, float max, string title) {
    float per = cur / max;
    int cnt = (int) (per * 10);
    panel.WritePublicText("*** " + title + " ***\n [ ", true);
    for (int i = 0 ; i < 10 ; i++) {
        if (cnt < i) {
            panel.WritePublicText(" - ", true);
        } else {
            panel.WritePublicText(" = ", true);
        }
    }
    panel.WritePublicText(" ] " + (int)(per * 100) + " %\n", true);
}

//------------------------------------------------------------------------------
// Solar Panel Cntrol Function
//------------------------------------------------------------------------------
public float GetSolarMaxPowerByName(string solarName) {
    return GetSolarMaxPower( GetSolar(solarName) );
}

public float GetSolarPowerByName(string solarName) {
    return GetSolarPower( GetSolar(solarName) );
}

public float GetSolarMaxPower(IMySolarPanel solar) {
    return GetWat(solar, 1);
}

public float GetSolarPower(IMySolarPanel solar) {
    return GetWat(solar, 2);
}

public IMySolarPanel GetSolar(string solarName) {
    // Load Solar
    IMySolarPanel s = GridTerminalSystem.GetBlockWithName(solarName) as IMySolarPanel;

    // check result
    if (s == null) { throw new Exception("Solar [ " + solarName + " ] not found." ); }

    // check type
    if (!(s is IMySolarPanel)) { throw new Exception("Solar [ " + solarName + " ] is invalid type."); }

    // result
    return s;
}

public float GetWat(IMySolarPanel solar, int line) {
    var detail = solar.DetailedInfo;
    string power = detail.Split(new string[] {"\n"}, StringSplitOptions.None)[line].Split(' ')[2];
    string unit = detail.Split(new string[] {"\n"}, StringSplitOptions.None)[line].Split(' ')[3];
    float value = Convert.ToSingle(power);
    if (unit == "kW") {
        value *= 1000;
    }
    return value;
}

//------------------------------------------------------------------------------
// Sound Cntrol Function
//------------------------------------------------------------------------------
public IMySoundBlock GetSound(string blockName) {
    // Load Sound Block
    IMySoundBlock b = GridTerminalSystem.GetBlockWithName(blockName) as IMySoundBlock;

    // check result
    if (b == null) { throw new Exception("Sound [ " + blockName + " ] not found." ); }

    // check type
    if (!(b is IMySoundBlock)) { throw new Exception("Sound [ " + blockName + " ] is invalid type."); }

    // result
    return b;
}

//------------------------------------------------------------------------------
// Common Function
//------------------------------------------------------------------------------
public void On(IMyTerminalBlock block) {
    block.GetActionWithName("OnOff_On").Apply(block);
}
public void Off(IMyTerminalBlock block) {
    block.GetActionWithName("OnOff_Off").Apply(block);
}


初版ソース

+ ...
int Main(string argument)
{

    //---------------------------------------------------------------------------------------------------   
    // Load TextPanel 2
    IMyTextPanel p2 = GridTerminalSystem.GetBlockWithName("Text panel 2") as IMyTextPanel; 
 
    // check result for PanelObject 
    if (p2 == null) { return -1; } 

    //---------------------------------------------------------------------------------------------------  
    // Load Solar
    IMySolarPanel s = GridTerminalSystem.GetBlockWithName("Solar") as IMySolarPanel;   
 
    // check result for Thruster 
    if (s == null) { p2.WritePublicText(" [ERROR] Solar Offline", false); return -2; } 
     
    // check type 
    if (!(s is IMySolarPanel)) { p2.WritePublicText(" [ERROR] invalid Solar", false); return -3; }  

    //---------------------------------------------------------------------------------------------------    
    float max = GetPanelMaxPower(s) ; 
    float cur = GetPanelPower(s); 
    p2.WritePublicText(" [INFO] Solar Power Monitoring\n", false);     
    p2.WritePublicText(" [INFO] MAX: " + max + " W\n", true);     
    p2.WritePublicText(" [INFO] CUR: " + cur + " W\n", true);     
 
    //---------------------------------------------------------------------------------------------------     
    // Power generating efficiency
    float per = cur / 120000; 
    int per_i = (int) (per * 10); 
    p2.WritePublicText("\n*** Generating efficiency ***\n [ ", true);  
    for (int i = 0 ; i < 10 ; i++) { 
        if (per_i < i) { 
            p2.WritePublicText(" - ", true); 
        } else { 
            p2.WritePublicText(" = ", true);  
        } 
    } 
    p2.WritePublicText(" ] " + (int)(per * 100) + " %\n", true);   
 
    //---------------------------------------------------------------------------------------------------    
    // Power consumption
    per = cur / max; 
    per_i = (int) (per * 10); 
    p2.WritePublicText("\n*** Power consumption ***\n [ ", true);  
    for (int i = 0 ; i < 10 ; i++) { 
        if (per_i < i) { 
            p2.WritePublicText(" - ", true); 
         } else { 
           p2.WritePublicText(" = ", true);  
        } 
    } 
    p2.WritePublicText(" ] " + (int)(per * 100) + " %\n", true);   
 
    //---------------------------------------------------------------------------------------------------   
    // Load Sound 
    IMySoundBlock sound = GridTerminalSystem.GetBlockWithName("Sound") as IMySoundBlock;    
  
    // check result for SoundBlock  
    if (sound == null) { p2.WritePublicText(" [ERROR] Sound Offline", true); return -2; }  
      
    // check type  
    if (!(sound is IMySoundBlock)) { p2.WritePublicText(" [ERROR] invalid SoundBlock", true); return -3; }   
 
    //---------------------------------------------------------------------------------------------------    
    // 20000 W Alert
    //---------------------------------------------------------------------------------------------------     
    if (cur < 20000) {
        sound.GetActionWithName("OnOff_On").Apply(sound);    
    } else {
        sound.GetActionWithName("OnOff_Off").Apply(sound);    
    }

    return 0;
}

public float GetPanelMaxPower(IMySolarPanel panel)  
{  
    var _d = panel.DetailedInfo; 
    string _power = _d.Split(new string[] {"\n"}, StringSplitOptions.None)[1].Split(' ')[2]; //Checking the MAX Output   
    string _unit = _d.Split(new string[] {"\n"}, StringSplitOptions.None)[1].Split(' ')[3]; //Checking the MAX Output  unit
    float value = Convert.ToSingle(_power);
    if (_unit == "kW") {
        value *= 1000;
    }
    return value;
} 
public float GetPanelPower(IMySolarPanel panel)   
{   
    var _d = panel.DetailedInfo;    
    string _power = _d.Split(new string[] {"\n"}, StringSplitOptions.None)[2].Split(' ')[2]; //Checking the Output    
    string _unit = _d.Split(new string[] {"\n"}, StringSplitOptions.None)[2].Split(' ')[3]; //Checking the Output  unit
    float value = Convert.ToSingle(_power); 
    if (_unit == "kW") { 
        value *= 1000; 
    } 
    return value; 
}
添付ファイル
ウィキ募集バナー