Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open cv and Python Scripting with Razer Hydra #237

Open
yabix007 opened this issue Jan 27, 2025 · 13 comments
Open

Open cv and Python Scripting with Razer Hydra #237

yabix007 opened this issue Jan 27, 2025 · 13 comments

Comments

@yabix007
Copy link

Is it possible to get the hydra data through python. Iam using opencv with an iPhone. Iam trying to implement the razer hydra in python to use it with open cv. How to import pysixense in my OpenCv Scripts? I already installed the hydra sdk. But got no plan how to install the pysixense library? Can I import the freePie hydra plugin in my Phyton Scripts? Thanks in advance.

@AndersMalmgren
Copy link
Owner

Hi, no Python is only used as a scripting language. The plugins uses c# and currently only run under Windows

@yabix007
Copy link
Author

yabix007 commented Jan 28, 2025

Thank you for the explanation.

I noticed that if i try to acces the hydra update event in FreePie it doesnt do anything. for example

Hydra-Setup und Key-Handling

def bla():
if hydra[0].joyx > 0.5:
keyboard.setKey(Key.D,)

if starting:
hydra[0].enabled = True # Hydra activate
hydra[0].update += bla # Event-Handler for Hydra

#It should press key.D but it doesnt.

#when i put

wiimote[0].acceleration.update += bla

#now it works. I dont undertand it. There for iam forced to use a wiimote for using the hydra. Thats weird

@AndersMalmgren
Copy link
Owner

hydra and wii are two different plugins with no shared dependencies

@yabix007
Copy link
Author

yabix007 commented Jan 28, 2025

Ah ok good to know. Sadly it doesn't work for me. Is there anything missing in my script?

Here is the Script that only works if I uncomment the Wiimote line:

 # Function that is executed on the update event of the Hydra
 def walk():
     # Control movement based on the current speed
     #if True: #keyboard.getPressed(Key.O)
     if hydra[0].joyx > 0.5:
         keyboard.setKeyDown(Key.D)  # Press "D" for right
     elif hydra[0].joyx < -0.5:
         keyboard.setKeyDown(Key.A)  # Press "A" for left
     else:
         keyboard.setKeyUp(Key.D)  # Release "D"
         keyboard.setKeyUp(Key.A)  # Release "A"
 
     if hydra[0].joyy > 0.5:
         keyboard.setKeyDown(Key.W)  # Press "W" for up
     elif hydra[0].joyy < -0.5:
         keyboard.setKeyDown(Key.S)  # Press "S" for down
     else:
         keyboard.setKeyUp(Key.W)  # Release "W"
         keyboard.setKeyUp(Key.S)  # Release "S"
 
 # When FreePIE starts
 if starting:
 	hydra[0].enabled = True  # Enable Hydra
 	#wiimote[0].acceleration.update += walk 
 	hydra[0].update += walk # why does this not work? What is the correct syntax ?
 

@yabix007
Copy link
Author

What's wrong with this line?

hydra[0].update += walk

@JabberwockPL
Copy link

JabberwockPL commented Jan 29, 2025

I am not (yet) familiar with the intricacies of update procedure, but I have noticed that the Hydra plugin source is missing the line:
public Action OnUpdate { get; set; }
which is present in other plugins, so maybe that is why it does not work.

However, the good news is that the plugin does not require it, as you can just read the data directly. The minimal script might be:

diagnostics.watch(hydra[0].trigger)

Try if it works (as separate script, not IN your script) - the watch tab should show pressing of the trigger of one of the controllers (try both, as Hydra sometimes gets confused which is which).

@MarijnS95
Copy link
Contributor

Since the Python script is supposed to re-run at a regular interval, that's where you can read values (directly outside of the proposed def walk() functions). I don't remember why some plugins have separate OnUpdate callbacks, perhaps because they change their values less frequently than axis values which "should" just be processed every script interval?

@JabberwockPL
Copy link

If I understand it correctly, the reason for the use of 'update' is that the scripts, especially involving complex calculations, might be somewhat resource intensive, so it might be better to wait for new data and not perform every operation on old data in every cycle.

@MarijnS95
Copy link
Contributor

MarijnS95 commented Jan 29, 2025

It depends; if a certain API doesn't have any sort of update "callback" but only polling methods, the FreePIE engine would have had to compare if anything changed to decide if the OnUpdate delegate should be called (or, as is probably the case with hydra here, not implement that and instead expect the user to read back the value every iteration. If their computation is complex, they can implement caching).

@yabix007
Copy link
Author

yabix007 commented Jan 29, 2025

This script works, if I access the hydra directly. For more complex scripts, the update callback is necessaty i think. So I delete the walk function. It worked

if keyboard.getKeyDown(Key.F3):
	if hydra[0].joyx > 0.5:
		keyboard.setKeyDown(Key.D)  # Press "D" for right
	elif hydra[0].joyx < -0.5:
		keyboard.setKeyDown(Key.A)  # Press "A" for left
	else:
		keyboard.setKeyUp(Key.D)  # Release "D"
		keyboard.setKeyUp(Key.A)  # Release "A"

	if hydra[0].joyy > 0.5:
		keyboard.setKeyDown(Key.W)  # Press "W" for up
	elif hydra[0].joyy < -0.5:
		keyboard.setKeyDown(Key.S)  # Press "S" for down
	else:
		keyboard.setKeyUp(Key.W)  # Release "W"
		keyboard.setKeyUp(Key.S)  # Release "S"

I put this to a walk funtion to it, then it worked properly, too!

def walk():
	if keyboard.getKeyDown(Key.F3):
		if hydra[0].joyx > 0.5:
			keyboard.setKeyDown(Key.D)  # Press "D" for right
		elif hydra[0].joyx < -0.5:
			keyboard.setKeyDown(Key.A)  # Press "A" for left
		else:
			keyboard.setKeyUp(Key.D)  # Release "D"
			keyboard.setKeyUp(Key.A)  # Release "A"

		if hydra[0].joyy > 0.5:
			keyboard.setKeyDown(Key.W)  # Press "W" for up
		elif hydra[0].joyy < -0.5:
			keyboard.setKeyDown(Key.S)  # Press "S" for down
		else:
			keyboard.setKeyUp(Key.W)  # Release "W"
			keyboard.setKeyUp(Key.S)  # Release "S"
walk()

As soon i add

if starting:
	hydra[0].update += walk

instead of walk()
it stops working

@AndersMalmgren
Copy link
Owner

It depends; if a certain API doesn't have any sort of update "callback" but only polling methods, the FreePIE engine would have had to compare if anything changed to decide if the OnUpdate delegate should be called (or, as is probably the case with hydra here, not implement that and instead expect the user to read back the value every iteration. If their computation is complex, they can implement caching).

Yeah, its a convenient helper for users of freepie. So they dont need to track when updates occour themself.

@MarijnS95
Copy link
Contributor

Hydra uses polling, but still tracks a sequence number to see if the data has changed:

//This method will be executed each iteration of the script
for (int i = 0; i < 2; i++)
{
var lastSequence = Controller[i].sequence_number;
Sixense.GetNewestData(i, out Controller[i]);
if (lastSequence == Controller[i].sequence_number)
continue;

It does raise an OnUpdate() delegate, but on the plugin and not on the individual controllers:

@yabix007 perhaps you can replace hydra[0].update += walk with hydra.update += walk and see if that works for you?

@yabix007
Copy link
Author

This is the error i get when add this:

hydra.update += walk

to the script

'Array[HydraPluginGlobal]' object has no attribute 'update'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants