Stream Camera while Controlling GoPiGo


#1

Hello Forum!

I want to stream what the picam capture while i control the GoPiGo with “w” “a” “s” “d” as the script provided “basic_test_all.py”.

In order to the streaming video i wrote this little code and it works:

import all the libraries needed
h=200
w=300
camera = PiCamera()
camera.resolution(w,h)
camera.framerate = 5
rawCapture = PiRGBArray(camera, size(w,h))
time.sleep(0.1)

from frame in camera.capture_continuous (rawCapture, format"bgr", use_video_port=True):
     image_RGB = frame.array
     

     cv2.imshow("Image", image_RGB)
     key = cv2.waitKey(1) & 0xFF
     rawCapture.truncate(0)
     if key == ord("q"):
         break

With this code, where and how would you put the:

   while True:
           print "nCmd:",
           a = raw_input()
           if a=='w':
                    fwd()
           elif a=='a':
                    left()
           elif a=='d':
                    right()
           elif a=='s':
                    bwd()
           elif a=='x':
                    stop()

in order to work at the same time?

Thanks a bunch again!


#2

Hey,
To make them work at the same time, you can either have 2 different programs running in 2 sessions via VNC or SSH or you can have a single program with multiple threads. It would be hard to get it to work without threads in a single program.

-Karan


#3

Sorry, but i’m a noob and i don’t know what you mean by threads. Is there an easy way to get all this stuff running into one script ?

Thanks again karan!


#4

check out this link here :Python Multithreaded Programming.

Running several threads is similar to running several different programs concurrently

#5

Thanks Exadon!

I’ve been testing some examples, and i understand how it works (more or less). On the scripts i wrote, It seems that only works one thread.

So, I own a Raspberry pi B+, can i deal with threading codes whit the B+ ? or should i upgrade to Raspberry pi 2 in order to send two programs by two different threads just to run them in parallel ?

Seriously, thank you very much!


#6

You are fine with any model of the raspberry pi.


#7

Well, I’ve been trying some examples but i can only execute one program.

I wrote this code:

import threading

class prog(threading.Thread):

       def __init__(self): 
               threading.Thread.__init__(self)
       def run(self):
               while True:
                      print "First program"

class prog2(threading.Thread):
def run(self):
while True:
print “Second program”

my_program1=prog()
my_program2=prog2()

my_program1.start()
my_program2.start()

And… with this code i get all the time: “First program”

So if you please, can tell me why the second program isn’t running, i’ll be grateful.

Thanks a bunch!


#8

Hey,
Stackoverflow might be a better place to look for answers around threading. I am not sure what the problem is exactly but there will be a lot of good posts on stackoverflow to get you started with threading like this: http://stackoverflow.com/questions/2846653/python-multithreading-for-dummies .

-Karan


#9

Thanks again Karan!!

I’ll ask there!