First program troubles

The brains of your robot

First program troubles

Postby DannyM » Sun Dec 07, 2014 10:09 pm

Hi,

I have build the Pi Camera Robot and connected it to my LAN. With help of the blog I've installed OpenCV, Numby and Python on my windows7 computer.

I ran the examples, and they worked.

Now I have tried to write my first py script.

I tried to let the camera take pictures of 0 to 180 degrees pan in steps of 10deg, but there is a unsupported array type, what is wrong?

Danny

Code: Select all
#! /usr/bin/python

# This example shows how to use the websockets interface to make the robot move around

import time
import argparse
import py_websockets_bot
import cv2

latest_small_camera_image = None
i = 0

#---------------------------------------------------------------------------------------------------
def camera_small_image_callback( image, image_time ):
   
    global latest_small_camera_image
   
    # Put image processing here...
   
    latest_small_camera_image = image
   
#---------------------------------------------------------------------------------------------------       
def change_angle ( angle, angle_time ):
#    global angle
    bot.set_neck_angles( pan_angle_degrees=angle, tilt_angle_degrees=90.0 )
    cv2.imshow( "image", latest_small_camera_image )
    time.sleep( angle_time )


#---------------------------------------------------------------------------------------------------
if __name__ == "__main__":

    # Set up a parser for command line arguments
    parser = argparse.ArgumentParser( "Moves the robot around" )
    parser.add_argument( "hostname", default="192.168.1.38", nargs='?', help="The ip address of the robot" )

    args = parser.parse_args()

    # Connect to the robot
    bot = py_websockets_bot.WebsocketsBot( args.hostname )

    # Drive forwards
    bot.set_motor_speeds( 15.0, 15.0 )
    time.sleep( 1.0 )

    # Stop
    bot.set_motor_speeds( 0.0, 0.0 )
    time.sleep( 0.75 )

   # Turn left
    bot.set_motor_speeds( -15.0, 0.0 )
    time.sleep( 1.25 )

    # Stop
    bot.set_motor_speeds( 0.0, 0.0 )
    time.sleep( 0.75 )

    # Drive forwards
    bot.set_motor_speeds( 15.0, 15.0 )
    time.sleep( 1.0 )

    # Stop
    bot.set_motor_speeds( 0.0, 0.0 )
    time.sleep( 0.75 )

    # Stop
    bot.set_motor_speeds( 0.0, 0.0 )
    time.sleep( 0.75 )

    # Start streaming images from the camera
    bot.start_streaming_small_camera_images( camera_small_image_callback )

    # Run in a loop until i is  180deg
    try:

        for i in range( 10, 180, 10 ):
            bot.update()
           
           # if latest_small_camera_image != None:
            #change_angle ( i, 1.0 )
            cv2.imshow( "small_image", latest_small_camera_image )
               
            cv2.waitKey( 1 )
               
    except KeyboardInterrupt:
        pass    # Catch Ctrl+C


    # Look right
#    bot.set_neck_angles( pan_angle_degrees=180.0, tilt_angle_degrees=90.0 )
#    time.sleep( 2.0 )

    # Centre the neck
    bot.centre_neck()
    time.sleep( 1.0 )


       
    # Disconnect from the robot
    bot.disconnect()



    # Look left
#    bot.set_neck_angles( pan_angle_degrees=0.0, tilt_angle_degrees=90.0 )
#        time.sleep( 2.0 )



    # Start streaming images from the camera
#    bot.start_streaming_camera_images( camera_image_callback )
#    bot.start_streaming_small_camera_images( camera_small_image_callback )

    # Run in a loop until the user presses Ctrl+C to quit
#    try:
   
#        while True:
#            bot.update()
           
#            if latest_camera_image != None:
#                cv2.imshow( "image", latest_camera_image )
               
#            if latest_small_camera_image != None:
#                cv2.imshow( "small_image", latest_small_camera_image )
               
#            cv2.waitKey( 1 )
               
#    except KeyboardInterrupt:
#        pass    # Catch Ctrl+C



    # Disconnect from the robot
#    bot.disconnect()
DannyM
 
Posts: 8
Joined: Sun Dec 07, 2014 9:21 pm

Re: First program troubles

Postby Alan » Mon Dec 08, 2014 2:57 pm

Hi Danny,

Welcome to the forums. :)

I think that the problem may be that you're commented out the line

# if latest_small_camera_image != None:

so you may be passing None to cv2.imshow at some point.

To simplify the code, I would probably start without using the callback (although it may be more elegant if you can use it in the future). Can you try something like

Code: Select all
bot.start_streaming_small_camera_images()

# Run in a loop until i is  180deg
try:

    for i in range( 10, 180, 10 ):
       
        change_angle ( i, 1.0 )
        bot.update()
        latest_small_camera_image = bot.get_latest_small_camera_image()
        cv2.imshow( "small_image", latest_small_camera_image )
        cv2.waitKey( 1 )

except KeyboardInterrupt:
    pass    # Catch Ctrl+C


Hope that helps.

Regards

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am

Re: First program troubles

Postby DannyM » Mon Dec 08, 2014 6:25 pm

Hi Alan,

I tried this, but no luck.
I think there is something missing in my install of python, opencv or Numpy.

Looks like some problems with the array.

Code: Select all
Connecting to ws://192.168.1.38:80/robot_control/websocket
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupport
ed array type) in unknown function, file C:\slave\WinInstallerMegaPack\src\openc
v\modules\core\src\array.cpp, line 2482
Traceback (most recent call last):
  File "look180deg.py", line 50, in <module>
    change_angle ( i, 1.0 )
  File "look180deg.py", line 25, in change_angle
    cv2.imshow( "image", latest_small_camera_image )
cv2.error: C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\array.cpp:2
482: error: (-206) Unrecognized or unsupported array type

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\python27\lib\multiprocessing\forking.py", line 373, in main
    prepare(preparation_data)
  File "c:\python27\lib\multiprocessing\forking.py", line 488, in prepare
    '__parents_main__', file, path_name, etc
  File "C:\opencv\examples\look180deg.py", line 43, in <module>
    bot.start_streaming_small_camera_images()
NameError: name 'bot' is not defined
DannyM
 
Posts: 8
Joined: Sun Dec 07, 2014 9:21 pm

Re: First program troubles

Postby Alan » Tue Dec 09, 2014 11:12 am

Hi Danny,

I think that it's more likely that latest_small_camera_image is equal to None for some reason.

Can please you try putting

print "latest_small_camera_image =", latest_small_camera_image

before the line that reads

cv2.imshow( "small_image", latest_small_camera_image )

Also, do the example scripts get_image.py and get_image_using_callbacks.py work for you? If they work then everything is fine with your installation.

Regards

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am

Re: First program troubles

Postby DannyM » Wed Dec 10, 2014 6:39 pm

Hi Alan,

Both example scripts work indeed...

Changed the script accordingly..

But some errors about the array are stil showing.
I have read some things about the numpy addon and array's. Is it possible it is something like that?

C:\py_websockets_bot\examples>python LookAround.py 192.168.1.38
Connecting to ws://192.168.1.38:80/robot_control/websocket
latest_small_camera_image = None
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupport
ed array type) in unknown function, file C:\slave\WinInstallerMegaPack\src\openc
v\modules\core\src\array.cpp, line 2482
Traceback (most recent call last):
File "LookAround.py", line 78, in <module>
cv2.imshow( "small_image", latest_small_camera_image )
cv2.error: C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\array.cpp:2
482: error: (-206) Unrecognized or unsupported array type

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\python27\lib\multiprocessing\forking.py", line 373, in main
prepare(preparation_data)
File "c:\python27\lib\multiprocessing\forking.py", line 488, in prepare
'__parents_main__', file, path_name, etc
File "C:\py_websockets_bot\examples\LookAround.py", line 86, in <module>
bot.centre_neck()
NameError: name 'bot' is not defined
DannyM
 
Posts: 8
Joined: Sun Dec 07, 2014 9:21 pm

Re: First program troubles

Postby Alan » Wed Dec 10, 2014 9:50 pm

Hi Danny,

There might just be simple typo in the script, or a line missing somewhere. Maybe if you start from one of the working example scripts (get_image.py is probably best) and then modify it slowly to get close to what you want, you might have more joy?

Alternatively if you're still having problems could you post the complete script (i.e. select all, Ctrl-C) or email it to me at abroun@dawnrobotics.co.uk?

Cheers

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am


Return to Software

Who is online

Users browsing this forum: No registered users and 0 guests