0

Here is the code that I'm trying to run via ssh into my raspberry pi. It usually works fine when I have a keyboard and monitor connected directly to the raspberry pi, but it doesn't run when I am using ssh.

import pygame, sys, time
from pygame.locals import *

pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
#screen = pygame.display.set_mode((400,300))
#pygame.display.set_caption('Hello World')

interval = 0.01

# get count of joysticks=1, axes=27, buttons=19 for DualShock 3

joystick_count = pygame.joystick.get_count()
print("joystick_count")
print(joystick_count)
print("--------------")

numaxes = joystick.get_numaxes()
print("numaxes")
print(numaxes)
print("--------------")

numbuttons = joystick.get_numbuttons()
print("numbuttons")
print(numbuttons)
wprint("--------------")

loopQuit = False
while loopQuit == False:

    # test joystick axes
    # outstr = ""
    # for i in range(0,4):
    #       axis = joystick.get_axis(i)
    #       outstr = outstr + str(i) + ":" + str(axis) + "|"
    # print(outstr)

    # test controller buttons
    outstr = ""
    for i in range(0,numbuttons):
        button = joystick.get_button(i)
        outstr = outstr + str(i) + ":" + str(button) + "|"
    print(outstr)

    for event in pygame.event.get():
        if event.type == QUIT:
            loopQuit = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                loopQuit = True

The error that I am getting is Traceback (most recent call last): File "testing_joystick.py", line 47, in for event in pygame.event.get(): pygame.error: video system not initialized

Can anyone help me figure out how to get around this error please?

Windell
  • 1
  • 2

3 Answers3

2

I know it's been a while since the question has been posted, but I faced the same problem today.

For me the sudo command resolved the ssh problem.

sudo python code_path.py
fean
  • 546
  • 4
  • 12
  • 37
1

I had a similar issue as described. The problem appears when no X server is available, that's why it works when you have a monitor connected to the Raspberry.

A solution is to run ssh with the X forwarding option:

    ssh -X pi@raspberry_ip_address

Then your program should run as normal:

    python your_code.py

Hope this helps anybody else.

Cappe
  • 11
  • 2
0

Well you have not initialized your window yet. It looks like you may or may not have commented it out: #screen = pygame.display.set_mode((400, 300)). Many of pygame's events rely on the window. Simply un-comment your screen initialization and it should work fine.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • I found out that it works fine when I have the monitor and keyboard connected directly to the Pi. But doesn't work via ssh. My program doesn't use the screen at all at this point so I commented it out because I still got the same error when it was I commented. Do I need to do anything special if my terminal is via ssh into the Pi? – Windell Aug 08 '16 at 00:48
  • What do you mean by _the pi_? – Christian Dean Aug 08 '16 at 01:42
  • My apologies, by Pi I meant raspberry Pi – Windell Aug 09 '16 at 06:29
  • You have your key-board and monitor connected to a _raspberry pi_? Do you mean a robot, a computer, or something else? – Christian Dean Aug 09 '16 at 06:32
  • A raspberry Pi is a single board computer capable of hdmi video, keyboard and mouse input via usb. It is about the size of a deck of cards. It also has an Ethernet port for network access. It runs a flavor of Debian Linux. – Windell Aug 09 '16 at 07:25
  • Could you please post the python TraceBack. It's the error message that your getting. – Christian Dean Aug 09 '16 at 14:07