question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`ValueError: invalid literal for int() with base 10: ''` when using InfraredSensor in Threading

See original GitHub issue
  • ev3dev version: 4.14.117-ev3dev-2.3.5-ev3
  • ev3dev-lang-python version: ||/ Name Version Architecture Description ++±====================================-=======================-=======================-============================================================================== ii micropython-ev3dev2 2.1.0 all Python language bindings for ev3dev for MicroPython ii python3-ev3dev 1.2.0 all Python language bindings for ev3dev ii python3-ev3dev2 2.1.0 all Python language bindings for ev3dev

I’ve got an error in a parallel Thread that uses the InfraredSensor when running the below program. The error message is not easy to understand, so I’m not sure what the problem is. May I have some help troubleshooting this case? Thank you very much.

#!/usr/bin/env python3


from ev3dev2.motor import LargeMotor, MediumMotor, MoveTank, OUTPUT_A, OUTPUT_B, OUTPUT_C
from ev3dev2.sensor import INPUT_1, INPUT_3, INPUT_4
from ev3dev2.sensor.lego import TouchSensor, ColorSensor, InfraredSensor

from random import randint
from threading import Thread


class Ev3rstorm:
    def __init__(
            self,
            left_foot_motor_port: str = OUTPUT_B, right_foot_motor_port: str = OUTPUT_C,
            ir_sensor_port: str = INPUT_4, ir_beacon_channel: int = 1):
        self.tank_driver = MoveTank(left_motor_port=left_foot_motor_port,
                                    right_motor_port=right_foot_motor_port,
                                    motor_class=LargeMotor)

        self.ir_sensor = InfraredSensor(address=ir_sensor_port)
        self.ir_beacon_channel = ir_beacon_channel


    def drive_once_by_ir_beacon(self, speed: float = 100):
        # forward
        if self.ir_sensor.top_left(self.ir_beacon_channel) and self.ir_sensor.top_right(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=speed,
                right_speed=speed)

        # backward
        elif self.ir_sensor.bottom_left(self.ir_beacon_channel) and self.ir_sensor.bottom_right(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=-speed,
                right_speed=-speed)

        # turn left on the spot
        elif self.ir_sensor.top_left(self.ir_beacon_channel) and self.ir_sensor.bottom_right(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=-speed,
                right_speed=speed)

        # turn right on the spot
        elif self.ir_sensor.top_right(self.ir_beacon_channel) and self.ir_sensor.bottom_left(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=speed,
                right_speed=-speed)

        # turn left forward
        elif self.ir_sensor.top_left(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=0,
                right_speed=speed)

        # turn right forward
        elif self.ir_sensor.top_right(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=speed,
                right_speed=0)

        # turn left backward
        elif self.ir_sensor.bottom_left(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=0,
                right_speed=-speed)

        # turn right backward
        elif self.ir_sensor.bottom_right(self.ir_beacon_channel):
            self.tank_driver.on(
                left_speed=-speed,
                right_speed=0)

        # otherwise stop
        else:
            self.tank_driver.off(brake=False)

    def keep_driving_by_ir_beacon(self, speed: float = 100):
        while True:
            self.drive_once_by_ir_beacon(speed=speed)


    def dance_whenever_ir_beacon_pressed(self):
        while True:
            while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
                self.tank_driver.on_for_seconds(
                    left_speed=randint(-100, 100),
                    right_speed=randint(-100, 100),
                    seconds=1,
                    brake=False,
                    block=True)
        
    
    def main(self, driving_speed: float = 100):
        # ERROR
        # Traceback (most recent call last):
        # File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
        #   self.run()
        # File "/usr/lib/python3.5/threading.py", line 862, in run
        #   self._target(*self._args, **self._kwargs)
        # File "/home/robot/Ev3rstorm/Ev3rstorm-Super.EV3Dev2.Python3.Threading.py", line 170, in dance_whenever_ir_beacon_pressed
        #   while self.ir_sensor.beacon(channel=self.ir_beacon_channel):
        # File "/usr/lib/python3/dist-packages/ev3dev2/sensor/lego.py", line 909, in beacon
        #   return 'beacon' in self.buttons_pressed(channel)
        # File "/usr/lib/python3/dist-packages/ev3dev2/sensor/lego.py", line 919, in buttons_pressed
        #   return self._BUTTON_VALUES.get(self.value(channel), [])
        # File "/usr/lib/python3/dist-packages/ev3dev2/sensor/__init__.py", line 203, in value
        #   self._value[n], value = self.get_attr_int(self._value[n], 'value' + str(n))
        # File "/usr/lib/python3/dist-packages/ev3dev2/__init__.py", line 307, in get_attr_int
        #   return attribute, int(value)
        # ValueError: invalid literal for int() with base 10: ''
        Thread(target=self.dance_whenever_ir_beacon_pressed,
               daemon=True).start()

        self.keep_driving_by_ir_beacon(speed=driving_speed)


if __name__ == '__main__':
    EV3RSTORM = Ev3rstorm()
    EV3RSTORM.main()

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TheVinhLuong102commented, Jul 7, 2020

@dlech thank you. In that case, I think the above program actually does not make the IR sensor switch among multiple modes, because the program only checks for various button presses. So probably this issue is of the same nature as https://github.com/ev3dev/ev3dev/issues/1269 – i.e. resulting from multiple threads accessing the sensor values at the same time.

1reaction
dlechcommented, Jul 5, 2020

In addition to what @WasabiFan said…

The EV3 IR sensor does not like swapping modes frequently.

Using the IR beacon for heading/distance is one mode of operation and using the remote control buttons is another mode of operation (actually there are two different modes for remote control buttons).

Programs should only use one mode of operation or wait for several seconds after switching modes for best results.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix this ValueError invalid literal for int with base 10 ...
You can solve this error by using the Python isdigit() method to check whether the value is number or not.
Read more >
ValueError: invalid literal for int() with base 10: '' - Stack Overflow
The reason you are getting this error is that you are trying to convert a space character to an integer, which is totally...
Read more >
Python ValueError: invalid literal for int() with base 10 Solution
Our error message tells us there is an invalid literal for an integer in base 10. This means the value we have passed...
Read more >
OCR for analog meter using opencv - #19 by jhhbe - Third party ...
Hi Guys, I want to use the OpenCV platform to read off an analog water meter. ... int(reading) ValueError: invalid literal for int()...
Read more >
Python Scientific lecture notes
12 Image manipulation and processing using Numpy and Scipy ... SyntaxError: invalid syntax ... ValueError: invalid literal for int() with base 10: 'a'....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found