Last active
October 19, 2023 08:10
-
-
Save antonvh/695ab1c36b61171714a84f700858cdb3 to your computer and use it in GitHub Desktop.
PS3/PS4 LEGO MINDSTORMS remote control script with front wheel calibration and shoulder button trim.
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 pybricks-micropython | |
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, | |
InfraredSensor, UltrasonicSensor, GyroSensor) | |
from pybricks.parameters import (Port, Stop, Direction, Button, Color, | |
SoundFile, ImageFile, Align) | |
from pybricks.tools import print, wait, StopWatch | |
import struct | |
# Declare motors | |
left_motor = Motor(Port.B) | |
right_motor = Motor(Port.C) | |
steer_motor = Motor(Port.A) | |
forward = 0 | |
left = 0 | |
# Auto center steering wheels. | |
steer_motor.run_until_stalled(250) | |
steer_motor.reset_angle(80) | |
steer_motor.run_target(300,0) | |
# A helper function for converting stick values (0 - 255) | |
# to more usable numbers (-100 - 100) | |
def scale(val, src, dst): | |
""" | |
Scale the given value from the scale of src to the scale of dst. | |
val: float or int | |
src: tuple | |
dst: tuple | |
example: print(scale(99, (0.0, 99.0), (-1.0, +1.0))) | |
""" | |
return (float(val-src[0]) / (src[1]-src[0])) * (dst[1]-dst[0])+dst[0] | |
# Open the Gamepad event file: | |
# /dev/input/event3 is for PS3 gamepad | |
# /dev/input/event4 is for PS4 gamepad | |
# look at contents of /proc/bus/input/devices if either one of them doesn't work. | |
# use 'cat /proc/bus/input/devices' and look for the event file. | |
infile_path = "/dev/input/event4" | |
# open file in binary mode | |
in_file = open(infile_path, "rb") | |
# Read from the file | |
# long int, long int, unsigned short, unsigned short, unsigned int | |
FORMAT = 'llHHI' | |
EVENT_SIZE = struct.calcsize(FORMAT) | |
event = in_file.read(EVENT_SIZE) | |
while event: | |
(tv_sec, tv_usec, ev_type, code, value) = struct.unpack(FORMAT, event) | |
if ev_type == 1: # A button was pressed or released. | |
if code == 310 and value == 0: | |
steer_motor.reset_angle(steer_motor.angle()-5) | |
if code == 311 and value == 0: | |
steer_motor.reset_angle(steer_motor.angle()+5) | |
elif ev_type == 3: # Stick was moved | |
if code == 0: | |
left = scale(value, (0,255), (40, -40)) | |
if code == 4: # Righ stick vertical | |
forward = scale(value, (0,255), (100,-100)) | |
# Set motor voltages. | |
left_motor.dc(forward) | |
right_motor.dc(forward) | |
# Track the steering angle | |
steer_motor.track_target(left) | |
# Finally, read another event | |
event = in_file.read(EVENT_SIZE) | |
in_file.close() |
Author
antonvh
commented
Oct 19, 2023
via email
Check the pybricks documentation for the reset_angle() method. I don’t think the way you use it, has the result you are looking for.
Best to Isolate the reset code until you are happy with the result, then put it in the larger program.
… On 18 Oct 2023, at 20:07, ImMuniz ***@***.***> wrote:
@ImMuniz commented on this gist.
hey, i'm having a problem with something in my code, i tried to reset the direction motor by pressing X on the controller, but it is not resetting the angle of the motor, i tried putting a print command to see if the problem was with the code of the X button, but i worked, so it's not a problem with the button code, could you help me with it? Here is the code i'm using (i changed the steer motor name to left motor):
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase
import struct
Declare motors
left_motor = Motor(Port.A)
right_motor = Motor(Port.B)
Auto center steering wheels.
left_motor.run_until_stalled(250)
left_motor.reset_angle(80)
left_motor.run_target(300,0)
Initialize variables.
Assuming sticks are in the middle when starting.
left_stick_x = 124
right_stick_x = 124
A helper function for converting stick values (0 - 255)
to more usable numbers (-100 - 100)
def scale(val, src, dst):
"""
Scale the given value from the scale of src to the scale of dst.
val: float or int
src: tuple
dst: tuple
example: print(scale(99, (0.0, 99.0), (-1.0, +1.0)))
"""
return (float(val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0]
Find the PS3 Gamepad:
/dev/input/event3 is the usual file handler for the gamepad.
look at contents of /proc/bus/input/devices if it doesn't work.
infile_path = "/dev/input/event4"
open file in binary mode
in_file = open(infile_path, "rb")
Read from the file
long int, long int, unsigned short, unsigned short, unsigned int
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)
event = in_file.read(EVENT_SIZE)
while event:
(tv_sec, tv_usec, ev_type, code, value) = struct.unpack(FORMAT, event)
if ev_type == 3 and code == 0:
left_stick_x = value
if ev_type == 3 and code == 4:
right_stick_x = value
if ev_type == 1:
if code == 304 and value == 0:
left_motor.reset_angle(left_motor.angle())
print("ta despressionado")
elif code == 304 and value == 1:
left_motor.reset_angle(left_motor.angle())
print("ta pressionado")
# Scale stick positions to -100,100
forward = scale(right_stick_x, (0, 255), (-100, 100)) # Use right_stick_x here
left = scale(left_stick_x, (0, 255), (-100, 100)) # Use left_stick_x here
# Set motor voltages. If we're steering left, the left motor
# must run backwards so it has a -left component
# It has a forward component for going forward too.
left_motor.dc(left)
right_motor.dc(forward)
# Reinicialize direction motors
# Finally, read another event
event = in_file.read(EVENT_SIZE)
in_file.close()
—
Reply to this email directly, view it on GitHub <https://gist.github.com/antonvh/695ab1c36b61171714a84f700858cdb3#gistcomment-4730296> or unsubscribe <https://github.com/notifications/unsubscribe-auth/AACW5HDGFK4FYXU5NFN3PG3YAALFLBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMZXHEYDONBWU52HE2LHM5SXFJTDOJSWC5DF>.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment