Subversion Repositories HomeAutomation

Rev

Rev 2076 | Rev 2275 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1765 linlun 1
 
2
PID_ModuleNames    = [ "PID" ];
3
PID_Intervals      = function() { return [ 1, 5, 10, 15, 20 ]; };
4
//PID_Energy         = function() { return [ 0, 500, 1000, 5000, 10000 ]; };
5
PID_Temperatures    = function() { return [ 20, 25, 30, 35 ]; };
2271 linlun 6
PID_Constants       = function() { return [ 0, 0.1, 0.5, 1, 10, 100, 1000 ]; };
7
PID_TimeUnits   = function() { return [ "s", "ms"]; };
8
PID_Times   = function() { return [ 1, 5, 10, 15, 20, 40, 60]; };
1765 linlun 9
PID_Aliases        = function() { return Module_GetAliasNames(PID_ModuleNames); };
10
PID_AvailableIds   = function() { return Module_GetAvailableIds(PID_ModuleNames); };
11
 
12
function PID_SetReportInterval(alias_name, interval)
13
{
14
    if (arguments.length < 2)
15
    {
16
        Log("\033[31mNot enough parameters given.\033[0m\n");
17
        return false;
18
    }
19
 
20
    var aliases_data = Module_ResolveAlias(alias_name, PID_ModuleNames);
21
    var found = false;
22
 
23
    for (var name in aliases_data)
24
    {
25
        var variables = {"Time"     : interval };
26
 
27
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables))
28
        {
29
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
30
        }
31
        else
32
        {
33
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
34
        }
35
 
36
        found = true;
37
    }
38
 
39
    if (!found)
40
    {
41
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
42
        return false;
43
    }
44
 
45
    return true;
46
}
47
Console_RegisterCommand(PID_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, PID_Aliases(), PID_Intervals()); });
48
 
49
 
50
function PID_setValue(alias_name, temperature)
51
{
52
    if (arguments.length < 2)
53
    {
54
        Log("\033[31mNot enough parameters given.\033[0m\n");
55
        return false;
56
    }
57
    var aliases_data = Module_ResolveAlias(alias_name, PID_ModuleNames);
58
    var found = false;
59
 
60
    Log ("Called SetValue: "+temperature+"\n");
61
 
62
    for (var name in aliases_data)
63
    {
64
        var variables = {   "Value"     : temperature,
65
                    "SensorId"     : 0 };
66
 
67
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Temperature_Celsius", variables))
68
        {
69
            Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
70
        }
71
        else
72
        {
73
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
74
        }
75
 
76
        found = true;
77
    }
78
 
79
    if (!found)
80
    {
81
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
82
        return false;
83
    }
84
 
85
    return true;
86
}
87
Console_RegisterCommand(PID_setValue, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, PID_Aliases(), PID_Temperatures());  });
88
 
89
 
90
function PID_setParameters(alias_name, KP,KI,KD,Time,Unit)
91
{
92
    if (arguments.length < 6)
93
    {
94
        Log("\033[31mNot enough parameters given.\033[0m\n");
95
        return false;
96
    }
97
    var aliases_data = Module_ResolveAlias(alias_name, PID_ModuleNames);
98
    var found = false;
99
 
100
    for (var name in aliases_data)
101
    {
102
        var variables = {   "K_P"     : KP,
103
                    "K_I"     : KI,
104
                    "K_D"     : KD,
105
                    "RegulatorTime"     : Time,
106
                    "TimeUnit"     : Unit };
2271 linlun 107
        var variables_P_I = {   "K_P"     : KP,
108
                    "K_I"     : KI };
109
        var variables_D_T = {   "K_D"     : KD,
110
                    "RegulatorTime"     : Time,
111
                    "TimeUnit"     : Unit };
1765 linlun 112
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_PARAMETER", variables))
113
        {
114
            Log("\033[32mCommand sent successfully to " + name + ".\033[0m");
115
            Log("\033[32mK_P = " + KP + "K_I = " + KI + "K_D = " + KD + "RegTime = " + Time + "Unit = " + Unit + ".\033[0m\n");
116
        }
117
        else
118
        {
119
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
120
        }
121
 
2271 linlun 122
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_PARAMETER_P_I", variables_P_I))
123
        {
124
            Log("\033[32mK_P = " + KP + "K_I = " + KI + ".\033[0m\n");
125
        }
126
        else
127
        {
128
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
129
        }
130
 
131
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_PARAMETER_D_T", variables_D_T))
132
        {
133
            Log("\033[32mK_D = " + KD + "RegTime = " + Time + "Unit = " + Unit + ".\033[0m\n");
134
        }
135
        else
136
        {
137
            Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
138
        }
139
 
1765 linlun 140
        found = true;
141
    }
142
 
143
    if (!found)
144
    {
145
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
146
        return false;
147
    }
148
 
149
    return true;
150
}
2271 linlun 151
Console_RegisterCommand(PID_setParameters, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, PID_Aliases(), PID_Constants(), PID_Constants(), PID_Constants(), PID_Times(), PID_TimeUnits()); });
1765 linlun 152
 
