Closed mathieucarbou closed 5 months ago
(reopening)
These are the lines I would like to understand how to adjust them:
double tempBrightness;
if (Thyristor::getFrequency() == 50) {
tempBrightness = -1.5034e-10 * pow(bri, 5) + 9.5843e-08 * pow(bri, 4)
- 2.2953e-05 * pow(bri, 3) + 0.0025471 * pow(bri, 2) - 0.14965 * bri + 9.9846;
} else if (Thyristor::getFrequency() == 60) {
tempBrightness = -1.2528e-10 * pow(bri, 5) + 7.9866e-08 * pow(bri, 4)
- 1.9126e-05 * pow(bri, 3) + 0.0021225 * pow(bri, 2) - 0.12471 * bri + 8.3201;
Hi Mathieu, very briefly, I had estimated them using Octave and they approximate the inverse of formula to compute energy of a sine wave. The idea was to use a polynomial formula instead of the common lookup table to save memory on Arduino Uno trading off computational time... I'm not sure if this is still meaningful with modern MCU.
Can you tell which sensor you use to measure the output power? At the time, I had used a commercial a smart plug, and the result seemed reasonable, however I'm not sure about the reliability of that device...
You are touching all the points I would have to improve... Unfortunately, I have few issues to solve outside here, but I guarantee I will come back on all of these.
Hello,
So I am measuring the output power with a PZEM004Tv3, multimeter (RMS support) and oscilloscope (Owen HD).
I've also drawn a comparison of 3 implementations here:
https://docs.google.com/spreadsheets/d/1dCpAydu3WHekbXEdUZt53acCa6aSjtCBxW6lvv89Ku0/edit#gid=0
V3 is really the best that is closest to reality, but it could be improved:
For the LUT, I have converted all the values to a LUT table. I am only using levels from 0-100, not 0-255 to have less data and to save memory I am using PROGMEM.
I have 4 LUT per version:
Vrms factor for 60Hz
=> I am multiplying this factor by the input measured voltage to get the dimmed voltage (Vrms)
So basically, the idea would be to shift the curve a little for the smaller values. I really don't know how to achieve that.
For example, at 25% on the slider, I have:
"angle_deg": 113,
"angle_rad": 1.975119352,
"delay": 6287,
"level": 25,
"vrms_factor": 0.506137013,
"voltage_in": 208.6000061,
"voltage_out": 105.5801849,
But to be closer to reality (111V), I should have instead the values that are shown at around 28%:
"angle_deg": 110,
"angle_rad": 1.931451201,
"delay": 6148,
"level": 28,
"vrms_factor": 0.529244006,
"voltage_in": 209.5,
"voltage_out": 110.8766174,
Note: this is normal that my grid voltage is low: the EV car is charging ;-)
Here are some data:
level | Output Vrms calculated based on input voltage | Output Vrms on multimeter | diff |
---|---|---|---|
2% | 10V | 5V | -5V |
4% | 24.5V | 29V | -5V |
16% | 90.5V | 94.5V | - 5V |
25% | 111V | 115V | - 4V |
35% | 126V | 130V | - 4V |
50% | 152.5V | 156.5V | - 4V |
60% | 169.5V | 172.5V | - 3V |
70% | 183.5V | 185.5V | - 2V |
80% | 195V | 195V | 0V |
90% | 207V | 207V | 0V |
update:
I did another test today, more targetted at some values, but this time using the exact brigthness values you have.
BRI 3 4 10 40 128 220 250
V 0 -5 -3 -2.5 -3 0 -1
BRI is the value tested (0-255) V is the difference : Vrms calculated - multimeter (the multimeter always show a higher value)
I've chose these value based on this graph:
V1 is calculating an angle with the formula:
return acos(2 * level / 255.0 - 1);
So I think a quick and dirty adjustment would be to compute the Vrms from a level value that is shifted, but for example +3 for lower values, then +2, then +1, then 0.
Example: if I want the vrms of level=4 (out of 255), then I should get level - 7.
Not sure the algo can be changed because if the algo changes, than consequently the delay changes, and the vrms follows.
So it is like I would need a different algo to grab the right vrms.
Here is the algo I used to test and apply the shift.
Shifting by +2 works well for 40 and 128. But not after.
I need to fund a function which is applying a shift decreasingly, and not linearly.
+3 from 4 to 10, +2 from 11 to 175 (?), etc...
uint16_t Mycila::Dimmer::_lookupFiringDelay(uint8_t level, float frequency) {
if (level == 0)
return 500000 / frequency; // semi-period in microseconds
if (level == MYCILA_DIMMER_MAX_LEVEL)
return 0;
// // https://github.com/fabianoriccardi/dimmable-light/blob/main/src/dimmable_light_linearized.h
if (frequency == 60)
return -1.2528e-7 * pow(level, 5) + 7.9866e-5 * pow(level, 4) - 1.9126e-2 * pow(level, 3) + 2.1225 * powf(level, 2) - 124.71 * level + 8320.1;
else
return -1.5034e-7 * pow(level, 5) + 9.5843e-5 * pow(level, 4) - 2.2953e-2 * pow(level, 3) + 2.5471 * pow(level, 2) - 149.65 * level + 9984.6;
}
float Mycila::Dimmer::_lookupVrmsFactor(uint8_t level, float frequency) {
if (level == 0)
return 0;
if (level > MYCILA_DIMMER_MAX_LEVEL - 4)
return 1;
level += 2;
const uint16_t delay = _lookupFiringDelay(level, frequency);
const float angle = Mycila::Dimmer::_delayToPhaseAngle(delay, frequency);
return Mycila::Dimmer::_vrmsFactor(angle);
}
These are the textbook functions to convert a power ratio to a phase delay ratio:
def phase2duty(phase):
"""
Phase delay ratio [0,1] to power ratio [0,1]
Sine square CDF (Cumulative Distribution Function)
"""
return sin(2 * pi * phase) / (2 * pi) - phase + 1
def duty2phase(duty):
"""
Power delay ratio [0,1] to phase ratio [0,1]
Invert Sine square CDF, Bisection method
"""
bounds = [0, 1]
for _ in range(32):
phase = (bounds[0] + bounds[1]) / 2
bounds[duty > phase2duty(phase)] = phase
return phase
The duty2phase
should only be used to build a lookup table since it's quite expensive.
The source of your issue could be due to the polynomial error since it's not constant and it can be quite high:
This d2p
function is much more accurate:
phase = (powf(1 - duty, 0.31338) - powf(duty, 0.31338) + 1) / 2
Of course the accuracy is as good as the grid remains a perfect sine wave.
thanks a lot!
I would like to tweak how the delay is generated based on the level value (0-255).
I saw the polynomial equation but fail to understand where it comes from and how to tweak it.
I did a sample of all possible values with the current algorithm:
with the normal dimmer, here are the matching brightness values to set to be at a apecific power level (in terms of watts):
same for the linearised version:
I would like to tweak (maybe much more the second version) in order to better match the power output to the resistive load.
This would be equivalent to have a function which, depending on the value, output the right voltage value maybe.