Arduino-Temperature-Control-Library icon indicating copy to clipboard operation
Arduino-Temperature-Control-Library copied to clipboard

patch: add error code handling of temperatures

Open Andersama opened this issue 2 years ago • 12 comments

tentative patch for handling issue #236

  • adds unit types to force handling of different units with respect to each other
  • implicit conversions to floats only for result types from existing functions
  • basic error codes added as an enum, edit as needed

Please test that this compiles on hardware before commiting* I don't have an arduino laying around anymore.

Just looked back at the underlying code for the basic errors I added, the originals all appear to be from a bitmask, could be considerably faster to just pass along the error state from what was read. EG: take

		if (scratchPad[TEMP_LSB] & 1) { // Fault Detected
			if (scratchPad[HIGH_ALARM_TEMP] & 1) {
				//Serial.println("open detected");
				r.reading.raw = DEVICE_FAULT_OPEN_RAW;
				r.error_code = DallasTemperature::device_error_code::device_fault_open;
				return r; //return DEVICE_FAULT_OPEN_RAW;
			}
			else if (scratchPad[HIGH_ALARM_TEMP] >> 1 & 1) {
				//Serial.println("short to ground detected");
				r.reading.raw = DEVICE_FAULT_SHORTGND_RAW;
				r.error_code = DallasTemperature::device_error_code::device_fault_shortgnd;
				return r;
			}
			else if (scratchPad[HIGH_ALARM_TEMP] >> 2 & 1) {
				//Serial.println("short to Vdd detected");
				r.reading.raw = DEVICE_FAULT_SHORTVDD_RAW;
				r.error_code = DallasTemperature::device_error_code::device_fault_shortvdd;
				return r;
			}
			else {
				// We don't know why there's a fault, exit with disconnect value
				r.reading.raw = DEVICE_DISCONNECTED_RAW;
				r.error_code = DallasTemperature::device_error_code::device_disconnected;
				return r;
			}
		}

and turn it into something like:

		if (scratchPad[TEMP_LSB] & 1) { // Fault Detected
			        r.reading.raw = (scratchPad[HIGH_ALARM_TEMP] & 0x7) | 0x8; // treat 0x8 as "fault detected" or "disconnected"
				r.error_code = DallasTemperature::device_error_code::device_fault_open;
				return r;
		}

Andersama avatar Mar 11 '23 11:03 Andersama

Some example usage:


bool address_ok = sensors.getAddress(deviceAddress, 0);
if (!address_ok)
   return;

DallasTemperature::request_t request = sensors.requestTemperatures();
DallasTemperature::celsius_result_t temp_reading= sensors.getTempC(deviceAddress);
// consider utility functions for testing for specific error flags / codes
if (temp_reading.error_code & DallasTemperature::device_error_code::device_fault_open) {
   //display error for shorted ground
}
if (temp_reading.error_code & DallasTemperature::device_error_code::device_fault_shortgnd) {
   //display error for shorted ground
}
if (temp_reading.error_code & DallasTemperature::device_error_code::device_fault_shortvdd) {
   //display error for shorted vdd
}
// etc...for other errors
if (temp_reading.error_code != DallasTemperature::device_error_code::device_connected) {
   return;
}

float f = temp_reading.value.celcius; // do something with the temperature
//float f = temp_reading; // for backwards compatibility

Andersama avatar Mar 23 '23 06:03 Andersama

If I understand this correctly, applying this patch would require refactoring existing usage of the library. I don't think that's acceptable as that would require firmware changes to every project that uses this library. --edit I see how it works now. I'm testing this in a project that needs error reporting. will report back results

LokiMetaSmith avatar Nov 16 '23 18:11 LokiMetaSmith

You may want to add the other edit for simplifying the flags, should in theory cut the number of jumps in the resulting code. Not sure if I made a patch for that in my own fork.

Andersama avatar Nov 18 '23 21:11 Andersama

Hello @Andersama

I am trying your example code. I get an error compiling at the line float f = temp_reading.celcius; // do something with the temperature

Compiler error

C:\Users\Public\Downloads\Arduino\PubInv\DallasTemprature\Arduino-Temperature-Control-Library\Arduino-Temperature-Control-Library.ino: In function 'void isError_AndPrintTemperature(uint8_t*)':
Arduino-Temperature-Control-Library:200:26: error: 'struct DallasTemperature::celsius_result_t' has no member named 'celcius'
   float f = temp_reading.celcius; // do something with the temperature
                          ^
Multiple libraries were found for "OneWire.h"
 Used: C:\Users\Admin\Documents\Arduino\libraries\OneWire
 Not used: C:\Users\Admin\Documents\Arduino\libraries\MAX31850_OneWire
exit status 1
'struct DallasTemperature::celsius_result_t' has no member named 'celcius'

I am attaching a zip of my sketch which produced this error on an Arduino Due Arduino-Temperature-Control-LibraryForAndersama.zip

Full Example Please

Could you provide a fully functional example which compiles? I do not read C++ well enough to trouble shoot this error.

Thanks.

ForrestErickson avatar Nov 28 '23 15:11 ForrestErickson

@ForrestErickson well glad you decided to test it first. That error's saying that the type doesn't contain a member called "celsius" which, on second viewing, is true. Badly written example on my part try value.celsius.

Andersama avatar Apr 26 '24 17:04 Andersama

@LokiMetaSmith yeah, from what I remember there was a discussion about magic numbers somewhere, so this was my suggestion instead. I kept the original magic values in the commit so this should be backwards compatible. I was aiming though to remove the nested if/else if chain.

Andersama avatar Apr 26 '24 17:04 Andersama

Like you @Andersama I don't have an Arduino to hand so I'm hesitant to merge. @RobTillaart any thoughts?

milesburton avatar Apr 28 '24 16:04 milesburton

Had a quick look and besides error codes it introduces a few new types. I understand that this allows compile time checking, which is good.

However what worries me is that it might make existing projects more complex for the average Arduino user.

Furthermore does this extra type checking help the novice Arduino user? I know arguments for both sides.

So I need to review the details in more depth and check what happens with existing code too. I do not have time to dive into these tests on short term.

RobTillaart avatar Apr 28 '24 17:04 RobTillaart

@milesburton I see the logs of the build are removed (auto xleanup). Can you redo the build ci test, as that would show if there are more warnings or so.

RobTillaart avatar Apr 28 '24 17:04 RobTillaart

Annoyingly that doesn't appear to be as straight forward as I'd like. Let me pull the change and push a dummy commit. Hopefully that'll persuade github to rerun the jobs

milesburton avatar Apr 28 '24 18:04 milesburton

Well whatever exactly I was thinking at the time's a bit lost to me at this point, it's been a year, but I do remember trying to make it compatible abi wise as possible.

Andersama avatar Apr 28 '24 19:04 Andersama

FYI, I am traveling now and will not be back to my lab and hardware for some weeks. I have an Arduino DUE system with three MAX31850 on which I can test once I get caught up.

ForrestErickson avatar Apr 30 '24 19:04 ForrestErickson