go-bsbmp icon indicating copy to clipboard operation
go-bsbmp copied to clipboard

panic: runtime error: integer divide by zero

Open hyzhak opened this issue 6 years ago • 3 comments

I've got an error:

panic: runtime error: integer divide by zero

goroutine 40 [running]:
github.com/d2r2/go-bsbmp.(*SensorBMP180).ReadPressureMult10Pa(0x2502020, 0x2500080, 0x3, 0x4087becc, 0xc0000000, 0x4087becc)
	/home/pi/go/src/github.com/d2r2/go-bsbmp/bmp180.go:340 +0xfa4
github.com/d2r2/go-bsbmp.(*BMP).ReadAltitude(0x2500090, 0x3, 0x4087becc, 0x1, 0x4b2038)
	/home/pi/go/src/github.com/d2r2/go-bsbmp/bmp.go:213 +0x38
main.main.func3(0x2500090)

hyzhak avatar Jun 29 '19 10:06 hyzhak

@hyzhak, debug information you have provided is very limited. I can't figure out what happens. I can only theoretically assume that you specific sensor sample has empty coefficient set. Did you try another sample?

To give better debug output, take example1.go file, comment the line

logger.ChangePackageLogLevel("bsbmp", logger.InfoLevel)

, and finally run whole example. Put whole debug output here. I hope this give more details on issue root cause.

d2r2 avatar Jun 30 '19 04:06 d2r2

Sensor (BMP180) is working fine, but once in a while (about once a day) it fails with divide by zero error.

Here is sample of log from the last fail

2019-07-01T08:33:30.787 [     i2c] DEBUG  Write 2 hex bytes: [f42f]
2019-07-01T08:33:30.788 [     i2c] DEBUG  Write U8 47 to reg 0xF4
2019-07-01T08:33:30.788 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.788 [     i2c] DEBUG  Read 1 hex bytes: [2b]
2019-07-01T08:33:30.789 [     i2c] DEBUG  Read U8 43 from reg 0xF4
2019-07-01T08:33:30.794 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.795 [     i2c] DEBUG  Read 1 hex bytes: [0b]
2019-07-01T08:33:30.795 [     i2c] DEBUG  Read U8 11 from reg 0xF4
2019-07-01T08:33:30.796 [     i2c] DEBUG  Write 1 hex bytes: [f6]
2019-07-01T08:33:30.797 [     i2c] DEBUG  Read 2 hex bytes: [6555]
2019-07-01T08:33:30.798 [     i2c] DEBUG  Read U16 25941 from reg 0xF6
2019-07-01T08:33:30.799 [     i2c] DEBUG  Write 1 hex bytes: [aa]
2019-07-01T08:33:30.802 [     i2c] DEBUG  Read 22 hex bytes: [2295fb73c63e8289629c41cb1973002d8000d1f60a6f]
2019/07/01 08:33:30 ....
2019-07-01T08:33:30.803 [     i2c] DEBUG  Write 2 hex bytes: [f42f]
2019-07-01T08:33:30.803 [     i2c] DEBUG  Write U8 47 to reg 0xF4
2019-07-01T08:33:30.804 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.805 [     i2c] DEBUG  Read 1 hex bytes: [2b]
2019-07-01T08:33:30.805 [     i2c] DEBUG  Read U8 43 from reg 0xF4
2019-07-01T08:33:30.811 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.812 [     i2c] DEBUG  Read 1 hex bytes: [0b]
2019-07-01T08:33:30.813 [     i2c] DEBUG  Read U8 11 from reg 0xF4
2019-07-01T08:33:30.814 [     i2c] DEBUG  Write 1 hex bytes: [f6]
2019-07-01T08:33:30.815 [     i2c] DEBUG  Read 2 hex bytes: [6555]
2019-07-01T08:33:30.816 [     i2c] DEBUG  Read U16 25941 from reg 0xF6
2019-07-01T08:33:30.817 [     i2c] DEBUG  Write 2 hex bytes: [f4b4]
2019-07-01T08:33:30.818 [     i2c] DEBUG  Write 2 hex bytes: [f42f]
2019-07-01T08:33:30.818 [     i2c] DEBUG  Write U8 180 to reg 0xF4
2019-07-01T08:33:30.818 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.818 [     i2c] DEBUG  Write U8 47 to reg 0xF4
2019-07-01T08:33:30.818 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.819 [     i2c] DEBUG  Read 1 hex bytes: [b0]
2019-07-01T08:33:30.819 [     i2c] DEBUG  Read U8 176 from reg 0xF4
2019-07-01T08:33:30.820 [     i2c] DEBUG  Read 1 hex bytes: [b0]
2019-07-01T08:33:30.820 [     i2c] DEBUG  Read U8 176 from reg 0xF4
2019-07-01T08:33:30.825 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.825 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.825 [     i2c] DEBUG  Read 1 hex bytes: [b0]
2019-07-01T08:33:30.826 [     i2c] DEBUG  Read U8 176 from reg 0xF4
2019-07-01T08:33:30.826 [     i2c] DEBUG  Read 1 hex bytes: [b0]
2019-07-01T08:33:30.826 [     i2c] DEBUG  Read U8 176 from reg 0xF4
2019-07-01T08:33:30.831 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.831 [     i2c] DEBUG  Write 1 hex bytes: [f4]
2019-07-01T08:33:30.832 [     i2c] DEBUG  Read 1 hex bytes: [90]
2019-07-01T08:33:30.832 [     i2c] DEBUG  Read U8 144 from reg 0xF4
2019-07-01T08:33:30.832 [     i2c] DEBUG  Write 1 hex bytes: [f6]
2019-07-01T08:33:30.832 [     i2c] DEBUG  Read 1 hex bytes: [90]
2019-07-01T08:33:30.832 [     i2c] DEBUG  Read U8 144 from reg 0xF4
2019-07-01T08:33:30.832 [     i2c] DEBUG  Read 3 bytes starting from reg 0xF6...
2019-07-01T08:33:30.833 [     i2c] DEBUG  Write 1 hex bytes: [f6]
2019-07-01T08:33:30.833 [     i2c] DEBUG  Read 2 hex bytes: [a625]
2019-07-01T08:33:30.833 [     i2c] DEBUG  Read U16 42533 from reg 0xF6
2019-07-01T08:33:30.833 [     i2c] DEBUG  Write 1 hex bytes: [aa]
2019-07-01T08:33:30.834 [     i2c] DEBUG  Read 3 hex bytes: [a62580]
2019-07-01T08:33:30.834 [     i2c] DEBUG  Write 1 hex bytes: [aa]
2019-07-01T08:33:30.836 [     i2c] DEBUG  Read 22 hex bytes: [2295fb73c63e8289629c41cb1973002d8000d1f60a6f]
2019/07/01 08:33:30 ....
2019-07-01T08:33:30.839 [     i2c] DEBUG  Read 22 hex bytes: [0000bc33000000000000001000000003550206000000]
panic: runtime error: integer divide by zero

