Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
1374 linlun 1
 
2
function PID(type, name, id)
3
{
4
    this.CanService(type, name, id);
5
    this.myLastValue = new Array();
6
    this.registerEventCallback("online", function(args) { _PID_Online(args); });
7
}
8
 
9
function _PID_Online(data) {
10
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "CONFIG_SENSOR");
11
    sendMessage(canMessage);
12
    canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "CONFIG_ACTUATOR");
13
    sendMessage(canMessage);
14
    canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "CONFIG_PARAMETER");
15
    sendMessage(canMessage);
16
    canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "Report_Interval");
17
    sendMessage(canMessage);
18
}
19
 
20
extend(PID, CanService);
21
 
22
PID.prototype.myReportInterval = null;
23
 
24
PID.prototype.sensorModuleType = null;
25
PID.prototype.sensorModuleId = null;
26
PID.prototype.sensorId = null;
27
 
28
PID.prototype.actuatorModuleType = null;
29
PID.prototype.actuatorModuleId = null;
30
PID.prototype.actuatorId = null;
31
 
32
PID.prototype.K_P= null;
33
PID.prototype.K_I= null;
34
PID.prototype.K_D= null;
35
PID.prototype.TimeUnit= null;
36
PID.prototype.RegulatorTime = null;
37
 
38
PID.prototype.Measurment = null;
39
PID.prototype.Reference = null;
40
PID.prototype.PWM = null;
41
PID.prototype.Sum = null;
42
 
43
PID.prototype.canMessageHandler = function(canMessage)
44
{
45
    if (canMessage.getDirectionFlag() == "From_Owner")
46
    {
47
        switch (canMessage.getCommandName())
48
        {
49
        case "DEBUG":
50
            //<variable name="P_term" type="uint" start_bit="0" bit_length="16" unit="" description=""/>
51
            //<variable name="I_term" type="uint" start_bit="16" bit_length="16" unit="" description=""/>
52
            //<variable name="D_term" type="uint" start_bit="32" bit_length="16" unit="" description=""/>
53
            //<variable name="Sum" type="uint" start_bit="48" bit_length="16" unit="" description=""/>
54
            //this.myLastValue[canMessage.getData("Id")] = canMessage.getData("Value");
55
            //this.callEvent("newValue", canMessage.getData("Id"));
56
        break;
57
        case "CONFIG_SENSOR":
58
            this.sensorModuleType= canMessage.getData("sensorModuleType");
59
            this.sensorModuleId= canMessage.getData("sensorModuleId");
60
            this.sensorId= canMessage.getData("sensorId");
1380 linlun 61
              log("conf_sensor: " + this.sensorModuleType + " " + this.sensorModuleId + " " + this.sensorId + "\n");
1374 linlun 62
            //this.callEvent("newValue", canMessage.getData("Id"));
63
        break;
64
        case "CONFIG_ACTUATOR":
65
            this.actuatorModuleType= canMessage.getData("actuatorModuleType");
66
            this.actuatorModuleId= canMessage.getData("actuatorModuleId");
67
            this.actuatorId= canMessage.getData("actuatorId");
68
            //this.callEvent("newValue", canMessage.getData("Id"));
1380 linlun 69
log("conf_act: " + this.actuatorModuleType + " " + this.actuatorModuleId + " " + this.actuatorId + "\n");
1374 linlun 70
 
71
        break;
72
        case "CONFIG_PARAMETER":
73
            this.K_P= canMessage.getData("K_P");
74
            this.K_I= canMessage.getData("K_I");
75
            this.K_D= canMessage.getData("K_D");
76
            this.TimeUnit= canMessage.getData("TimeUnit");
77
            this.RegulatorTime= canMessage.getData("RegulatorTime");
1380 linlun 78
log("conf_param: " + this.K_P + " " + this.K_I + " " + this.K_D + " " + this.RegulatorTime +  " " + this.TimeUnit + "\n");
1374 linlun 79
            //this.callEvent("newValue", canMessage.getData("Id"));
80
        break;
81
        case "PID_STATUS":
82
            this.Measurment= canMessage.getData("Measurment");
83
            this.Reference= canMessage.getData("Reference");
84
            this.PWM= canMessage.getData("PWM");
85
            this.Sum= canMessage.getData("Sum");
1380 linlun 86
log("PID status: mea:" + this.Measurment + " ref:" + this.Reference + " pwm:" + this.PWM/100 + " sum:" + this.Sum + "\n");
1374 linlun 87
            //this.callEvent("newValue", canMessage.getData("Id"));
88
        break;
89
        case "Temperature_Celsius":
90
            this.Reference= canMessage.getData("Value");
1380 linlun 91
log("PID status: ref:" + this.Reference + "\n");
1374 linlun 92
            //this.callEvent("newValue", canMessage.getData("Id"));
93
        break;
94
        case "Report_Interval":
95
            this.myReportInterval = canMessage.getData("Time");
96
        break;
97
        }
98
    }
99
    this.CanService.prototype.canMessageHandler.call(this, canMessage);
100
}
101
 
102
PID.prototype.setValue = function(temperature)
103
{
1380 linlun 104
log ("Called SetValue: "+temperature+"\n");
1374 linlun 105
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "Temperature_Celsius");
1380 linlun 106
    canMessage.setData("Value", ""+temperature);
1374 linlun 107
    canMessage.setData("SensorId", 0);
1380 linlun 108
log ("> "+canMessage.toString()+"\n");
1374 linlun 109
    sendMessage(canMessage);
1380 linlun 110
 
111
 
1374 linlun 112
}
113
 
1380 linlun 114
PID.prototype.getConfiguration = function()
115
{
116
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "CONFIG_SENSOR");
117
    sendMessage(canMessage);
118
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "CONFIG_ACTUATOR");
119
    sendMessage(canMessage);
120
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "CONFIG_PARAMETER");
121
    sendMessage(canMessage);
122
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "Report_Interval");
123
    sendMessage(canMessage);
124
}
1374 linlun 125
 
1380 linlun 126
PID.prototype.setParameters = function(KP,KI,KD,Time,Unit)
127
{
128
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "CONFIG_PARAMETER");
129
    canMessage.setData("K_P", ""+KP);
130
    canMessage.setData("K_I", ""+KI);
131
    canMessage.setData("K_D", ""+KD);
132
    canMessage.setData("RegulatorTime", ""+Time);
133
    canMessage.setData("TimeUnit", ""+Unit);
134
//log ("> "+canMessage.toString()+"\n");
135
    sendMessage(canMessage);
136
}
137
 
138
 
1374 linlun 139
PID.prototype.setReportInterval = function(time)
140
{
141
    var canMessage = new CanMessage("act", "To_Owner", this.myName, this.myId, "Report_Interval");
142
    canMessage.setData("Time", time);
143
    sendMessage(canMessage);
144
}
145
 
146
PID.prototype.getReportInterval = function(time)
147
{
148
 
149
}