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