Skip to content

Commit 9068add

Browse files
committed
Add ability to set calibration directly
1 parent bf3682a commit 9068add

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

src/driver/drv_pwrCal.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static int latest_raw_voltage;
1616
static float latest_raw_current;
1717
static int latest_raw_power;
1818

19+
#define VERY_SMALL_VAL 0.001f
1920
//#define PWRCAL_DEBUG
2021

2122
static commandResult_t Calibrate(const char *cmd, const char *args, float raw,
@@ -31,7 +32,7 @@ static commandResult_t Calibrate(const char *cmd, const char *args, float raw,
3132
CMD_GetResultString(CMD_RES_BAD_ARGUMENT));
3233
return CMD_RES_BAD_ARGUMENT;
3334
}
34-
#define VERY_SMALL_VAL 0.001f
35+
3536
if (raw > -VERY_SMALL_VAL && raw < VERY_SMALL_VAL) {
3637
ADDLOG_ERROR(LOG_FEATURE_ENERGYMETER, "Calibration incorrect - connect load first.");
3738
return CMD_RES_ERROR;
@@ -70,6 +71,53 @@ static commandResult_t CalibratePower(const void *context, const char *cmd,
7071
return Calibrate(cmd, args, latest_raw_power, &power_cal, CFG_OBK_POWER);
7172
}
7273

74+
static commandResult_t CalibrationSet(const void *context, const char *cmd,
75+
const char *args, int cmdFlags) {
76+
Tokenizer_TokenizeString(args, 0);
77+
int numArgs = Tokenizer_GetArgsCount();
78+
79+
// 1. If called without parameters, print current constants to logs
80+
if (numArgs == 0) {
81+
ADDLOG_INFO(LOG_FEATURE_ENERGYMETER, "Current Calibration Constants: V=%f, I=%f, P=%f",
82+
voltage_cal, current_cal, power_cal);
83+
return CMD_RES_OK;
84+
}
85+
86+
// 2. Ensure we have exactly 3 arguments for V, I, and P
87+
if (numArgs < 3) {
88+
ADDLOG_ERROR(LOG_FEATURE_ENERGYMETER, "CalibrationSet requires 3 args: [VoltageCal] [CurrentCal] [PowerCal]");
89+
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
90+
}
91+
92+
float vals[3];
93+
vals[0] = Tokenizer_GetArgFloat(0); // Voltage
94+
vals[1] = Tokenizer_GetArgFloat(1); // Current
95+
vals[2] = Tokenizer_GetArgFloat(2); // Power
96+
97+
// 3. Validation: NOT NaN, Positive, and > VERY_SMALL_VAL
98+
for (int i = 0; i < 3; i++) {
99+
if (isnan(vals[i]) || vals[i] <= VERY_SMALL_VAL) {
100+
ADDLOG_ERROR(LOG_FEATURE_ENERGYMETER, "CalibrationSet: Argument %d is invalid (%f). Must be > %f and not NaN.",
101+
i + 1, vals[i], VERY_SMALL_VAL);
102+
return CMD_RES_BAD_ARGUMENT;
103+
}
104+
}
105+
106+
// 4. Apply and Save
107+
voltage_cal = vals[0];
108+
current_cal = vals[1];
109+
power_cal = vals[2];
110+
111+
CFG_SetPowerMeasurementCalibrationFloat(CFG_OBK_VOLTAGE, voltage_cal);
112+
CFG_SetPowerMeasurementCalibrationFloat(CFG_OBK_CURRENT, current_cal);
113+
CFG_SetPowerMeasurementCalibrationFloat(CFG_OBK_POWER, power_cal);
114+
115+
ADDLOG_INFO(LOG_FEATURE_ENERGYMETER, "Calibration Constants updated to: V=%f, I=%f, P=%f",
116+
voltage_cal, current_cal, power_cal);
117+
118+
return CMD_RES_OK;
119+
}
120+
73121
static float Scale(float raw, float cal) {
74122
return (cal_type == PWR_CAL_MULTIPLY ? raw * cal : raw / cal);
75123
}
@@ -100,6 +148,11 @@ void PwrCal_Init(pwr_cal_type_t type, float default_voltage_cal,
100148
//cmddetail:"fn":"CalibratePower","file":"driver/drv_pwrCal.c","requires":"",
101149
//cmddetail:"examples":""}
102150
CMD_RegisterCommand("PowerSet", CalibratePower, NULL);
151+
//cmddetail:{"name":"CalibrationSet","args":"[VoltageCal][CurrentCal][PowerCal]",
152+
//cmddetail:"descr":"Directly sets or displays calibration constants. Call without args to print current values. Provide 3 floats (>0.001) to overwrite.",
153+
//cmddetail:"fn":"CalibrationSet","file":"driver/drv_pwrCal.c","requires":"",
154+
//cmddetail:"examples":"CalibrationSet 0.1245 0.0052 1.002"}
155+
CMD_RegisterCommand("CalibrationSet", CalibrationSet, NULL);
103156
}
104157

105158
void PwrCal_Scale(int raw_voltage, float raw_current, int raw_power,

0 commit comments

Comments
 (0)