I'm writing some code for my robot's stereoscopic vision.

This is how I'm calculating the stereo vision in python (I'm using the roborealm API).

COGXR = rr.GetVariable("COG_X_RIGHT")
COGYR = rr.GetVariable("COG_Y_RIGHT")
COGXL = rr.GetVariable("COG_X_LEFT")
COGYL = rr.GetVariable("COG_Y_LEFT")

Z_Actual = (11 * 5.5)/COGXL-COGXR
X_Actual = (COGXL*Z_Actual)/(5.5)
Y_Actual = (COGYL*Z_Actual)/(5.5)

Whenever I run the program however, I get this error message:

Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Loren\Desktop\My Robots\RoboRealm\API\API\Python\SteroTestNINA.py", line 289, in <module>
    Z_Actual = (11 * 5.5) / rr.GetVariable("COG_X_LEFT") - rr.GetVariable("COG_X_RIGHT")
TypeError: unsupported operand type(s) for /: 'float' and 'str'

This doesn't make much sense to me because COGXR and COGXL are not actually float or string variables. They come up as integers whenever I "print" them regularly. But for some reason when I stick them in an equation like this it wants to treat it as a float or a string.

Any idea's what I'm doing wrong?

BTW, haven't been to daniweb in a while

Dani AI

Generated

Most likely cause: RoboRealm’s GetVariable returns text, and sometimes an empty string when no blob/centroid is present. That explains the original TypeError (attempting arithmetic between a float and a string) and the later ValueError from trying to convert "" to a float. was correct to suspect string output, and ’s printed "33" shows the API is returning text. Also be careful with operator precedence: a/b-c is parsed as (a/b) - c; if you meant to divide by (b-c) you must use explicit parentheses.

A robust pattern is: inspect the raw return with repr(...), convert safely, and skip or log frames where conversion fails. Example helper (Python 2/3 compatible) and usage:

def to_float(value):
    if value is None:
        return None
    s = value if isinstance(value, str) else str(value)
    if s.strip() == '':
        return None
    try:
        return float(s)
    except ValueError:
        return None

left_raw = rr.GetVariable("COG_X_LEFT")
right_raw = rr.GetVariable("COG_X_RIGHT")
left = to_float(left_raw)
right = to_float(right_raw)

if left is None or right is None:
    # log or ignore this frame; use repr(left_raw)/repr(right_raw) to debug
else:
    denom = left - right
    if denom == 0:
        # avoid ZeroDivisionError
    else:
        z_actual = (11.0 * 5.5) / denom   # parentheses ensure intended math

Extra tips: print repr(...) of raw API returns when debugging, guard against empty/whitespace strings, and handle zero denominators. In Python 2 use from __future__ import division (or make literals floats, e.g. 11.0) to avoid integer-division surprises. These checks make the stereo computation resilient to missing detections and will prevent the TypeError/ValueError you saw.

Recommended Answers

All 6 Replies

what is the value of COGXR??

Can u give

print COGXR

before the Z_Actual thing and see what the output is?

Yes.

Apparently, COGXR equals "33"

Here's what it looks like before the exception is raised

Width:  160 Height:  120
IMAGE_COUNT = 541
33
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Loren\Desktop\My Robots\RoboRealm\API\API\Python\SteroTestNINA.py", line 291, in <module>
    Z_Actual = (11 * 5.5)/COGXL-COGXR
TypeError: unsupported operand type(s) for /: 'float' and 'str'

Any ideas what's wrong?

I guess your output is in string.

try

Z_Actual = (11 * 5.5)/float(COGXL)-float(COGXR)

Okay, I tried that, but now I'm getting this:

Width:  160 Height:  120
IMAGE_COUNT = 274

Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Loren\Desktop\My Robots\RoboRealm\API\API\Python\SteroTestNINA.py", line 291, in <module>
    Z_Actual = (11 * 5.5)/float(COGXL)-float(COGXR)
ValueError: empty string for float()

ok...
what do get if u ..

print float(COGXL)
print float(COGXR)

before the calculations.

Ur error message says one of them is empty.

Thanks for the tips, xav.vijay!

The code is working and I'm getting some stereo vision responses from my robot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.