Last active
February 2, 2021 03:57
-
-
Save artpoz/083777cb1f8c9efe6418c75d18bdd32b to your computer and use it in GitHub Desktop.
PS3EXPLOR3R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
__author__ = 'Artur Poznanski' | |
import evdev | |
import ev3dev.auto as ev3 | |
import threading | |
import time | |
## Some helpers ## | |
def clamp(n, minn, maxn): | |
return max(min(maxn, n), minn) | |
def scale(val, src, dst): | |
return (float(val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0] | |
def scale_stick(value): | |
return scale(value,(0,255),(-1000,1000)) | |
def dc_clamp(value): | |
return clamp(value,-1000,1000) | |
## Initializing ## | |
print("Finding ps3 controller...") | |
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()] | |
for device in devices: | |
if device.name == 'PLAYSTATION(R)3 Controller': | |
ps3dev = device.fn | |
gamepad = evdev.InputDevice(ps3dev) | |
forward_speed = 0 | |
side_speed = 0 | |
running = True | |
class MotorThread(threading.Thread): | |
def __init__(self): | |
self.right_motor = ev3.LargeMotor(ev3.OUTPUT_C) | |
self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B) | |
threading.Thread.__init__(self) | |
def run(self): | |
print("Engine running!") | |
while running: | |
self.right_motor.run_forever(speed_sp=dc_clamp(forward_speed+side_speed)) | |
self.left_motor.run_forever(speed_sp=dc_clamp(-forward_speed+side_speed)) | |
self.right_motor.stop() | |
self.left_motor.stop() | |
motor_thread = MotorThread() | |
motor_thread.setDaemon(True) | |
motor_thread.start() | |
for event in gamepad.read_loop(): #this loops infinitely | |
if event.type == 3: #A stick is moved | |
if event.code == 0: #X axis on left stick | |
forward_speed = -scale_stick(event.value) | |
if event.code == 1: #Y axis on left stick | |
side_speed = -scale_stick(event.value) | |
if side_speed < 100 and side_speed > -100: | |
side_speed = 0 | |
if forward_speed < 100 and forward_speed > -100: | |
forward_speed = 0 | |
if event.type == 1 and event.code == 302 and event.value == 1: | |
print("X button is pressed. Stopping.") | |
running = False | |
time.sleep(0.5) # Wait for the motor thread to finish | |
break |
Code is fixed to the latest ev3dev version (ev3dev-jessie-ev3-generic-2017-02-11)
Hi, I'm new to programming and ev3s, ive installed the ev3 dev and managed to get the ps3 remote to connect but i was wanting to also use the output of A on the ev3 to control the smaller motor for a gripper I made, I was wondering if you could help me with configuring it in your code. I think I know how to define it in your script for the init__(self), but got no idea how to configure it to use say the r1 button for turning one way and l2 for other way. Please could you help
Cheers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good evening from Spain. This program used to work but since I updated ev3dev it doesn't. I'm a total noob (just read, cut and paste). Could you please help me?
Thanks in advance