153
function PID_getConfiguration(alias_name)
154
{
155
    if (arguments.length < 1)
156
    {
157
        Log("\033[31mNot enough parameters given.\033[0m\n");
158
        return false;
159
    }
160
    var aliases_data = Module_ResolveAlias(alias_name, PID_ModuleNames);
161
    var found = false;
162
 
163
    for (var name in aliases_data)
164
    {
165
        var variables = {};
166
 
167
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_SENSOR", variables))
168
        {
169
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
170
        }
171
        else
172
        {
173
            Log("\033[31mFailed to send command 1 to " + name + ".\033[0m\n");
174
        }
175
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_ACTUATOR", variables))
176
        {
177
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
178
        }
179
        else
180
        {
181
            Log("\033[31mFailed to send command 2 to " + name + ".\033[0m\n");
182
        }
183
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_PARAMETER", variables))
184
        {
185
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
186
        }
187
        else
188
        {
189
            Log("\033[31mFailed to send command 3 to " + name + ".\033[0m\n");
190
        }
191
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables))
192
        {
193
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
194
        }
195
        else
196
        {
197
            Log("\033[31mFailed to send command 4 to " + name + ".\033[0m\n");
198
        }
2271 linlun 199
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_PARAMETER_P_I", variables))
200
        {
201
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
202
        }
203
        else
204
        {
205
            Log("\033[31mFailed to send command 5 to " + name + ".\033[0m\n");
206
        }
207
        if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG_PARAMETER_D_T", variables))
208
        {
209
            //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
210
        }
211
        else
212
        {
213
            Log("\033[31mFailed to send command 6 to " + name + ".\033[0m\n");
214
        }
1765 linlun 215
        found = true;
216
    }
217
 
218
    if (!found)
219
    {
220
        Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
221
        return false;
222
    }
223
 
224
    return true;
225
}
226
Console_RegisterCommand(PID_getConfiguration, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, PID_Aliases()); });
227
 
