forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
power-model.c
174 lines (143 loc) · 3.33 KB
/
power-model.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdlib.h>
#include <string.h>
#include "power-model.h"
struct power_model;
struct power_device {
struct power_model *pm;
resistor_sample_fn r1, r2, r3;
float or1, or2, or3; /* previous values of resistors */
void *cookie;
float i;
};
#define MAX_DEVICES_PER_MODEL 8
struct power_model {
int ndevices;
struct power_device *d[MAX_DEVICES_PER_MODEL];
float max_current;
float nominal_voltage;
float actual_voltage;
float internal_resistance;
float actual_current;
int enabled;
};
struct power_device *new_power_device(void * cookie, resistor_sample_fn r1,
resistor_sample_fn r2, resistor_sample_fn r3)
{
struct power_device *d = malloc(sizeof(*d));
d->r1 = r1;
d->r2 = r2;
d->r3 = r3;
d->or1 = 0;
d->or2 = 0;
d->or3 = 0;
d->i = 0;
d->cookie = cookie;
return d;
}
struct power_model *new_power_model(float max_current, float voltage,
float internal_resistance)
{
struct power_model *m = malloc(sizeof(*m));
memset(m, 0, sizeof(m));
m->nominal_voltage = voltage;
m->max_current = max_current;
m->internal_resistance = internal_resistance;
m->enabled = 1;
return m;
}
void power_model_add_device(struct power_model *m, struct power_device *device)
{
int n = m->ndevices;
if (n >= MAX_DEVICES_PER_MODEL)
return;
m->d[n] = device;
m->ndevices++;
device->pm = m;
}
static void update_resistance(float *r, float new_r)
{
*r = *r + ((new_r - *r) / 2.0);
}
static void power_model_update_resistances(struct power_model *m)
{
float nr1, nr2, nr3;
int i;
for (i = 0; i < m->ndevices; i++) {
struct power_device *d = m->d[i];
void *cookie = d->cookie;
nr1 = d->r1(cookie);
nr2 = d->r2(cookie);
if (nr1 < nr2)
nr1 = nr2;
nr3 = d->r3(cookie);
update_resistance(&d->or1, nr1);
update_resistance(&d->or2, nr2);
update_resistance(&d->or3, nr3);
}
}
void power_model_compute(struct power_model *m)
{
int i;
float total_resistance = m->internal_resistance;
float conductance = 0.0;
power_model_update_resistances(m);
for (i = 0; i < m->ndevices; i++) {
struct power_device *d = m->d[i];
float r;
r = d->or1 + d->or3;
conductance += 1.0 / r;
}
total_resistance += 1.0 / conductance;
m->actual_current = (float) m->enabled * m->nominal_voltage / total_resistance;
if (m->actual_current > m->max_current) {
m->actual_voltage = m->max_current * total_resistance;
} else {
m->actual_voltage = m->actual_current * total_resistance;
}
for (i = 0; i < m->ndevices; i++) {
struct power_device *d = m->d[i];
float r = d->or1 + d->or3;
d->i = m->actual_voltage / r;
}
}
float device_current(struct power_device *d)
{
return d->i;
}
float device_max_current(struct power_device *d)
{
return d->pm->nominal_voltage / d->r3(d->cookie);
}
float power_model_total_current(struct power_model *m)
{
return m->actual_current;
}
struct power_device *power_model_get_device(struct power_model *m, int i)
{
if (i < 0 || i >= m->ndevices)
return NULL;
return m->d[i];
}
void free_power_model(struct power_model *m)
{
int i;
for (i = 0; i < m->ndevices; i++)
free(m->d[i]);
free(m);
}
float power_model_nominal_voltage(struct power_model *m)
{
return m->nominal_voltage;
}
float power_model_actual_voltage(struct power_model *m)
{
return m->actual_voltage;
}
void power_model_enable(struct power_model *m)
{
m->enabled = 1;
}
void power_model_disable(struct power_model *m)
{
m->enabled = 0;
}