It takes the FW a short time to configure the sensor (any sensor, some longer than others). If you try reading the sensor value before it’s updated with an actual value, it will raise the SensorError exception. One way to make the program work is to make the program delay between setting up the sensor and trying to get a reading. Another way would be to deal with the error (use try: and except brickpi3.SensorError:).
It works now after having it wait a fraction of second.
However when it gets data from the Ultrasonic sensor it gives the error again.
I even made it wait 1 second after setting up the sensor.
With the program you posted, line 58 keeps calling the scan function, which keep re-configuring the ultrasonic sensor. Try doing the ultrasonic sensor configuration before the loop, where you do the touch sensor configuration.
Also, you should avoid continually resetting the encoders on lines 52, 53, 75, and 76. You should reset them once at the start of the program (where you should do sensor configuration).
If you want the robot to drive continually, either you should keep updating the target position for the motors (instead of setting them to 360 degrees), or switch to a different motor control function (such as set_motor_power or set_motor_dps).
OK, thanks. I died what you said and it really doesn’t fix much. I tried running the sensor example again and it gave one error and then was OK. My program also sometimes doesn’t work on certain ports.
Perhaps I just need to let it pass if it encounters that error.
Could the firmware be buggy? I could try reverting g to a older version and see if that helps. I would like to mention that I realized I had this error before your firmware update, just in the form of zero being returned of course.
If the errors have always been intermittent, I’m guessing it’s either an NXT Ultrasonic sensor problem, or a connection issue (poorly soldered connection, bad cable, etc.).
I won’t say that the Firmware is perfect, but I haven’t noticed any problems with using the NXT ultrasonic sensor (or any others for that matter). If you get some good readings, you could just wrap the get_sensor function in try: except: to prevent the program from crashing, and then make the program deal with the bad values.
I recommend that you use a different sensor port, a different cable, and a different NXT ultrasonic sensor (if you have a second one) to see if you can figure out where the problem is.
In my experience the NXT and EV3 ultrasonic sensors both work well with the BrickPi3. They are totally different, but both seem to work well. Over the last 10 years, I have seen several NXT ultrasonic sensors that have stopped working, which makes me think there’s a chance you have a faulty sensor.
The errors are intentionally brought all the way to the user code, so that they can be dealt with according to the application. In some situations maybe it doesn’t matter if you get bad values, but for some applications, the sensors values are critical, and a wrong value can really mess things up. By exposing the errors to the user to deal with, the user can ensure reliable readings.