Im pulling this out of the shoutbox so this can help someone else in the future. I am still having trouble using the data that come back from the new Joystick service. I am using a stripped down minimum script. seen below. The script works and prints the data and also prints the "A not pressed" on each button press. But the If statement still wont match.

 

from org.myrobotlab.service import Joystick
from org.myrobotlab.service import Runtime
from time import sleep
#---------------------------------Create Services----------------------
joystick = Runtime.createAndStart("joystick","Joystick")
#----------------------Define callback function for Joystick-----------

def onJoystickInput(data):
  print data
  if (data.id == '0' and float(data.value) == 1.0):
    print "A Button has been pushed"
  else :
    print "A not pressed"
    
   
#----------------------Connect Peripherals-----------------------------------
joystick.setController(0); #PC only - Pi needs new
joystick.addInputListener(python)
# Tell the joystick to turn on
joystick.startPolling()

brotherbrown831

6 years 12 months ago

I solved it..The data.id for the A button is 'A'

 

A big thanks to Kwatters for anwering all my questions.

 

fixed example below

from org.myrobotlab.service import Joystick
from org.myrobotlab.service import Runtime
from time import sleep
#---------------------------------Create Services----------------------
joystick = Runtime.createAndStart("joystick","Joystick")
#----------------------Define callback function for Joystick-----------

def onJoystickInput(data):
  print data
  if (data.id == 'A' and float(data.value) == 1.0):
    print "A Button has been pushed"
  else :
    print "A not pressed"
    
   
#----------------------Connect Peripherals-----------------------------------
joystick.setController(0); #PC only - Pi needs new
joystick.addInputListener(python)
# Tell the joystick to turn on
joystick.startPolling()

brotherbrown831

6 years 12 months ago

In reply to by brotherbrown831

Now that I know how to identify and use the new variable (data), I want to create a way to capture the value and make it global so I can use it for other operations, such as providing the value of the ry axis as the speed and direction to a motor. 

in the exampample linked below i am attempting to capture the numeric value of the ry axis (-1 to1) and create a global float variable to be used in the main loop to drive the motorLeft between -255 and +255.

Currently I am having trouble capturing the data.value as a float and making it global. 

https://github.com/MyRobotLab/pyrobotlab/blob/master/home/brotherbrown8…

 

 

Hi bb831,

I did not test this, but I suspect the global is affecting the function of float and not the variable, like you intended:

perhaps something like:

def onJoystickInput(data):
  global ryValue
  ryValue = float(ryValue)
  

Thanks Grog, Kwatters was nice enought to show me how to make the new ryValue available to other methods without using 'global'. 

  if (data.id == 'ry'):
    ryValue = float(data.value) 
    moveLeftMotor(ryValue)
  
def moveLeftMotor(ryValue):
  motorleft.move(255*(ryValue))