228
function PID_OnMessage(module_name, module_id, command, variables)
229
{
230
    if (in_array(PID_ModuleNames, module_name))
231
    {
232
        var aliases_data = Module_LookupAliases({
233
            "module_name" : module_name,
234
            "module_id"   : module_id,
235
            "group"       : false
236
        });
237
 
238
        switch (command)
239
        {
240
            case "DEBUG":
241
                for (var alias_name in aliases_data)
242
                {
243
                    var last_value = {};
244
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
245
 
246
                    if (last_value_string)
247
                    {
248
                        last_value = eval("(" + last_value_string + ")");
249
                    }
250
 
2076 linlun 251
                    last_value["Debug"] = { "value" : { "P_term" : variables["P_term"],"I_term" : variables["I_term"],"D_term" : variables["D_term"],"Sum" : variables["Sum"] }, "timestamp" : get_time() };
1765 linlun 252
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
253
                    //Log("Got message DEBUG");
254
                }
255
            break;
256
            case "CONFIG_SENSOR":
257
                for (var alias_name in aliases_data)
258
                {
259
                    var last_value = {};
260
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
261
 
262
                    if (last_value_string)
263
                    {
264
                        last_value = eval("(" + last_value_string + ")");
265
                    }
266
 
2076 linlun 267
                    last_value["Sensor"] = { "value" : { "sensorModuleType" : variables["sensorModuleType"],"sensorModuleId" : variables["sensorModuleId"],"sensorId" : variables["sensorId"] }, "timestamp" : get_time() };
1765 linlun 268
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
269
                }
270
            break;
271
            case "CONFIG_ACTUATOR":
272
                for (var alias_name in aliases_data)
273
                {
274
                    var last_value = {};
275
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
276
 
277
                    if (last_value_string)
278
                    {
279
                        last_value = eval("(" + last_value_string + ")");
280
                    }
281
 
2076 linlun 282
                    last_value["Actuator"] = { "value" : { "actuatorModuleType" : variables["actuatorModuleType"],"actuatorModuleId" : variables["actuatorModuleId"],"actuatorId" : variables["actuatorId"] }, "timestamp" : get_time() };
1765 linlun 283
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
284
                }
285
            break;
286
            case "CONFIG_PARAMETER":
287
                for (var alias_name in aliases_data)
288
                {
289
                    var last_value = {};
290
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
291
 
292
                    if (last_value_string)
293
                    {
294
                        last_value = eval("(" + last_value_string + ")");
295
                    }
296
 
2076 linlun 297
                    last_value["Parameters"] = { "value" : { "K_P" : variables["K_P"],"K_I" : variables["K_I"],"K_D" : variables["K_D"],"TimeUnit" : variables["TimeUnit"],"RegulatorTime" : variables["RegulatorTime"] }, "timestamp" : get_time() };
1765 linlun 298
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
299
                }
300
            break;
301
            case "PID_STATUS":
302
                for (var alias_name in aliases_data)
303
                {
304
                    var last_value = {};
305
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
306
 
307
                    if (last_value_string)
308
                    {
309
                        last_value = eval("(" + last_value_string + ")");
310
                    }
311
 
2076 linlun 312
                    last_value["PID_status"] = { "value" : { "Measurment" : variables["Measurment"],"Reference" : variables["Reference"],"PWM" : variables["PWM"],"Sum" : variables["Sum"] }, "timestamp" : get_time() };
1765 linlun 313
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
314
                }
315
    //log("PID status: mea:" + this.Measurment + " ref:" + this.Reference + " pwm:" + this.PWM/100 + " sum:" + this.Sum + "\n");
316
                //this.callEvent("newValue", canMessage.getData("Id"));
317
 
318
    //var self = this;
319
    //  this.myHTTP.request(function(result, header, content) { self.httpCallback(result, header, content); }, "linuxz.mine.nu/canlogg/add.php?measurment="+ this.Measurment + "&reference="+this.Reference+"&pwm="+this.PWM/100+"&sum="+this.Sum+"&p="+this.K_P+"&i="+this.K_I+"&d="+this.K_D+"&timeunit="+this.TimeUnit+"&pidtime="+this.RegulatorTime+"");
320
            break;
321
            case "Temperature_Celsius":
322
                for (var alias_name in aliases_data)
323
                {
324
                    var last_value = {};
325
                    var last_value_string = Storage_GetParameter("LastValues", alias_name);
326
 
327
                    if (last_value_string)
328
                    {
329
                        last_value = eval("(" + last_value_string + ")");
330
                    }
331
 
2076 linlun 332
                    last_value["Reference"] = { "value" : variables["Value"], "timestamp" : get_time() };
1765 linlun 333
                    Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
334
                }  
335
                break;
336
            case "Report_Interval":
337
                this.myReportInterval = canMessage.getData("Time");
338
                break;
339
        }
340
    }
341
}
342
Module_RegisterToOnMessage("all", PID_OnMessage);