goroutine 27 [running]:
github.com/d2r2/go-bsbmp.(*SensorBMP180).ReadPressureMult10Pa(0x14d4138, 0x14ce650, 0x3, 0x403c3333, 0x40000000, 0x403c3333)
	/home/pi/go/src/github.com/d2r2/go-bsbmp/bmp180.go:340 +0xfa4
github.com/d2r2/go-bsbmp.(*BMP).ReadPressurePa(0x14ce660, 0x3, 0x403c3333, 0x1, 0x4b29e8)
	/home/pi/go/src/github.com/d2r2/go-bsbmp/bmp.go:177 +0x38
main.main.func3(0x14ce660)

hyzhak avatar Jul 01 '19 11:07 hyzhak

@hyzhak, I had one buggy BMP280, which works then did not work...

I can add extra check in every math division taken from sensor calculation specification, but my advice - try to run with any other BMP sample. I just want to clarify what cause the issue: sensor or embedded device. If BMP software produce this error every time with no exception, there is no doubt - code contains error, but in your case it looks you have something buggy - sensor or embedded device.

BTW, try to run BMP180 sensor with Python - would it have same behavior?

Another advice. You can add next code before any measurement:

err = sensor.IsValidCoefficients()
	if err != nil {
		lg.Fatal(err)
	}

, because it execute verification that coefficient set is not zero:

// checkCoefficient verify that compensation parameter looks valid.
func checkCoefficient(coef uint16, name string) error {
	if coef == 0 || coef == 0xFFFF {
		return fmt.Errorf("Coefficient %s is invalid: 0x%X", name, coef)
	}
	return nil
}

Once IsValidCoefficients() return non-empty error value, you should not run measurements, since you have some failure situation.

Again my major advice for you - find what unstable part you have. There could million of reasons of it, beginning from bad power supply, ending with a broken/invalid Linux firmware.

d2r2 avatar Jul 01 '19 12:07 d2r2